Skip to content

Commit 46fdab9

Browse files
author
Pavel Okhlopkov
committed
init
Signed-off-by: Pavel Okhlopkov <[email protected]>
1 parent 6f640b4 commit 46fdab9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+562
-386
lines changed

.golangci.yaml

+28
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ linters:
2424
- nolintlint
2525
- prealloc
2626
- revive
27+
- sloglint
2728
- staticcheck
2829
- stylecheck
2930
- unconvert
@@ -39,6 +40,33 @@ linters-settings:
3940
local-prefixes: github.com/flant/
4041
nolintlint:
4142
allow-unused: true
43+
sloglint:
44+
# Enforce not mixing key-value pairs and attributes.
45+
no-mixed-args: true
46+
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only).
47+
kv-only: false
48+
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
49+
attr-only: false
50+
# Enforce not using global loggers.
51+
no-global: ""
52+
# Enforce using methods that accept a context.
53+
context: ""
54+
# Enforce using static values for log messages.
55+
static-msg: false
56+
# Enforce using constants instead of raw keys.
57+
no-raw-keys: false
58+
# Enforce a single key naming convention.
59+
key-naming-case: ""
60+
# Enforce not using specific keys.
61+
forbidden-keys:
62+
- level
63+
- msg
64+
- logger
65+
- source
66+
- stacktrace
67+
- time
68+
# Enforce putting arguments on separate lines.
69+
args-on-sep-lines: false
4270
depguard:
4371
rules:
4472
Main:

cmd/shell-operator/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"gopkg.in/alecthomas/kingpin.v2"
1010

11+
"github.com/deckhouse/deckhouse/go_lib/log"
1112
"github.com/flant/kube-client/klogtologrus"
1213
"github.com/flant/shell-operator/pkg/app"
1314
"github.com/flant/shell-operator/pkg/debug"
@@ -35,6 +36,9 @@ func main() {
3536
return nil
3637
})
3738

39+
logger := log.NewLogger(log.Options{})
40+
log.SetDefault(logger)
41+
3842
// start main loop
3943
startCmd := kpApp.Command("start", "Start shell-operator.").
4044
Default().
@@ -45,7 +49,7 @@ func main() {
4549
rand.Seed(time.Now().UnixNano())
4650

4751
// Init logging and initialize a ShellOperator instance.
48-
operator, err := shell_operator.Init()
52+
operator, err := shell_operator.Init(logger.Named("shell-operator"))
4953
if err != nil {
5054
os.Exit(1)
5155
}

go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module github.com/flant/shell-operator
22

3-
go 1.22
3+
go 1.22.8
44

55
require (
6+
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031173751-c2c6e864aa6b
67
github.com/flant/kube-client v1.2.0
78
github.com/flant/libjq-go v1.6.3-0.20201126171326-c46a40ff22ee // branch: master
89
github.com/go-chi/chi/v5 v5.1.0
@@ -17,7 +18,7 @@ require (
1718
github.com/onsi/gomega v1.34.2
1819
github.com/pkg/errors v0.9.1
1920
github.com/prometheus/client_golang v1.20.5
20-
github.com/sirupsen/logrus v1.9.3
21+
github.com/sirupsen/logrus v1.9.3 // indirect
2122
github.com/stretchr/testify v1.9.0
2223
golang.org/x/time v0.7.0
2324
gopkg.in/alecthomas/kingpin.v2 v2.2.6
@@ -36,6 +37,7 @@ replace github.com/go-openapi/validate => github.com/flant/go-openapi-validate v
3637
require github.com/gojuno/minimock/v3 v3.4.0
3738

3839
require (
40+
github.com/DataDog/gostackparse v0.7.0 // indirect
3941
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
4042
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
4143
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4=
3+
github.com/DataDog/gostackparse v0.7.0/go.mod h1:lTfqcJKqS9KnXQGnyQMCugq3u1FP6UZMfWR0aitKFMM=
24
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
35
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
46
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
@@ -22,6 +24,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
2224
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2325
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2426
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
27+
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031173751-c2c6e864aa6b h1:x0T6FJSCibbVVj2N7ItjENm7h+ISiEZdypjFKVx9G88=
28+
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031173751-c2c6e864aa6b/go.mod h1:h+LOQH/71Rhjj+ws0MjBHduAsgAkVJZsRuhQNjFPLa8=
2529
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
2630
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
2731
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=

pkg/app/log.go

+36-49
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package app
22

33
import (
4-
"bytes"
54
"fmt"
65
"strings"
76
"time"
87

9-
log "github.com/sirupsen/logrus"
108
"gopkg.in/alecthomas/kingpin.v2"
119

10+
"github.com/deckhouse/deckhouse/go_lib/log"
1211
"github.com/flant/shell-operator/pkg/config"
1312
)
1413

@@ -45,33 +44,34 @@ func DefineLoggingFlags(cmd *kingpin.CmdClause) {
4544
}
4645

4746
// SetupLogging sets logger formatter and level.
48-
func SetupLogging(runtimeConfig *config.Config) {
49-
jsonFormatter := log.JSONFormatter{DisableTimestamp: LogNoTime}
50-
textFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, DisableColors: true}
51-
colorFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, ForceColors: true, FullTimestamp: true}
52-
switch strings.ToLower(LogType) {
53-
case "json":
54-
log.SetFormatter(&jsonFormatter)
55-
case "text":
56-
log.SetFormatter(&textFormatter)
57-
case "color":
58-
log.SetFormatter(&colorFormatter)
59-
default:
60-
log.SetFormatter(&jsonFormatter)
61-
}
62-
if LogProxyHookJSON {
63-
formatter := log.StandardLogger().Formatter
64-
log.SetFormatter(&ProxyJsonWrapperFormatter{WrappedFormatter: formatter})
65-
}
47+
func SetupLogging(runtimeConfig *config.Config, logger *log.Logger) {
48+
// TODO: if we need formatters - add to logger
49+
// jsonFormatter := log.JSONFormatter{DisableTimestamp: LogNoTime}
50+
// textFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, DisableColors: true}
51+
// colorFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, ForceColors: true, FullTimestamp: true}
52+
// switch strings.ToLower(LogType) {
53+
// case "json":
54+
// log.SetFormatter(&jsonFormatter)
55+
// case "text":
56+
// log.SetFormatter(&textFormatter)
57+
// case "color":
58+
// log.SetFormatter(&colorFormatter)
59+
// default:
60+
// log.SetFormatter(&jsonFormatter)
61+
// }
62+
// if LogProxyHookJSON {
63+
// formatter := log.StandardLogger().Formatter
64+
// log.SetFormatter(&ProxyJsonWrapperFormatter{WrappedFormatter: formatter})
65+
// }
6666

67-
setLogLevel(LogLevel)
67+
log.SetDefaultLevel(log.LogLevelFromStr(LogLevel))
6868

6969
runtimeConfig.Register("log.level",
7070
fmt.Sprintf("Global log level. Default duration for debug level is %s", ForcedDurationForDebugLevel),
7171
strings.ToLower(LogLevel),
7272
func(oldValue string, newValue string) error {
7373
log.Infof("Set log level to '%s'", newValue)
74-
setLogLevel(newValue)
74+
log.SetDefaultLevel(log.LogLevelFromStr(newValue))
7575
return nil
7676
}, func(oldValue string, newValue string) time.Duration {
7777
if strings.ToLower(newValue) == "debug" {
@@ -81,32 +81,19 @@ func SetupLogging(runtimeConfig *config.Config) {
8181
})
8282
}
8383

84-
func setLogLevel(logLevel string) {
85-
switch strings.ToLower(logLevel) {
86-
case "debug":
87-
log.SetLevel(log.DebugLevel)
88-
case "error":
89-
log.SetLevel(log.ErrorLevel)
90-
case "info":
91-
log.SetLevel(log.InfoLevel)
92-
default:
93-
log.SetLevel(log.InfoLevel)
94-
}
95-
}
96-
97-
type ProxyJsonWrapperFormatter struct {
98-
WrappedFormatter log.Formatter
99-
}
84+
// type ProxyJsonWrapperFormatter struct {
85+
// WrappedFormatter log.Formatter
86+
// }
10087

101-
func (f *ProxyJsonWrapperFormatter) Format(entry *log.Entry) ([]byte, error) {
102-
// if proxying the json message is intended, just return the bytes
103-
// TODO: Find a more elegant way to carry this info
104-
if entry.Data[ProxyJsonLogKey] == true {
105-
b := bytes.NewBufferString(entry.Message)
106-
b.WriteString("\n")
107-
return b.Bytes(), nil
108-
}
88+
// func (f *ProxyJsonWrapperFormatter) Format(entry *log.Entry) ([]byte, error) {
89+
// // if proxying the json message is intended, just return the bytes
90+
// // TODO: Find a more elegant way to carry this info
91+
// if entry.Data[ProxyJsonLogKey] == true {
92+
// b := bytes.NewBufferString(entry.Message)
93+
// b.WriteString("\n")
94+
// return b.Bytes(), nil
95+
// }
10996

110-
// otherwise, use the wrapped formatter
111-
return f.WrappedFormatter.Format(entry)
112-
}
97+
// // otherwise, use the wrapped formatter
98+
// return f.WrappedFormatter.Format(entry)
99+
// }

pkg/config/config.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package config
22

33
import (
44
"fmt"
5+
"log/slog"
56
"sort"
67
"strings"
78
"sync"
89
"time"
910

10-
log "github.com/sirupsen/logrus"
11+
"github.com/deckhouse/deckhouse/go_lib/log"
1112
)
1213

1314
/**
@@ -42,16 +43,16 @@ type Config struct {
4243
temporalValues map[string]*TemporalValue
4344
expireTicker *time.Ticker
4445

45-
logEntry *log.Entry
46+
logger *log.Logger
4647
}
4748

48-
func NewConfig() *Config {
49+
func NewConfig(logger *log.Logger) *Config {
4950
return &Config{
5051
params: make(map[string]*Parameter),
5152
values: make(map[string]string),
5253
temporalValues: make(map[string]*TemporalValue),
5354
errors: make(map[string]error),
54-
logEntry: log.WithField("component", "runtimeConfig"),
55+
logger: logger.With(slog.String("component", "runtimeConfig")),
5556
}
5657
}
5758

@@ -254,7 +255,7 @@ func (c *Config) expireOverrides() {
254255

255256
for _, expire := range expires {
256257
name, oldValue, newValue := expire[0], expire[1], expire[2]
257-
c.logEntry.Debugf("Parameter '%s' expired", name)
258+
c.logger.Debug("Parameter is expired", slog.String("parameter", name))
258259
c.callOnChange(name, oldValue, newValue)
259260
}
260261
}
@@ -266,8 +267,8 @@ func (c *Config) callOnChange(name string, oldValue string, newValue string) {
266267
}
267268
err := c.params[name].onChange(oldValue, newValue)
268269
if err != nil {
269-
c.logEntry.Errorf("OnChange handler failed for '%s' during value change from '%s' to '%s': %v",
270-
name, oldValue, newValue, err)
270+
c.logger.Error("OnChange handler failed for parameter during value change values",
271+
slog.String("parameter", name), slog.String("old_value", oldValue), slog.String("new_value", newValue), slog.String("error", err.Error()))
271272
}
272273
c.m.Lock()
273274
delete(c.errors, name)

pkg/config/config_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/deckhouse/deckhouse/go_lib/log"
89
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestConfig_Register(t *testing.T) {
12-
c := NewConfig()
13+
c := NewConfig(log.NewNop())
1314

1415
c.Register("log.level", "", "info", nil, nil)
1516

@@ -32,7 +33,7 @@ func TestConfig_Register(t *testing.T) {
3233
}
3334

3435
func TestConfig_OnChange(t *testing.T) {
35-
c := NewConfig()
36+
c := NewConfig(log.NewNop())
3637

3738
newValue := ""
3839
c.Register("log.level", "", "info", func(oldValue string, n string) error {
@@ -66,7 +67,7 @@ func TestConfig_OnChange(t *testing.T) {
6667

6768
func TestConfig_Errors(t *testing.T) {
6869
var err error
69-
c := NewConfig()
70+
c := NewConfig(log.NewNop())
7071

7172
c.Register("log.level", "", "info", func(oldValue string, n string) error {
7273
if n == "debug" {

0 commit comments

Comments
 (0)