Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit 264dcd2

Browse files
committed
feat: Add log level and formatter selector
1 parent 46447e7 commit 264dcd2

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"AWS_ACCESS_KEY_ID": "${env:AWS_ACCESS_KEY_ID}",
1616
"AWS_SECRET_ACCESS_KEY": "${env:AWS_SECRET_ACCESS_KEY}"
1717
},
18-
"args": []
18+
"args": ["--loglevel=debug"]
1919
}
2020
]
2121
}

cmd/kubernetes-tagger/config.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"flag"
55
"os"
66
"path/filepath"
7+
"time"
78

9+
"github.com/Sirupsen/logrus"
810
"github.com/fsnotify/fsnotify"
911
"github.com/oxyno-zeta/kubernetes-tagger/pkg/kubernetes-tagger/config"
1012
"github.com/spf13/pflag"
1113
"github.com/spf13/viper"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/client-go/tools/leaderelection/resourcelock"
16+
componentbaseconfig "k8s.io/component-base/config"
1217
)
1318

1419
// Kubernetes configuration home path
@@ -20,6 +25,8 @@ func configureViper(onChange func(e fsnotify.Event)) {
2025
flag.String("namespace", "kube-system", "Namespace where "+projectName+" is deployed")
2126
flag.String("kubeconfig", kubeConfigPath, "Kubernetes configuration file path")
2227
flag.String("address", ":8085", "The address to expose health and prometheus metrics")
28+
flag.String("loglevel", "info", "Log level")
29+
flag.String("logformat", "json", "Log format")
2330
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
2431
pflag.Parse()
2532
viper.BindPFlags(pflag.CommandLine)
@@ -33,3 +40,39 @@ func configureViper(onChange func(e fsnotify.Event)) {
3340
viper.WatchConfig()
3441
viper.OnConfigChange(onChange)
3542
}
43+
44+
// Default values for leader election
45+
const (
46+
defaultLeaseDuration = 15 * time.Second
47+
defaultRenewDeadline = 10 * time.Second
48+
defaultRetryPeriod = 2 * time.Second
49+
)
50+
51+
func defaultLeaderElectionConfiguration() componentbaseconfig.LeaderElectionConfiguration {
52+
return componentbaseconfig.LeaderElectionConfiguration{
53+
LeaderElect: true,
54+
LeaseDuration: metav1.Duration{Duration: defaultLeaseDuration},
55+
RenewDeadline: metav1.Duration{Duration: defaultRenewDeadline},
56+
RetryPeriod: metav1.Duration{Duration: defaultRetryPeriod},
57+
ResourceLock: resourcelock.EndpointsResourceLock,
58+
}
59+
}
60+
61+
func configureLogger() {
62+
// Log level
63+
lvl, err := logrus.ParseLevel(context.Configuration.LogLevel)
64+
if err != nil {
65+
logrus.Fatal(err)
66+
}
67+
logrus.SetLevel(lvl)
68+
69+
// Log Formatter
70+
switch context.Configuration.LogFormat {
71+
case "json":
72+
logrus.SetFormatter(&logrus.JSONFormatter{})
73+
case "text":
74+
logrus.SetFormatter(&logrus.TextFormatter{})
75+
default:
76+
logrus.Fatalf("Log format not supported: %s", context.Configuration.LogFormat)
77+
}
78+
}

cmd/kubernetes-tagger/kubernetes-tagger.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
ctx "context"
55
"os"
6-
"time"
76

87
"github.com/oxyno-zeta/kubernetes-tagger/pkg/kubernetes-tagger/utils"
98

@@ -17,14 +16,12 @@ import (
1716
"github.com/Sirupsen/logrus"
1817
"github.com/spf13/viper"
1918
v1 "k8s.io/api/core/v1"
20-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2119
"k8s.io/client-go/kubernetes"
2220
"k8s.io/client-go/kubernetes/scheme"
2321
"k8s.io/client-go/rest"
2422
"k8s.io/client-go/tools/clientcmd"
2523
"k8s.io/client-go/tools/leaderelection"
2624
"k8s.io/client-go/tools/leaderelection/resourcelock"
27-
componentbaseconfig "k8s.io/component-base/config"
2825

2926
kube_record "k8s.io/client-go/tools/record"
3027
)
@@ -34,23 +31,6 @@ const projectName = "kubernetes-tagger"
3431

3532
var context = &business.Context{}
3633

37-
// Default values for leader election
38-
const (
39-
defaultLeaseDuration = 15 * time.Second
40-
defaultRenewDeadline = 10 * time.Second
41-
defaultRetryPeriod = 2 * time.Second
42-
)
43-
44-
func defaultLeaderElectionConfiguration() componentbaseconfig.LeaderElectionConfiguration {
45-
return componentbaseconfig.LeaderElectionConfiguration{
46-
LeaderElect: true,
47-
LeaseDuration: metav1.Duration{Duration: defaultLeaseDuration},
48-
RenewDeadline: metav1.Duration{Duration: defaultRenewDeadline},
49-
RetryPeriod: metav1.Duration{Duration: defaultRetryPeriod},
50-
ResourceLock: resourcelock.EndpointsResourceLock,
51-
}
52-
}
53-
5434
func main() {
5535
// Get Hostname to have unique id for container
5636
id, err := os.Hostname()
@@ -68,6 +48,9 @@ func main() {
6848

6949
readConfiguration()
7050

51+
// Configure logger
52+
configureLogger()
53+
7154
versionObj := version.GetVersion()
7255
logrus.WithFields(logrus.Fields{
7356
"build-date": versionObj.BuildDate,

pkg/kubernetes-tagger/config/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type Configuration struct {
88
Namespace string `mapstructure:"namespace"`
99
Kubeconfig string `mapstructure:"kubeconfig"`
1010
Address string `mapstructure:"address"`
11+
LogLevel string `mapstructure:"loglevel"`
12+
LogFormat string `mapstructure:"logformat"`
1113
AWS *AWSConfig `mapstructure:"aws"`
1214
Rules []*RuleConfig `mapstructure:"rules"`
1315
}

0 commit comments

Comments
 (0)