Skip to content

Commit

Permalink
feat: replace deprecated logger methods
Browse files Browse the repository at this point in the history
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
  • Loading branch information
juev committed Dec 18, 2024
1 parent 95c8f23 commit 35c8edb
Show file tree
Hide file tree
Showing 13 changed files with 530 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hooks

import (
"fmt"
"log/slog"
"time"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -54,11 +56,12 @@ func ObjFilter(obj *unstructured.Unstructured) (gohook.FilterResult, error) {
func run(input *gohook.HookInput) error {
for _, o := range input.Snapshots["pods"] {
podSpec := o.(*podSpecFilteredObj)
input.Logger.Infof("Got podSpec: %+v", podSpec)
input.Logger.Info("Got podSpec",
slog.String("spec", fmt.Sprintf("%+v", podSpec)))
}

input.Logger.Infof("Hello from on_kube.pods2! I have %d snapshots\n",
len(input.Snapshots))
input.Logger.Info("Hello from on_kube.pods2! I have snapshots",
slog.Int("count", len(input.Snapshots)))

input.MetricsCollector.Add("addon_go_hooks_total", 1.0, nil)

Expand Down
13 changes: 8 additions & 5 deletions pkg/addon-operator/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package addon_operator

import (
"fmt"
"log/slog"

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

Expand All @@ -18,22 +19,24 @@ import (
func (op *AddonOperator) bootstrap() error {
log.Info(shapp.AppStartMessage)

log.Infof("Search modules in: %s", app.ModulesDir)
log.Info("Search modules",
slog.String("path", app.ModulesDir))

log.Infof("Addon-operator namespace: %s", op.DefaultNamespace)
log.Info("Addon-operator namespace",
slog.String("namespace", op.DefaultNamespace))

// Debug server.
// TODO: rewrite shapp global variables to the addon-operator ones
var err error
op.DebugServer, err = shell_operator.RunDefaultDebugServer(shapp.DebugUnixSocket, shapp.DebugHttpServerAddr, op.Logger.Named("debug-server"))
if err != nil {
log.Errorf("Fatal: start Debug server: %s", err)
log.Error("Fatal: start Debug server", log.Err(err))
return fmt.Errorf("start Debug server: %w", err)
}

err = op.Assemble(op.DebugServer)
if err != nil {
log.Errorf("Fatal: %s", err)
log.Error("Fatal", log.Err(err))
return fmt.Errorf("assemble Debug server: %w", err)
}

Expand Down Expand Up @@ -68,7 +71,7 @@ func (op *AddonOperator) Assemble(debugServer *debug.Server) (err error) {
// SetupKubeConfigManager sets manager, which reads configuration for Modules from a cluster
func (op *AddonOperator) SetupKubeConfigManager(bk backend.ConfigHandler) {
if op.KubeConfigManager != nil {
log.Warnf("KubeConfigManager is already set")
log.Warn("KubeConfigManager is already set")
// return if kube config manager is already set
return
}
Expand Down
256 changes: 162 additions & 94 deletions pkg/addon-operator/operator.go

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions pkg/addon-operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package addon_operator

import (
"context"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"testing"

"github.com/deckhouse/deckhouse/pkg/log"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8types "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

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

"github.com/flant/addon-operator/pkg/addon-operator/converge"
mockhelm "github.com/flant/addon-operator/pkg/helm/test/mock"
mockhelmresmgr "github.com/flant/addon-operator/pkg/helm_resources_manager/test/mock"
Expand Down Expand Up @@ -513,7 +515,8 @@ func Test_HandleConvergeModules_global_changed(t *testing.T) {

g.Eventually(convergeDone(op), "30s", "200ms").Should(BeTrue())

log.Infof("Converge done, got %d tasks in history", len(taskHandleHistory))
log.Info("Converge done, got tasks in history",
slog.Int("count", len(taskHandleHistory)))

// Save current history length to ignore first converge tasks later.
ignoreTasksCount := len(taskHandleHistory)
Expand All @@ -534,7 +537,8 @@ func Test_HandleConvergeModules_global_changed(t *testing.T) {
g.Expect(cmPatched.Data).Should(HaveKey("global"))
g.Expect(cmPatched.Data["global"]).Should(Equal("param: newValue"))

log.Infof("ConfigMap patched, got %d tasks in history", len(taskHandleHistory))
log.Info("ConfigMap patched, got tasks in history",
slog.Int("count", len(taskHandleHistory)))

// Expect ConvergeModules appears in queue.
g.Eventually(func() bool {
Expand Down
33 changes: 23 additions & 10 deletions pkg/helm/helm3/helm3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package helm3
import (
"bytes"
"fmt"
"log/slog"
"os/exec"
"sort"
"strings"
"time"

"github.com/deckhouse/deckhouse/pkg/log"
k8syaml "sigs.k8s.io/yaml"

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

"github.com/flant/addon-operator/pkg/helm/client"
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/shell-operator/pkg/executor"
Expand Down Expand Up @@ -87,7 +89,7 @@ func (h *Helm3Client) initAndVersion() error {
}
stdout = strings.Join([]string{stdout, stderr}, "\n")
stdout = strings.ReplaceAll(stdout, "\n", " ")
log.Infof("Helm 3 version: %s", stdout)
log.Info("Helm 3 version", slog.String("version", stdout))

return nil
}
Expand Down Expand Up @@ -161,12 +163,20 @@ func (h *Helm3Client) UpgradeRelease(releaseName string, chart string, valuesPat
args = append(args, setValue)
}

h.Logger.Infof("Running helm upgrade for release '%s' with chart '%s' in namespace '%s' ...", releaseName, chart, namespace)
h.Logger.Info("Running helm upgrade for release ...",
slog.String("release", releaseName),
slog.String("chart", chart),
slog.String("namespace", namespace))
stdout, stderr, err := h.cmd(args...)
if err != nil {
return fmt.Errorf("helm upgrade failed: %s:\n%s %s", err, stdout, stderr)
}
h.Logger.Infof("Helm upgrade for release '%s' with chart '%s' in namespace '%s' successful:\n%s\n%s", releaseName, chart, namespace, stdout, stderr)
h.Logger.Info("Helm upgrade for release successful",
slog.String("release", releaseName),
slog.String("chart", chart),
slog.String("namespace", namespace),
slog.String("stdout", stdout),
slog.String("stderr", stderr))

return nil
}
Expand All @@ -191,7 +201,7 @@ func (h *Helm3Client) GetReleaseValues(releaseName string) (utils.Values, error)
}

func (h *Helm3Client) DeleteRelease(releaseName string) (err error) {
h.Logger.Debugf("helm release '%s': execute helm uninstall", releaseName)
h.Logger.Debug("helm release: execute helm uninstall", slog.String("release", releaseName))

args := []string{
"uninstall", releaseName,
Expand All @@ -202,7 +212,7 @@ func (h *Helm3Client) DeleteRelease(releaseName string) (err error) {
return fmt.Errorf("helm uninstall %s invocation error: %v\n%v %v", releaseName, err, stdout, stderr)
}

h.Logger.Debugf("helm release %s deleted", releaseName)
h.Logger.Debug("helm release deleted", slog.String("release", releaseName))
return
}

Expand Down Expand Up @@ -230,9 +240,9 @@ func (h *Helm3Client) ListReleasesNames() ([]string, error) {
return nil, fmt.Errorf("helm list failed: %v\n%s %s", err, stdout, stderr)
}

list := []struct {
var list []struct {
Name string `json:"name"`
}{}
}

if err := k8syaml.Unmarshal([]byte(stdout), &list); err != nil {
return nil, fmt.Errorf("helm list returned invalid json: %v", err)
Expand Down Expand Up @@ -278,12 +288,15 @@ func (h *Helm3Client) Render(releaseName string, chart string, valuesPaths []str
args = append(args, setValue)
}

h.Logger.Debugf("Render helm templates for chart '%s' in namespace '%s' ...", chart, namespace)
h.Logger.Debug("Render helm templates for chart ...",
slog.String("chart", chart),
slog.String("namespace", namespace))
stdout, stderr, err := h.cmd(args...)
if err != nil {
return "", fmt.Errorf("helm upgrade failed: %s:\n%s %s", err, stdout, stderr)
}
h.Logger.Infof("Render helm templates for chart '%s' was successful", chart)
h.Logger.Info("Render helm templates for chart was successful",
slog.String("chart", chart))

return stdout, nil
}
Loading

0 comments on commit 35c8edb

Please sign in to comment.