Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
Pavel Okhlopkov committed Nov 1, 2024
1 parent 6f640b4 commit 46fdab9
Show file tree
Hide file tree
Showing 65 changed files with 562 additions and 386 deletions.
28 changes: 28 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ linters:
- nolintlint
- prealloc
- revive
- sloglint
- staticcheck
- stylecheck
- unconvert
Expand All @@ -39,6 +40,33 @@ linters-settings:
local-prefixes: github.com/flant/
nolintlint:
allow-unused: true
sloglint:
# Enforce not mixing key-value pairs and attributes.
no-mixed-args: true
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only).
kv-only: false
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
attr-only: false
# Enforce not using global loggers.
no-global: ""
# Enforce using methods that accept a context.
context: ""
# Enforce using static values for log messages.
static-msg: false
# Enforce using constants instead of raw keys.
no-raw-keys: false
# Enforce a single key naming convention.
key-naming-case: ""
# Enforce not using specific keys.
forbidden-keys:
- level
- msg
- logger
- source
- stacktrace
- time
# Enforce putting arguments on separate lines.
args-on-sep-lines: false
depguard:
rules:
Main:
Expand Down
6 changes: 5 additions & 1 deletion cmd/shell-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"gopkg.in/alecthomas/kingpin.v2"

"github.com/deckhouse/deckhouse/go_lib/log"
"github.com/flant/kube-client/klogtologrus"
"github.com/flant/shell-operator/pkg/app"
"github.com/flant/shell-operator/pkg/debug"
Expand Down Expand Up @@ -35,6 +36,9 @@ func main() {
return nil
})

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

// start main loop
startCmd := kpApp.Command("start", "Start shell-operator.").
Default().
Expand All @@ -45,7 +49,7 @@ func main() {
rand.Seed(time.Now().UnixNano())

// Init logging and initialize a ShellOperator instance.
operator, err := shell_operator.Init()
operator, err := shell_operator.Init(logger.Named("shell-operator"))
if err != nil {
os.Exit(1)
}
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/flant/shell-operator

go 1.22
go 1.22.8

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

require (
github.com/DataDog/gostackparse v0.7.0 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4=
github.com/DataDog/gostackparse v0.7.0/go.mod h1:lTfqcJKqS9KnXQGnyQMCugq3u1FP6UZMfWR0aitKFMM=
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
Expand All @@ -22,6 +24,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031173751-c2c6e864aa6b h1:x0T6FJSCibbVVj2N7ItjENm7h+ISiEZdypjFKVx9G88=
github.com/deckhouse/deckhouse/go_lib/log v0.0.0-20241031173751-c2c6e864aa6b/go.mod h1:h+LOQH/71Rhjj+ws0MjBHduAsgAkVJZsRuhQNjFPLa8=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
Expand Down
85 changes: 36 additions & 49 deletions pkg/app/log.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package app

import (
"bytes"
"fmt"
"strings"
"time"

log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"

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

Expand Down Expand Up @@ -45,33 +44,34 @@ func DefineLoggingFlags(cmd *kingpin.CmdClause) {
}

// SetupLogging sets logger formatter and level.
func SetupLogging(runtimeConfig *config.Config) {
jsonFormatter := log.JSONFormatter{DisableTimestamp: LogNoTime}
textFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, DisableColors: true}
colorFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, ForceColors: true, FullTimestamp: true}
switch strings.ToLower(LogType) {
case "json":
log.SetFormatter(&jsonFormatter)
case "text":
log.SetFormatter(&textFormatter)
case "color":
log.SetFormatter(&colorFormatter)
default:
log.SetFormatter(&jsonFormatter)
}
if LogProxyHookJSON {
formatter := log.StandardLogger().Formatter
log.SetFormatter(&ProxyJsonWrapperFormatter{WrappedFormatter: formatter})
}
func SetupLogging(runtimeConfig *config.Config, logger *log.Logger) {
// TODO: if we need formatters - add to logger
// jsonFormatter := log.JSONFormatter{DisableTimestamp: LogNoTime}
// textFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, DisableColors: true}
// colorFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, ForceColors: true, FullTimestamp: true}
// switch strings.ToLower(LogType) {
// case "json":
// log.SetFormatter(&jsonFormatter)
// case "text":
// log.SetFormatter(&textFormatter)
// case "color":
// log.SetFormatter(&colorFormatter)
// default:
// log.SetFormatter(&jsonFormatter)
// }
// if LogProxyHookJSON {
// formatter := log.StandardLogger().Formatter
// log.SetFormatter(&ProxyJsonWrapperFormatter{WrappedFormatter: formatter})
// }

setLogLevel(LogLevel)
log.SetDefaultLevel(log.LogLevelFromStr(LogLevel))

runtimeConfig.Register("log.level",
fmt.Sprintf("Global log level. Default duration for debug level is %s", ForcedDurationForDebugLevel),
strings.ToLower(LogLevel),
func(oldValue string, newValue string) error {
log.Infof("Set log level to '%s'", newValue)
setLogLevel(newValue)
log.SetDefaultLevel(log.LogLevelFromStr(newValue))
return nil
}, func(oldValue string, newValue string) time.Duration {
if strings.ToLower(newValue) == "debug" {
Expand All @@ -81,32 +81,19 @@ func SetupLogging(runtimeConfig *config.Config) {
})
}

func setLogLevel(logLevel string) {
switch strings.ToLower(logLevel) {
case "debug":
log.SetLevel(log.DebugLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "info":
log.SetLevel(log.InfoLevel)
default:
log.SetLevel(log.InfoLevel)
}
}

type ProxyJsonWrapperFormatter struct {
WrappedFormatter log.Formatter
}
// type ProxyJsonWrapperFormatter struct {
// WrappedFormatter log.Formatter
// }

func (f *ProxyJsonWrapperFormatter) Format(entry *log.Entry) ([]byte, error) {
// if proxying the json message is intended, just return the bytes
// TODO: Find a more elegant way to carry this info
if entry.Data[ProxyJsonLogKey] == true {
b := bytes.NewBufferString(entry.Message)
b.WriteString("\n")
return b.Bytes(), nil
}
// func (f *ProxyJsonWrapperFormatter) Format(entry *log.Entry) ([]byte, error) {
// // if proxying the json message is intended, just return the bytes
// // TODO: Find a more elegant way to carry this info
// if entry.Data[ProxyJsonLogKey] == true {
// b := bytes.NewBufferString(entry.Message)
// b.WriteString("\n")
// return b.Bytes(), nil
// }

// otherwise, use the wrapped formatter
return f.WrappedFormatter.Format(entry)
}
// // otherwise, use the wrapped formatter
// return f.WrappedFormatter.Format(entry)
// }
15 changes: 8 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package config

import (
"fmt"
"log/slog"
"sort"
"strings"
"sync"
"time"

log "github.com/sirupsen/logrus"
"github.com/deckhouse/deckhouse/go_lib/log"
)

/**
Expand Down Expand Up @@ -42,16 +43,16 @@ type Config struct {
temporalValues map[string]*TemporalValue
expireTicker *time.Ticker

logEntry *log.Entry
logger *log.Logger
}

func NewConfig() *Config {
func NewConfig(logger *log.Logger) *Config {
return &Config{
params: make(map[string]*Parameter),
values: make(map[string]string),
temporalValues: make(map[string]*TemporalValue),
errors: make(map[string]error),
logEntry: log.WithField("component", "runtimeConfig"),
logger: logger.With(slog.String("component", "runtimeConfig")),
}
}

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

for _, expire := range expires {
name, oldValue, newValue := expire[0], expire[1], expire[2]
c.logEntry.Debugf("Parameter '%s' expired", name)
c.logger.Debug("Parameter is expired", slog.String("parameter", name))
c.callOnChange(name, oldValue, newValue)
}
}
Expand All @@ -266,8 +267,8 @@ func (c *Config) callOnChange(name string, oldValue string, newValue string) {
}
err := c.params[name].onChange(oldValue, newValue)
if err != nil {
c.logEntry.Errorf("OnChange handler failed for '%s' during value change from '%s' to '%s': %v",
name, oldValue, newValue, err)
c.logger.Error("OnChange handler failed for parameter during value change values",
slog.String("parameter", name), slog.String("old_value", oldValue), slog.String("new_value", newValue), slog.String("error", err.Error()))
}
c.m.Lock()
delete(c.errors, name)
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"testing"
"time"

"github.com/deckhouse/deckhouse/go_lib/log"
"github.com/stretchr/testify/assert"
)

func TestConfig_Register(t *testing.T) {
c := NewConfig()
c := NewConfig(log.NewNop())

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

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

func TestConfig_OnChange(t *testing.T) {
c := NewConfig()
c := NewConfig(log.NewNop())

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

func TestConfig_Errors(t *testing.T) {
var err error
c := NewConfig()
c := NewConfig(log.NewNop())

c.Register("log.level", "", "info", func(oldValue string, n string) error {
if n == "debug" {
Expand Down
Loading

0 comments on commit 46fdab9

Please sign in to comment.