Skip to content

Commit

Permalink
Adding the ability to configure LOG_LEVEL dynamically from environmen…
Browse files Browse the repository at this point in the history
…t variables. (#25)

This PR adds a feature that allows changing the application log level
dynamically.
  • Loading branch information
mvleandro authored Oct 17, 2023
1 parent c9302b0 commit 35d1677
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
15 changes: 13 additions & 2 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"keess/kube_syncer"

"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
Expand All @@ -12,7 +13,7 @@ import (
func New() *cli.App {
app := cli.NewApp()
app.Name = "Keess"
app.Version = "v0.1.13"
app.Version = "v0.2.13"
app.Usage = "Keep stuff synchronized."
app.Description = "Keep secrets and configmaps synchronized."
app.Suggest = true
Expand Down Expand Up @@ -62,10 +63,16 @@ func run(c *cli.Context) error {
viper.SetEnvPrefix("KEESS")
viper.AutomaticEnv()

viper.WatchConfig()

kubeConfigPath := c.String("config")
sourceContext := c.String("sourceContext")
destinationContexts := c.StringSlice("destinationContexts")
developmentMode := c.Bool("developmentMode")
initialLogLevel := viper.GetString("LOG_LEVEL")
if initialLogLevel == "" {
initialLogLevel = "INFO"
}

if kubeConfigPath == "" {
kubeConfigPath = viper.GetString("CONFIG_PATH")
Expand All @@ -86,7 +93,11 @@ func run(c *cli.Context) error {
fmt.Printf("Starting %s %s\n", c.App.Name, c.App.Version)

var syncer kube_syncer.Syncer
err := syncer.Start(kubeConfigPath, developmentMode, sourceContext, destinationContexts)
err := syncer.Start(kubeConfigPath, developmentMode, initialLogLevel, sourceContext, destinationContexts)

viper.OnConfigChange(func(e fsnotify.Event) {
syncer.SetLogLevel(viper.GetString("LOG_LEVEL"))
})

if err == nil {
return syncer.Run()
Expand Down
4 changes: 2 additions & 2 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.13
version: 0.2.13

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "v0.1.13"
appVersion: "v0.2.13"
4 changes: 4 additions & 0 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ spec:
- name: KEESS_DEVELOPMENT_MODE
value: "{{ .Values.developmentMode }}"
{{- end }}
{{- if .Values.logLevel }}
- name: LOG_LEVEL
value: "{{ .Values.logLevel }}"
{{- end }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
- name: config
Expand Down
7 changes: 7 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ image:
# Overrides the image tag whose default is the chart appVersion.
tag: "main-8eec28338b8f92ef13248ef2b01d453de0489d29-17"


# Default Keess config
# clusterName: ""
# remoteClusters: ""
# developmentMode: "true"
# logLevel: "INFO"

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
Expand Down
2 changes: 1 addition & 1 deletion kube_syncer/configmap_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (w ConfigMapWatcher) Watch() <-chan abstractions.ISynchronizable {
})

if err != nil {
panic(err)
w.logger.Error(err)
}

w.logger.Info("Watching configMaps events.")
Expand Down
2 changes: 1 addition & 1 deletion kube_syncer/namespace_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (w NamespaceWatcher) Watch() <-chan abstractions.ISynchronizable {
})

if err != nil {
panic(err)
w.logger.Error(err)
}

w.logger.Info("Watching namespaces events.")
Expand Down
2 changes: 1 addition & 1 deletion kube_syncer/secret_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (w SecretWatcher) Watch() <-chan abstractions.ISynchronizable {
})

if err != nil {
panic(err)
w.logger.Error(err)
}

w.logger.Info("Watching secrets events.")
Expand Down
43 changes: 32 additions & 11 deletions kube_syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kube_syncer
import (
"context"
"flag"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -32,6 +33,8 @@ type Syncer struct {

// The logger object.
logger *zap.SugaredLogger

atom zap.AtomicLevel
}

func init() {
Expand All @@ -41,24 +44,42 @@ func init() {
abstractions.EntitiesToLabeledNamespaces["Secrets"] = make(map[string]runtime.Object)
}

func (s *Syncer) SetLogLevel(logLevel string) {
level, err := zapcore.ParseLevel(logLevel)

if err == nil {
s.atom.SetLevel(level)
} else {
s.logger.Error(err)
}
}

// Load the kubeClient based in the given configuration.
func (s *Syncer) Start(kubeConfigPath string, developmentMode bool, sourceContext string, destinationContexts []string) error {
var loggerConfig zap.Config
func (s *Syncer) Start(kubeConfigPath string, developmentMode bool, initialLogLevel string, sourceContext string, destinationContexts []string) error {
s.atom = zap.NewAtomicLevel()

// To keep the example deterministic, disable timestamps in the output.
var encoderCfg zapcore.EncoderConfig

if developmentMode {
loggerConfig = zap.NewDevelopmentConfig()
encoderCfg = zap.NewDevelopmentEncoderConfig()
} else {
loggerConfig = zap.NewProductionConfig()
encoderCfg = zap.NewProductionEncoderConfig()
}

loggerConfig.EncoderConfig.TimeKey = "timestamp"
loggerConfig.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
encoderCfg.TimeKey = "timestamp"
encoderCfg.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)

zapLogger, err := loggerConfig.Build()
if err != nil {
s.logger.Error(err)
}
abstractions.Logger = zapLogger.Sugar()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
s.atom,
))
defer logger.Sync()

s.SetLogLevel(initialLogLevel)

abstractions.Logger = logger.Sugar()
s.logger = abstractions.Logger

s.sourceContext = sourceContext
Expand Down

0 comments on commit 35d1677

Please sign in to comment.