This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
88 lines (77 loc) · 2.51 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/oxyno-zeta/kubernetes-tagger/pkg/kubernetes-tagger/config"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/leaderelection/resourcelock"
componentbaseconfig "k8s.io/component-base/config"
)
// Kubernetes configuration home path.
const kubeConfig = ".kube/config"
func configureViper(onChange func(e fsnotify.Event)) error {
kubeConfigPath := filepath.Join(os.Getenv("HOME"), kubeConfig)
// Flags
flag.String("namespace", "kube-system", "Namespace where "+projectName+" is deployed")
flag.String("kubeconfig", kubeConfigPath, "Kubernetes configuration file path")
flag.String("address", ":8085", "The address to expose health and prometheus metrics")
flag.String("loglevel", "info", "Log level")
flag.String("logformat", "json", "Log format")
flag.String("provider", "aws", "Kubernetes Provider")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
// Check error
if err != nil {
return err
}
// Add config file name
viper.SetConfigName(config.RecommendedConfigFileName)
// Add config possible path
viper.AddConfigPath("/etc/" + projectName + "/")
viper.AddConfigPath("$HOME/." + projectName)
viper.AddConfigPath(".")
// Watch configuration file change
viper.WatchConfig()
viper.OnConfigChange(onChange)
// Default
return nil
}
// Default values for leader election.
const (
defaultLeaseDuration = 15 * time.Second
defaultRenewDeadline = 10 * time.Second
defaultRetryPeriod = 2 * time.Second
)
func defaultLeaderElectionConfiguration() componentbaseconfig.LeaderElectionConfiguration {
return componentbaseconfig.LeaderElectionConfiguration{
LeaderElect: true,
LeaseDuration: metav1.Duration{Duration: defaultLeaseDuration},
RenewDeadline: metav1.Duration{Duration: defaultRenewDeadline},
RetryPeriod: metav1.Duration{Duration: defaultRetryPeriod},
ResourceLock: resourcelock.EndpointsResourceLock,
}
}
func configureLogger() {
// Log level
lvl, err := logrus.ParseLevel(context.Configuration.LogLevel)
if err != nil {
logrus.Fatal(err)
}
logrus.SetLevel(lvl)
// Log Formatter
switch context.Configuration.LogFormat {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
default:
logrus.Fatalf("Log format not supported: %s", context.Configuration.LogFormat)
}
}