Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pkg/app/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ var DebugUnixSocket = "/var/run/shell-operator/debug.socket"

var DebugHttpServerAddr = ""

var (
DebugKeepTmpFilesVar = "no"
DebugKeepTmpFiles = false
)
var DebugKeepTmpFiles = false

var DebugKubernetesAPI = false

Expand All @@ -27,11 +24,11 @@ func DefineDebugFlags(kpApp *kingpin.Application, cmd *kingpin.CmdClause) {
Default(DebugHttpServerAddr).
StringVar(&DebugHttpServerAddr)

cmd.Flag("debug-keep-tmp-files", "set to yes to disable cleanup of temporary files").
cmd.Flag("debug-keep-tmp-files", "set to true to disable cleanup of temporary files").
Envar("DEBUG_KEEP_TMP_FILES").
Hidden().
Default(DebugKeepTmpFilesVar).
StringVar(&DebugKeepTmpFilesVar)
Default("false").
BoolVar(&DebugKeepTmpFiles)

cmd.Flag("debug-kubernetes-api", "enable client-go debug messages").
Envar("DEBUG_KUBERNETES_API").
Expand Down
24 changes: 24 additions & 0 deletions pkg/app/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDebugKeepTmpFiles_DefaultIsFalse(t *testing.T) {
assert.False(t, DebugKeepTmpFiles, "DebugKeepTmpFiles should default to false")
}

// TestDebugKeepTmpFilesIsBool verifies the variable is the bool type
// and that simple assignment behaves correctly (no string comparison needed).
func TestDebugKeepTmpFilesIsBool(t *testing.T) {
saved := DebugKeepTmpFiles
defer func() { DebugKeepTmpFiles = saved }()

DebugKeepTmpFiles = true
assert.True(t, DebugKeepTmpFiles)

DebugKeepTmpFiles = false
assert.False(t, DebugKeepTmpFiles)
}
8 changes: 5 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"

"github.com/deckhouse/deckhouse/pkg/log"

pkg "github.com/flant/shell-operator/pkg"
)

/**
Expand Down Expand Up @@ -52,7 +54,7 @@ func NewConfig(logger *log.Logger) *Config {
values: make(map[string]string),
temporalValues: make(map[string]*TemporalValue),
errors: make(map[string]error),
logger: logger.With(slog.String("component", "runtimeConfig")),
logger: logger.With(slog.String(pkg.LogKeyComponent, "runtimeConfig")),
}
}

Expand Down Expand Up @@ -261,7 +263,7 @@ func (c *Config) expireOverrides() {

for _, expire := range expires {
name, oldValue, newValue := expire[0], expire[1], expire[2]
c.logger.Debug("Parameter is expired", slog.String("parameter", name))
c.logger.Debug("Parameter is expired", slog.String(pkg.LogKeyParameter, name))
c.callOnChange(name, oldValue, newValue)
}
}
Expand All @@ -274,7 +276,7 @@ func (c *Config) callOnChange(name string, oldValue string, newValue string) {
err := c.params[name].onChange(oldValue, newValue)
if err != nil {
c.logger.Error("OnChange handler failed for parameter during value change values",
slog.String("parameter", name), slog.String("old_value", oldValue), slog.String("new_value", newValue), log.Err(err))
slog.String(pkg.LogKeyParameter, name), slog.String(pkg.LogKeyOldValue, oldValue), slog.String(pkg.LogKeyNewValue, newValue), log.Err(err))
}
c.m.Lock()
delete(c.errors, name)
Expand Down
5 changes: 3 additions & 2 deletions pkg/debug/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/go-chi/chi/v5/middleware"
"gopkg.in/yaml.v3"

pkg "github.com/flant/shell-operator/pkg"
utils "github.com/flant/shell-operator/pkg/utils/file"
structuredLogger "github.com/flant/shell-operator/pkg/utils/structured-logger"
)
Expand Down Expand Up @@ -70,7 +71,7 @@ func (s *Server) Init() error {
return fmt.Errorf("Debug HTTP server fail to listen on '%s': %w", address, err)
}

s.logger.Info("Debug endpoint listen on address", slog.String("address", address))
s.logger.Info("Debug endpoint listen on address", slog.String(pkg.LogKeyAddress, address))

go func() {
if err := http.Serve(listener, s.Router); err != nil {
Expand Down Expand Up @@ -145,7 +146,7 @@ func handleFormattedOutput(writer http.ResponseWriter, request *http.Request, ha
format = strings.TrimPrefix(format, ".")
}

structuredLogger.GetLogEntry(request).Debug("used format", slog.String("format", format))
structuredLogger.GetLogEntry(request).Debug("used format", slog.String(pkg.LogKeyFormat, format))

switch format {
case "text":
Expand Down
19 changes: 10 additions & 9 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/deckhouse/deckhouse/pkg/log"
"go.opentelemetry.io/otel"

pkg "github.com/flant/shell-operator/pkg"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)

Expand All @@ -26,7 +27,7 @@ const (
func Run(cmd *exec.Cmd) error {
// TODO context: hook name, hook phase, hook binding
// TODO observability
log.Debug("Executing command", slog.String("command", strings.Join(cmd.Args, " ")), slog.String("dir", cmd.Dir))
log.Debug("Executing command", slog.String(pkg.LogKeyCommand, strings.Join(cmd.Args, " ")), slog.String(pkg.LogKeyDir, cmd.Dir))

return cmd.Run()
}
Expand Down Expand Up @@ -100,8 +101,8 @@ func NewExecutor(dir string, entrypoint string, args []string, envs []string) *E

func (e *Executor) Output() ([]byte, error) {
e.logger.Debug("Executing command",
slog.String("command", strings.Join(e.cmd.Args, " ")),
slog.String("dir", e.cmd.Dir))
slog.String(pkg.LogKeyCommand, strings.Join(e.cmd.Args, " ")),
slog.String(pkg.LogKeyDir, e.cmd.Dir))
return e.cmd.Output()
}

Expand All @@ -117,12 +118,12 @@ func (e *Executor) RunAndLogLines(ctx context.Context, logLabels map[string]stri

stdErr := bytes.NewBuffer(nil)
logEntry := utils.EnrichLoggerWithLabels(e.logger, logLabels)
stdoutLogEntry := logEntry.With("output", "stdout")
stderrLogEntry := logEntry.With("output", "stderr")
stdoutLogEntry := logEntry.With(pkg.LogKeyOutput, "stdout")
stderrLogEntry := logEntry.With(pkg.LogKeyOutput, "stderr")

log.Debug("Executing command",
slog.String("command", strings.Join(e.cmd.Args, " ")),
slog.String("dir", e.cmd.Dir))
slog.String(pkg.LogKeyCommand, strings.Join(e.cmd.Args, " ")),
slog.String(pkg.LogKeyDir, e.cmd.Dir))

plo := &proxyLogger{
ctx: ctx,
Expand Down Expand Up @@ -208,7 +209,7 @@ func (pl *proxyLogger) Write(p []byte) (int, error) {
}()

if !ok {
pl.logger.Debug("json log line not map[string]interface{}", slog.Any("line", line))
pl.logger.Debug("json log line not map[string]interface{}", slog.Any(pkg.LogKeyLine, line))

// fall back to using the logger
pl.logger.Info(string(p))
Expand Down Expand Up @@ -308,7 +309,7 @@ func (pl *proxyLogger) mergeAndLogInputLog(ctx context.Context, inputLog map[str
if len(logLine) > 10000 {
logLine = fmt.Sprintf("%s:truncated", logLine[:10000])

logger.Log(ctx, lvl.Level(), msg, slog.Any("hook", map[string]any{
logger.Log(ctx, lvl.Level(), msg, slog.Any(pkg.LogKeyHook, map[string]any{
"truncated": logLine,
}))

Expand Down
17 changes: 9 additions & 8 deletions pkg/hook/controller/admission_bindings_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/deckhouse/deckhouse/pkg/log"
v1 "k8s.io/api/admission/v1"

pkg "github.com/flant/shell-operator/pkg"
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
htypes "github.com/flant/shell-operator/pkg/hook/types"
"github.com/flant/shell-operator/pkg/webhook/admission"
Expand Down Expand Up @@ -71,8 +72,8 @@ func (c *AdmissionBindingsController) EnableValidatingBindings() {
}
if config.Webhook.Metadata.ConfigurationId != confId {
log.Error("Possible bug!!! kubernetesValidating has non-unique configurationIds",
slog.String("configurationID", config.Webhook.Metadata.ConfigurationId),
slog.String("confID", confId))
slog.String(pkg.LogKeyConfigurationID, config.Webhook.Metadata.ConfigurationId),
slog.String(pkg.LogKeyConfID, confId))
}
}
c.ConfigurationId = confId
Expand Down Expand Up @@ -107,8 +108,8 @@ func (c *AdmissionBindingsController) EnableMutatingBindings() {
}
if config.Webhook.Metadata.ConfigurationId != confId {
log.Error("Possible bug!!! kubernetesMutating has non-unique configurationIds",
slog.String("configurationID", config.Webhook.Metadata.ConfigurationId),
slog.String("confID", confId))
slog.String(pkg.LogKeyConfigurationID, config.Webhook.Metadata.ConfigurationId),
slog.String(pkg.LogKeyConfID, confId))
}
}
c.ConfigurationId = confId
Expand Down Expand Up @@ -145,8 +146,8 @@ func (c *AdmissionBindingsController) CanHandleEvent(event admission.Event) bool
func (c *AdmissionBindingsController) HandleEvent(_ context.Context, event admission.Event) BindingExecutionInfo {
if c.ConfigurationId != event.ConfigurationId {
log.Error("Possible bug!!! Unknown validating event: no binding for configurationId",
slog.String("configurationId", event.ConfigurationId),
slog.String("webhookId", event.WebhookId))
slog.String(pkg.LogKeyConfigId, event.ConfigurationId),
slog.String(pkg.LogKeyWebhookId, event.WebhookId))
return BindingExecutionInfo{
BindingContext: []bctx.BindingContext{},
AllowFailure: false,
Expand All @@ -156,8 +157,8 @@ func (c *AdmissionBindingsController) HandleEvent(_ context.Context, event admis
link, hasKey := c.AdmissionLinks[event.WebhookId]
if !hasKey {
log.Error("Possible bug!!! Unknown validating event: no binding for configurationId",
slog.String("configurationId", event.ConfigurationId),
slog.String("webhookId", event.WebhookId))
slog.String(pkg.LogKeyConfigId, event.ConfigurationId),
slog.String(pkg.LogKeyWebhookId, event.WebhookId))
return BindingExecutionInfo{
BindingContext: []bctx.BindingContext{},
AllowFailure: false,
Expand Down
9 changes: 5 additions & 4 deletions pkg/hook/controller/conversion_bindings_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/deckhouse/deckhouse/pkg/log"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

pkg "github.com/flant/shell-operator/pkg"
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
htypes "github.com/flant/shell-operator/pkg/hook/types"
"github.com/flant/shell-operator/pkg/webhook/conversion"
Expand Down Expand Up @@ -64,7 +65,7 @@ func (c *ConversionBindingsController) EnableConversionBindings() {
}
}
log.Info("conversion binding controller: add webhook from config",
slog.String("name", config.Webhook.Metadata.Name))
slog.String(pkg.LogKeyName, config.Webhook.Metadata.Name))
c.webhookManager.AddWebhook(config.Webhook)
}
}
Expand All @@ -85,7 +86,7 @@ func (c *ConversionBindingsController) CanHandleEvent(crdName string, _ *v1.Conv
func (c *ConversionBindingsController) HandleEvent(_ context.Context, crdName string, request *v1.ConversionRequest, rule conversion.Rule) BindingExecutionInfo {
_, hasKey := c.Links[crdName]
if !hasKey {
log.Error("Possible bug!!! No binding for conversion event for crd", slog.String("crd", crdName))
log.Error("Possible bug!!! No binding for conversion event for crd", slog.String(pkg.LogKeyCRD, crdName))
return BindingExecutionInfo{
BindingContext: []bctx.BindingContext{},
AllowFailure: false,
Expand All @@ -94,8 +95,8 @@ func (c *ConversionBindingsController) HandleEvent(_ context.Context, crdName st
link, has := c.Links[crdName][rule]
if !has {
log.Error("Possible bug!!! Event has an unknown conversion rule: no binding was registered",
slog.String("rule", rule.String()),
slog.String("crd", crdName))
slog.String(pkg.LogKeyRule, rule.String()),
slog.String(pkg.LogKeyCRD, crdName))
return BindingExecutionInfo{
BindingContext: []bctx.BindingContext{},
AllowFailure: false,
Expand Down
11 changes: 6 additions & 5 deletions pkg/hook/controller/kubernetes_bindings_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/deckhouse/deckhouse/pkg/log"

pkg "github.com/flant/shell-operator/pkg"
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
htypes "github.com/flant/shell-operator/pkg/hook/types"
kubeeventsmanager "github.com/flant/shell-operator/pkg/kube_events_manager"
Expand Down Expand Up @@ -132,9 +133,9 @@ func (c *kubernetesBindingsController) UpdateMonitor(monitorId string, kind, api

utils.EnrichLoggerWithLabels(c.logger, link.BindingConfig.Monitor.Metadata.LogLabels).
Info("Monitor is recreated",
slog.String("bindingName", link.BindingConfig.BindingName),
slog.String("kind", link.BindingConfig.Monitor.Kind),
slog.String("apiVersion", link.BindingConfig.Monitor.ApiVersion))
slog.String(pkg.LogKeyBindingName, link.BindingConfig.BindingName),
slog.String(pkg.LogKeyKind, link.BindingConfig.Monitor.Kind),
slog.String(pkg.LogKeyAPIVersion, link.BindingConfig.Monitor.ApiVersion))

// Synchronization has no meaning for UpdateMonitor. Just emit Added event to handle objects of
// a new kind.
Expand Down Expand Up @@ -166,7 +167,7 @@ func (c *kubernetesBindingsController) UnlockEvents() {
func (c *kubernetesBindingsController) UnlockEventsFor(monitorID string) {
m := c.kubeEventsManager.GetMonitor(monitorID)
if m == nil {
log.Warn("monitor was not found", slog.String("monitorID", monitorID))
log.Warn("monitor was not found", slog.String(pkg.LogKeyMonitorID, monitorID))
return
}
m.EnableKubeEventCb()
Expand Down Expand Up @@ -223,7 +224,7 @@ func (c *kubernetesBindingsController) setBindingMonitorLinks(monitorId string,
func (c *kubernetesBindingsController) HandleEvent(_ context.Context, kubeEvent kemtypes.KubeEvent) BindingExecutionInfo {
link, hasKey := c.getBindingMonitorLinksById(kubeEvent.MonitorId)
if !hasKey {
log.Error("Possible bug!!! Unknown kube event: no such monitor id registered", slog.String("monitorID", kubeEvent.MonitorId))
log.Error("Possible bug!!! Unknown kube event: no such monitor id registered", slog.String(pkg.LogKeyMonitorID, kubeEvent.MonitorId))
return BindingExecutionInfo{
BindingContext: []bctx.BindingContext{},
AllowFailure: false,
Expand Down
3 changes: 1 addition & 2 deletions pkg/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"go.opentelemetry.io/otel/attribute"
"golang.org/x/time/rate"

"github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/executor"
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
"github.com/flant/shell-operator/pkg/hook/config"
Expand Down Expand Up @@ -139,7 +138,7 @@ func (h *Hook) Run(ctx context.Context, _ htypes.BindingType, context []bctx.Bin

// remove tmp file on hook exit
defer func() {
if app.DebugKeepTmpFilesVar != "yes" {
if !h.KeepTemporaryHookFiles {
_ = os.Remove(contextPath)
_ = os.Remove(metricsPath)
_ = os.Remove(conversionPath)
Expand Down
Loading
Loading