forked from AymenSegni/kube-admission-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 2.05 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
package main
import (
"fmt"
"os"
"strings"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/gommon/log"
"github.com/spf13/viper"
"github.com/aymensegni/kube-admission-controller/admission"
)
func main() {
// Load json config file
viper.SetConfigName("local")
viper.AddConfigPath("./config/")
viper.SetConfigType("json")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
// ENV overwrites
viper.SetEnvPrefix("AMBIDEXTER")
viper.AutomaticEnv()
viper.SetDefault("debug", true)
debug := viper.GetBool("debug")
viper.SetDefault("loglevel", "")
logLevel := viper.GetString("loglevel")
viper.SetDefault("denyLatestTag", true)
denyLatestTag := viper.GetBool("denyLatestTag")
registryWhitelist := viper.GetStringSlice("registryWhiteList")
viper.SetDefault("port", 443)
port := viper.GetInt("port")
viper.SetDefault("servercert", "")
serverCert := viper.GetString("servercert")
viper.SetDefault("serverkey", "")
serverKey := viper.GetString("serverkey")
if len(registryWhitelist) > 0 {
log.Infof("Accepting only images from registries: %+v", registryWhitelist)
} else {
log.Warn("Accepting images from ALL registries")
}
e := echo.New()
e.POST("/pods", admission.AdmitPods(denyLatestTag, registryWhitelist))
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `{"time":"${time_rfc3339_nano}","remote_ip":"${remote_ip}","host":"${host}",` +
`"method":"${method}","uri":"${uri}","status":${status},"latency_micro_sec":"${latency}"}` + "\n",
Output: os.Stdout}))
if debug {
log.Info("Setting debug level logging")
e.Logger.SetLevel(log.DEBUG)
} else if strings.ToLower(logLevel) == "info" {
log.Info("Setting info level logging")
e.Logger.SetLevel(log.INFO)
}
if serverCert != "" && serverKey != "" {
log.Infof("Starting HTTPS server with certs [%s %s]", serverCert, serverKey)
e.StartTLS(
fmt.Sprintf(":%d", port),
serverCert,
serverKey)
} else {
panic("Kubernetes api-server can only talk to a TLS enabled web server")
}
}