-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (72 loc) · 2.21 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
package main
import (
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/klog/v2"
"github.com/skos-ninja/k8s-annotation-admission/handler"
"github.com/skos-ninja/k8s-annotation-admission/pkg/annotations"
"github.com/skos-ninja/k8s-annotation-admission/pkg/requests"
)
var cmd = &cobra.Command{
Use: "k8s-annotation-admission",
Short: "",
Long: "",
RunE: runE,
PostRun: func(cmd *cobra.Command, args []string) {
klog.Flush()
},
}
func init() {
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./config")
viper.AddConfigPath(".")
cmd.Flags().IntP("port", "p", 8080, "Specify port to run server on")
viper.BindPFlag("port", cmd.Flags().Lookup("port"))
cmd.Flags().StringP("tls-cert", "c", "", "Specify TLS certificate path")
viper.BindPFlag("tls-cert", cmd.Flags().Lookup("tls-cert"))
cmd.Flags().StringP("tls-key", "k", "", "Specify TLS key path")
viper.BindPFlag("tls-key", cmd.Flags().Lookup("tls-key"))
cmd.Flags().BoolP(annotations.FlagWarning, "w", false, "Only warn on a failed validation")
viper.BindPFlag(annotations.FlagWarning, cmd.Flags().Lookup(annotations.FlagWarning))
cmd.Flags().StringP(annotations.FlagKey, "a", "{}", "Specify annotations")
viper.BindPFlag(annotations.FlagKey, cmd.Flags().Lookup(annotations.FlagKey))
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
}
func main() {
if err := cmd.Execute(); err != nil {
panic(err)
}
}
func runE(cmd *cobra.Command, args []string) error {
requests.RegisterAdmission("/validate", handler.Handler)
annotations.InitValidations()
port := viper.GetInt("port")
server := &http.Server{
Addr: fmt.Sprintf(":%v", port),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
tlsCert := viper.GetString("tls-cert")
tlsKey := viper.GetString("tls-key")
if tlsCert != "" && tlsKey != "" {
sCert, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
if err != nil {
return err
}
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{sCert},
}
klog.Info("Listening on tls :", port)
return server.ListenAndServeTLS("", "")
}
klog.Info("Listening on :", port)
return server.ListenAndServe()
}