-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.go
142 lines (114 loc) · 3.08 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
)
func main() {
initLogrus()
enableCertReload := flag.Bool("cert-reload", false, "enable certificate reload")
v := flag.Bool("version", false, "show version")
flag.Parse()
if *v {
fmt.Printf("windows-gmsa-webhook v%s\n", getVersion())
os.Exit(0)
}
kubeClient, err := createKubeClient()
if err != nil {
panic(err)
}
randomHostname := env_bool("RANDOM_HOSTNAME")
options := []WebhookOption{WithCertReload(*enableCertReload)}
options = append(options, WithRandomHostname(randomHostname))
webhook := newWebhookWithOptions(kubeClient, options...)
tlsConfig := &tlsConfig{
crtPath: env("TLS_CRT"),
keyPath: env("TLS_KEY"),
}
port := env_int("HTTPS_PORT", 443)
if err = webhook.start(port, tlsConfig, nil); err != nil {
panic(err)
}
}
var logLevels = map[string]logrus.Level{
"panic": logrus.PanicLevel,
"fatal": logrus.FatalLevel,
"error": logrus.ErrorLevel,
"warn": logrus.WarnLevel,
"info": logrus.InfoLevel,
"debug": logrus.DebugLevel,
"trace": logrus.TraceLevel,
}
func initLogrus() {
logrus.SetOutput(os.Stdout)
logLevel := logrus.DebugLevel
invalid := false
rawLogLevel, present := os.LookupEnv("LOG_LEVEL")
if present {
if level, valid := logLevels[strings.ToLower(rawLogLevel)]; valid {
logLevel = level
} else {
invalid = true
}
}
logrus.SetLevel(logLevel)
if invalid {
keys := make([]string, len(logLevels))
i := 0
for key := range logLevels {
keys[i] = key
i++
}
logrus.Warningf("Unknown log level %s, valid log levels are: %v", rawLogLevel, strings.Join(keys, ", "))
}
}
func createKubeClient() (*kubeClient, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
config.QPS = env_float("QPS", rest.DefaultQPS)
config.Burst = env_int("BURST", rest.DefaultBurst)
logrus.Infof("QPS: %f, Burst: %d", config.QPS, config.Burst)
return newKubeClient(config)
}
func env_float(key string, defaultFloat float32) float32 {
if v, found := os.LookupEnv(key); found {
if i, err := strconv.ParseFloat(v, 32); err == nil {
return float32(i)
}
logrus.Warningf("unable to parse environment variable %s with value %s; using default value %f", key, v, defaultFloat)
}
return defaultFloat
}
func env_bool(key string) bool {
if v, found := os.LookupEnv(key); found {
// Convert string to bool
if boolValue, err := strconv.ParseBool(v); err == nil {
return boolValue
}
// throw error if unable to parse
panic(fmt.Errorf("unable to parse environment variable %s with value %s to bool", key, v))
}
// return bool default value: false
return false
}
func env_int(key string, defaultInt int) int {
if v, found := os.LookupEnv(key); found {
if i, err := strconv.Atoi(v); err == nil {
return i
}
logrus.Warningf("unable to parse environment variable %s with value %s; using default value %d", key, v, defaultInt)
}
return defaultInt
}
func env(key string) string {
if value, found := os.LookupEnv(key); found {
return value
}
panic(fmt.Errorf("%s env var not found", key))
}