diff --git a/pkg/addon-operator/admission_http_server.go b/pkg/addon-operator/admission_http_server.go index 25a516200..44d53b78b 100644 --- a/pkg/addon-operator/admission_http_server.go +++ b/pkg/addon-operator/admission_http_server.go @@ -10,6 +10,8 @@ import ( "time" "github.com/deckhouse/deckhouse/pkg/log" + + "github.com/flant/addon-operator/pkg" ) type AdmissionServer struct { @@ -30,7 +32,7 @@ func NewAdmissionServer(listenPort, certsDir string) *AdmissionServer { func (as *AdmissionServer) RegisterHandler(route string, handler http.Handler) { if _, ok := as.routes[route]; ok { log.Fatal("Route is already registered", - slog.String("route", route)) + slog.String(pkg.LogKeyRoute, route)) } as.routes[route] = handler @@ -45,7 +47,7 @@ func (as *AdmissionServer) start(ctx context.Context) { } log.Debug("Registered admission routes", - slog.String("routes", fmt.Sprintf("%v", as.routes))) + slog.String(pkg.LogKeyRoutes, fmt.Sprintf("%v", as.routes))) srv := &http.Server{ Addr: fmt.Sprintf(":%s", as.listenPort), diff --git a/pkg/addon-operator/bootstrap.go b/pkg/addon-operator/bootstrap.go index a35ed1718..a23a72ad4 100644 --- a/pkg/addon-operator/bootstrap.go +++ b/pkg/addon-operator/bootstrap.go @@ -6,6 +6,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/app" "github.com/flant/addon-operator/pkg/kube_config_manager" "github.com/flant/addon-operator/pkg/kube_config_manager/backend" @@ -25,11 +26,11 @@ func (op *AddonOperator) bootstrap() error { // Log the path where modules will be searched log.Info("Search modules", - slog.String("path", app.ModulesDir)) + slog.String(pkg.LogKeyPath, app.ModulesDir)) // Log the namespace in which the operator will work log.Info("Addon-operator namespace", - slog.String("namespace", op.DefaultNamespace)) + slog.String(pkg.LogKeyNamespace, op.DefaultNamespace)) // Initialize the debug server for troubleshooting and monitoring // TODO: rewrite shapp global variables to the addon-operator one diff --git a/pkg/addon-operator/handler_manager_events.go b/pkg/addon-operator/handler_manager_events.go index 604e598a2..d3d296a6d 100644 --- a/pkg/addon-operator/handler_manager_events.go +++ b/pkg/addon-operator/handler_manager_events.go @@ -21,12 +21,12 @@ func (op *AddonOperator) RegisterManagerEventsHandlers() { // Register handler for schedule events op.engine.ManagerEventsHandler.WithScheduleEventHandler(func(ctx context.Context, crontab string) []sh_task.Task { logLabels := map[string]string{ - "event.id": uuid.Must(uuid.NewV4()).String(), + pkg.LogKeyEventID: uuid.Must(uuid.NewV4()).String(), pkg.LogKeyBinding: string(htypes.Schedule), } logEntry := utils.EnrichLoggerWithLabels(op.Logger, logLabels) logEntry.Debug("Create tasks for 'schedule' event", - slog.String("event", crontab)) + slog.String(pkg.LogKeyEvent, crontab)) // Handle global hook schedule events return op.ModuleManager.HandleScheduleEvent( @@ -40,12 +40,12 @@ func (op *AddonOperator) RegisterManagerEventsHandlers() { // Register handler for kubernetes events op.engine.ManagerEventsHandler.WithKubeEventHandler(func(ctx context.Context, kubeEvent types.KubeEvent) []sh_task.Task { logLabels := map[string]string{ - "event.id": uuid.Must(uuid.NewV4()).String(), + pkg.LogKeyEventID: uuid.Must(uuid.NewV4()).String(), pkg.LogKeyBinding: string(htypes.OnKubernetesEvent), } logEntry := utils.EnrichLoggerWithLabels(op.Logger, logLabels) logEntry.Debug("Create tasks for 'kubernetes' event", - slog.String("event", kubeEvent.String())) + slog.String(pkg.LogKeyEvent, kubeEvent.String())) // Handle kubernetes events for global and module hooks tailTasks := op.ModuleManager.HandleKubeEvent( @@ -73,19 +73,19 @@ func (op *AddonOperator) createGlobalHookTaskFactory( } hookLabels := utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: globalHook.GetName(), - "hook.type": "global", - "queue": info.QueueName, + pkg.LogKeyHook: globalHook.GetName(), + pkg.LogKeyHookType: "global", + pkg.LogKeyQueue: info.QueueName, }) if len(info.BindingContext) > 0 { - hookLabels["binding.name"] = info.BindingContext[0].Binding + hookLabels[pkg.LogKeyBindingName] = info.BindingContext[0].Binding if bindingType == htypes.OnKubernetesEvent { - hookLabels["watchEvent"] = string(info.BindingContext[0].WatchEvent) + hookLabels[pkg.LogKeyWatchEvent] = string(info.BindingContext[0].WatchEvent) } } - delete(hookLabels, "task.id") + delete(hookLabels, pkg.LogKeyTaskID) newTask := sh_task.NewTask(task.GlobalHookRun). WithLogLabels(hookLabels). @@ -118,20 +118,20 @@ func (op *AddonOperator) createModuleHookTaskFactory( } hookLabels := utils.MergeLabels(logLabels, map[string]string{ - "module": module.GetName(), - pkg.LogKeyHook: moduleHook.GetName(), - "hook.type": "module", - "queue": info.QueueName, + pkg.LogKeyModule: module.GetName(), + pkg.LogKeyHook: moduleHook.GetName(), + pkg.LogKeyHookType: "module", + pkg.LogKeyQueue: info.QueueName, }) if len(info.BindingContext) > 0 { - hookLabels["binding.name"] = info.BindingContext[0].Binding + hookLabels[pkg.LogKeyBindingName] = info.BindingContext[0].Binding if bindingType == htypes.OnKubernetesEvent { - hookLabels["watchEvent"] = string(info.BindingContext[0].WatchEvent) + hookLabels[pkg.LogKeyWatchEvent] = string(info.BindingContext[0].WatchEvent) } } - delete(hookLabels, "task.id") + delete(hookLabels, pkg.LogKeyTaskID) newTask := sh_task.NewTask(task.ModuleHookRun). WithLogLabels(hookLabels). diff --git a/pkg/addon-operator/handler_module_manager.go b/pkg/addon-operator/handler_module_manager.go index 57f5b611d..b3a1a9811 100644 --- a/pkg/addon-operator/handler_module_manager.go +++ b/pkg/addon-operator/handler_module_manager.go @@ -20,7 +20,7 @@ import ( func (op *AddonOperator) StartModuleManagerEventHandler() { go func() { - logEntry := op.Logger.With("operator.component", "handleManagerEvents") + logEntry := op.Logger.With(pkg.LogKeyOperatorComponent, "handleManagerEvents") for { select { case schedulerEvent := <-op.ModuleManager.SchedulerEventCh(): @@ -28,9 +28,9 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { // dynamically_enabled_extender case dynamic_extender.DynamicExtenderEvent: logLabels := map[string]string{ - "event.id": uuid.Must(uuid.NewV4()).String(), - "type": "ModuleScheduler event", - "event_source": "DymicallyEnabledExtenderChanged", + pkg.LogKeyEventID: uuid.Must(uuid.NewV4()).String(), + pkg.LogKeyType: "ModuleScheduler event", + pkg.LogKeyEventSource: "DymicallyEnabledExtenderChanged", } eventLogEntry := utils.EnrichLoggerWithLabels(logEntry, logLabels) // if global hooks haven't been run yet, script enabled extender fails due to missing global values @@ -50,7 +50,7 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { // if converge has already begun - restart it immediately if op.engine.TaskQueues.GetMain().Length() > 0 && RemoveCurrentConvergeTasks(op.getConvergeQueues(), logLabels, op.Logger) && op.ConvergeState.GetPhase() != converge.StandBy { logEntry.Info("ConvergeModules: global hook dynamic modification detected, restart current converge process", - slog.String("phase", string(op.ConvergeState.GetPhase()))) + slog.String(pkg.LogKeyPhase, string(op.ConvergeState.GetPhase()))) op.engine.TaskQueues.GetMain().AddFirst(convergeTask) op.logTaskAdd(eventLogEntry, "DynamicExtender is updated, put first", convergeTask) } else { @@ -65,9 +65,9 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { // kube_config_extender case config.KubeConfigEvent: logLabels := map[string]string{ - "event.id": uuid.Must(uuid.NewV4()).String(), - "type": "ModuleScheduler event", - "event_source": "KubeConfigExtenderChanged", + pkg.LogKeyEventID: uuid.Must(uuid.NewV4()).String(), + pkg.LogKeyType: "ModuleScheduler event", + pkg.LogKeyEventSource: "KubeConfigExtenderChanged", } eventLogEntry := utils.EnrichLoggerWithLabels(logEntry, logLabels) switch event.Type { @@ -77,10 +77,10 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { case config.KubeConfigChanged: eventLogEntry.Debug("ModuleManagerEventHandler-KubeConfigChanged", - slog.Bool("globalSectionChanged", event.GlobalSectionChanged), - slog.Any("moduleValuesChanged", event.ModuleValuesChanged), - slog.Any("moduleEnabledStateChanged", event.ModuleEnabledStateChanged), - slog.Any("ModuleMaintenanceChanged", event.ModuleMaintenanceChanged)) + slog.Bool(pkg.LogKeyGlobalSectionChanged, event.GlobalSectionChanged), + slog.Any(pkg.LogKeyModuleValuesChanged, event.ModuleValuesChanged), + slog.Any(pkg.LogKeyModuleEnabledStateChanged, event.ModuleEnabledStateChanged), + slog.Any(pkg.LogKeyModuleMaintenanceChanged, event.ModuleMaintenanceChanged)) if !op.ModuleManager.GetKubeConfigValid() { eventLogEntry.Info("KubeConfig become valid") } @@ -129,7 +129,7 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { // if main queue isn't empty and there was another convergeModules task: if op.engine.TaskQueues.GetMain().Length() > 0 && RemoveCurrentConvergeTasks(op.getConvergeQueues(), logLabels, op.Logger) { logEntry.Info("ConvergeModules: kube config modification detected, restart current converge process", - slog.String("phase", string(op.ConvergeState.GetPhase()))) + slog.String(pkg.LogKeyPhase, string(op.ConvergeState.GetPhase()))) // put ApplyKubeConfig->NewConvergeModulesTask sequence in the beginning of the main queue if kubeConfigTask != nil { op.engine.TaskQueues.GetMain().AddFirst(kubeConfigTask) @@ -174,8 +174,8 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { op.engine.TaskQueues.GetMain().AddAfter(kubeConfigTask.GetId(), reloadTasks[i]) } logEntry.Info("ConvergeModules: kube config modification detected, append tasks to rerun modules", - slog.Int("count", len(reloadTasks)), - slog.Any("modules", modulesToRerun)) + slog.Int(pkg.LogKeyCount, len(reloadTasks)), + slog.Any(pkg.LogKeyModules, modulesToRerun)) op.logTaskAdd(logEntry, "tail", reloadTasks...) } } @@ -187,8 +187,8 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { case HelmReleaseStatusEvent := <-op.HelmResourcesManager.Ch(): logLabels := map[string]string{ - "event.id": uuid.Must(uuid.NewV4()).String(), - "module": HelmReleaseStatusEvent.ModuleName, + pkg.LogKeyEventID: uuid.Must(uuid.NewV4()).String(), + pkg.LogKeyModule: HelmReleaseStatusEvent.ModuleName, } eventLogEntry := utils.EnrichLoggerWithLabels(logEntry, logLabels) @@ -219,8 +219,8 @@ func (op *AddonOperator) StartModuleManagerEventHandler() { op.engine.TaskQueues.GetMain().AddLast(newTask.WithQueuedAt(time.Now())) op.logTaskAdd(logEntry, fmt.Sprintf("detected %s, append", additionalDescription), newTask) } else { - eventLogEntry.With("task.flow", "noop").Info("Detected event, ModuleRun task already queued", - slog.String("description", additionalDescription)) + eventLogEntry.With(pkg.LogKeyTaskFlow, "noop").Info("Detected event, ModuleRun task already queued", + slog.String(pkg.LogKeyDescription, additionalDescription)) } } } diff --git a/pkg/addon-operator/operator.go b/pkg/addon-operator/operator.go index 0ea78b3af..c6a2d6a7d 100644 --- a/pkg/addon-operator/operator.go +++ b/pkg/addon-operator/operator.go @@ -234,7 +234,7 @@ func (op *AddonOperator) Setup() error { return fmt.Errorf("global hooks directory: %s", err) } log.Info("global hooks directory", - slog.String("dir", globalHooksDir)) + slog.String(pkg.LogKeyDir, globalHooksDir)) tempDir, err := ensureTempDirectory(shapp.TempDir) if err != nil { @@ -443,7 +443,7 @@ func (op *AddonOperator) BootstrapMainQueue(tqs *queue.TaskQueueSet) { tqs.NewNamedQueue("main", op.TaskService.Handle, queue.WithCompactionCallback(queueutils.CompactionCallback(op.ModuleManager, op.Logger)), queue.WithCompactableTypes(queueutils.MergeTasks...), - queue.WithLogger(op.Logger.With("operator.component", "mainQueue")), + queue.WithLogger(op.Logger.With(pkg.LogKeyOperatorComponent, "mainQueue")), ) tasks := op.CreateBootstrapTasks(logLabels) @@ -457,7 +457,7 @@ func (op *AddonOperator) BootstrapMainQueue(tqs *queue.TaskQueueSet) { // Add "DiscoverHelmReleases" task to detect unknown releases and purge them. // this task will run only after the first converge, to keep all modules discoverLabels := utils.MergeLabels(logLabels, map[string]string{ - "queue": "main", + pkg.LogKeyQueue: "main", pkg.LogKeyBinding: string(task.DiscoverHelmReleases), }) discoverTask := sh_task.NewTask(task.DiscoverHelmReleases). @@ -482,10 +482,10 @@ func (op *AddonOperator) CreateBootstrapTasks(logLabels map[string]string) []sh_ for _, hookName := range onStartupHooks { hookLogLabels := utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: hookName, - "hook.type": "global", - "queue": "main", - pkg.LogKeyBinding: string(htypes.OnStartup), + pkg.LogKeyHook: hookName, + pkg.LogKeyHookType: "global", + pkg.LogKeyQueue: "main", + pkg.LogKeyBinding: string(htypes.OnStartup), }) onStartupBindingContext := bc.BindingContext{Binding: string(htypes.OnStartup)} @@ -509,10 +509,10 @@ func (op *AddonOperator) CreateBootstrapTasks(logLabels map[string]string) []sh_ schedHooks := op.ModuleManager.GetGlobalHooksInOrder(htypes.Schedule) for _, hookName := range schedHooks { hookLogLabels := utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: hookName, - "hook.type": "global", - "queue": "main", - pkg.LogKeyBinding: string(task.GlobalHookEnableScheduleBindings), + pkg.LogKeyHook: hookName, + pkg.LogKeyHookType: "global", + pkg.LogKeyQueue: "main", + pkg.LogKeyBinding: string(task.GlobalHookEnableScheduleBindings), }) newTask := sh_task.NewTask(task.GlobalHookEnableScheduleBindings). @@ -529,10 +529,10 @@ func (op *AddonOperator) CreateBootstrapTasks(logLabels map[string]string) []sh_ kubeHooks := op.ModuleManager.GetGlobalHooksInOrder(htypes.OnKubernetesEvent) for _, hookName := range kubeHooks { hookLogLabels := utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: hookName, - "hook.type": "global", - "queue": "main", - pkg.LogKeyBinding: string(task.GlobalHookEnableKubernetesBindings), + pkg.LogKeyHook: hookName, + pkg.LogKeyHookType: "global", + pkg.LogKeyQueue: "main", + pkg.LogKeyBinding: string(task.GlobalHookEnableKubernetesBindings), }) newTask := sh_task.NewTask(task.GlobalHookEnableKubernetesBindings). @@ -547,7 +547,7 @@ func (op *AddonOperator) CreateBootstrapTasks(logLabels map[string]string) []sh_ // Task to wait for kubernetes.Synchronization. waitLogLabels := utils.MergeLabels(logLabels, map[string]string{ - "queue": "main", + pkg.LogKeyQueue: "main", pkg.LogKeyBinding: string(task.GlobalHookWaitKubernetesSynchronization), }) waitTask := sh_task.NewTask(task.GlobalHookWaitKubernetesSynchronization). @@ -560,7 +560,7 @@ func (op *AddonOperator) CreateBootstrapTasks(logLabels map[string]string) []sh_ // Add "ConvergeModules" task to run modules converge sequence for the first time. convergeLabels := utils.MergeLabels(logLabels, map[string]string{ - "queue": "main", + pkg.LogKeyQueue: "main", pkg.LogKeyBinding: string(task.ConvergeModules), }) convergeTask := converge.NewConvergeModulesTask(eventDescription, converge.OperatorStartup, convergeLabels) @@ -574,13 +574,13 @@ func (op *AddonOperator) CreateAndStartParallelQueues() { for i := range app.NumberOfParallelQueues { queueName := fmt.Sprintf(app.ParallelQueueNamePattern, i) if op.IsQueueExists(queueName) { - log.Warn("Parallel queue already exists", slog.String("queue", queueName)) + log.Warn("Parallel queue already exists", slog.String(pkg.LogKeyQueue, queueName)) continue } op.startQueue(queueName, op.TaskService.ParallelHandle) log.Debug("Parallel queue started", - slog.String("queue", queueName)) + slog.String(pkg.LogKeyQueue, queueName)) } } @@ -594,7 +594,7 @@ func (op *AddonOperator) startQueue(queueName string, handler func(ctx context.C op.engine.TaskQueues.NewNamedQueue(queueName, handler, queue.WithCompactionCallback(queueutils.CompactionCallback(op.ModuleManager, op.Logger)), queue.WithCompactableTypes(queueutils.MergeTasks...), - queue.WithLogger(op.Logger.With("operator.component", "queue", "queue", queueName)), + queue.WithLogger(op.Logger.With(pkg.LogKeyOperatorComponent, "queue", "queue", queueName)), ) op.engine.TaskQueues.GetByName(queueName).Start(op.ctx) } @@ -615,7 +615,7 @@ func (op *AddonOperator) CreateAndStartQueuesForGlobalHooks() { op.CreateAndStartQueue(hookBinding.Queue) log.Debug("Queue started for global 'schedule' hook", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hookName)) } } @@ -624,7 +624,7 @@ func (op *AddonOperator) CreateAndStartQueuesForGlobalHooks() { op.CreateAndStartQueue(hookBinding.Queue) log.Debug("Queue started for global 'kubernetes' hook", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hookName)) } } @@ -647,7 +647,7 @@ func (op *AddonOperator) CreateAndStartQueuesForModuleHooks(moduleName string) { op.CreateAndStartQueue(hookBinding.Queue) log.Debug("Queue started for module 'schedule'", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hook.GetName())) } } @@ -660,7 +660,7 @@ func (op *AddonOperator) CreateAndStartQueuesForModuleHooks(moduleName string) { op.CreateAndStartQueue(hookBinding.Queue) log.Debug("Queue started for module 'kubernetes'", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hook.GetName())) } } @@ -697,8 +697,8 @@ func (op *AddonOperator) CreateReloadModulesTasks(moduleNames []string, logLabel } newLogLabels := utils.MergeLabels(logLabels) - newLogLabels["module"] = moduleName - delete(newLogLabels, "task.id") + newLogLabels[pkg.LogKeyModule] = moduleName + delete(newLogLabels, pkg.LogKeyTaskID) newTask := sh_task.NewTask(task.ModuleRun). WithLogLabels(newLogLabels). diff --git a/pkg/addon-operator/queue.go b/pkg/addon-operator/queue.go index 1203e2f99..fd876e3ca 100644 --- a/pkg/addon-operator/queue.go +++ b/pkg/addon-operator/queue.go @@ -5,6 +5,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/addon-operator/converge" "github.com/flant/addon-operator/pkg/task" "github.com/flant/addon-operator/pkg/utils" @@ -118,20 +119,20 @@ func RemoveCurrentConvergeTasks(convergeQueues []*queue.TaskQueue, logLabels map case task.ParallelModuleRun: if hm.ParallelRunMetadata == nil || hm.ParallelRunMetadata.CancelF == nil { logEntry.Warn("Couldn't get parallelRun metadata for the parallel task", - slog.String("type", string(t.GetType())), - slog.String("module", hm.ModuleName), - slog.String("description", hm.EventDescription), - slog.String("queue", queue.Name)) + slog.String(pkg.LogKeyType, string(t.GetType())), + slog.String(pkg.LogKeyModule, hm.ModuleName), + slog.String(pkg.LogKeyDescription, hm.EventDescription), + slog.String(pkg.LogKeyQueue, queue.Name)) } else { // cancel parallel task context hm.ParallelRunMetadata.CancelF() } } logEntry.Debug("Drained converge task", - slog.String("type", string(t.GetType())), - slog.String("module", hm.ModuleName), - slog.String("description", hm.EventDescription), - slog.String("queue", queue.Name)) + slog.String(pkg.LogKeyType, string(t.GetType())), + slog.String(pkg.LogKeyModule, hm.ModuleName), + slog.String(pkg.LogKeyDescription, hm.EventDescription), + slog.String(pkg.LogKeyQueue, queue.Name)) return false } return true @@ -176,9 +177,9 @@ func RemoveCurrentConvergeTasksFromId(q *queue.TaskQueue, afterId string, logLab } hm := task.HookMetadataAccessor(t) logEntry.Debug("Drained converge task", - slog.String("type", string(t.GetType())), - slog.String("module", hm.ModuleName), - slog.String("description", hm.EventDescription)) + slog.String(pkg.LogKeyType, string(t.GetType())), + slog.String(pkg.LogKeyModule, hm.ModuleName), + slog.String(pkg.LogKeyDescription, hm.EventDescription)) return false } return true @@ -213,8 +214,8 @@ func RemoveAdjacentConvergeModules(q *queue.TaskQueue, afterId string, logLabels if t.GetType() == task.ConvergeModules { hm := task.HookMetadataAccessor(t) logEntry.Debug("Drained adjacent ConvergeModules task", - slog.String("type", string(t.GetType())), - slog.String("description", hm.EventDescription)) + slog.String(pkg.LogKeyType, string(t.GetType())), + slog.String(pkg.LogKeyDescription, hm.EventDescription)) return false } diff --git a/pkg/helm/helm3lib/helm3lib.go b/pkg/helm/helm3lib/helm3lib.go index 90526c6b9..a7e1eb11b 100644 --- a/pkg/helm/helm3lib/helm3lib.go +++ b/pkg/helm/helm3lib/helm3lib.go @@ -31,6 +31,7 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/rest" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/helm/client" "github.com/flant/addon-operator/pkg/helm/post_renderer" "github.com/flant/addon-operator/pkg/utils" @@ -46,7 +47,7 @@ func Init(opts *Options, logger *log.Logger) error { func ReinitActionConfig(logger *log.Logger) error { logger.Debug("Reinitialize Helm 3 lib action configuration") - return actionConfigInit(logger.With("operator.component", "helm3lib")) + return actionConfigInit(logger.With(pkg.LogKeyOperatorComponent, "helm3lib")) } // LibClient use helm3 package as Go library. @@ -72,7 +73,7 @@ var ( ) func NewClient(logger *log.Logger, labels map[string]string) client.HelmClient { - logEntry := logger.With("operator.component", "helm3lib") + logEntry := logger.With(pkg.LogKeyOperatorComponent, "helm3lib") once.Do(func() { kube.ManagedFieldsManager = "helm" @@ -152,7 +153,7 @@ func initAndVersion(logger *log.Logger) error { return err } - logger.Info("Helm 3 version", slog.String("version", chartutil.DefaultCapabilities.HelmVersion.Version)) + logger.Info("Helm 3 version", slog.String(pkg.LogKeyVersion, chartutil.DefaultCapabilities.HelmVersion.Version)) return nil } @@ -184,7 +185,7 @@ func (h *LibClient) UpgradeRelease(releaseName, modulePath string, valuesPaths [ } return h.upgradeRelease(releaseName, modulePath, valuesPaths, setValues, labels, namespace) } - h.Logger.Debug("helm release upgraded", slog.String("version", releaseName)) + h.Logger.Debug("helm release upgraded", slog.String(pkg.LogKeyVersion, releaseName)) return nil } @@ -235,9 +236,9 @@ func (h *LibClient) upgradeRelease(releaseName, modulePath string, valuesPaths [ } h.Logger.Info("Running helm upgrade for release", - slog.String("release", releaseName), - slog.String("chart", modulePath), - slog.String("namespace", namespace)) + slog.String(pkg.LogKeyRelease, releaseName), + slog.String(pkg.LogKeyChart, modulePath), + slog.String(pkg.LogKeyNamespace, namespace)) histClient := action.NewHistory(actionConfig) // Max is not working!!! Sort the final of releases by your own // histClient.Max = 1 @@ -260,7 +261,7 @@ func (h *LibClient) upgradeRelease(releaseName, modulePath string, valuesPaths [ _, err = instClient.Run(loaded, resultValues) return err } - h.Logger.Debug("old releases found", slog.Int("count", len(releases))) + h.Logger.Debug("old releases found", slog.Int(pkg.LogKeyCount, len(releases))) if len(releases) > 0 { // https://github.com/fluxcd/helm-controller/issues/149 // looking through this issue you can find the common error: another operation (install/upgrade/rollback) is in progress @@ -269,9 +270,9 @@ func (h *LibClient) upgradeRelease(releaseName, modulePath string, valuesPaths [ latestRelease := releases[0] nsReleaseName := fmt.Sprintf("%s/%s", latestRelease.Namespace, latestRelease.Name) h.Logger.Debug("Latest release info", - slog.String("release", nsReleaseName), - slog.Int("version", latestRelease.Version), - slog.String("status", string(latestRelease.Info.Status))) + slog.String(pkg.LogKeyRelease, nsReleaseName), + slog.Int(pkg.LogKeyVersion, latestRelease.Version), + slog.String(pkg.LogKeyStatus, string(latestRelease.Info.Status))) if latestRelease.Info.Status.IsPending() { objectName := fmt.Sprintf("%s.%s.v%d", storage.HelmStorageType, latestRelease.Name, latestRelease.Version) kubeClient, err := actionConfig.KubernetesClientSet() @@ -282,35 +283,35 @@ func (h *LibClient) upgradeRelease(releaseName, modulePath string, valuesPaths [ switch actionConfig.Releases.Name() { case driver.ConfigMapsDriverName: h.Logger.Debug("ConfigMap for helm", - slog.Int("version", latestRelease.Version), - slog.String("release", nsReleaseName), - slog.String("status", string(latestRelease.Info.Status)), - slog.String("driver", driver.ConfigMapsDriverName)) + slog.Int(pkg.LogKeyVersion, latestRelease.Version), + slog.String(pkg.LogKeyRelease, nsReleaseName), + slog.String(pkg.LogKeyStatus, string(latestRelease.Info.Status)), + slog.String(pkg.LogKeyDriver, driver.ConfigMapsDriverName)) err := kubeClient.CoreV1().ConfigMaps(latestRelease.Namespace).Delete(context.TODO(), objectName, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("couldn't delete configmap %s of release %s: %w", objectName, nsReleaseName, err) } - h.Logger.Debug("ConfigMap was deleted", slog.String("name", objectName)) + h.Logger.Debug("ConfigMap was deleted", slog.String(pkg.LogKeyName, objectName)) case driver.SecretsDriverName: h.Logger.Debug("Secret for helm will be deleted", - slog.Int("version", latestRelease.Version), - slog.String("release", nsReleaseName), - slog.String("status", string(latestRelease.Info.Status)), - slog.String("driver", driver.ConfigMapsDriverName)) + slog.Int(pkg.LogKeyVersion, latestRelease.Version), + slog.String(pkg.LogKeyRelease, nsReleaseName), + slog.String(pkg.LogKeyStatus, string(latestRelease.Info.Status)), + slog.String(pkg.LogKeyDriver, driver.ConfigMapsDriverName)) err := kubeClient.CoreV1().Secrets(latestRelease.Namespace).Delete(context.TODO(), objectName, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("couldn't delete secret %s of release %s: %w", objectName, nsReleaseName, err) } - h.Logger.Debug("Secret was deleted", slog.String("name", objectName)) + h.Logger.Debug("Secret was deleted", slog.String(pkg.LogKeyName, objectName)) default: // memory and sql storages a bit more trickier - doing a rollback is justified h.Logger.Debug("Helm will be rollback", - slog.Int("version", latestRelease.Version), - slog.String("release", nsReleaseName), - slog.String("status", string(latestRelease.Info.Status)), - slog.String("driver", driver.ConfigMapsDriverName)) + slog.Int(pkg.LogKeyVersion, latestRelease.Version), + slog.String(pkg.LogKeyRelease, nsReleaseName), + slog.String(pkg.LogKeyStatus, string(latestRelease.Info.Status)), + slog.String(pkg.LogKeyDriver, driver.ConfigMapsDriverName)) h.rollbackLatestRelease(releases) } } @@ -321,9 +322,9 @@ func (h *LibClient) upgradeRelease(releaseName, modulePath string, valuesPaths [ return fmt.Errorf("helm upgrade failed: %s\n", err) } h.Logger.Info("Helm upgrade successful", - slog.String("release", releaseName), - slog.String("chart", modulePath), - slog.String("namespace", namespace)) + slog.String(pkg.LogKeyRelease, releaseName), + slog.String(pkg.LogKeyChart, modulePath), + slog.String(pkg.LogKeyNamespace, namespace)) return nil } @@ -332,7 +333,7 @@ func (h *LibClient) rollbackLatestRelease(releases []*release.Release) { latestRelease := releases[0] nsReleaseName := fmt.Sprintf("%s/%s", latestRelease.Namespace, latestRelease.Name) - h.Logger.Info("Trying to rollback", slog.String("release", nsReleaseName)) + h.Logger.Info("Trying to rollback", slog.String(pkg.LogKeyRelease, nsReleaseName)) if latestRelease.Version == 1 || options.HistoryMax == 1 || len(releases) == 1 { rb := action.NewUninstall(actionConfig) @@ -340,7 +341,7 @@ func (h *LibClient) rollbackLatestRelease(releases []*release.Release) { _, err := rb.Run(latestRelease.Name) if err != nil { h.Logger.Warn("Failed to uninstall pending release", - slog.String("release", nsReleaseName), + slog.String(pkg.LogKeyRelease, nsReleaseName), log.Err(err)) return } @@ -358,13 +359,13 @@ func (h *LibClient) rollbackLatestRelease(releases []*release.Release) { err := rb.Run(latestRelease.Name) if err != nil { h.Logger.Warn("Failed to rollback pending release", - slog.String("release", nsReleaseName), + slog.String(pkg.LogKeyRelease, nsReleaseName), log.Err(err)) return } } - h.Logger.Info("Rollback successful", slog.String("release", nsReleaseName)) + h.Logger.Info("Rollback successful", slog.String(pkg.LogKeyRelease, nsReleaseName)) } func (h *LibClient) GetReleaseValues(releaseName string) (utils.Values, error) { @@ -414,7 +415,7 @@ func (h *LibClient) GetReleaseChecksum(releaseName string) (string, error) { } func (h *LibClient) DeleteRelease(releaseName string) error { - h.Logger.Debug("helm release: execute helm uninstall", slog.String("release", releaseName)) + h.Logger.Debug("helm release: execute helm uninstall", slog.String(pkg.LogKeyRelease, releaseName)) un := action.NewUninstall(actionConfig) _, err := un.Run(releaseName) @@ -422,7 +423,7 @@ func (h *LibClient) DeleteRelease(releaseName string) error { return fmt.Errorf("helm uninstall %s invocation error: %v\n", releaseName, err) } - h.Logger.Debug("helm release deleted", slog.String("release", releaseName)) + h.Logger.Debug("helm release deleted", slog.String(pkg.LogKeyRelease, releaseName)) return nil } @@ -485,8 +486,8 @@ func (h *LibClient) Render(releaseName, modulePath string, valuesPaths, setValue } h.Logger.Debug("Render helm templates for chart ...", - slog.String("chart", modulePath), - slog.String("namespace", namespace)) + slog.String(pkg.LogKeyChart, modulePath), + slog.String(pkg.LogKeyNamespace, namespace)) loaded, err := loadChart(releaseName, modulePath) if err != nil { @@ -516,7 +517,7 @@ func (h *LibClient) Render(releaseName, modulePath string, valuesPaths, setValue rs.Manifest += fmt.Sprintf("\n\n\n%v", err) } - h.Logger.Info("Render helm templates for chart was successful", slog.String("chart", modulePath)) + h.Logger.Info("Render helm templates for chart was successful", slog.String(pkg.LogKeyChart, modulePath)) return rs.Manifest, nil } diff --git a/pkg/helm/nelm/logger.go b/pkg/helm/nelm/logger.go index 71b082a06..2d9931d80 100644 --- a/pkg/helm/nelm/logger.go +++ b/pkg/helm/nelm/logger.go @@ -8,6 +8,8 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" nelmlog "github.com/werf/nelm/pkg/log" + + "github.com/flant/addon-operator/pkg" ) var _ nelmlog.Logger = (*NelmLogger)(nil) @@ -27,7 +29,7 @@ func (n *NelmLogger) Trace(ctx context.Context, format string, a ...interface{}) } func (n *NelmLogger) TraceStruct(ctx context.Context, obj interface{}, format string, a ...interface{}) { - n.logger.Log(ctx, log.LevelTrace.Level(), fmt.Sprintf(format, a...), slog.Any("obj", obj)) + n.logger.Log(ctx, log.LevelTrace.Level(), fmt.Sprintf(format, a...), slog.Any(pkg.LogKeyObj, obj)) } func (n *NelmLogger) TracePush(ctx context.Context, _, format string, a ...interface{}) { diff --git a/pkg/helm/nelm/nelm.go b/pkg/helm/nelm/nelm.go index 0fc7aaffb..a74972118 100644 --- a/pkg/helm/nelm/nelm.go +++ b/pkg/helm/nelm/nelm.go @@ -24,6 +24,7 @@ import ( "k8s.io/client-go/rest" "sigs.k8s.io/yaml" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/helm/client" "github.com/flant/addon-operator/pkg/helm/helm3lib" "github.com/flant/addon-operator/pkg/utils" @@ -84,11 +85,11 @@ func (s *SafeNelmActions) ReleaseGet(ctx context.Context, name, namespace string defer func() { if r := recover(); r != nil { s.logger.Error("panic in ReleaseGet", - slog.Any("panic", r), - slog.String("release", name), - slog.String("namespace", namespace), - slog.Int("revision", opts.Revision), - slog.String("stack", string(debug.Stack())), + slog.Any(pkg.LogKeyPanic, r), + slog.String(pkg.LogKeyRelease, name), + slog.String(pkg.LogKeyNamespace, namespace), + slog.Int(pkg.LogKeyRevision, opts.Revision), + slog.String(pkg.LogKeyStack, string(debug.Stack())), ) err = fmt.Errorf("panic in ReleaseGet: %v", r) } @@ -100,16 +101,16 @@ func (s *SafeNelmActions) ReleaseInstall(ctx context.Context, name, namespace st defer func() { if r := recover(); r != nil { s.logger.Error("panic in ReleaseInstall", - slog.Any("panic", r), - slog.String("release", name), - slog.String("namespace", namespace), - slog.String("chart", opts.Chart), - slog.String("default_chart_name", opts.DefaultChartName), - slog.Bool("force_adoption", opts.ForceAdoption), - slog.Bool("auto_rollback", opts.AutoRollback), - slog.Int("values_files_count", len(opts.ValuesFiles)), - slog.Int("extra_labels_count", len(opts.ExtraLabels)), - slog.String("stack", string(debug.Stack())), + slog.Any(pkg.LogKeyPanic, r), + slog.String(pkg.LogKeyRelease, name), + slog.String(pkg.LogKeyNamespace, namespace), + slog.String(pkg.LogKeyChart, opts.Chart), + slog.String(pkg.LogKeyDefaultChartName, opts.DefaultChartName), + slog.Bool(pkg.LogKeyForceAdoption, opts.ForceAdoption), + slog.Bool(pkg.LogKeyAutoRollback, opts.AutoRollback), + slog.Int(pkg.LogKeyValuesFilesCount, len(opts.ValuesFiles)), + slog.Int(pkg.LogKeyExtraLabelsCount, len(opts.ExtraLabels)), + slog.String(pkg.LogKeyStack, string(debug.Stack())), ) err = fmt.Errorf("panic in ReleaseInstall: %v", r) } @@ -121,12 +122,12 @@ func (s *SafeNelmActions) ReleaseUninstall(ctx context.Context, name, namespace defer func() { if r := recover(); r != nil { s.logger.Error("panic in ReleaseUninstall", - slog.Any("panic", r), - slog.String("release", name), - slog.String("namespace", namespace), - slog.String("delete_propagation", opts.DefaultDeletePropagation), - slog.Bool("delete_release_namespace", opts.DeleteReleaseNamespace), - slog.String("stack", string(debug.Stack())), + slog.Any(pkg.LogKeyPanic, r), + slog.String(pkg.LogKeyRelease, name), + slog.String(pkg.LogKeyNamespace, namespace), + slog.String(pkg.LogKeyDeletePropagation, opts.DefaultDeletePropagation), + slog.Bool(pkg.LogKeyDeleteReleaseNamespace, opts.DeleteReleaseNamespace), + slog.String(pkg.LogKeyStack, string(debug.Stack())), ) err = fmt.Errorf("panic in ReleaseUninstall: %v", r) } @@ -139,9 +140,9 @@ func (s *SafeNelmActions) ReleaseList(ctx context.Context, opts action.ReleaseLi defer func() { if r := recover(); r != nil { s.logger.Error("panic in ReleaseList", - slog.Any("panic", r), - slog.String("namespace", opts.ReleaseNamespace), - slog.String("stack", string(debug.Stack())), + slog.Any(pkg.LogKeyPanic, r), + slog.String(pkg.LogKeyNamespace, opts.ReleaseNamespace), + slog.String(pkg.LogKeyStack, string(debug.Stack())), ) err = fmt.Errorf("panic in ReleaseList: %v", r) } @@ -154,13 +155,13 @@ func (s *SafeNelmActions) ChartRender(ctx context.Context, opts action.ChartRend defer func() { if r := recover(); r != nil { s.logger.Error("panic in ChartRender", - slog.Any("panic", r), - slog.String("chart", opts.Chart), - slog.String("release", opts.ReleaseName), - slog.String("namespace", opts.ReleaseNamespace), - slog.Bool("remote", opts.Remote), - slog.Int("values_files_count", len(opts.ValuesFiles)), - slog.String("stack", string(debug.Stack())), + slog.Any(pkg.LogKeyPanic, r), + slog.String(pkg.LogKeyChart, opts.Chart), + slog.String(pkg.LogKeyRelease, opts.ReleaseName), + slog.String(pkg.LogKeyNamespace, opts.ReleaseNamespace), + slog.Bool(pkg.LogKeyRemote, opts.Remote), + slog.Int(pkg.LogKeyValuesFilesCount, len(opts.ValuesFiles)), + slog.String(pkg.LogKeyStack, string(debug.Stack())), ) err = fmt.Errorf("panic in ChartRender: %v", r) } @@ -199,7 +200,7 @@ func NewNelmClient(opts *CommonOptions, logger *log.Logger, labels map[string]st featgate.FeatGateCleanNullFields.Enable() - nelmLogger := logger.With("operator.component", "nelm") + nelmLogger := logger.With(pkg.LogKeyOperatorComponent, "nelm") return &NelmClient{ logger: nelmLogger, @@ -231,11 +232,11 @@ func (c *NelmClient) GetReleaseLabels(releaseName, labelName string) (string, er ReleaseStorageDriver: c.opts.HelmDriver, }) if err != nil { - c.logger.Debug("Failed to get nelm release", log.Err(err), slog.String("release", releaseName)) + c.logger.Debug("Failed to get nelm release", log.Err(err), slog.String(pkg.LogKeyRelease, releaseName)) var releaseNotFoundErr *action.ReleaseNotFoundError if errors.As(err, &releaseNotFoundErr) { - c.logger.Debug("Release not found when getting labels", slog.String("release", releaseName)) + c.logger.Debug("Release not found when getting labels", slog.String(pkg.LogKeyRelease, releaseName)) // Return the original ReleaseNotFoundError so it can be checked upstream return "", err } @@ -299,9 +300,9 @@ func (c *NelmClient) LastReleaseStatus(releaseName string) (string, string, erro func (c *NelmClient) UpgradeRelease(releaseName, modulePath string, valuesPaths []string, setValues []string, releaseLabels map[string]string, namespace string) error { logger := c.logger.With( - slog.String("release_name", releaseName), - slog.String("chart", modulePath), - slog.String("namespace", namespace), + slog.String(pkg.LogKeyReleaseName, releaseName), + slog.String(pkg.LogKeyChart, modulePath), + slog.String(pkg.LogKeyNamespace, namespace), ) logger.Info("Running nelm upgrade for release") @@ -370,9 +371,9 @@ func (c *NelmClient) UpgradeRelease(releaseName, modulePath string, valuesPaths } logger.Info("Nelm upgrade successful", - slog.String("release", releaseName), - slog.String("chart", modulePath), - slog.String("namespace", namespace)) + slog.String(pkg.LogKeyRelease, releaseName), + slog.String(pkg.LogKeyChart, modulePath), + slog.String(pkg.LogKeyNamespace, namespace)) return nil } @@ -406,7 +407,7 @@ func (c *NelmClient) GetReleaseValues(releaseName string) (utils.Values, error) } func (c *NelmClient) GetReleaseChecksum(releaseName string) (string, error) { - logger := c.logger.With(slog.String("release_name", releaseName)) + logger := c.logger.With(slog.String(pkg.LogKeyReleaseName, releaseName)) releaseGetResult, err := c.actions.ReleaseGet(context.TODO(), releaseName, *c.opts.Namespace, action.ReleaseGetOptions{ KubeConnectionOptions: common.KubeConnectionOptions{ @@ -439,7 +440,7 @@ func (c *NelmClient) GetReleaseChecksum(releaseName string) (string, error) { } func (c *NelmClient) DeleteRelease(releaseName string) error { - c.logger.Debug("nelm release: execute nelm uninstall", slog.String("release", releaseName)) + c.logger.Debug("nelm release: execute nelm uninstall", slog.String(pkg.LogKeyRelease, releaseName)) if err := c.actions.ReleaseUninstall(context.TODO(), releaseName, *c.opts.Namespace, action.ReleaseUninstallOptions{ KubeConnectionOptions: common.KubeConnectionOptions{ @@ -458,7 +459,7 @@ func (c *NelmClient) DeleteRelease(releaseName string) error { return fmt.Errorf("nelm uninstall release %q: %w", releaseName, err) } - c.logger.Debug("nelm release deleted", slog.String("release", releaseName)) + c.logger.Debug("nelm release deleted", slog.String(pkg.LogKeyRelease, releaseName)) return nil } @@ -494,7 +495,7 @@ func (c *NelmClient) ListReleasesNames() ([]string, error) { chartName = release.Chart.Name } if release.Name == "" { - c.logger.Warn("release name is empty, skipped", slog.String("chart", chartName)) + c.logger.Warn("release name is empty, skipped", slog.String(pkg.LogKeyChart, chartName)) continue } @@ -508,8 +509,8 @@ func (c *NelmClient) ListReleasesNames() ([]string, error) { func (c *NelmClient) Render(releaseName, modulePath string, valuesPaths, setValues []string, releaseLabels map[string]string, namespace string, debug bool) (string, error) { c.logger.Debug("Render nelm templates for chart ...", - slog.String("chart", modulePath), - slog.String("namespace", namespace)) + slog.String(pkg.LogKeyChart, modulePath), + slog.String(pkg.LogKeyNamespace, namespace)) // Add client annotations extraAnnotations := make(map[string]string) @@ -551,7 +552,7 @@ func (c *NelmClient) Render(releaseName, modulePath string, valuesPaths, setValu return "", fmt.Errorf("render nelm chart %q: %w", modulePath, err) } - c.logger.Info("Render nelm templates for chart was successful", slog.String("chart", modulePath)) + c.logger.Info("Render nelm templates for chart was successful", slog.String(pkg.LogKeyChart, modulePath)) var result strings.Builder for _, resource := range chartRenderResult.Resources { diff --git a/pkg/helm_resources_manager/helm_resources_manager.go b/pkg/helm_resources_manager/helm_resources_manager.go index 9c597b5d9..53d846fbe 100644 --- a/pkg/helm_resources_manager/helm_resources_manager.go +++ b/pkg/helm_resources_manager/helm_resources_manager.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/labels" cr_cache "sigs.k8s.io/controller-runtime/pkg/cache" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/app" . "github.com/flant/addon-operator/pkg/helm_resources_manager/types" klient "github.com/flant/kube-client/client" @@ -113,7 +114,7 @@ func (hm *helmResourcesManager) Ch() chan ReleaseStatusEvent { func (hm *helmResourcesManager) StartMonitor(moduleName string, manifests []manifest.Manifest, defaultNamespace string, lastReleaseStatus func(releaseName string) (revision string, status string, err error)) { log.Debug("Start helm resources monitor for module", - slog.String("module", moduleName)) + slog.String(pkg.LogKeyModule, moduleName)) hm.StopMonitor(moduleName) cfg := &ResourceMonitorConfig{ @@ -138,12 +139,12 @@ func (hm *helmResourcesManager) StartMonitor(moduleName string, manifests []mani func (hm *helmResourcesManager) absentResourcesCallback(moduleName string, unexpectedStatus bool, absent []manifest.Manifest, defaultNs string) { log.Debug("Detect absent resources for module", - slog.String("module", moduleName)) + slog.String(pkg.LogKeyModule, moduleName)) for _, m := range absent { log.Debug("absent module", - slog.String("namespace", m.Namespace(defaultNs)), - slog.String("kind", m.Kind()), - slog.String("module", m.Name())) + slog.String(pkg.LogKeyNamespace, m.Namespace(defaultNs)), + slog.String(pkg.LogKeyKind, m.Kind()), + slog.String(pkg.LogKeyModule, m.Name())) } hm.eventCh <- ReleaseStatusEvent{ ModuleName: moduleName, diff --git a/pkg/helm_resources_manager/resources_monitor.go b/pkg/helm_resources_manager/resources_monitor.go index eeaeaac46..696913ba3 100644 --- a/pkg/helm_resources_manager/resources_monitor.go +++ b/pkg/helm_resources_manager/resources_monitor.go @@ -15,6 +15,7 @@ import ( cr_cache "sigs.k8s.io/controller-runtime/pkg/cache" cr_client "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/flant/addon-operator/pkg" klient "github.com/flant/kube-client/client" "github.com/flant/kube-client/manifest" ) @@ -76,7 +77,7 @@ func NewResourcesMonitor(ctx context.Context, cfg *ResourceMonitorConfig) *Resou absentCb: cfg.AbsentCb, helmStatusGetter: cfg.HelmStatusGetter, - logger: cfg.Logger.With("operator.component", "HelmResourceMonitor"), + logger: cfg.Logger.With(pkg.LogKeyOperatorComponent, "HelmResourceMonitor"), } } @@ -107,8 +108,8 @@ func (r *ResourcesMonitor) Start() { if status != "deployed" { r.logger.Debug("Helm release is in unexpected status", - slog.String("module", r.moduleName), - slog.String("status", status)) + slog.String(pkg.LogKeyModule, r.moduleName), + slog.String(pkg.LogKeyStatus, status)) if r.absentCb != nil { r.absentCb(r.moduleName, true, []manifest.Manifest{}, r.defaultNamespace) } @@ -144,9 +145,9 @@ func (r *ResourcesMonitor) GetHelmReleaseStatus(moduleName string) (string, erro return "", err } r.logger.Debug("Helm release", - slog.String("module", moduleName), - slog.String("revision", revision), - slog.String("status", status)) + slog.String(pkg.LogKeyModule, moduleName), + slog.String(pkg.LogKeyRevision, revision), + slog.String(pkg.LogKeyStatus, status)) return status, nil } @@ -287,7 +288,7 @@ func (r *ResourcesMonitor) listResources(ctx context.Context, nsgvk namespacedGV Kind: nsgvk.GVK.Kind + "List", }) log.Debug("List objects from cache", - slog.String("nsgvk", fmt.Sprintf("%v", nsgvk))) + slog.String(pkg.LogKeyNsgvk, fmt.Sprintf("%v", nsgvk))) err := r.cache.List(ctx, objList, cr_client.InNamespace(nsgvk.Namespace)) if err != nil { return nil, fmt.Errorf("couldn't list objects from cache: %v", err) diff --git a/pkg/kube_config_manager/backend/configmap/configmap.go b/pkg/kube_config_manager/backend/configmap/configmap.go index b5fd0ad43..60504db14 100644 --- a/pkg/kube_config_manager/backend/configmap/configmap.go +++ b/pkg/kube_config_manager/backend/configmap/configmap.go @@ -16,6 +16,7 @@ import ( corev1 "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/tools/cache" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/kube_config_manager/config" kcmcontext "github.com/flant/addon-operator/pkg/kube_config_manager/context" "github.com/flant/addon-operator/pkg/utils" @@ -38,7 +39,7 @@ func New(kubeClient *client.Client, namespace, name string, logger *log.Logger) } backend := &Backend{ - logger: logger.With("operator.component", "ConfigHandler"). + logger: logger.With(pkg.LogKeyOperatorComponent, "ConfigHandler"). With("backend", "configmap"), namespace: namespace, name: name, @@ -58,7 +59,7 @@ func (b Backend) LoadConfig(ctx context.Context, _ ...string) (*config.KubeConfi if obj == nil { b.logger.Info("Initial config from ConfigMap: resource is not found", - slog.String("name", b.name)) + slog.String(pkg.LogKeyName, b.name)) return nil, nil } @@ -82,10 +83,10 @@ func (b Backend) saveGlobalConfigValues(ctx context.Context, values utils.Values if b.isDebugEnabled(ctx) { b.logger.Info("Save global values to ConfigMap", - slog.String("name", b.name), - slog.String("values", values.DebugString())) + slog.String(pkg.LogKeyName, b.name), + slog.String(pkg.LogKeyValues, values.DebugString())) } else { - b.logger.Info("Save global values to ConfigMap", slog.String("name", b.name)) + b.logger.Info("Save global values to ConfigMap", slog.String(pkg.LogKeyName, b.name)) } err := b.mergeValues(ctx, "global", globalKubeConfig.GetValues()) @@ -107,13 +108,13 @@ func (b Backend) saveModuleConfigValues(ctx context.Context, moduleName string, if b.isDebugEnabled(ctx) { b.logger.Info("Save module values to ConfigMap", - slog.String("moduleName", moduleName), - slog.String("configMapName", b.name), - slog.String("values", values.DebugString())) + slog.String(pkg.LogKeyModuleName, moduleName), + slog.String(pkg.LogKeyConfigMapName, b.name), + slog.String(pkg.LogKeyValues, values.DebugString())) } else { b.logger.Info("Save module values to ConfigMap", - slog.String("moduleName", moduleName), - slog.String("configMapName", b.name)) + slog.String(pkg.LogKeyModuleName, moduleName), + slog.String(pkg.LogKeyConfigMapName, b.name)) } err := b.mergeValues(ctx, utils.ModuleNameToValuesKey(moduleName), moduleKubeConfig.GetValues()) //nolint: staticcheck,nolintlint @@ -354,7 +355,7 @@ func (b Backend) StartInformer(ctx context.Context, eventC chan config.Event) { err := b.handleConfigMapEvent(obj.(*v1.ConfigMap), eventC) if err != nil { b.logger.Error("Handle ConfigMap 'add' error", - slog.String("configMapName", b.name), + slog.String(pkg.LogKeyConfigMapName, b.name), log.Err(err)) } }, @@ -363,7 +364,7 @@ func (b Backend) StartInformer(ctx context.Context, eventC chan config.Event) { err := b.handleConfigMapEvent(obj.(*v1.ConfigMap), eventC) if err != nil { b.logger.Error("Handle ConfigMap 'update' error", - slog.String("configMapName", b.name), + slog.String(pkg.LogKeyConfigMapName, b.name), log.Err(err)) } }, @@ -386,15 +387,15 @@ func (b Backend) logConfigMapEvent(ctx context.Context, obj interface{}, eventNa objYaml, err := yaml.Marshal(obj) if err != nil { b.logger.Info("Dump ConfigMap error", - slog.String("configMapName", b.name), - slog.String("eventName", eventName), + slog.String(pkg.LogKeyConfigMapName, b.name), + slog.String(pkg.LogKeyEventName, eventName), log.Err(err)) return } b.logger.Info("Dump ConfigMap", - slog.String("configMapName", b.name), - slog.String("eventName", eventName), - slog.String("value", string(objYaml))) + slog.String(pkg.LogKeyConfigMapName, b.name), + slog.String(pkg.LogKeyEventName, eventName), + slog.String(pkg.LogKeyValue, string(objYaml))) } func (b Backend) handleConfigMapEvent(obj *v1.ConfigMap, eventC chan config.Event) error { @@ -409,7 +410,7 @@ func (b Backend) handleConfigMapEvent(obj *v1.ConfigMap, eventC chan config.Even eventC <- config.Event{Key: "batch", Err: err} // Do not update caches to detect changes on next update. b.logger.Error("ConfigMap invalid", - slog.String("configMapName", b.name), + slog.String(pkg.LogKeyConfigMapName, b.name), log.Err(err)) return err } diff --git a/pkg/kube_config_manager/kube_config_manager.go b/pkg/kube_config_manager/kube_config_manager.go index 0b8baa9a2..f03de7e03 100644 --- a/pkg/kube_config_manager/kube_config_manager.go +++ b/pkg/kube_config_manager/kube_config_manager.go @@ -10,6 +10,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/kube_config_manager/backend" "github.com/flant/addon-operator/pkg/kube_config_manager/config" kcmcontext "github.com/flant/addon-operator/pkg/kube_config_manager/context" @@ -40,8 +41,8 @@ type KubeConfigManager struct { func NewKubeConfigManager(ctx context.Context, bk backend.ConfigHandler, runtimeConfig *runtimeConfig.Config, logger *log.Logger) *KubeConfigManager { cctx, cancel := context.WithCancel(ctx) - logger = logger.With("component", "KubeConfigManager") - logger.With("backend", fmt.Sprintf("%T", bk)).Info("Setup KubeConfigManager backend") + logger = logger.With(pkg.LogKeyComponent, "KubeConfigManager") + logger.With(pkg.LogKeyBackend, fmt.Sprintf("%T", bk)).Info("Setup KubeConfigManager backend") // Runtime config to enable logging all events from the ConfigMap at runtime. if runtimeConfig != nil { @@ -212,7 +213,7 @@ func (kcm *KubeConfigManager) handleConfigEvent(obj config.Event) { Type: config.KubeConfigInvalid, } kcm.logger.Error("Config invalid", - slog.String("name", obj.Key), + slog.String(pkg.LogKeyName, obj.Key), log.Err(obj.Err)) return } @@ -265,7 +266,7 @@ func (kcm *KubeConfigManager) handleDeleteEvent(moduleName string, cfg *config.M moduleMaintenanceChanged := make(map[string]utils.Maintenance) - kcm.logger.Info("module section deleted", slog.String("name", moduleName)) + kcm.logger.Info("module section deleted", slog.String(pkg.LogKeyName, moduleName)) modulesChanged = append(modulesChanged, moduleName) if kcm.currentConfig.Modules[moduleName].GetEnabled() != "" && kcm.currentConfig.Modules[moduleName].GetEnabled() != "n/d" { modulesStateChanged = append(modulesStateChanged, moduleName) @@ -390,10 +391,10 @@ func (kcm *KubeConfigManager) handleBatchConfigEvent(obj config.Event) { } kcm.logger.Info("Module section changed. Enabled flag transition", - slog.String("moduleName", moduleName), - slog.String("previous", kcm.currentConfig.Modules[moduleName].GetEnabled()), - slog.String("current", moduleCfg.GetEnabled()), - slog.String("maintenanceFlag", moduleCfg.GetMaintenanceState().String())) + slog.String(pkg.LogKeyModuleName, moduleName), + slog.String(pkg.LogKeyPrevious, kcm.currentConfig.Modules[moduleName].GetEnabled()), + slog.String(pkg.LogKeyCurrent, moduleCfg.GetEnabled()), + slog.String(pkg.LogKeyMaintenanceFlag, moduleCfg.GetMaintenanceState().String())) } else { modulesChanged = append(modulesChanged, moduleName) if moduleCfg.GetEnabled() != "" && moduleCfg.GetEnabled() != "n/d" { @@ -405,9 +406,9 @@ func (kcm *KubeConfigManager) handleBatchConfigEvent(obj config.Event) { } kcm.logger.Info("Module section added", - slog.String("moduleName", moduleName), - slog.String("enabledFlag", moduleCfg.GetEnabled()), - slog.String("maintenanceFlag", moduleCfg.GetMaintenanceState().String())) + slog.String(pkg.LogKeyModuleName, moduleName), + slog.String(pkg.LogKeyEnabledFlag, moduleCfg.GetEnabled()), + slog.String(pkg.LogKeyMaintenanceFlag, moduleCfg.GetMaintenanceState().String())) } } } @@ -421,7 +422,7 @@ func (kcm *KubeConfigManager) handleBatchConfigEvent(obj config.Event) { } } kcm.logger.Info("Module sections deleted", - slog.String("modules", fmt.Sprintf("%+v", currentModuleNames))) + slog.String(pkg.LogKeyModules, fmt.Sprintf("%+v", currentModuleNames))) } // Update state after successful parsing. diff --git a/pkg/labels.go b/pkg/labels.go index c60537191..1e3c3edd1 100644 --- a/pkg/labels.go +++ b/pkg/labels.go @@ -11,6 +11,108 @@ const ( LogKeyTaskID = "task.id" LogKeyTaskFlow = "task.flow" LogKeyWatchEvent = "watchEvent" + + LogKeyAfterChecksum = "afterChecksum" + LogKeyArgs = "args" + LogKeyAutoRollback = "auto_rollback" + LogKeyBackend = "backend" + LogKeyBeforeChecksum = "beforeChecksum" + LogKeyBindings = "bindings" + LogKeyChannels = "channels" + LogKeyChart = "chart" + LogKeyChecksum = "checksum" + LogKeyCombinedMonitorIDs = "combinedMonitorIDs" + LogKeyComponent = "component" + LogKeyConfig = "config" + LogKeyConfigMapName = "configMapName" + LogKeyCount = "count" + LogKeyCritical = "critical" + LogKeyCurrent = "current" + LogKeyCurrentEnvironment = "currentEnvironment" + LogKeyDefaultChartName = "default_chart_name" + LogKeyDeletePropagation = "delete_propagation" + LogKeyDeleteReleaseNamespace = "delete_release_namespace" + LogKeyDescription = "description" + LogKeyDiff = "diff" + LogKeyDir = "dir" + LogKeyDone = "done" + LogKeyDriver = "driver" + LogKeyEnabledFlag = "enabledFlag" + LogKeyEnabledModules = "enabledModules" + LogKeyError = "error" + LogKeyEvent = "event" + LogKeyEventID = "event.id" + LogKeyEventName = "eventName" + LogKeyEventSource = "event_source" + LogKeyExtender = "extender" + LogKeyEventTriggeredByBinding = "event.triggered-by.binding" + LogKeyEventTriggeredByBindingName = "event.triggered-by.binding.name" + LogKeyEventTriggeredByHook = "event.triggered-by.hook" + LogKeyEventTriggeredByWatchEvent = "event.triggered-by.watchEvent" + LogKeyExtraLabelsCount = "extra_labels_count" + LogKeyFile = "file" + LogKeyFinalList = "finalList" + LogKeyForceAdoption = "force_adoption" + LogKeyGlobalSectionChanged = "globalSectionChanged" + LogKeyGot = "got" + LogKeyHookFilePath = "hook_file_path" + LogKeyHookName = "hookName" + LogKeyHookNameSnake = "hook_name" + LogKeyID = "id" + LogKeyKey = "key" + LogKeyKind = "kind" + LogKeyMaintenanceFlag = "maintenanceFlag" + LogKeyModuleEnabledStateChanged = "moduleEnabledStateChanged" + LogKeyModuleMaintenanceChanged = "ModuleMaintenanceChanged" + LogKeyModuleName = "moduleName" + LogKeyModuleNameSnake = "module_name" + LogKeyModules = "modules" + LogKeyModuleStartup = "moduleStartup" + LogKeyModuleState = "module.state" + LogKeyModulesToDisable = "modulesToDisable" + LogKeyModulesToEnable = "modulesToEnable" + LogKeyModuleValuesChanged = "moduleValuesChanged" + LogKeyMonitorIDs = "monitorIDs" + LogKeyName = "name" + LogKeyNamespace = "namespace" + LogKeyNewChecksum = "newChecksum" + LogKeyNsgvk = "nsgvk" + LogKeyObj = "obj" + LogKeyOldValues = "oldValues" + LogKeyOperation = "operation" + LogKeyOperatorComponent = "operator.component" + LogKeyOutput = "output" + LogKeyPanic = "panic" + LogKeyPath = "path" + LogKeyPaths = "paths" + LogKeyPhase = "phase" + LogKeyPrevious = "previous" + LogKeyPythonVenv = "pythonVenv" + LogKeyRelease = "release" + LogKeyReleaseName = "release_name" + LogKeyRemote = "remote" + LogKeyRequest = "request" + LogKeyResult = "result" + LogKeyRevision = "revision" + LogKeyRoute = "route" + LogKeyRoutes = "routes" + LogKeySavedChecksum = "savedChecksum" + LogKeyScheduled = "scheduled" + LogKeyStack = "stack" + LogKeyState = "state" + LogKeyStatus = "status" + LogKeySyncDone = "syncDone" + LogKeySyncNeeded = "syncNeeded" + LogKeySyncQueued = "syncQueued" + LogKeyTargetEnvironment = "targetEnvironment" + LogKeyTaskType = "task_type" + LogKeyType = "type" + LogKeyValue = "value" + LogKeyValues = "values" + LogKeyValuesFilesCount = "values_files_count" + LogKeyValuesType = "valuesType" + LogKeyVersion = "version" + LogKeyVertex = "vertex" ) const ( diff --git a/pkg/module_manager/environment_manager/evironment_manager.go b/pkg/module_manager/environment_manager/evironment_manager.go index cae077065..2e758454c 100644 --- a/pkg/module_manager/environment_manager/evironment_manager.go +++ b/pkg/module_manager/environment_manager/evironment_manager.go @@ -11,6 +11,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/utils" ) @@ -74,7 +75,7 @@ func makedev(majorNumber int64, minorNumber int64) int { func (m *Manager) DisassembleEnvironmentForModule(moduleName, modulePath string, targetEnvironment Environment) error { logEntry := utils.EnrichLoggerWithLabels(m.logger, map[string]string{ - "operator.component": "EnvironmentManager.DisassembleEnvironmentForModule", + pkg.LogKeyOperatorComponent: "EnvironmentManager.DisassembleEnvironmentForModule", }) m.l.Lock() defer m.l.Unlock() @@ -85,9 +86,9 @@ func (m *Manager) DisassembleEnvironmentForModule(moduleName, modulePath string, } logEntry.Debug("Disassembling environment", - slog.String("module", moduleName), - slog.Any("currentEnvironment", currentEnvironment), - slog.Any("targetEnvironment", targetEnvironment)) + slog.String(pkg.LogKeyModule, moduleName), + slog.Any(pkg.LogKeyCurrentEnvironment, currentEnvironment), + slog.Any(pkg.LogKeyTargetEnvironment, targetEnvironment)) chrootedModuleEnvPath := filepath.Join(m.chroot, moduleName) for _, properties := range m.objects { @@ -131,7 +132,7 @@ func (m *Manager) DisassembleEnvironmentForModule(moduleName, modulePath string, func (m *Manager) AssembleEnvironmentForModule(moduleName, modulePath string, targetEnvironment Environment) error { logEntry := utils.EnrichLoggerWithLabels(m.logger, map[string]string{ - "operator.component": "EnvironmentManager.AssembleEnvironmentForModule", + pkg.LogKeyOperatorComponent: "EnvironmentManager.AssembleEnvironmentForModule", }) m.l.Lock() @@ -143,17 +144,17 @@ func (m *Manager) AssembleEnvironmentForModule(moduleName, modulePath string, ta } logEntry.Debug("Preparing environment", - slog.String("module", moduleName), - slog.Any("currentEnvironment", currentEnvironment), - slog.Any("targetEnvironment", targetEnvironment)) + slog.String(pkg.LogKeyModule, moduleName), + slog.Any(pkg.LogKeyCurrentEnvironment, currentEnvironment), + slog.Any(pkg.LogKeyTargetEnvironment, targetEnvironment)) chrootedModuleEnvPath := filepath.Join(m.chroot, moduleName) if currentEnvironment == NoEnvironment { logEntry.Debug("Preparing environment - creating the module's directory", - slog.String("module", moduleName), - slog.Any("currentEnvironment", currentEnvironment), - slog.Any("targetEnvironment", targetEnvironment)) + slog.String(pkg.LogKeyModule, moduleName), + slog.Any(pkg.LogKeyCurrentEnvironment, currentEnvironment), + slog.Any(pkg.LogKeyTargetEnvironment, targetEnvironment)) chrootedModuleDir := filepath.Join(chrootedModuleEnvPath, modulePath) if err := os.MkdirAll(chrootedModuleDir, 0o755); err != nil { diff --git a/pkg/module_manager/loader/fs/fs.go b/pkg/module_manager/loader/fs/fs.go index eee5b00cc..e04cd9be4 100644 --- a/pkg/module_manager/loader/fs/fs.go +++ b/pkg/module_manager/loader/fs/fs.go @@ -13,6 +13,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/app" "github.com/flant/addon-operator/pkg/module_manager/models/modules" "github.com/flant/addon-operator/pkg/utils" @@ -196,7 +197,7 @@ func resolveDirEntry(dirPath string, entry os.DirEntry) (string, string, error) if errors.As(err, &e) { if e.Err.Error() == "no such file or directory" { log.Warn("Symlink target does not exist. Ignoring module", - slog.String("dir", dirPath)) + slog.String(pkg.LogKeyDir, dirPath)) return "", "", nil } } @@ -210,7 +211,7 @@ func resolveDirEntry(dirPath string, entry os.DirEntry) (string, string, error) if name != utils.ValuesFileName { log.Warn("Ignore dir while searching for modules", - slog.String("dir", absPath)) + slog.String(pkg.LogKeyDir, absPath)) } return "", "", nil } diff --git a/pkg/module_manager/models/hooks/kind/batch_hook.go b/pkg/module_manager/models/hooks/kind/batch_hook.go index abbaaaff4..e90a3874d 100644 --- a/pkg/module_manager/models/hooks/kind/batch_hook.go +++ b/pkg/module_manager/models/hooks/kind/batch_hook.go @@ -143,7 +143,7 @@ func (h *BatchHook) Execute(ctx context.Context, configVersion string, bContext if err != nil { h.Hook.Logger.With(pkg.LogKeyHook, h.GetName()). Error("Remove tmp file", - slog.String("file", f), + slog.String(pkg.LogKeyFile, f), log.Err(err)) } } @@ -503,8 +503,8 @@ func (h *BatchHook) prepareConfigValuesJsonFile(moduleSafeName string, configVal } h.Hook.Logger.Debug("Prepared module hook config values", - slog.String("module", moduleSafeName), - slog.String("values", configValues.DebugString())) + slog.String(pkg.LogKeyModule, moduleSafeName), + slog.String(pkg.LogKeyValues, configValues.DebugString())) return path, nil } @@ -522,8 +522,8 @@ func (h *BatchHook) prepareValuesJsonFile(moduleSafeName string, values utils.Va } h.Hook.Logger.Debug("Prepared module hook values", - slog.String("module", moduleSafeName), - slog.String("values", values.DebugString())) + slog.String(pkg.LogKeyModule, moduleSafeName), + slog.String(pkg.LogKeyValues, values.DebugString())) return path, nil } diff --git a/pkg/module_manager/models/hooks/kind/check.go b/pkg/module_manager/models/hooks/kind/check.go index 872c97c77..e85fa26d8 100644 --- a/pkg/module_manager/models/hooks/kind/check.go +++ b/pkg/module_manager/models/hooks/kind/check.go @@ -25,6 +25,7 @@ import ( "github.com/deckhouse/module-sdk/pkg/settingscheck" "github.com/google/uuid" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/utils" "github.com/flant/shell-operator/pkg/executor" ) @@ -54,7 +55,7 @@ func (c *SettingsCheck) Check(ctx context.Context, settings utils.Values) (setti // Remove tmp files after execution defer func() { if err = os.Remove(tmp); err != nil { - c.logger.Error("remove tmp file", slog.String("file", tmp), log.Err(err)) + c.logger.Error("remove tmp file", slog.String(pkg.LogKeyFile, tmp), log.Err(err)) } }() diff --git a/pkg/module_manager/models/hooks/kind/shellhook.go b/pkg/module_manager/models/hooks/kind/shellhook.go index e3a215518..2bf82091f 100644 --- a/pkg/module_manager/models/hooks/kind/shellhook.go +++ b/pkg/module_manager/models/hooks/kind/shellhook.go @@ -64,10 +64,10 @@ func NewShellHook(name, path, moduleName string, keepTemporaryHookFiles bool, lo } logger.Debug("Created a new shell hook", - slog.String("module", sh.moduleName), - slog.String("hookName", sh.Name), - slog.String("path", sh.Path), - slog.String("pythonVenv", sh.pythonVenv), + slog.String(pkg.LogKeyModule, sh.moduleName), + slog.String(pkg.LogKeyHookName, sh.Name), + slog.String(pkg.LogKeyPath, sh.Path), + slog.String(pkg.LogKeyPythonVenv, sh.pythonVenv), ) return sh @@ -146,7 +146,7 @@ func (sh *ShellHook) Execute(ctx context.Context, configVersion string, bContext if err != nil { sh.Hook.Logger.With(pkg.LogKeyHook, sh.GetName()). Error("Remove tmp file", - slog.String("file", f), + slog.String(pkg.LogKeyFile, f), log.Err(err)) } } @@ -218,8 +218,8 @@ func (sh *ShellHook) Execute(ctx context.Context, configVersion string, bContext // version and a warning is thrown. func (sh *ShellHook) updateExecutorParamsForPython(command *string, envs, args *[]string) { sh.Hook.Logger.Debug("Updating hook's exec parameters", - slog.String("module", sh.moduleName), - slog.String("hookName", sh.Name), + slog.String(pkg.LogKeyModule, sh.moduleName), + slog.String(pkg.LogKeyHookName, sh.Name), ) if len(sh.pythonVenv) != 0 { @@ -230,8 +230,8 @@ func (sh *ShellHook) updateExecutorParamsForPython(command *string, envs, args * *args = append(newArgs, *args...) } else { sh.Hook.Logger.Warn("Module executes python hooks, but has no python virtual environment", - slog.String("module", sh.moduleName), - slog.String("hookName", sh.Name), + slog.String(pkg.LogKeyModule, sh.moduleName), + slog.String(pkg.LogKeyHookName, sh.Name), ) } } @@ -257,20 +257,20 @@ func (sh *ShellHook) getConfig() ([]byte, error) { WithChroot(utils.GetModuleChrootPath(sh.moduleName)) sh.Hook.Logger.Debug("Executing hook", - slog.String("args", strings.Join(args, " "))) + slog.String(pkg.LogKeyArgs, strings.Join(args, " "))) output, err := cmd.Output() if err != nil { sh.Hook.Logger.Debug("Hook config failed", slog.String(pkg.LogKeyHook, sh.Name), log.Err(err), - slog.String("output", string(output))) + slog.String(pkg.LogKeyOutput, string(output))) return nil, err } sh.Hook.Logger.Debug("Hook config output", slog.String(pkg.LogKeyHook, sh.Name), - slog.String("output", string(output))) + slog.String(pkg.LogKeyOutput, string(output))) return output, nil } @@ -569,8 +569,8 @@ func (sh *ShellHook) prepareConfigValuesJsonFile(moduleSafeName string, configVa } sh.Hook.Logger.Debug("Prepared module hook config values", - slog.String("module", moduleSafeName), - slog.String("values", configValues.DebugString())) + slog.String(pkg.LogKeyModule, moduleSafeName), + slog.String(pkg.LogKeyValues, configValues.DebugString())) return path, nil } @@ -588,8 +588,8 @@ func (sh *ShellHook) prepareValuesJsonFile(moduleSafeName string, values utils.V } sh.Hook.Logger.Debug("Prepared module hook values", - slog.String("module", moduleSafeName), - slog.String("values", values.DebugString())) + slog.String(pkg.LogKeyModule, moduleSafeName), + slog.String(pkg.LogKeyValues, values.DebugString())) return path, nil } diff --git a/pkg/module_manager/models/hooks/module_hook.go b/pkg/module_manager/models/hooks/module_hook.go index ddb3ee431..b5de31908 100644 --- a/pkg/module_manager/models/hooks/module_hook.go +++ b/pkg/module_manager/models/hooks/module_hook.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/flant/addon-operator/pkg" addon_op_types "github.com/flant/addon-operator/pkg/hook/types" gohook "github.com/flant/addon-operator/pkg/module_manager/go_hook" "github.com/flant/addon-operator/pkg/module_manager/models/hooks/kind" @@ -112,11 +113,11 @@ func (mh *ModuleHook) ApplyBindingActions(bindingActions []gohook.BindingAction) // Empty kind - "null" monitor. monitorCfg.Kind = "" monitorCfg.ApiVersion = "" - monitorCfg.Metadata.MetricLabels["kind"] = "" + monitorCfg.Metadata.MetricLabels[pkg.LogKeyKind] = "" case "updatekind": monitorCfg.Kind = action.Kind monitorCfg.ApiVersion = action.ApiVersion - monitorCfg.Metadata.MetricLabels["kind"] = action.Kind + monitorCfg.Metadata.MetricLabels[pkg.LogKeyKind] = action.Kind default: continue } diff --git a/pkg/module_manager/models/modules/basic.go b/pkg/module_manager/models/modules/basic.go index 5cbbfcf07..8b99900e0 100644 --- a/pkg/module_manager/models/modules/basic.go +++ b/pkg/module_manager/models/modules/basic.go @@ -251,12 +251,12 @@ func (bm *BasicModule) RegisterHooks(logger *log.Logger) ([]*hooks.ModuleHook, e return nil, fmt.Errorf("search module hooks failed: %w", err) } - logger.Debug("Found hooks", slog.Int("count", len(searchModuleHooksResult.Hooks))) + logger.Debug("Found hooks", slog.Int(pkg.LogKeyCount, len(searchModuleHooksResult.Hooks))) if logger.GetLevel() == log.LevelDebug { for _, h := range searchModuleHooksResult.Hooks { logger.Debug("ModuleHook", - slog.String("name", h.GetName()), - slog.String("path", h.GetPath())) + slog.String(pkg.LogKeyName, h.GetName()), + slog.String(pkg.LogKeyPath, h.GetPath())) } } @@ -339,7 +339,7 @@ func (bm *BasicModule) searchModuleShellHooks() ([]*kind.ShellHook, error) { // sort hooks by path sort.Strings(hooksRelativePaths) bm.logger.Debug("Hook paths", - slog.Any("paths", hooksRelativePaths)) + slog.Any(pkg.LogKeyPaths, hooksRelativePaths)) var ( checkPythonEnv sync.Once @@ -371,7 +371,7 @@ func (bm *BasicModule) searchModuleShellHooks() ([]*kind.ShellHook, error) { continue } - bm.logger.Warn("get batch hook config", slog.String("hook_file_path", hookPath), log.Err(err)) + bm.logger.Warn("get batch hook config", slog.String(pkg.LogKeyHookFilePath, hookPath), log.Err(err)) } shHook := kind.NewShellHook(hookName, hookPath, bm.safeName(), bm.keepTemporaryHookFiles, shapp.LogProxyHookJSON, bm.logger.Named("shell-hook"), options...) @@ -407,7 +407,7 @@ func (bm *BasicModule) searchModuleBatchHooks() (*searchModuleBatchHooksResult, // sort hooks by path sort.Strings(hooksRelativePaths) - bm.logger.Debug("sorted paths", slog.Any("paths", hooksRelativePaths)) + bm.logger.Debug("sorted paths", slog.Any(pkg.LogKeyPaths, hooksRelativePaths)) for _, hookPath := range hooksRelativePaths { hookName, err := normalizeHookPath(filepath.Dir(bm.Path), hookPath) @@ -463,12 +463,12 @@ func RecursiveGetBatchHookExecutablePaths(moduleName, dir string, logger *log.Lo if err := isExecutableBatchHookFile(moduleName, path, f); err != nil { if errors.Is(err, ErrFileNoExecutablePermissions) { - logger.Warn("file is skipped", slog.String("path", path), log.Err(err)) + logger.Warn("file is skipped", slog.String(pkg.LogKeyPath, path), log.Err(err)) return nil } - logger.Debug("file is skipped", slog.String("path", path), log.Err(err)) + logger.Debug("file is skipped", slog.String(pkg.LogKeyPath, path), log.Err(err)) return nil } @@ -549,13 +549,13 @@ func (bm *BasicModule) registerHooks(hks []*hooks.ModuleHook, logger *log.Logger return fmt.Errorf("`%s` module hook `%s` --config invalid: %w", bm.GetName(), moduleHook.GetName(), err) } - bm.logger.Debug("module hook config print", slog.String("module_name", bm.GetName()), slog.String("hook_name", moduleHook.GetName()), slog.Any("config", moduleHook.GetHookConfig().V1)) + bm.logger.Debug("module hook config print", slog.String(pkg.LogKeyModuleNameSnake, bm.GetName()), slog.String(pkg.LogKeyHookNameSnake, moduleHook.GetName()), slog.Any(pkg.LogKeyConfig, moduleHook.GetHookConfig().V1)) // Add hook info as log labels for _, kubeCfg := range moduleHook.GetHookConfig().OnKubernetesEvents { - kubeCfg.Monitor.Metadata.LogLabels["module"] = bm.GetName() + kubeCfg.Monitor.Metadata.LogLabels[pkg.LogKeyModule] = bm.GetName() kubeCfg.Monitor.Metadata.LogLabels[pkg.LogKeyHook] = moduleHook.GetName() - kubeCfg.Monitor.Metadata.LogLabels["hook.type"] = "module" + kubeCfg.Monitor.Metadata.LogLabels[pkg.LogKeyHookType] = "module" kubeCfg.Monitor.Metadata.MetricLabels = map[string]string{ pkg.MetricKeyHook: moduleHook.GetName(), @@ -570,8 +570,8 @@ func (bm *BasicModule) registerHooks(hks []*hooks.ModuleHook, logger *log.Logger bm.hooks.AddHook(moduleHook) hookLogEntry.Debug("Module hook", - slog.String("path", moduleHook.GetPath()), - slog.String("bindings", moduleHook.GetConfigDescription())) + slog.String(pkg.LogKeyPath, moduleHook.GetPath()), + slog.String(pkg.LogKeyBindings, moduleHook.GetConfigDescription())) } return nil @@ -669,10 +669,10 @@ func (bm *BasicModule) RunHooksByBinding(ctx context.Context, binding sh_op_type bc.Metadata.BindingType = binding metricLabels := map[string]string{ - "module": bm.GetName(), + pkg.LogKeyModule: bm.GetName(), pkg.MetricKeyHook: moduleHook.GetName(), pkg.MetricKeyBinding: string(binding), - "queue": "main", // AfterHelm,BeforeHelm hooks always handle in main queue + pkg.LogKeyQueue: "main", // AfterHelm,BeforeHelm hooks always handle in main queue pkg.MetricKeyActivation: logLabels[pkg.LogKeyEventType], } @@ -704,10 +704,10 @@ func (bm *BasicModule) RunHookByName(ctx context.Context, hookName string, bindi } metricLabels := map[string]string{ - "module": bm.GetName(), + pkg.LogKeyModule: bm.GetName(), pkg.MetricKeyHook: hookName, pkg.MetricKeyBinding: string(binding), - "queue": logLabels["queue"], + pkg.LogKeyQueue: logLabels[pkg.LogKeyQueue], pkg.MetricKeyActivation: logLabels[pkg.LogKeyEventType], } @@ -725,14 +725,14 @@ func (bm *BasicModule) RunHookByName(ctx context.Context, hookName string, bindi func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, precedingEnabledModules []string, logLabels map[string]string) (bool, error) { // Copy labels and set 'module' label. logLabels = utils.MergeLabels(logLabels) - logLabels["module"] = bm.GetName() + logLabels[pkg.LogKeyModule] = bm.GetName() logEntry := utils.EnrichLoggerWithLabels(bm.logger, logLabels) enabledScriptPath := filepath.Join(bm.Path, "enabled") configValuesPath, err := bm.prepareConfigValuesJsonFile(tmpDir) if err != nil { logEntry.Error("Prepare CONFIG_VALUES_PATH file", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) return false, err } @@ -742,9 +742,9 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec } err := os.Remove(configValuesPath) if err != nil { - bm.logger.With("module", bm.GetName()). + bm.logger.With(pkg.LogKeyModule, bm.GetName()). Error("Remove tmp file", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) } }() @@ -752,7 +752,7 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec valuesPath, err := bm.prepareValuesJsonFileForEnabledScript(tmpDir, precedingEnabledModules) if err != nil { logEntry.Error("Prepare VALUES_PATH file", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) return false, err } @@ -762,9 +762,9 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec } err := os.Remove(valuesPath) if err != nil { - bm.logger.With("module", bm.GetName()). + bm.logger.With(pkg.LogKeyModule, bm.GetName()). Error("Remove tmp file", - slog.String("path", configValuesPath), + slog.String(pkg.LogKeyPath, configValuesPath), log.Err(err)) } }() @@ -772,7 +772,7 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec enabledResultFilePath, err := bm.prepareModuleEnabledResultFile(tmpDir) if err != nil { logEntry.Error("Prepare MODULE_ENABLED_RESULT file", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) return false, err } @@ -782,9 +782,9 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec } err := os.Remove(enabledResultFilePath) if err != nil { - bm.logger.With("module", bm.GetName()). + bm.logger.With(pkg.LogKeyModule, bm.GetName()). Error("Remove tmp file", - slog.String("path", configValuesPath), + slog.String(pkg.LogKeyPath, configValuesPath), log.Err(err)) } }() @@ -792,7 +792,7 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec reasonFilePath, err := bm.prepareModuleEnabledReasonFile(tmpDir) if err != nil { logEntry.Error("Prepare MODULE_ENABLED_REASON file", - slog.String("path", reasonFilePath), + slog.String(pkg.LogKeyPath, reasonFilePath), log.Err(err)) return false, err } @@ -802,16 +802,16 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec } err := os.Remove(reasonFilePath) if err != nil { - bm.logger.With("module", bm.GetName()). + bm.logger.With(pkg.LogKeyModule, bm.GetName()). Error("Remove tmp file", - slog.String("path", enabledResultFilePath), + slog.String(pkg.LogKeyPath, enabledResultFilePath), log.Err(err)) } }() logEntry.Debug("Execute enabled script", - slog.String("path", enabledScriptPath), - slog.Any("modules", precedingEnabledModules)) + slog.String(pkg.LogKeyPath, enabledScriptPath), + slog.Any(pkg.LogKeyModules, precedingEnabledModules)) environ := os.Environ() @@ -840,10 +840,10 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec if usage != nil { // usage metrics metricLabels := map[string]string{ - "module": bm.GetName(), + pkg.LogKeyModule: bm.GetName(), pkg.MetricKeyHook: "enabled", pkg.MetricKeyBinding: "enabled", - "queue": logLabels["queue"], + pkg.LogKeyQueue: logLabels[pkg.LogKeyQueue], pkg.MetricKeyActivation: logLabels[pkg.LogKeyEventType], } bm.dc.MetricStorage.HistogramObserve(metrics.ModuleHookRunSysCPUSeconds, usage.Sys.Seconds(), metricLabels, nil) @@ -852,7 +852,7 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec } if err != nil { logEntry.Error("Fail to run enabled script", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) return false, err } @@ -860,7 +860,7 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec moduleEnabled, err := bm.readModuleEnabledResult(enabledResultFilePath) if err != nil { logEntry.Error("Read enabled result", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) return false, fmt.Errorf("bad enabled result") } @@ -870,14 +870,14 @@ func (bm *BasicModule) RunEnabledScript(ctx context.Context, tmpDir string, prec result = "Enabled" } logEntry.Info("Enabled script run successful", - slog.Bool("result", moduleEnabled), - slog.String("status", result)) + slog.Bool(pkg.LogKeyResult, moduleEnabled), + slog.String(pkg.LogKeyStatus, result)) var reason string if !moduleEnabled { reason, err = bm.readModuleEnabledReason(reasonFilePath) if err != nil { logEntry.Error("Read enabled result", - slog.String("path", enabledScriptPath), + slog.String(pkg.LogKeyPath, enabledScriptPath), log.Err(err)) return false, fmt.Errorf("bad enabled result") } @@ -952,8 +952,8 @@ func (bm *BasicModule) prepareValuesJsonFileWith(tmpdir string, values utils.Val } bm.logger.Debug("Prepared module hook values", - slog.String("module", bm.GetName()), - slog.String("values", values.DebugString())) + slog.String(pkg.LogKeyModule, bm.GetName()), + slog.String(pkg.LogKeyValues, values.DebugString())) return path, nil } @@ -969,7 +969,7 @@ func (bm *BasicModule) valuesForEnabledScript(precedingEnabledModules []string) bm.dc.GlobalValuesGetter.GetValues(true), utils.Values{ "global": map[string]interface{}{ - "enabledModules": precedingEnabledModules, + pkg.LogKeyEnabledModules: precedingEnabledModules, }, }, ) @@ -999,8 +999,8 @@ func (bm *BasicModule) prepareConfigValuesJsonFile(tmpDir string) (string, error } bm.logger.Debug("Prepared module hook config values", - slog.String("module", bm.GetName()), - slog.String("values", v.DebugString())) + slog.String(pkg.LogKeyModule, bm.GetName()), + slog.String(pkg.LogKeyValues, v.DebugString())) return path, nil } @@ -1016,11 +1016,11 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin ) logLabels = utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: h.GetName(), - "hook.type": "module", - pkg.LogKeyModule: bm.GetName(), - pkg.LogKeyBinding: string(bindingType), - "path": h.GetPath(), + pkg.LogKeyHook: h.GetName(), + pkg.LogKeyHookType: "module", + pkg.LogKeyModule: bm.GetName(), + pkg.LogKeyBinding: string(bindingType), + pkg.LogKeyPath: h.GetPath(), }) logEntry := utils.EnrichLoggerWithLabels(bm.logger, logLabels) @@ -1035,7 +1035,7 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin for _, info := range h.GetHookController().SnapshotsInfo() { logEntry.Debug("snapshot info", - slog.String("value", info)) + slog.String(pkg.LogKeyValue, info)) } prefixedConfigValues := bm.valuesStorage.GetConfigValues(true) @@ -1066,7 +1066,7 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin // Apply metric operations metricsErr := bm.dc.HookMetricsStorage.ApplyBatchOperations(hookResult.Metrics, map[string]string{ pkg.MetricKeyHook: h.GetName(), - "module": bm.GetName(), + pkg.LogKeyModule: bm.GetName(), }) if metricsErr != nil { return metricsErr @@ -1110,7 +1110,7 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin if configValuesPatchResult.ValuesChanged { logEntry.Debug("Module hook: validate module config values before update", - slog.String("module", h.GetName())) + slog.String(pkg.LogKeyModule, h.GetName())) // Validate merged static and new values. newValues, validationErr := bm.valuesStorage.GenerateNewConfigValues(configValuesPatchResult.Values, true) if validationErr != nil { @@ -1123,8 +1123,8 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin err := bm.dc.KubeConfigManager.SaveConfigValues(bm.GetName(), configValuesPatchResult.Values) if err != nil { logEntry.Debug("Module hook kube module config values stay unchanged", - slog.String("module", h.GetName()), - slog.String("values", bm.valuesStorage.GetConfigValues(false).DebugString())) + slog.String(pkg.LogKeyModule, h.GetName()), + slog.String(pkg.LogKeyValues, bm.valuesStorage.GetConfigValues(false).DebugString())) return fmt.Errorf("module hook '%s': set kube module config failed: %s", h.GetName(), err) } @@ -1132,8 +1132,8 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin logEntry.Debug("Module hook: kube module config values updated", slog.String(pkg.LogKeyHook, h.GetName()), - slog.String("module", bm.GetName()), - slog.String("values", bm.valuesStorage.GetConfigValues(false).DebugString())) + slog.String(pkg.LogKeyModule, bm.GetName()), + slog.String(pkg.LogKeyValues, bm.valuesStorage.GetConfigValues(false).DebugString())) } } @@ -1146,7 +1146,7 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin } if valuesPatchResult.ValuesChanged { logEntry.Debug("Module hook: validate module values before update", - slog.String("module", h.GetName())) + slog.String(pkg.LogKeyModule, h.GetName())) // Validate schema for updated module values validationErr := bm.valuesStorage.validateValues(valuesPatchResult.Values) @@ -1166,13 +1166,13 @@ func (bm *BasicModule) executeHook(ctx context.Context, h *hooks.ModuleHook, bin logEntry.Debug("Module hook: dynamic module values updated", slog.String(pkg.LogKeyHook, h.GetName()), - slog.String("module", bm.GetName()), - slog.String("values", bm.valuesStorage.GetValues(false).DebugString())) + slog.String(pkg.LogKeyModule, bm.GetName()), + slog.String(pkg.LogKeyValues, bm.valuesStorage.GetValues(false).DebugString())) } } logEntry.Debug("Module hook success", - slog.String("module", bm.GetName()), + slog.String(pkg.LogKeyModule, bm.GetName()), slog.String(pkg.LogKeyHook, h.GetName())) return nil @@ -1354,8 +1354,8 @@ func (bm *BasicModule) Validate() error { restoredName := utils.ModuleNameFromValuesKey(valuesKey) bm.logger.Info("Validating module", - slog.String("module", bm.GetName()), - slog.String("path", bm.GetPath())) + slog.String(pkg.LogKeyModule, bm.GetName()), + slog.String(pkg.LogKeyPath, bm.GetPath())) if bm.GetName() != restoredName { return fmt.Errorf("'%s' name should be in kebab-case and be restorable from camelCase: consider renaming to '%s'", bm.GetName(), restoredName) diff --git a/pkg/module_manager/models/modules/global.go b/pkg/module_manager/models/modules/global.go index e1e7dfbb4..fb652bc42 100644 --- a/pkg/module_manager/models/modules/global.go +++ b/pkg/module_manager/models/modules/global.go @@ -179,11 +179,11 @@ func (gm *GlobalModule) executeHook(ctx context.Context, h *hooks.GlobalHook, bi defer span.End() logLabels = utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: h.GetName(), - "hook.type": "module", - pkg.LogKeyModule: gm.GetName(), - pkg.LogKeyBinding: string(bindingType), - "path": h.GetPath(), + pkg.LogKeyHook: h.GetName(), + pkg.LogKeyHookType: "module", + pkg.LogKeyModule: gm.GetName(), + pkg.LogKeyBinding: string(bindingType), + pkg.LogKeyPath: h.GetPath(), }) // Convert bindingContext for version @@ -191,7 +191,7 @@ func (gm *GlobalModule) executeHook(ctx context.Context, h *hooks.GlobalHook, bi logEntry := utils.EnrichLoggerWithLabels(gm.logger, logLabels) for _, info := range h.GetHookController().SnapshotsInfo() { - logEntry.Debug("snapshot info", slog.String("value", info)) + logEntry.Debug("snapshot info", slog.String(pkg.LogKeyValue, info)) } prefixedConfigValues := gm.valuesStorage.GetConfigValues(true) @@ -202,7 +202,7 @@ func (gm *GlobalModule) executeHook(ctx context.Context, h *hooks.GlobalHook, bi metricLabels := map[string]string{ pkg.MetricKeyHook: h.GetName(), pkg.MetricKeyBinding: string(bindingType), - "queue": logLabels["queue"], + pkg.LogKeyQueue: logLabels[pkg.LogKeyQueue], pkg.MetricKeyActivation: logLabels[pkg.LogKeyEventType], } // usage metrics @@ -254,7 +254,7 @@ func (gm *GlobalModule) executeHook(ctx context.Context, h *hooks.GlobalHook, bi if err != nil { logEntry.Debug("Global hook kube config global values stay unchanged", slog.String(pkg.LogKeyHook, h.GetName()), - slog.String("value", gm.valuesStorage.GetConfigValues(false).DebugString())) + slog.String(pkg.LogKeyValue, gm.valuesStorage.GetConfigValues(false).DebugString())) return fmt.Errorf("global hook '%s': set kube config failed: %s", h.GetName(), err) } @@ -262,7 +262,7 @@ func (gm *GlobalModule) executeHook(ctx context.Context, h *hooks.GlobalHook, bi logEntry.Debug("Global hook: kube config global values updated", slog.String(pkg.LogKeyHook, h.GetName())) logEntry.Debug("New kube config global values", - slog.String("values", gm.valuesStorage.GetConfigValues(false).DebugString())) + slog.String(pkg.LogKeyValues, gm.valuesStorage.GetConfigValues(false).DebugString())) } // Apply patches for *Enabled keys. @@ -298,7 +298,7 @@ func (gm *GlobalModule) executeHook(ctx context.Context, h *hooks.GlobalHook, bi logEntry.Debug("Global hook: kube global values updated", slog.String(pkg.LogKeyHook, h.GetName())) logEntry.Debug("New global values", - slog.String("values", gm.valuesStorage.GetValues(false).DebugString())) + slog.String(pkg.LogKeyValues, gm.valuesStorage.GetValues(false).DebugString())) } // Apply patches for *Enabled keys. @@ -449,12 +449,12 @@ func (gm *GlobalModule) searchAndRegisterHooks() ([]*hooks.GlobalHook, error) { return nil, fmt.Errorf("search module hooks failed: %w", err) } - gm.logger.Debug("Found global hooks", slog.Int("count", len(hks))) + gm.logger.Debug("Found global hooks", slog.Int(pkg.LogKeyCount, len(hks))) if gm.logger.GetLevel() == log.LevelDebug { for _, h := range hks { gm.logger.Debug("GlobalHook", slog.String(pkg.LogKeyHook, h.GetName()), - slog.String("path", h.GetPath())) + slog.String(pkg.LogKeyPath, h.GetPath())) } } @@ -471,13 +471,13 @@ func (gm *GlobalModule) searchAndRegisterHooks() ([]*hooks.GlobalHook, error) { // Add hook info as log labels for _, kubeCfg := range globalHook.GetHookConfig().OnKubernetesEvents { kubeCfg.Monitor.Metadata.LogLabels[pkg.LogKeyHook] = globalHook.GetName() - kubeCfg.Monitor.Metadata.LogLabels["hook.type"] = "global" + kubeCfg.Monitor.Metadata.LogLabels[pkg.LogKeyHookType] = "global" kubeCfg.Monitor.Metadata.MetricLabels = map[string]string{ pkg.MetricKeyHook: globalHook.GetName(), pkg.MetricKeyBinding: kubeCfg.BindingName, - "module": "", // empty "module" label for label set consistency with module hooks - "queue": kubeCfg.Queue, - "kind": kubeCfg.Monitor.Kind, + pkg.LogKeyModule: "", // empty "module" label for label set consistency with module hooks + pkg.LogKeyQueue: kubeCfg.Queue, + pkg.LogKeyKind: kubeCfg.Monitor.Kind, } } @@ -488,8 +488,8 @@ func (gm *GlobalModule) searchAndRegisterHooks() ([]*hooks.GlobalHook, error) { } hookLogEntry.Debug("Module hook from path", - slog.String("path", globalHook.GetPath()), - slog.String("bindings", globalHook.GetConfigDescription())) + slog.String(pkg.LogKeyPath, globalHook.GetPath()), + slog.String(pkg.LogKeyBindings, globalHook.GetConfigDescription())) } return hks, nil @@ -556,7 +556,7 @@ func (gm *GlobalModule) searchGlobalShellHooks(hooksDir string) ([]*kind.ShellHo // sort hooks by path sort.Strings(hooksRelativePaths) gm.logger.Debug("Hook paths", - slog.Any("paths", hooksRelativePaths)) + slog.Any(pkg.LogKeyPaths, hooksRelativePaths)) for _, hookPath := range hooksRelativePaths { hookName, err := normalizeHookPath(hooksDir, hookPath) @@ -581,8 +581,8 @@ func (gm *GlobalModule) searchGlobalShellHooks(hooksDir string) ([]*kind.ShellHo count = strconv.Itoa(len(hks)) } gm.logger.Info("Found global shell hooks in dir", - slog.String("count", count), - slog.String("dir", hooksDir)) + slog.String(pkg.LogKeyCount, count), + slog.String(pkg.LogKeyDir, hooksDir)) return hks, nil } @@ -608,7 +608,7 @@ func (gm *GlobalModule) searchGlobalBatchHooks(hooksDir string) ([]*kind.BatchHo // sort hooks by path sort.Strings(hooksRelativePaths) gm.logger.Debug("Hook paths", - slog.Any("path", hooksRelativePaths)) + slog.Any(pkg.LogKeyPath, hooksRelativePaths)) for _, hookPath := range hooksRelativePaths { hookName, err := normalizeHookPath(hooksDir, hookPath) @@ -648,8 +648,8 @@ func (gm *GlobalModule) searchGlobalBatchHooks(hooksDir string) ([]*kind.BatchHo } gm.logger.Info("Found global batch hooks in dir", - slog.String("count", count), - slog.String("dir", hooksDir)) + slog.String(pkg.LogKeyCount, count), + slog.String(pkg.LogKeyDir, hooksDir)) return hks, nil } @@ -664,7 +664,7 @@ func (gm *GlobalModule) searchGlobalGoHooks() []*kind.GoHook { } gm.logger.Info("Found global Go hooks", - slog.String("count", count)) + slog.String(pkg.LogKeyCount, count)) return goHooks } diff --git a/pkg/module_manager/models/modules/helm.go b/pkg/module_manager/models/modules/helm.go index 184a2de55..5bcd0ac09 100644 --- a/pkg/module_manager/models/modules/helm.go +++ b/pkg/module_manager/models/modules/helm.go @@ -119,7 +119,7 @@ func NewHelmModule(bm *BasicModule, namespace string, tmpDir string, deps *HelmM if !isHelm { hm.logger.Info("module has neither Chart.yaml nor templates/ dir, is't not a helm chart", - slog.String("name", bm.GetName())) + slog.String(pkg.LogKeyName, bm.GetName())) return nil, ErrModuleIsNotHelm } @@ -210,14 +210,14 @@ func (hm *HelmModule) RunHelmInstall(ctx context.Context, logLabels map[string]s // Also check for nelm ReleaseNotFoundError var releaseNotFoundErr *action.ReleaseNotFoundError if !errors.As(err, &releaseNotFoundErr) { - logEntry.Warn("get release label failed", log.Err(err), slog.String("release", helmReleaseName)) + logEntry.Warn("get release label failed", log.Err(err), slog.String(pkg.LogKeyRelease, helmReleaseName)) return fmt.Errorf("get release label failed: %w", err) } - logEntry.Debug("release not found when checking unmanaged state", slog.String("release", helmReleaseName)) + logEntry.Debug("release not found when checking unmanaged state", slog.String(pkg.LogKeyRelease, helmReleaseName)) } if isUnmanaged == "true" { - logEntry.Info("helm release is Unmanaged, skip helm upgrade", slog.String("release", helmReleaseName)) + logEntry.Info("helm release is Unmanaged, skip helm upgrade", slog.String(pkg.LogKeyRelease, helmReleaseName)) return ErrReleaseIsUnmanaged } @@ -271,7 +271,7 @@ func (hm *HelmModule) RunHelmInstall(ctx context.Context, logLabels map[string]s return err } - logEntry.Debug("chart has resources", slog.Int("count", len(manifests))) + logEntry.Debug("chart has resources", slog.Int(pkg.LogKeyCount, len(manifests))) span.AddEvent("ModuleRun-HelmPhase-helm-check-upgrade") // Skip upgrades if nothing is changed @@ -341,7 +341,7 @@ func (hm *HelmModule) shouldRunHelmUpgrade(helmClient client.HelmClient, release revision, status, err := helmClient.LastReleaseStatus(releaseName) if revision == "0" { - logEntry.Debug("helm release not exists: should run upgrade", slog.String("release", releaseName)) + logEntry.Debug("helm release not exists: should run upgrade", slog.String(pkg.LogKeyRelease, releaseName)) return true, nil } @@ -352,8 +352,8 @@ func (hm *HelmModule) shouldRunHelmUpgrade(helmClient client.HelmClient, release // Run helm upgrade if last release isn't `deployed` if strings.ToLower(status) != "deployed" { logEntry.Debug("helm release: should run upgrade", - slog.String("release", releaseName), - slog.String("status", strings.ToLower(status))) + slog.String(pkg.LogKeyRelease, releaseName), + slog.String(pkg.LogKeyStatus, strings.ToLower(status))) return true, nil } @@ -361,7 +361,7 @@ func (hm *HelmModule) shouldRunHelmUpgrade(helmClient client.HelmClient, release recordedChecksum, err := helmClient.GetReleaseChecksum(releaseName) if err != nil { logEntry.Debug("helm release get values error, no upgrade", - slog.String("release", releaseName), + slog.String(pkg.LogKeyRelease, releaseName), log.Err(err)) return false, err } @@ -370,9 +370,9 @@ func (hm *HelmModule) shouldRunHelmUpgrade(helmClient client.HelmClient, release // Run helm upgrade if checksum is changed. if recordedChecksum != checksum { logEntry.Debug("helm release checksum is changed: should run upgrade", - slog.String("release", releaseName), - slog.String("checksum", recordedChecksum), - slog.String("newChecksum", checksum)) + slog.String(pkg.LogKeyRelease, releaseName), + slog.String(pkg.LogKeyChecksum, recordedChecksum), + slog.String(pkg.LogKeyNewChecksum, checksum)) return true, nil } @@ -385,14 +385,14 @@ func (hm *HelmModule) shouldRunHelmUpgrade(helmClient client.HelmClient, release // Run helm upgrade if there are absent resources if len(absent) > 0 { logEntry.Debug("helm release has absent resources: should run upgrade", - slog.String("release", releaseName), - slog.Int("count", len(absent)), + slog.String(pkg.LogKeyRelease, releaseName), + slog.Int(pkg.LogKeyCount, len(absent)), ) return true, nil } logEntry.Debug("helm release is unchanged: skip release upgrade", - slog.String("release", releaseName)) + slog.String(pkg.LogKeyRelease, releaseName)) return false, nil } @@ -409,8 +409,8 @@ func (hm *HelmModule) PrepareValuesYamlFile() (string, error) { } hm.logger.Debug("Prepared module helm values info", - slog.String("moduleName", hm.name), - slog.String("values", hm.values.DebugString())) + slog.String(pkg.LogKeyModuleName, hm.name), + slog.String(pkg.LogKeyValues, hm.values.DebugString())) return path, nil } diff --git a/pkg/module_manager/module_manager.go b/pkg/module_manager/module_manager.go index e38606271..2d7f3b0ee 100644 --- a/pkg/module_manager/module_manager.go +++ b/pkg/module_manager/module_manager.go @@ -242,7 +242,7 @@ func (mm *ModuleManager) ApplyNewKubeConfigValues(kubeConfig *config.KubeConfig, if ok { if globalValuesChanged { mm.logger.Debug("Applying global values", - slog.String("values", fmt.Sprintf("%v", newGlobalValues))) + slog.String(pkg.LogKeyValues, fmt.Sprintf("%v", newGlobalValues))) mm.global.SaveConfigValues(newGlobalValues) } delete(valuesMap, mm.global.GetName()) @@ -257,9 +257,9 @@ func (mm *ModuleManager) ApplyNewKubeConfigValues(kubeConfig *config.KubeConfig, if mod.GetConfigValues(false).Checksum() != values.Checksum() { mm.logger.Debug("Applying values to module", - slog.String("moduleName", moduleName), - slog.String("values", fmt.Sprintf("%v", values)), - slog.String("oldValues", fmt.Sprintf("%v", mod.GetConfigValues(false)))) + slog.String(pkg.LogKeyModuleName, moduleName), + slog.String(pkg.LogKeyValues, fmt.Sprintf("%v", values)), + slog.String(pkg.LogKeyOldValues, fmt.Sprintf("%v", mod.GetConfigValues(false)))) mod.SaveConfigValues(values) } } @@ -337,7 +337,7 @@ func (mm *ModuleManager) warnAboutUnknownModules(kubeConfig *config.KubeConfig) } if len(unknownNames) > 0 { mm.logger.Warn("KubeConfigManager has values for unknown modules", - slog.Any("modules", unknownNames)) + slog.Any(pkg.LogKeyModules, unknownNames)) } } @@ -449,7 +449,7 @@ func (mm *ModuleManager) RefreshStateFromHelmReleases(logLabels map[string]strin } mm.logger.Debug("Following releases found", - slog.Any("modules", releasedModules)) + slog.Any(pkg.LogKeyModules, releasedModules)) return mm.stateFromHelmReleases(releasedModules), nil } @@ -468,7 +468,7 @@ func (mm *ModuleManager) stateFromHelmReleases(releases []string) *ModulesState if len(purge) > 0 { mm.logger.Info("Modules to purge found", - slog.Any("modules", purge)) + slog.Any(pkg.LogKeyModules, purge)) } return &ModulesState{ @@ -512,8 +512,8 @@ func (mm *ModuleManager) SetModuleMaintenanceState(moduleName string, state util if bm := mm.GetModule(moduleName); bm != nil { bm.SetMaintenanceState(state) mm.logger.Info("set module management state", - slog.String("module", moduleName), - slog.String("state", state.String())) + slog.String(pkg.LogKeyModule, moduleName), + slog.String(pkg.LogKeyState, state.String())) if state == utils.NoResourceReconciliation { mm.dependencies.MetricStorage.Grouped().GaugeSet(moduleMaintenanceMetricGroup, moduleMaintenanceMetricName, 1, map[string]string{pkg.MetricKeyModule: moduleName, "state": utils.NoResourceReconciliation.String()}) } else { @@ -526,7 +526,7 @@ func (mm *ModuleManager) SetModuleMaintenanceState(moduleName string, state util // - mm.enabledModules func (mm *ModuleManager) RefreshEnabledState(logLabels map[string]string) (*ModulesState, error) { refreshLogLabels := utils.MergeLabels(logLabels, map[string]string{ - "operator.component": "ModuleManager.RefreshEnabledState", + pkg.LogKeyOperatorComponent: "ModuleManager.RefreshEnabledState", }) logEntry := utils.EnrichLoggerWithLabels(mm.logger, refreshLogLabels) @@ -536,7 +536,7 @@ func (mm *ModuleManager) RefreshEnabledState(logLabels map[string]string) (*Modu } logEntry.Info("Enabled modules", - slog.Any("modules", enabledModules)) + slog.Any(pkg.LogKeyModules, enabledModules)) once.Do(mm.modules.SetInited) var ( @@ -558,9 +558,9 @@ func (mm *ModuleManager) RefreshEnabledState(logLabels map[string]string) (*Modu modulesToEnable = utils.SortByReference(modulesToEnable, mm.modules.NamesInOrder()) logEntry.Debug("Refresh state results", - slog.Any("enabledModules", enabledModules), - slog.Any("modulesToDisable", modulesToDisable), - slog.Any("modulesToEnable", modulesToEnable)) + slog.Any(pkg.LogKeyEnabledModules, enabledModules), + slog.Any(pkg.LogKeyModulesToDisable, modulesToDisable), + slog.Any(pkg.LogKeyModulesToEnable, modulesToEnable)) // We've to ignore enabledModules patch in case default moduleLoader is in use, otherwise it breaks applying global hooks patches with default moduleLoader switch mm.moduleLoader.(type) { @@ -668,8 +668,8 @@ func (mm *ModuleManager) DeleteModule(ctx context.Context, moduleName string, lo deleteLogLabels := utils.MergeLabels(logLabels, map[string]string{ - "module": ml.GetName(), - "queue": "main", + pkg.LogKeyModule: ml.GetName(), + pkg.LogKeyQueue: "main", }) logEntry := utils.EnrichLoggerWithLabels(mm.logger, deleteLogLabels) @@ -695,11 +695,11 @@ func (mm *ModuleManager) DeleteModule(ctx context.Context, moduleName string, lo if !releaseExists { if err != nil { logEntry.Warn("Cannot find helm release for module", - slog.String("module", ml.GetName()), + slog.String(pkg.LogKeyModule, ml.GetName()), log.Err(err)) } else { logEntry.Warn("Cannot find helm release for module.", - slog.String("module", ml.GetName())) + slog.String(pkg.LogKeyModule, ml.GetName())) } } else { helmClientOptions := []helm.ClientOption{ @@ -742,8 +742,8 @@ func (mm *ModuleManager) RunModule(ctx context.Context, moduleName string, logLa span.AddEvent("ModuleRun-HelmPhase") logLabels = utils.MergeLabels(logLabels, map[string]string{ - "module": bm.GetName(), - "queue": "main", + pkg.LogKeyModule: bm.GetName(), + pkg.LogKeyQueue: "main", }) // Hooks can delete release resources, so pause resources monitor before run hooks. @@ -1050,17 +1050,17 @@ func (mm *ModuleManager) applyEnabledPatch(enabledPatch utils.ValuesPatch, exten switch op.Op { case "add": mm.logger.Debug("apply dynamic enable", - slog.String("module", modName), - slog.Bool("value", *v)) + slog.String(pkg.LogKeyModule, modName), + slog.Bool(pkg.LogKeyValue, *v)) case "remove": mm.logger.Debug("apply dynamic enable: module removed from dynamic enable", - slog.String("module", modName)) + slog.String(pkg.LogKeyModule, modName)) } extender.UpdateStatus(modName, op.Op, *v) mm.logger.Info("dynamically enabled module status change", - slog.String("module", modName), - slog.String("operation", op.Op), - slog.Bool("state", *v)) + slog.String(pkg.LogKeyModule, modName), + slog.String(pkg.LogKeyOperation, op.Op), + slog.Bool(pkg.LogKeyState, *v)) } return nil @@ -1114,11 +1114,11 @@ func (mm *ModuleManager) ApplyBindingActions(moduleHook *hooks.ModuleHook, bindi // Empty kind - "null" monitor. monitorCfg.Kind = "" monitorCfg.ApiVersion = "" - monitorCfg.Metadata.MetricLabels["kind"] = "" + monitorCfg.Metadata.MetricLabels[pkg.LogKeyKind] = "" case "updatekind": monitorCfg.Kind = action.Kind monitorCfg.ApiVersion = action.ApiVersion - monitorCfg.Metadata.MetricLabels["kind"] = action.Kind + monitorCfg.Metadata.MetricLabels[pkg.LogKeyKind] = action.Kind default: continue } @@ -1463,8 +1463,8 @@ func (mm *ModuleManager) registerModules(scriptEnabledExtender *script_extender. for _, mod := range mods { if set.Has(mod.GetName()) { mm.logger.Warn("module is not registered, because it has a duplicate", - slog.String("module", mod.GetName()), - slog.String("path", mod.GetPath())) + slog.String(pkg.LogKeyModule, mod.GetName()), + slog.String(pkg.LogKeyPath, mod.GetPath())) continue } @@ -1487,7 +1487,7 @@ func (mm *ModuleManager) registerModules(scriptEnabledExtender *script_extender. return fmt.Errorf("initialize scheduler: %w", err) } - mm.logger.Debug("Found modules", slog.Any("modules", set.NamesInOrder())) + mm.logger.Debug("Found modules", slog.Any(pkg.LogKeyModules, set.NamesInOrder())) mm.l.Lock() mm.modules = set diff --git a/pkg/module_manager/module_manager_hooks.go b/pkg/module_manager/module_manager_hooks.go index 93dfc0510..c3aabcde7 100644 --- a/pkg/module_manager/module_manager_hooks.go +++ b/pkg/module_manager/module_manager_hooks.go @@ -139,7 +139,7 @@ func (mm *ModuleManager) registerGlobalHooks(gm *modules.GlobalModule) error { } func (mm *ModuleManager) RegisterModuleHooks(ml *modules.BasicModule, logLabels map[string]string) error { - logEntry := utils.EnrichLoggerWithLabels(mm.logger, logLabels).With(slog.String("module", ml.GetName())) + logEntry := utils.EnrichLoggerWithLabels(mm.logger, logLabels).With(slog.String(pkg.LogKeyModule, ml.GetName())) hks, err := ml.RegisterHooks(logEntry) if err != nil { @@ -175,8 +175,8 @@ func (mm *ModuleManager) RegisterModuleHooks(ml *modules.BasicModule, logLabels // It is a handler of task MODULE_RUN func (mm *ModuleManager) RunModuleHooks(ctx context.Context, m *modules.BasicModule, bt sh_op_types.BindingType, logLabels map[string]string) error { logLabels = utils.MergeLabels(logLabels, map[string]string{ - "module": m.GetName(), - "queue": "main", + pkg.LogKeyModule: m.GetName(), + pkg.LogKeyQueue: "main", }) m.SetStateEnabled(true) diff --git a/pkg/module_manager/scheduler/extenders/dynamically_enabled/dynamic.go b/pkg/module_manager/scheduler/extenders/dynamically_enabled/dynamic.go index 1630fa2a0..a24a7c435 100644 --- a/pkg/module_manager/scheduler/extenders/dynamically_enabled/dynamic.go +++ b/pkg/module_manager/scheduler/extenders/dynamically_enabled/dynamic.go @@ -7,6 +7,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders" ) @@ -45,7 +46,7 @@ func (e *Extender) UpdateStatus(moduleName, operation string, value bool) { } default: log.Warn("Unknown patch operation", - slog.String("operation", operation)) + slog.String(pkg.LogKeyOperation, operation)) } e.l.Unlock() } diff --git a/pkg/module_manager/scheduler/extenders/script_enabled/script.go b/pkg/module_manager/scheduler/extenders/script_enabled/script.go index 5b84e2e05..1c2da4ced 100644 --- a/pkg/module_manager/scheduler/extenders/script_enabled/script.go +++ b/pkg/module_manager/scheduler/extenders/script_enabled/script.go @@ -11,6 +11,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" pointer "k8s.io/utils/ptr" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders" exerror "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/error" "github.com/flant/addon-operator/pkg/module_manager/scheduler/node" @@ -69,7 +70,7 @@ func (e *Extender) AddBasicModule(module node.ModuleInterface) { if os.IsNotExist(err) { moduleD.scriptState = noEnabledScript log.Debug("MODULE is ENABLED. Enabled script doesn't exist!", - slog.String("module", module.GetName())) + slog.String(pkg.LogKeyModule, module.GetName())) } else { moduleD.scriptState = statError moduleD.stateDescription = fmt.Sprintf("Cannot stat enabled script for '%s' module: %v", module.GetName(), err) @@ -79,7 +80,7 @@ func (e *Extender) AddBasicModule(module node.ModuleInterface) { if utils_file.CheckExecutablePermissions(f) != nil { moduleD.scriptState = nonExecutableScript log.Warn("Found non-executable enabled script for module - assuming enabled state", - slog.String("module", module.GetName())) + slog.String(pkg.LogKeyModule, module.GetName())) } } @@ -99,7 +100,7 @@ func (e *Extender) Filter(moduleName string, logLabels map[string]string) (*bool case "": var isEnabled bool refreshLogLabels := utils.MergeLabels(logLabels, map[string]string{ - "extender": "ScriptEnabled", + pkg.LogKeyExtender: "ScriptEnabled", }) isEnabled, err = moduleDescriptor.module.RunEnabledScript(context.Background(), e.tmpDir, e.GetEnabledModules(), refreshLogLabels) if err != nil { @@ -114,11 +115,11 @@ func (e *Extender) Filter(moduleName string, logLabels map[string]string) (*bool case nonExecutableScript: log.Warn("Found non-executable enabled script for module - assuming enabled state", - slog.String("module", moduleDescriptor.module.GetName())) + slog.String(pkg.LogKeyModule, moduleDescriptor.module.GetName())) case noEnabledScript: log.Debug("MODULE is ENABLED. Enabled script doesn't exist!", - slog.String("module", moduleDescriptor.module.GetName())) + slog.String(pkg.LogKeyModule, moduleDescriptor.module.GetName())) } if err != nil { diff --git a/pkg/module_manager/scheduler/extenders/static/static.go b/pkg/module_manager/scheduler/extenders/static/static.go index 047e3bc6b..097577446 100644 --- a/pkg/module_manager/scheduler/extenders/static/static.go +++ b/pkg/module_manager/scheduler/extenders/static/static.go @@ -10,6 +10,7 @@ import ( "github.com/ettle/strcase" "gopkg.in/yaml.v3" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders" "github.com/flant/addon-operator/pkg/utils" ) @@ -30,13 +31,13 @@ func NewExtender(staticValuesFilePaths string) (*Extender, error) { fileInfo, err := os.Stat(valuesFile) if err != nil { log.Warn("Couldn't stat file", - slog.String("file", valuesFile)) + slog.String(pkg.LogKeyFile, valuesFile)) continue } if fileInfo.IsDir() { log.Error("File is a directory", - slog.String("file", valuesFile)) + slog.String(pkg.LogKeyFile, valuesFile)) continue } @@ -44,7 +45,7 @@ func NewExtender(staticValuesFilePaths string) (*Extender, error) { if err != nil { if os.IsNotExist(err) { log.Debug("File doesn't exist", - slog.String("file", valuesFile)) + slog.String(pkg.LogKeyFile, valuesFile)) continue } return nil, err diff --git a/pkg/module_manager/scheduler/scheduler.go b/pkg/module_manager/scheduler/scheduler.go index 075e2acf6..75b65abac 100644 --- a/pkg/module_manager/scheduler/scheduler.go +++ b/pkg/module_manager/scheduler/scheduler.go @@ -18,6 +18,7 @@ import ( "github.com/goccy/go-graphviz" "github.com/hashicorp/go-multierror" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders" dynamic_extender "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/dynamically_enabled" exerror "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders/error" @@ -343,7 +344,7 @@ func (s *Scheduler) ApplyExtenders(extendersEnv string) error { appliedExtenders := []extenders.ExtenderName{} if len(extendersEnv) == 0 { log.Warn("ADDON_OPERATOR_APPLIED_MODULE_EXTENDERS variable isn't set - default list will be applied", - slog.Any("values", defaultAppliedExtenders)) + slog.Any(pkg.LogKeyValues, defaultAppliedExtenders)) appliedExtenders = defaultAppliedExtenders } else { availableExtenders := make(map[extenders.ExtenderName]bool, len(s.extenders)) @@ -400,7 +401,7 @@ func (s *Scheduler) ApplyExtenders(extendersEnv string) error { } log.Info("The list of applied module extenders", - slog.Any("finalList", finalList)) + slog.Any(pkg.LogKeyFinalList, finalList)) return nil } @@ -640,7 +641,7 @@ func (s *Scheduler) getModuleNamesByOrder(onlyEnabled bool, logLabels map[string if err := s.customBFS(s.root.GetName(), func(name, prevName string, prevDepth int) (bool, int) { logEntry.Debug("Module Scheduler: traversing the graph", - slog.String("vertex", name)) + slog.String(pkg.LogKeyVertex, name)) var depth int vertex, props, err := s.dag.VertexWithProperties(name) @@ -727,7 +728,7 @@ func (s *Scheduler) GetGraphState(logLabels map[string]string) ( /*enabled modul if s.err != nil { logEntry.Warn("Module Scheduler: graph in a faulty state and will be recalculated", - slog.String("error", s.err.Error())) + slog.String(pkg.LogKeyError, s.err.Error())) recalculateGraph = true } @@ -892,7 +893,7 @@ outerCycle: // reset any previous errors s.err = nil logEntry.Debug("Graph was successfully updated", - slog.String("diff", fmt.Sprintf("%v", s.diff))) + slog.String(pkg.LogKeyDiff, fmt.Sprintf("%v", s.diff))) metaDiffSlice := make([]string, 0, len(metaDiff)) for moduleName := range metaDiff { diff --git a/pkg/task/functional/scheduler.go b/pkg/task/functional/scheduler.go index 4eac66cb1..8357d0372 100644 --- a/pkg/task/functional/scheduler.go +++ b/pkg/task/functional/scheduler.go @@ -8,6 +8,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/app" "github.com/flant/addon-operator/pkg/module_manager/scheduler/extenders" "github.com/flant/addon-operator/pkg/task" @@ -107,7 +108,7 @@ func (s *Scheduler) Add(reqs ...*Request) { defer s.mtx.Unlock() for _, req := range reqs { - s.logger.Debug("add request", slog.Any("request", req)) + s.logger.Debug("add request", slog.Any(pkg.LogKeyRequest, req)) // update module s.requests[req.Name] = req // undone module @@ -178,7 +179,7 @@ func (s *Scheduler) reschedule(done string) { // schedule module if ready if ready { - s.logger.Debug("trigger scheduling", slog.String("scheduled", req.Name), slog.Any("done", done)) + s.logger.Debug("trigger scheduling", slog.String(pkg.LogKeyScheduled, req.Name), slog.Any(pkg.LogKeyDone, done)) s.scheduled[req.Name] = struct{}{} s.processCh <- req } @@ -200,7 +201,7 @@ func (s *Scheduler) handleRequest(idx int, req *Request) { }) if err := s.queueService.AddLastTaskToQueue(queueName, moduleTask); err != nil { - s.logger.Error("add last task to queue", slog.String("queue", queueName), slog.Any("error", err)) + s.logger.Error("add last task to queue", slog.String(pkg.LogKeyQueue, queueName), slog.Any(pkg.LogKeyError, err)) } } diff --git a/pkg/task/hook_metadata.go b/pkg/task/hook_metadata.go index 1c10f65f7..4314daf51 100644 --- a/pkg/task/hook_metadata.go +++ b/pkg/task/hook_metadata.go @@ -10,6 +10,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/module_manager/models/modules" bindingcontext "github.com/flant/shell-operator/pkg/hook/binding_context" "github.com/flant/shell-operator/pkg/hook/task_metadata" @@ -115,8 +116,8 @@ func HookMetadataAccessor(t task.Task) HookMetadata { meta, ok := taskMeta.(HookMetadata) if !ok { log.Error("Possible Bug! task metadata is not of type ModuleHookMetadata", - slog.String("type", string(t.GetType())), - slog.String("got", fmt.Sprintf("%T", meta))) + slog.String(pkg.LogKeyType, string(t.GetType())), + slog.String(pkg.LogKeyGot, fmt.Sprintf("%T", meta))) return HookMetadata{} } diff --git a/pkg/task/queue/queue.go b/pkg/task/queue/queue.go index c638532fb..8f79712f8 100644 --- a/pkg/task/queue/queue.go +++ b/pkg/task/queue/queue.go @@ -8,6 +8,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/addon-operator/converge" "github.com/flant/addon-operator/pkg/app" "github.com/flant/addon-operator/pkg/task" @@ -49,7 +50,7 @@ func (s *Service) startQueue(queueName string, handler func(ctx context.Context, s.engine.TaskQueues.NewNamedQueue(queueName, handler, queue.WithCompactionCallback(callback), queue.WithCompactableTypes(MergeTasks...), - queue.WithLogger(s.logger.With("operator.component", "queue", "queue", queueName)), + queue.WithLogger(s.logger.With(pkg.LogKeyOperatorComponent, "queue", "queue", queueName)), ) s.engine.TaskQueues.GetByName(queueName).Start(s.ctx) } @@ -175,8 +176,8 @@ func (s *Service) RemoveAdjacentConvergeModules(queueName string, afterId string hm := task.HookMetadataAccessor(t) s.logger.Debug("Drained adjacent ConvergeModules task", - slog.String("type", string(t.GetType())), - slog.String("description", hm.EventDescription)) + slog.String(pkg.LogKeyType, string(t.GetType())), + slog.String(pkg.LogKeyDescription, hm.EventDescription)) return false } diff --git a/pkg/task/queue/utils.go b/pkg/task/queue/utils.go index 9ffaf3682..3171139e1 100644 --- a/pkg/task/queue/utils.go +++ b/pkg/task/queue/utils.go @@ -5,6 +5,7 @@ import ( "github.com/deckhouse/deckhouse/pkg/log" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/module_manager/models/modules" "github.com/flant/addon-operator/pkg/task" sh_task "github.com/flant/shell-operator/pkg/task" @@ -25,9 +26,9 @@ func CompactionCallback(moduleManager ModuleManager, logger *log.Logger) Callbac thm := task.HookMetadataAccessor(compactedTask) if thm.IsSynchronization() { logger.Debug("Compacted synchronization task, marking as Done", - slog.String("hook", thm.HookName), - slog.String("binding", thm.Binding), - slog.String("id", thm.KubernetesBindingId)) + slog.String(pkg.LogKeyHook, thm.HookName), + slog.String(pkg.LogKeyBinding, thm.Binding), + slog.String(pkg.LogKeyID, thm.KubernetesBindingId)) if thm.ModuleName == "" { if moduleManager != nil && moduleManager.GlobalSynchronizationState() != nil { diff --git a/pkg/task/service/converge.go b/pkg/task/service/converge.go index 3142b5a89..e018768f0 100644 --- a/pkg/task/service/converge.go +++ b/pkg/task/service/converge.go @@ -85,7 +85,7 @@ func (s *TaskHandlerService) logConvergeProgress(convergeTasks int, t sh_task.Ta moduleTasks := s.queueService.GetNumberOfConvergeTasks() if moduleTasks > 0 { - log.Info("Converge modules in progress", slog.Int("count", moduleTasks)) + log.Info("Converge modules in progress", slog.Int(pkg.LogKeyCount, moduleTasks)) } } } diff --git a/pkg/task/service/service.go b/pkg/task/service/service.go index 7d8ec0abb..ef753fff8 100644 --- a/pkg/task/service/service.go +++ b/pkg/task/service/service.go @@ -128,7 +128,7 @@ func (s *TaskHandlerService) Handle(ctx context.Context, t sh_task.Task) queue.T transformTask, ok := s.taskFactory[t.GetType()] if !ok { - s.logger.Error("TaskHandlerService: unknown task type", slog.String("task_type", string(t.GetType()))) + s.logger.Error("TaskHandlerService: unknown task type", slog.String(pkg.LogKeyTaskType, string(t.GetType()))) return queue.TaskResult{} } @@ -167,7 +167,7 @@ func (s *TaskHandlerService) ParallelHandle(ctx context.Context, t sh_task.Task) transformTask, ok = s.taskFactory[t.GetType()] if !ok { - s.logger.Error("TaskHandlerService: unknown task type", slog.String("task_type", string(t.GetType()))) + s.logger.Error("TaskHandlerService: unknown task type", slog.String(pkg.LogKeyTaskType, string(t.GetType()))) return queue.TaskResult{} } @@ -184,10 +184,10 @@ func (s *TaskHandlerService) ParallelHandle(ctx context.Context, t sh_task.Task) hm := task.HookMetadataAccessor(t) s.logger.Debug("parallel task done", - slog.String("task_type", string(t.GetType())), - slog.String("module", hm.ModuleName), - slog.Bool("critical", hm.Critical), - slog.String("result", string(res.Status))) + slog.String(pkg.LogKeyTaskType, string(t.GetType())), + slog.String(pkg.LogKeyModule, hm.ModuleName), + slog.Bool(pkg.LogKeyCritical, hm.Critical), + slog.String(pkg.LogKeyResult, string(res.Status))) if !hm.Critical { if t.GetType() == task.ModuleRun { diff --git a/pkg/task/tasks/apply-kube-config-values/task.go b/pkg/task/tasks/apply-kube-config-values/task.go index 8711fbcf8..dca0e2c9b 100644 --- a/pkg/task/tasks/apply-kube-config-values/task.go +++ b/pkg/task/tasks/apply-kube-config-values/task.go @@ -9,6 +9,7 @@ import ( metricsstorage "github.com/deckhouse/deckhouse/pkg/metrics-storage" "go.opentelemetry.io/otel" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/kube_config_manager" "github.com/flant/addon-operator/pkg/kube_config_manager/config" "github.com/flant/addon-operator/pkg/metrics" @@ -86,7 +87,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { res.Status = queue.Fail s.logger.Error("HandleApplyKubeConfigValues failed, requeue task to retry after delay.", - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(handleErr)) s.metricStorage.CounterAdd(metrics.ModulesDiscoverErrorsTotal, 1.0, map[string]string{}) diff --git a/pkg/task/tasks/converge-modules/task.go b/pkg/task/tasks/converge-modules/task.go index 2feeabb3c..69ca1132a 100644 --- a/pkg/task/tasks/converge-modules/task.go +++ b/pkg/task/tasks/converge-modules/task.go @@ -103,7 +103,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { taskEvent, ok := s.shellTask.GetProp(converge.ConvergeEventProp).(converge.ConvergeEvent) if !ok { s.logger.Error("Possible bug! Wrong prop type in ConvergeModules: got another type instead string.", - slog.String("type", fmt.Sprintf("%T(%#[1]v)", s.shellTask.GetProp("event")))) + slog.String(pkg.LogKeyType, fmt.Sprintf("%T(%#[1]v)", s.shellTask.GetProp("event")))) res.Status = queue.Fail @@ -235,8 +235,8 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { if handleErr != nil { res.Status = queue.Fail s.logger.Error("ConvergeModules failed, requeue task to retry after delay.", - slog.String("phase", string(s.convergeState.GetPhase())), - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.String(pkg.LogKeyPhase, string(s.convergeState.GetPhase())), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(handleErr)) s.metricStorage.CounterAdd(metrics.ModulesDiscoverErrorsTotal, 1.0, map[string]string{}) s.shellTask.UpdateFailureMessage(handleErr.Error()) @@ -260,13 +260,13 @@ func (s *Task) CreateBeforeAllTasks(logLabels map[string]string, eventDescriptio tasks := make([]sh_task.Task, 0, len(beforeAllHooks)) for _, hookName := range beforeAllHooks { hookLogLabels := utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: hookName, - "hook.type": "global", - "queue": "main", - pkg.LogKeyBinding: string(hookTypes.BeforeAll), + pkg.LogKeyHook: hookName, + pkg.LogKeyHookType: "global", + pkg.LogKeyQueue: "main", + pkg.LogKeyBinding: string(hookTypes.BeforeAll), }) // remove task.id — it is set by NewTask - delete(hookLogLabels, "task.id") + delete(hookLogLabels, pkg.LogKeyTaskID) // bc := module_manager.BindingContext{BindingContext: hook.BindingContext{Binding: stringmodule_manager.BeforeAll)}} // bc.KubernetesSnapshots := ModuleManager.GetGlobalHook(hookName).HookController.KubernetesSnapshots() @@ -304,12 +304,12 @@ func (s *Task) CreateAfterAllTasks(logLabels map[string]string, eventDescription tasks := make([]sh_task.Task, 0, len(afterAllHooks)) for i, hookName := range afterAllHooks { hookLogLabels := utils.MergeLabels(logLabels, map[string]string{ - pkg.LogKeyHook: hookName, - "hook.type": "global", - "queue": "main", - pkg.LogKeyBinding: string(hookTypes.AfterAll), + pkg.LogKeyHook: hookName, + pkg.LogKeyHookType: "global", + pkg.LogKeyQueue: "main", + pkg.LogKeyBinding: string(hookTypes.AfterAll), }) - delete(hookLogLabels, "task.id") + delete(hookLogLabels, pkg.LogKeyTaskID) afterAllBc := bc.BindingContext{ Binding: string(hookTypes.AfterAll), @@ -354,7 +354,7 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo // Add ModuleDelete tasks to delete helm releases of disabled modules. log.Debug("The following modules are going to be disabled", - slog.Any("modules", state.ModulesToDisable)) + slog.Any(pkg.LogKeyModules, state.ModulesToDisable)) for _, moduleName := range state.ModulesToDisable { ev := events.ModuleEvent{ ModuleName: moduleName, @@ -362,8 +362,8 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo } s.moduleManager.SendModuleEvent(ev) newLogLabels := utils.MergeLabels(logLabels) - newLogLabels["module"] = moduleName - delete(newLogLabels, "task.id") + newLogLabels[pkg.LogKeyModule] = moduleName + delete(newLogLabels, pkg.LogKeyTaskID) newTask := sh_task.NewTask(task.ModuleDelete). WithLogLabels(newLogLabels). @@ -381,7 +381,7 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo // Add ModuleRun tasks to install or reload enabled modules. newlyEnabled := utils.ListToMapStringStruct(state.ModulesToEnable) log.Debug("The following modules are going to be enabled/rerun", - slog.String("modules", fmt.Sprintf("%v", state.AllEnabledModulesByOrder))) + slog.String(pkg.LogKeyModules, fmt.Sprintf("%v", state.AllEnabledModulesByOrder))) var functionalModules []string for _, modules := range state.AllEnabledModulesByOrder { @@ -396,12 +396,12 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo } newLogLabels := utils.MergeLabels(logLabels) - delete(newLogLabels, "task.id") + delete(newLogLabels, pkg.LogKeyTaskID) switch { // create parallel moduleRun task case len(modules) > 1: parallelRunMetadata := task.ParallelRunMetadata{} - newLogLabels["modules"] = strings.Join(modules, ",") + newLogLabels[pkg.LogKeyModules] = strings.Join(modules, ",") for _, moduleName := range modules { ev := events.ModuleEvent{ ModuleName: moduleName, @@ -447,7 +447,7 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo EventType: events.ModuleEnabled, } s.moduleManager.SendModuleEvent(ev) - newLogLabels["module"] = modules[0] + newLogLabels[pkg.LogKeyModule] = modules[0] doModuleStartup := false if _, has := newlyEnabled[modules[0]]; has { // add EnsureCRDs task if module is about to be enabled @@ -477,7 +477,7 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo default: log.Error("Invalid ModulesState", - slog.String("state", fmt.Sprintf("%v", state))) + slog.String(pkg.LogKeyState, fmt.Sprintf("%v", state))) } } @@ -494,8 +494,8 @@ func (s *Task) CreateConvergeModulesTasks(state *module_manager.ModulesState, lo s.moduleManager.SendModuleEvent(ev) newLogLabels := utils.MergeLabels(logLabels) - delete(newLogLabels, "task.id") - newLogLabels["module"] = module + delete(newLogLabels, pkg.LogKeyTaskID) + newLogLabels[pkg.LogKeyModule] = module doModuleStartup := false // add EnsureCRDs task if module is about to be enabled diff --git a/pkg/task/tasks/discover-helm-release/task.go b/pkg/task/tasks/discover-helm-release/task.go index 43d68daf4..2e780293f 100644 --- a/pkg/task/tasks/discover-helm-release/task.go +++ b/pkg/task/tasks/discover-helm-release/task.go @@ -62,7 +62,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { res.Status = queue.Fail s.logger.Error("Discover helm releases failed, requeue task to retry after delay.", - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(err)) s.shellTask.UpdateFailureMessage(err.Error()) diff --git a/pkg/task/tasks/global-hook-enable-kubernetes-bindings/task.go b/pkg/task/tasks/global-hook-enable-kubernetes-bindings/task.go index 1ed895485..5416f9755 100644 --- a/pkg/task/tasks/global-hook-enable-kubernetes-bindings/task.go +++ b/pkg/task/tasks/global-hook-enable-kubernetes-bindings/task.go @@ -148,7 +148,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { pkg.MetricKeyActivation: converge.OperatorStartup.String(), }) s.logger.Error("Global hook enable kubernetes bindings failed, requeue task to retry after delay.", - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(err)) s.shellTask.UpdateFailureMessage(err.Error()) s.shellTask.WithQueuedAt(queuedAt) @@ -164,7 +164,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { for _, tsk := range parallelSyncTasksToWait { if err := s.queueService.AddLastTaskToQueue(tsk.GetQueueName(), tsk); err != nil { s.logger.Error("Queue is not created while run GlobalHookEnableKubernetesBindings parallel wait task!", - slog.String("queue", tsk.GetQueueName())) + slog.String(pkg.LogKeyQueue, tsk.GetQueueName())) continue } @@ -179,7 +179,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { for _, tsk := range parallelSyncTasks { if err := s.queueService.AddLastTaskToQueue(tsk.GetQueueName(), tsk); err != nil { s.logger.Error("Queue is not created while run GlobalHookEnableKubernetesBindings parallel sync task!", - slog.String("queue", tsk.GetQueueName())) + slog.String(pkg.LogKeyQueue, tsk.GetQueueName())) } } diff --git a/pkg/task/tasks/global-hook-run/task.go b/pkg/task/tasks/global-hook-run/task.go index 1894e60e3..54ca26728 100644 --- a/pkg/task/tasks/global-hook-run/task.go +++ b/pkg/task/tasks/global-hook-run/task.go @@ -143,9 +143,9 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { // Task 'tsk' will be combined, so remove it from the GlobalSynchronizationState. if thm.IsSynchronization() { s.logger.Debug("Synchronization task is combined, mark it as Done", - slog.String("name", thm.HookName), + slog.String(pkg.LogKeyName, thm.HookName), slog.String(pkg.LogKeyBinding, thm.Binding), - slog.String("id", thm.KubernetesBindingId)) + slog.String(pkg.LogKeyID, thm.KubernetesBindingId)) s.moduleManager.GlobalSynchronizationState().DoneForBinding(thm.KubernetesBindingId) } @@ -158,14 +158,14 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { // Extra monitor IDs can be returned if several Synchronization binding contexts are combined. if len(combineResult.MonitorIDs) > 0 { s.logger.Debug("Task monitorID. Combined monitorIDs.", - slog.Any("monitorIDs", hm.MonitorIDs), - slog.Any("combinedMonitorIDs", combineResult.MonitorIDs)) + slog.Any(pkg.LogKeyMonitorIDs, hm.MonitorIDs), + slog.Any(pkg.LogKeyCombinedMonitorIDs, combineResult.MonitorIDs)) hm.MonitorIDs = combineResult.MonitorIDs } s.logger.Debug("Got monitorIDs", - slog.Any("monitorIDs", hm.MonitorIDs)) + slog.Any(pkg.LogKeyMonitorIDs, hm.MonitorIDs)) s.shellTask.UpdateMetadata(hm) } @@ -195,7 +195,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { errors = 1.0 s.logger.Error("Global hook failed, requeue task to retry after delay.", - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(err)) s.shellTask.UpdateFailureMessage(err.Error()) @@ -208,9 +208,9 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { success = 1.0 s.logger.Debug("GlobalHookRun success", - slog.String("beforeChecksum", beforeChecksum), - slog.String("afterChecksum", afterChecksum), - slog.String("savedChecksum", hm.ValuesChecksum)) + slog.String(pkg.LogKeyBeforeChecksum, beforeChecksum), + slog.String(pkg.LogKeyAfterChecksum, afterChecksum), + slog.String(pkg.LogKeySavedChecksum, hm.ValuesChecksum)) res.Status = queue.Success @@ -271,10 +271,10 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { // Save event source info to add it as props to the task and use in logger later. triggeredBy := []slog.Attr{ - slog.String("event.triggered-by.hook", logLabels[pkg.LogKeyHook]), - slog.String("event.triggered-by.binding", logLabels[pkg.LogKeyBinding]), - slog.String("event.triggered-by.binding.name", logLabels[pkg.LogKeyBindingName]), - slog.String("event.triggered-by.watchEvent", logLabels[pkg.LogKeyWatchEvent]), + slog.String(pkg.LogKeyEventTriggeredByHook, logLabels[pkg.LogKeyHook]), + slog.String(pkg.LogKeyEventTriggeredByBinding, logLabels[pkg.LogKeyBinding]), + slog.String(pkg.LogKeyEventTriggeredByBindingName, logLabels[pkg.LogKeyBindingName]), + slog.String(pkg.LogKeyEventTriggeredByWatchEvent, logLabels[pkg.LogKeyWatchEvent]), } delete(logLabels, pkg.LogKeyHook) diff --git a/pkg/task/tasks/module-delete/task.go b/pkg/task/tasks/module-delete/task.go index 7fe8d3c8f..0980e30ae 100644 --- a/pkg/task/tasks/module-delete/task.go +++ b/pkg/task/tasks/module-delete/task.go @@ -79,7 +79,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { baseModule := s.moduleManager.GetModule(hm.ModuleName) - s.logger.Debug("Module delete", slog.String("name", hm.ModuleName)) + s.logger.Debug("Module delete", slog.String(pkg.LogKeyName, hm.ModuleName)) // Register module hooks to run afterHelmDelete hooks on startup. // It's a noop if registration is done before. @@ -101,7 +101,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { s.metricStorage.CounterAdd(metrics.ModuleDeleteErrorsTotal, 1.0, map[string]string{pkg.MetricKeyModule: hm.ModuleName}) s.logger.Error("Module delete failed, requeue task to retry after delay.", - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(err)) s.shellTask.UpdateFailureMessage(err.Error()) @@ -109,7 +109,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { res.Status = queue.Fail } else { - s.logger.Debug("Module delete success", slog.String("name", hm.ModuleName)) + s.logger.Debug("Module delete success", slog.String(pkg.LogKeyName, hm.ModuleName)) res.Status = queue.Success } @@ -120,7 +120,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { func (s *Task) drainModuleQueues(modName string) { m := s.moduleManager.GetModule(modName) if m == nil { - s.logger.Warn("Module is absent when we try to drain its queue", slog.String("module", modName)) + s.logger.Warn("Module is absent when we try to drain its queue", slog.String(pkg.LogKeyModule, modName)) return } diff --git a/pkg/task/tasks/module-ensure-crds/task.go b/pkg/task/tasks/module-ensure-crds/task.go index 28d9d5c84..d567280bc 100644 --- a/pkg/task/tasks/module-ensure-crds/task.go +++ b/pkg/task/tasks/module-ensure-crds/task.go @@ -9,6 +9,7 @@ import ( crdinstaller "github.com/deckhouse/module-sdk/pkg/crd-installer" "go.opentelemetry.io/otel" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/addon-operator/converge" "github.com/flant/addon-operator/pkg/module_manager" "github.com/flant/addon-operator/pkg/module_manager/models/modules" @@ -102,7 +103,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { baseModule := s.moduleManager.GetModule(hm.ModuleName) - s.logger.Debug("Module ensureCRDs", slog.String("name", hm.ModuleName)) + s.logger.Debug("Module ensureCRDs", slog.String(pkg.LogKeyName, hm.ModuleName)) if appliedGVKs, err := s.EnsureCRDs(baseModule); err != nil { s.moduleManager.UpdateModuleLastErrorAndNotify(baseModule, err) diff --git a/pkg/task/tasks/module-hook-run/task.go b/pkg/task/tasks/module-hook-run/task.go index 1ae8e9773..24a997ae3 100644 --- a/pkg/task/tasks/module-hook-run/task.go +++ b/pkg/task/tasks/module-hook-run/task.go @@ -163,9 +163,9 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { // Task 'tsk' will be combined, so remove it from the SynchronizationState. if thm.IsSynchronization() { s.logger.Debug("Synchronization task is combined, mark it as Done", - slog.String("name", thm.HookName), - slog.String("binding", thm.Binding), - slog.String("id", thm.KubernetesBindingId)) + slog.String(pkg.LogKeyName, thm.HookName), + slog.String(pkg.LogKeyBinding, thm.Binding), + slog.String(pkg.LogKeyID, thm.KubernetesBindingId)) baseModule.Synchronization().DoneForBinding(thm.KubernetesBindingId) } @@ -179,7 +179,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { hm.MonitorIDs = append(hm.MonitorIDs, combineResult.MonitorIDs...) } - s.logger.Debug("Got monitorIDs", slog.Any("monitorIDs", hm.MonitorIDs)) + s.logger.Debug("Got monitorIDs", slog.Any(pkg.LogKeyMonitorIDs, hm.MonitorIDs)) s.shellTask.UpdateMetadata(hm) } @@ -211,7 +211,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { errors = 1.0 s.logger.Error("Module hook failed, requeue task to retry after delay.", - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(err)) s.shellTask.UpdateFailureMessage(err.Error()) @@ -224,7 +224,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { } else { success = 1.0 - s.logger.Debug("Module hook success", slog.String("name", hm.HookName)) + s.logger.Debug("Module hook success", slog.String(pkg.LogKeyName, hm.HookName)) res.Status = queue.Success s.moduleManager.UpdateModuleHookStatusAndNotify(baseModule, hm.HookName, nil) @@ -258,10 +258,10 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { logLabels := s.shellTask.GetLogLabels() // Save event source info to add it as props to the task and use in logger later. triggeredBy := []slog.Attr{ - slog.String("event.triggered-by.hook", logLabels[pkg.LogKeyHook]), - slog.String("event.triggered-by.binding", logLabels[pkg.LogKeyBinding]), - slog.String("event.triggered-by.binding.name", logLabels[pkg.LogKeyBindingName]), - slog.String("event.triggered-by.watchEvent", logLabels[pkg.LogKeyWatchEvent]), + slog.String(pkg.LogKeyEventTriggeredByHook, logLabels[pkg.LogKeyHook]), + slog.String(pkg.LogKeyEventTriggeredByBinding, logLabels[pkg.LogKeyBinding]), + slog.String(pkg.LogKeyEventTriggeredByBindingName, logLabels[pkg.LogKeyBindingName]), + slog.String(pkg.LogKeyEventTriggeredByWatchEvent, logLabels[pkg.LogKeyWatchEvent]), } delete(logLabels, pkg.LogKeyHook) delete(logLabels, pkg.LogKeyHookType) diff --git a/pkg/task/tasks/module-run/task.go b/pkg/task/tasks/module-run/task.go index 2c88e0f03..46d67727d 100644 --- a/pkg/task/tasks/module-run/task.go +++ b/pkg/task/tasks/module-run/task.go @@ -138,8 +138,8 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non s.logger.Error("ModuleRun failed. Requeue task to retry after delay.", slog.String(pkg.LogKeyModule, hm.ModuleName), - slog.String("phase", string(baseModule.GetPhase())), - slog.Int("count", s.shellTask.GetFailureCount()+1), + slog.String(pkg.LogKeyPhase, string(baseModule.GetPhase())), + slog.Int(pkg.LogKeyCount, s.shellTask.GetFailureCount()+1), log.Err(moduleRunErr)) s.metricStorage.CounterAdd(metrics.ModuleRunErrorsTotal, 1.0, map[string]string{pkg.MetricKeyModule: hm.ModuleName}) @@ -186,7 +186,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non if moduleRunErr == nil { if hm.DoModuleStartup { s.logger.Debug("ModuleRun phase", - slog.String("phase", string(baseModule.GetPhase()))) + slog.String(pkg.LogKeyPhase, string(baseModule.GetPhase()))) treg := trace.StartRegion(context.Background(), "ModuleRun-OnStartup") @@ -212,7 +212,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non if baseModule.GetPhase() == modules.OnStartupDone { span.AddEvent("module on startup done") - s.logger.Debug("ModuleRun phase", slog.String("phase", string(baseModule.GetPhase()))) + s.logger.Debug("ModuleRun phase", slog.String(pkg.LogKeyPhase, string(baseModule.GetPhase()))) if baseModule.HasKubernetesHooks() { s.moduleManager.SetModulePhaseAndNotify(baseModule, modules.QueueSynchronizationTasks) } else { @@ -229,7 +229,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non if baseModule.GetPhase() == modules.QueueSynchronizationTasks { span.AddEvent("module queue synchronization tasks") - s.logger.Debug("ModuleRun phase", slog.String("phase", string(baseModule.GetPhase()))) + s.logger.Debug("ModuleRun phase", slog.String(pkg.LogKeyPhase, string(baseModule.GetPhase()))) // ModuleHookRun.Synchronization tasks for bindings with the "main" queue. mainSyncTasks := make([]sh_task.Task, 0) @@ -247,18 +247,18 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non } taskLogLabels := utils.MergeLabels(s.shellTask.GetLogLabels(), map[string]string{ - "binding": string(htypes.OnKubernetesEvent) + "Synchronization", - "module": hm.ModuleName, - pkg.LogKeyHook: hook.GetName(), - "hook.type": "module", - "queue": queueName, + pkg.LogKeyBinding: string(htypes.OnKubernetesEvent) + "Synchronization", + pkg.LogKeyModule: hm.ModuleName, + pkg.LogKeyHook: hook.GetName(), + pkg.LogKeyHookType: "module", + pkg.LogKeyQueue: queueName, }) if len(info.BindingContext) > 0 { - taskLogLabels["binding.name"] = info.BindingContext[0].Binding + taskLogLabels[pkg.LogKeyBindingName] = info.BindingContext[0].Binding } - delete(taskLogLabels, "task.id") + delete(taskLogLabels, pkg.LogKeyTaskID) kubernetesBindingID := uuid.Must(uuid.NewV4()).String() parallelRunMetadata := &task.ParallelRunMetadata{} @@ -309,7 +309,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non for _, tsk := range parallelSyncTasksToWait { if err := s.queueService.AddLastTaskToQueue(tsk.GetQueueName(), tsk); err != nil { s.logger.Error("queue is not found while EnableKubernetesBindings task", - slog.String("queue", tsk.GetQueueName())) + slog.String(pkg.LogKeyQueue, tsk.GetQueueName())) continue } @@ -324,7 +324,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non for _, tsk := range parallelSyncTasks { if err := s.queueService.AddLastTaskToQueue(tsk.GetQueueName(), tsk); err != nil { s.logger.Error("queue is not found while EnableKubernetesBindings task", - slog.String("queue", tsk.GetQueueName())) + slog.String(pkg.LogKeyQueue, tsk.GetQueueName())) } } @@ -336,7 +336,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non } else { // There are tasks to wait. s.moduleManager.SetModulePhaseAndNotify(baseModule, modules.WaitForSynchronization) - s.logger.With("module.state", "wait-for-synchronization"). + s.logger.With(pkg.LogKeyModuleState, "wait-for-synchronization"). Debug("ModuleRun wait for Synchronization") } @@ -366,10 +366,10 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non // Debug messages every fifth second: print Synchronization state. if time.Now().UnixNano()%5000000000 == 0 { s.logger.Debug("ModuleRun wait Synchronization state", - slog.Bool("moduleStartup", hm.DoModuleStartup), - slog.Bool("syncNeeded", baseModule.SynchronizationNeeded()), - slog.Bool("syncQueued", baseModule.Synchronization().HasQueued()), - slog.Bool("syncDone", baseModule.Synchronization().IsCompleted())) + slog.Bool(pkg.LogKeyModuleStartup, hm.DoModuleStartup), + slog.Bool(pkg.LogKeySyncNeeded, baseModule.SynchronizationNeeded()), + slog.Bool(pkg.LogKeySyncQueued, baseModule.Synchronization().HasQueued()), + slog.Bool(pkg.LogKeySyncDone, baseModule.Synchronization().IsCompleted())) baseModule.Synchronization().DebugDumpState(s.logger) } s.logger.Debug("Synchronization not completed, keep ModuleRun task in repeat mode") @@ -385,7 +385,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non if baseModule.GetPhase() == modules.EnableScheduleBindings { span.AddEvent("module enable schedule bindings") - s.logger.Debug("ModuleRun phase", slog.String("phase", string(baseModule.GetPhase()))) + s.logger.Debug("ModuleRun phase", slog.String(pkg.LogKeyPhase, string(baseModule.GetPhase()))) s.moduleManager.EnableModuleScheduleBindings(hm.ModuleName) s.moduleManager.SetModulePhaseAndNotify(baseModule, modules.CanRunHelm) @@ -405,7 +405,7 @@ func (s *Task) Handle(ctx context.Context) (res queue.TaskResult) { //nolint:non if baseModule.GetPhase() == modules.CanRunHelm { span.AddEvent("module can run helm") - s.logger.Debug("ModuleRun phase", slog.String("phase", string(baseModule.GetPhase()))) + s.logger.Debug("ModuleRun phase", slog.String(pkg.LogKeyPhase, string(baseModule.GetPhase()))) // run beforeHelm, helm, afterHelm valuesChanged, moduleRunErr = s.moduleManager.RunModule(ctx, baseModule.GetName(), s.shellTask.GetLogLabels()) } @@ -432,7 +432,7 @@ func (s *Task) CreateAndStartQueuesForModuleHooks(moduleName string) { s.queueService.CreateAndStartQueue(hookBinding.Queue, taskqueue.CompactionCallback(s.moduleManager, s.logger)) log.Debug("Queue started for module 'schedule'", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hook.GetName())) } } @@ -445,7 +445,7 @@ func (s *Task) CreateAndStartQueuesForModuleHooks(moduleName string) { s.queueService.CreateAndStartQueue(hookBinding.Queue, taskqueue.CompactionCallback(s.moduleManager, s.logger)) log.Debug("Queue started for module 'kubernetes'", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hook.GetName())) } } diff --git a/pkg/task/tasks/parallel-module-run/task.go b/pkg/task/tasks/parallel-module-run/task.go index dc83744fa..f8e73fc1a 100644 --- a/pkg/task/tasks/parallel-module-run/task.go +++ b/pkg/task/tasks/parallel-module-run/task.go @@ -85,7 +85,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { if hm.ParallelRunMetadata == nil { s.logger.Error("Possible bug! Couldn't get task ParallelRunMetadata for a parallel task.", - slog.String("description", hm.EventDescription)) + slog.String(pkg.LogKeyDescription, hm.EventDescription)) res.Status = queue.Fail return res } @@ -96,7 +96,7 @@ func (s *Task) Handle(ctx context.Context) queue.TaskResult { s.parallelTaskChannels.Set(s.shellTask.GetId(), parallelChannel) s.logger.Debug("ParallelModuleRun available parallel event channels", - slog.String("channels", fmt.Sprintf("%v", s.parallelTaskChannels.Channels()))) + slog.String(pkg.LogKeyChannels, fmt.Sprintf("%v", s.parallelTaskChannels.Channels()))) for moduleName, moduleMetadata := range hm.ParallelRunMetadata.GetModulesMetadata() { queueName := fmt.Sprintf(app.ParallelQueueNamePattern, i%(app.NumberOfParallelQueues-1)) @@ -131,7 +131,7 @@ L: select { case parallelEvent := <-parallelChannel: s.logger.Debug("ParallelModuleRun event received", - slog.String("event", fmt.Sprintf("%v", parallelEvent))) + slog.String(pkg.LogKeyEvent, fmt.Sprintf("%v", parallelEvent))) if len(parallelEvent.ErrorMessage()) != 0 { if tasksErrors[parallelEvent.ModuleName()] != parallelEvent.ErrorMessage() { @@ -211,7 +211,7 @@ func (s *Task) CreateAndStartQueuesForModuleHooks(moduleName string) { s.queueService.CreateAndStartQueue(hookBinding.Queue, taskqueue.CompactionCallback(s.moduleManager, s.logger)) log.Debug("Queue started for module 'schedule'", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hook.GetName())) } } @@ -224,7 +224,7 @@ func (s *Task) CreateAndStartQueuesForModuleHooks(moduleName string) { s.queueService.CreateAndStartQueue(hookBinding.Queue, taskqueue.CompactionCallback(s.moduleManager, s.logger)) log.Debug("Queue started for module 'kubernetes'", - slog.String("queue", hookBinding.Queue), + slog.String(pkg.LogKeyQueue, hookBinding.Queue), slog.String(pkg.LogKeyHook, hook.GetName())) } } diff --git a/pkg/utils/jsonpatch.go b/pkg/utils/jsonpatch.go index c52eddfe6..407a624b5 100644 --- a/pkg/utils/jsonpatch.go +++ b/pkg/utils/jsonpatch.go @@ -11,6 +11,8 @@ import ( lazynode "github.com/deckhouse/module-sdk/pkg/utils/lazy-node" "github.com/pkg/errors" + + "github.com/flant/addon-operator/pkg" ) const ( @@ -55,7 +57,7 @@ func (o Operation) Kind() string { // Path reads the "path" field of the Operation. func (o Operation) Path() (string, error) { - if obj, ok := o["path"]; ok && obj != nil { + if obj, ok := o[pkg.LogKeyPath]; ok && obj != nil { var op string err := json.Unmarshal(*obj, &op) @@ -86,7 +88,7 @@ func (o Operation) From() (string, error) { } func (o Operation) value() *lazynode.LazyNode { - if obj, ok := o["value"]; ok { + if obj, ok := o[pkg.LogKeyValue]; ok { return lazynode.NewLazyNode(obj) } @@ -95,7 +97,7 @@ func (o Operation) value() *lazynode.LazyNode { // ValueInterface decodes the operation value into an interface. func (o Operation) ValueInterface() (any, error) { - if obj, ok := o["value"]; ok && obj != nil { + if obj, ok := o[pkg.LogKeyValue]; ok && obj != nil { var v any err := json.Unmarshal(*obj, &v) diff --git a/pkg/utils/loader.go b/pkg/utils/loader.go index 9fb0fcb6b..d7f5c718f 100644 --- a/pkg/utils/loader.go +++ b/pkg/utils/loader.go @@ -8,6 +8,8 @@ import ( "strings" "github.com/deckhouse/deckhouse/pkg/log" + + "github.com/flant/addon-operator/pkg" ) const ( @@ -35,7 +37,7 @@ func LoadValuesFileFromDir(dir string, strictModeEnabled bool) (Values, error) { valuesYaml, err := os.ReadFile(valuesFilePath) if err != nil && os.IsNotExist(err) && !strictModeEnabled { log.Debug("No static values file", - slog.String("path", valuesFilePath), + slog.String(pkg.LogKeyPath, valuesFilePath), log.Err(err)) return nil, nil } diff --git a/pkg/utils/values.go b/pkg/utils/values.go index 95dca13e3..60ab21902 100644 --- a/pkg/utils/values.go +++ b/pkg/utils/values.go @@ -12,6 +12,7 @@ import ( "gopkg.in/yaml.v3" k8syaml "sigs.k8s.io/yaml" + "github.com/flant/addon-operator/pkg" utils_checksum "github.com/flant/shell-operator/pkg/utils/checksum" ) @@ -143,7 +144,7 @@ func (v Values) SectionByKey(key string) Values { newV, err := NewValues(data) if err != nil { log.Error("get section Values", - slog.String("key", key), + slog.String(pkg.LogKeyKey, key), log.Err(err)) } return newV diff --git a/pkg/values/validation/schemas.go b/pkg/values/validation/schemas.go index 88835e7ed..269d9b542 100644 --- a/pkg/values/validation/schemas.go +++ b/pkg/values/validation/schemas.go @@ -18,6 +18,7 @@ import ( "github.com/hashicorp/go-multierror" "sigs.k8s.io/yaml" + "github.com/flant/addon-operator/pkg" "github.com/flant/addon-operator/pkg/utils" "github.com/flant/addon-operator/pkg/values/validation/cel" "github.com/flant/addon-operator/pkg/values/validation/schema" @@ -91,8 +92,8 @@ func (st *SchemaStorage) Validate(valuesType SchemaType, moduleName string, valu schema := st.Schemas[valuesType] if schema == nil { log.Warn("schema is not found", - slog.String("module", moduleName), - slog.String("valuesType", string(valuesType))) + slog.String(pkg.LogKeyModule, moduleName), + slog.String(pkg.LogKeyValuesType, string(valuesType))) return nil } @@ -104,10 +105,10 @@ func (st *SchemaStorage) Validate(valuesType SchemaType, moduleName string, valu validationErr := validateObject(obj, schema, moduleName) if validationErr == nil { log.Debug("values are valid", - slog.String("valuesType", string(valuesType))) + slog.String(pkg.LogKeyValuesType, string(valuesType))) } else { log.Debug("values are NOT valid", - slog.String("valuesType", string(valuesType)), + slog.String(pkg.LogKeyValuesType, string(valuesType)), log.Err(validationErr)) } return validationErr