This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (52 loc) · 1.5 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
package main
import (
"flag"
"fmt"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"opa-admission-controller/internal"
"os"
"time"
)
func main() {
port := flag.Int("port", 8443, "port")
noSSL := flag.Bool("no-ssl", false, "don't use ssl")
configFile := flag.String("config", "/config/config.yaml", "path to config file")
level := zap.LevelFlag("loglevel", zap.InfoLevel, "loglevel")
flag.Parse()
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(*level)
logger, _ := config.Build([]zap.Option{}...)
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
yamlFile, err := os.Open(*configFile)
defer yamlFile.Close()
if err != nil {
sugar.Fatal(err)
}
byteValue, _ := ioutil.ReadAll(yamlFile)
mutations := make([]internal.Mutation, 0)
err = yaml.Unmarshal(byteValue, &mutations)
if err != nil {
sugar.Fatalf("Error unmarshalling config yaml %s", err)
}
controller := internal.Controller{Sugar: sugar, Mutations: mutations}
mux := http.NewServeMux()
mux.HandleFunc("/health", controller.HandleHealth)
mux.HandleFunc("/mutate", controller.HandleMutate)
s := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, // 1048576
}
sugar.Infof("starting https server on :%d", *port)
if *noSSL {
sugar.Fatal(s.ListenAndServe())
} else {
sugar.Fatal(s.ListenAndServeTLS("/etc/tls/tls.crt", "/etc/tls/tls.key"))
}
}