Skip to content

Commit

Permalink
change lib to deckhouse log
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
  • Loading branch information
Pavel Okhlopkov committed Oct 31, 2024
1 parent fb3295e commit 8b8666a
Show file tree
Hide file tree
Showing 47 changed files with 137 additions and 138 deletions.
10 changes: 5 additions & 5 deletions cmd/addon-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"

"github.com/deckhouse/deckhouse/go_lib/log"
addon_operator "github.com/flant/addon-operator/pkg/addon-operator"
"github.com/flant/addon-operator/pkg/app"
"github.com/flant/addon-operator/pkg/kube_config_manager/backend/configmap"
"github.com/flant/addon-operator/pkg/utils/stdliblogtologrus"
"github.com/flant/kube-client/klogtologrus"
sh_app "github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/debug"
"github.com/flant/shell-operator/pkg/unilogger"
utils_signal "github.com/flant/shell-operator/pkg/utils/signal"
)

Expand All @@ -35,8 +35,8 @@ const (
func main() {
kpApp := kingpin.New(app.AppName, fmt.Sprintf("%s %s: %s", app.AppName, app.Version, app.AppDescription))

logger := unilogger.NewLogger(unilogger.Options{})
unilogger.SetDefault(logger)
logger := log.NewLogger(log.Options{})
log.SetDefault(logger)

// override usage template to reveal additional commands with information about start command
kpApp.UsageTemplate(sh_app.OperatorUsageTemplate(app.AppName))
Expand Down Expand Up @@ -66,7 +66,7 @@ func main() {
kingpin.MustParse(kpApp.Parse(os.Args[1:]))
}

func start(logger *unilogger.Logger) func(_ *kingpin.ParseContext) error {
func start(logger *log.Logger) func(_ *kingpin.ParseContext) error {
return func(_ *kingpin.ParseContext) error {
sh_app.AppStartMessage = fmt.Sprintf("%s %s, shell-operator %s", app.AppName, app.Version, sh_app.Version)

Expand Down Expand Up @@ -170,7 +170,7 @@ func runHAMode(ctx context.Context, operator *addon_operator.AddonOperator) {

go func() {
<-ctx.Done()
unilogger.Info("Context canceled received")
log.Info("Context canceled received")
if err := syscall.Kill(1, syscall.SIGUSR2); err != nil {
operator.Logger.Fatal("Couldn't shutdown addon-operator", slog.String("error", err.Error()))
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module github.com/flant/addon-operator

go 1.22.4
go 1.23.1

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031111821-6d9459d45b78
github.com/dominikbraun/graph v0.23.0
github.com/ettle/strcase v0.2.0
github.com/flant/kube-client v1.2.0
github.com/flant/shell-operator v1.4.15-slog3
github.com/flant/shell-operator v0.0.0-20241031112753-639b2cc1e5ad
github.com/go-chi/chi/v5 v5.1.0
github.com/go-openapi/loads v0.19.5
github.com/go-openapi/spec v0.19.8
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031111821-6d9459d45b78 h1:H5ROFYwvuxE51WzcS9mSKDiD2/6tQCjNExqiUOxLvqY=
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031111821-6d9459d45b78/go.mod h1:okGCqOIU70vDhivVs/UmuSMTdZMHKNTVw2cRAv+Pzqc=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/distribution/distribution/v3 v3.0.0-beta.1 h1:X+ELTxPuZ1Xe5MsD3kp2wfGUhc8I+MPfRis8dZ818Ic=
Expand Down Expand Up @@ -134,8 +136,8 @@ github.com/flant/kube-client v1.2.0 h1:cOjnZgwoJVm4scqsMGcgO6qctJ+Z6wRVTFqvyhoAO
github.com/flant/kube-client v1.2.0/go.mod h1:qjNvqCqnBqS+kc9Hj+wMBbkFzfdyGyiX4r4teE/0Pm8=
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee h1:evii83J+/6QGNvyf6tjQ/p27DPY9iftxIBb37ALJRTg=
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee/go.mod h1:f+REaGl/+pZR97rbTcwHEka/MAipoQQ2Mc0iQUj4ak0=
github.com/flant/shell-operator v1.4.15-slog3 h1:R4UqvjGsHFcFLpCxy9AensPTNWXXy33OtvupJ+rcvgo=
github.com/flant/shell-operator v1.4.15-slog3/go.mod h1:vcq3LKAS3Zg/aMnRzzy7YCJYeRuhfyz3zoSqQRfbotA=
github.com/flant/shell-operator v0.0.0-20241031112753-639b2cc1e5ad h1:3X0Q1SbmOdsZSgaaIZBioR+bCdqDDIeOHj/iUPl1VHw=
github.com/flant/shell-operator v0.0.0-20241031112753-639b2cc1e5ad/go.mod h1:WvORh9wzl30x6LPlqVs+5OuEo04kw+kXMuWgPQKH71I=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
Expand Down
2 changes: 1 addition & 1 deletion pkg/addon-operator/admission_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path"
"time"

log "github.com/flant/shell-operator/pkg/unilogger"
log "github.com/deckhouse/deckhouse/go_lib/log"
)

type AdmissionServer struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/addon-operator/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package addon_operator

import (
log "github.com/deckhouse/deckhouse/go_lib/log"
"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"
"github.com/flant/addon-operator/pkg/module_manager"
sh_app "github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/debug"
shell_operator "github.com/flant/shell-operator/pkg/shell-operator"
log "github.com/flant/shell-operator/pkg/unilogger"
)

// Bootstrap inits all dependencies for a full-fledged AddonOperator instance.
Expand Down
4 changes: 2 additions & 2 deletions pkg/addon-operator/kube_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"fmt"

"github.com/deckhouse/deckhouse/go_lib/log"
"github.com/flant/addon-operator/pkg/app"
"github.com/flant/addon-operator/pkg/helm_resources_manager"
klient "github.com/flant/kube-client/client"
sh_app "github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/metric_storage"
"github.com/flant/shell-operator/pkg/unilogger"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)

Expand All @@ -28,7 +28,7 @@ func defaultHelmMonitorKubeClient(metricStorage *metric_storage.MetricStorage, m
return client
}

func InitDefaultHelmResourcesManager(ctx context.Context, metricStorage *metric_storage.MetricStorage, logger *unilogger.Logger) (helm_resources_manager.HelmResourcesManager, error) {
func InitDefaultHelmResourcesManager(ctx context.Context, metricStorage *metric_storage.MetricStorage, logger *log.Logger) (helm_resources_manager.HelmResourcesManager, error) {
kubeClient := defaultHelmMonitorKubeClient(metricStorage, DefaultHelmMonitorKubeClientMetricLabels)
if err := kubeClient.Init(); err != nil {
return nil, fmt.Errorf("initialize Kubernetes client for Helm resources manager: %s\n", err)
Expand Down
54 changes: 27 additions & 27 deletions pkg/addon-operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/leaderelection"

"github.com/deckhouse/deckhouse/go_lib/log"
"github.com/flant/addon-operator/pkg/addon-operator/converge"
"github.com/flant/addon-operator/pkg/app"
"github.com/flant/addon-operator/pkg/helm"
Expand Down Expand Up @@ -45,7 +46,6 @@ import (
shell_operator "github.com/flant/shell-operator/pkg/shell-operator"
sh_task "github.com/flant/shell-operator/pkg/task"
"github.com/flant/shell-operator/pkg/task/queue"
"github.com/flant/shell-operator/pkg/unilogger"
fileUtils "github.com/flant/shell-operator/pkg/utils/file"
"github.com/flant/shell-operator/pkg/utils/measure"
)
Expand Down Expand Up @@ -102,7 +102,7 @@ type AddonOperator struct {
// discoveredGVKs is a map of GVKs from applied modules' CRDs
discoveredGVKs map[string]struct{}

Logger *unilogger.Logger
Logger *log.Logger
}

type parallelQueueEvent struct {
Expand Down Expand Up @@ -135,7 +135,7 @@ func (pq *parallelTaskChannels) Delete(id string) {
pq.l.Unlock()
}

func NewAddonOperator(ctx context.Context, logger *unilogger.Logger) *AddonOperator {
func NewAddonOperator(ctx context.Context, logger *log.Logger) *AddonOperator {
cctx, cancel := context.WithCancel(ctx)
so := shell_operator.NewShellOperator(cctx, logger.Named("shell-operator"))

Expand Down Expand Up @@ -220,7 +220,7 @@ func (op *AddonOperator) Setup() error {
if err != nil {
return fmt.Errorf("global hooks directory: %s", err)
}
unilogger.Infof("Global hooks directory: %s", globalHooksDir)
log.Infof("Global hooks directory: %s", globalHooksDir)

tempDir, err := fileUtils.EnsureTempDirectory(sh_app.TempDir)
if err != nil {
Expand All @@ -243,7 +243,7 @@ func (op *AddonOperator) Start(_ context.Context) error {
return err
}

unilogger.Info("Start first converge for modules")
log.Info("Start first converge for modules")
// Loading the onStartup hooks into the queue and running all modules.
// Turning tracking changes on only after startup ends.

Expand Down Expand Up @@ -920,12 +920,12 @@ func (op *AddonOperator) CreateAndStartQueuesForGlobalHooks() {
h := op.ModuleManager.GetGlobalHook(hookName)
for _, hookBinding := range h.GetHookConfig().Schedules {
if op.CreateAndStartQueue(hookBinding.Queue) {
unilogger.Debugf("Queue '%s' started for global 'schedule' hook %s", hookBinding.Queue, hookName)
log.Debugf("Queue '%s' started for global 'schedule' hook %s", hookBinding.Queue, hookName)
}
}
for _, hookBinding := range h.GetHookConfig().OnKubernetesEvents {
if op.CreateAndStartQueue(hookBinding.Queue) {
unilogger.Debugf("Queue '%s' started for global 'kubernetes' hook %s", hookBinding.Queue, hookName)
log.Debugf("Queue '%s' started for global 'kubernetes' hook %s", hookBinding.Queue, hookName)
}
}
}
Expand All @@ -944,7 +944,7 @@ func (op *AddonOperator) CreateAndStartQueuesForModuleHooks(moduleName string) {
for _, hook := range scheduleHooks {
for _, hookBinding := range hook.GetHookConfig().Schedules {
if op.CreateAndStartQueue(hookBinding.Queue) {
unilogger.Debugf("Queue '%s' started for module 'schedule' hook %s", hookBinding.Queue, hook.GetName())
log.Debugf("Queue '%s' started for module 'schedule' hook %s", hookBinding.Queue, hook.GetName())
}
}
}
Expand All @@ -953,7 +953,7 @@ func (op *AddonOperator) CreateAndStartQueuesForModuleHooks(moduleName string) {
for _, hook := range kubeEventsHooks {
for _, hookBinding := range hook.GetHookConfig().OnKubernetesEvents {
if op.CreateAndStartQueue(hookBinding.Queue) {
unilogger.Debugf("Queue '%s' started for module 'kubernetes' hook %s", hookBinding.Queue, hook.GetName())
log.Debugf("Queue '%s' started for module 'kubernetes' hook %s", hookBinding.Queue, hook.GetName())
}
}
}
Expand All @@ -964,12 +964,12 @@ func (op *AddonOperator) CreateAndStartQueuesForModuleHooks(moduleName string) {
// h := op.ModuleManager.GetModuleHook(hookName)
// for _, hookBinding := range h.Config.Schedules {
// if op.CreateAndStartQueue(hookBinding.Queue) {
// unilogger.Debugf("Queue '%s' started for module 'schedule' hook %s", hookBinding.Queue, hookName)
// log.Debugf("Queue '%s' started for module 'schedule' hook %s", hookBinding.Queue, hookName)
// }
// }
// for _, hookBinding := range h.Config.OnKubernetesEvents {
// if op.CreateAndStartQueue(hookBinding.Queue) {
// unilogger.Debugf("Queue '%s' started for module 'kubernetes' hook %s", hookBinding.Queue, hookName)
// log.Debugf("Queue '%s' started for module 'kubernetes' hook %s", hookBinding.Queue, hookName)
// }
// }
//}
Expand All @@ -980,7 +980,7 @@ func (op *AddonOperator) CreateAndStartParallelQueues() {
for i := 0; i < app.NumberOfParallelQueues; i++ {
queueName := fmt.Sprintf(app.ParallelQueueNamePattern, i)
if op.engine.TaskQueues.GetByName(queueName) != nil {
unilogger.Warnf("Parallel queue %s already exists", queueName)
log.Warnf("Parallel queue %s already exists", queueName)
continue
}
op.engine.TaskQueues.NewNamedQueue(queueName, op.ParallelTasksHandler)
Expand All @@ -991,7 +991,7 @@ func (op *AddonOperator) CreateAndStartParallelQueues() {
func (op *AddonOperator) DrainModuleQueues(modName string) {
m := op.ModuleManager.GetModule(modName)
if m == nil {
unilogger.Warnf("Module %q is absent when we try to drain its queue", modName)
log.Warnf("Module %q is absent when we try to drain its queue", modName)
return
}

Expand Down Expand Up @@ -1465,7 +1465,7 @@ func (op *AddonOperator) HandleGlobalHookEnableKubernetesBindings(t sh_task.Task
for _, tsk := range parallelSyncTasksToWait {
q := op.engine.TaskQueues.GetByName(tsk.GetQueueName())
if q == nil {
unilogger.Errorf("Queue %s is not created while run GlobalHookEnableKubernetesBindings task!", tsk.GetQueueName())
log.Errorf("Queue %s is not created while run GlobalHookEnableKubernetesBindings task!", tsk.GetQueueName())
} else {
// Skip state creation if WaitForSynchronization is disabled.
thm := task.HookMetadataAccessor(tsk)
Expand All @@ -1478,7 +1478,7 @@ func (op *AddonOperator) HandleGlobalHookEnableKubernetesBindings(t sh_task.Task
for _, tsk := range parallelSyncTasks {
q := op.engine.TaskQueues.GetByName(tsk.GetQueueName())
if q == nil {
unilogger.Errorf("Queue %s is not created while run GlobalHookEnableKubernetesBindings task!", tsk.GetQueueName())
log.Errorf("Queue %s is not created while run GlobalHookEnableKubernetesBindings task!", tsk.GetQueueName())
} else {
q.AddLast(tsk)
}
Expand Down Expand Up @@ -2394,7 +2394,7 @@ func (op *AddonOperator) CreateConvergeModulesTasks(state *module_manager.Module
queuedAt := time.Now()

// Add ModuleDelete tasks to delete helm releases of disabled modules.
unilogger.Debugf("The following modules are going to be disabled: %v", state.ModulesToDisable)
log.Debugf("The following modules are going to be disabled: %v", state.ModulesToDisable)
for _, moduleName := range state.ModulesToDisable {
ev := events.ModuleEvent{
ModuleName: moduleName,
Expand All @@ -2417,7 +2417,7 @@ func (op *AddonOperator) CreateConvergeModulesTasks(state *module_manager.Module

// Add ModuleRun tasks to install or reload enabled modules.
newlyEnabled := utils.ListToMapStringStruct(state.ModulesToEnable)
unilogger.Debugf("The following modules are going to be enabled/rerun: %v", state.AllEnabledModulesByOrder)
log.Debugf("The following modules are going to be enabled/rerun: %v", state.AllEnabledModulesByOrder)
// sort modules' orders
sortedOrders := make(node.NodeWeightRange, 0, len(state.AllEnabledModulesByOrder))
for order := range state.AllEnabledModulesByOrder {
Expand Down Expand Up @@ -2509,13 +2509,13 @@ func (op *AddonOperator) CreateConvergeModulesTasks(state *module_manager.Module
modulesTasks = append(modulesTasks, newTask.WithQueuedAt(queuedAt))

default:
unilogger.Errorf("Invalid ModulesState %v", state)
log.Errorf("Invalid ModulesState %v", state)
}
}
// as resultingTasks contains new ensureCRDsTasks we invalidate
// ConvregeState.CRDsEnsured if there are new ensureCRDsTasks to execute
if op.ConvergeState.CRDsEnsured && len(resultingTasks) > 0 {
unilogger.Debug("CheckCRDsEnsured: set to false")
log.Debug("CheckCRDsEnsured: set to false")
op.ConvergeState.CRDsEnsured = false
}

Expand All @@ -2530,7 +2530,7 @@ func (op *AddonOperator) CreateConvergeModulesTasks(state *module_manager.Module
// the discovered GVKs
func (op *AddonOperator) CheckCRDsEnsured(t sh_task.Task) {
if !op.ConvergeState.CRDsEnsured && !ModuleEnsureCRDsTasksInQueueAfterId(op.engine.TaskQueues.GetMain(), t.GetId()) {
unilogger.Debug("CheckCRDsEnsured: set to true")
log.Debug("CheckCRDsEnsured: set to true")
op.ConvergeState.CRDsEnsured = true
// apply global values patch
op.discoveredGVKsLock.Lock()
Expand Down Expand Up @@ -2573,7 +2573,7 @@ func (op *AddonOperator) CheckConvergeStatus(t sh_task.Task) {
// Report modules left to process.
if convergeTasks > 0 && (t.GetType() == task.ModuleRun || t.GetType() == task.ModuleDelete) {
moduleTasks := ConvergeModulesInQueue(op.engine.TaskQueues.GetMain())
unilogger.Infof("Converge modules in progress: %d modules left to process in queue 'main'", moduleTasks)
log.Infof("Converge modules in progress: %d modules left to process in queue 'main'", moduleTasks)
}
}

Expand All @@ -2591,7 +2591,7 @@ func (op *AddonOperator) UpdateFirstConvergeStatus(convergeTasks int) {
case converge.FirstStarted:
// Switch to 'done' state after first converge is started and when no 'converge' tasks left in the queue.
if convergeTasks == 0 {
unilogger.Infof("First converge is finished. Operator is ready now.")
log.Infof("First converge is finished. Operator is ready now.")
op.ConvergeState.SetFirstRunPhase(converge.FirstDone)
}
}
Expand Down Expand Up @@ -2703,15 +2703,15 @@ func taskDescriptionForTaskFlowLog(tsk sh_task.Task, action string, phase string
}

// logTaskAdd prints info about queued tasks.
func (op *AddonOperator) logTaskAdd(logEntry *unilogger.Logger, action string, tasks ...sh_task.Task) {
func (op *AddonOperator) logTaskAdd(logEntry *log.Logger, action string, tasks ...sh_task.Task) {
logger := logEntry.With("task.flow", "add")
for _, tsk := range tasks {
logger.Info(taskDescriptionForTaskFlowLog(tsk, action, "", ""))
}
}

// logTaskStart prints info about task at start. Also prints event source info from task props.
func (op *AddonOperator) logTaskStart(logEntry *unilogger.Logger, tsk sh_task.Task) {
func (op *AddonOperator) logTaskStart(logEntry *log.Logger, tsk sh_task.Task) {
// Prevent excess messages for highly frequent tasks.
if tsk.GetType() == task.GlobalHookWaitKubernetesSynchronization {
return
Expand Down Expand Up @@ -2739,14 +2739,14 @@ func (op *AddonOperator) logTaskStart(logEntry *unilogger.Logger, tsk sh_task.Ta
}

// logTaskEnd prints info about task at the end. Info level used only for the ConvergeModules task.
func (op *AddonOperator) logTaskEnd(logEntry *unilogger.Logger, tsk sh_task.Task, result queue.TaskResult) {
func (op *AddonOperator) logTaskEnd(logEntry *log.Logger, tsk sh_task.Task, result queue.TaskResult) {
logger := logEntry.
With("task.flow", "end")
logger = utils.EnrichLoggerWithLabels(logger, tsk.GetLogLabels())

level := unilogger.LevelDebug
level := log.LevelDebug
if tsk.GetType() == task.ConvergeModules {
level = unilogger.LevelInfo
level = log.LevelInfo
}

logger.Log(context.TODO(), level.Level(), taskDescriptionForTaskFlowLog(tsk, "end", op.taskPhase(tsk), string(result.Status)))
Expand Down
Loading

0 comments on commit 8b8666a

Please sign in to comment.