-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 1.78 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 (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
)
func main() {
cert := flag.String("cert", "/etc/admission-webhook/tls/tls.crt", "Path to the certificate file")
key := flag.String("key", "/etc/admission-webhook/tls/tls.key", "Path to the key file")
port := flag.Int("port", 8443, "Port to listen on")
verbose := flag.Bool("verbose", false, "Enable verbose logging")
flag.Parse()
if *verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
http.HandleFunc("/mutate-cronjob", Mutate)
http.HandleFunc("/mutate-job", Mutate)
http.HandleFunc("/health", Healthcheck)
log.Printf("Listening on port %d...", *port)
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", *port), *cert, *key, nil))
}
func Mutate(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
defer r.Body.Close()
const writingErrorFormat = "Error writing response: %v"
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
_, err := fmt.Fprintf(w, "%s", err)
if err != nil {
log.Errorf(writingErrorFormat, err)
}
}
mutated := []byte{}
log.Debugf("Received request: %s", r.URL.Path)
log.Debugf("recv: %s\n", string(body))
switch r.URL.Path {
case "/mutate-cronjob":
mutated, err = MutateCronjobs(body)
case "/mutate-job":
mutated, err = MutateJobs(body)
}
if err != nil {
log.Errorf("Error mutating: %v", err)
w.WriteHeader(http.StatusInternalServerError)
_, err := fmt.Fprintf(w, "Error mutating: %s", err)
if err != nil {
log.Errorf(writingErrorFormat, err)
}
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(mutated)
if err != nil {
log.Errorf(writingErrorFormat, err)
}
}
func Healthcheck(writer http.ResponseWriter, _ *http.Request) {
writer.WriteHeader(http.StatusOK)
}