-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (128 loc) · 3.95 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/StatCan/daaas-aaw-toleration-injector/pkg/signals"
"gopkg.in/yaml.v3"
"k8s.io/api/admission/v1beta1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)
var (
masterURL string
kubeconfig string
)
func handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}
func handleHealthz(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
// read ns-conf.yaml and use in mutate.go to tolerate pods with special cases
func UnmarshalConf() map[string][]string {
yfile, err := ioutil.ReadFile("./app/ns-conf.yaml")
if err != nil {
log.Println(err)
}
conf := make(map[string][]string)
err2 := yaml.Unmarshal(yfile, &conf)
if err2 != nil {
log.Println(err2)
}
// list big cpu namespaces in namespace slice
// for k, v := range conf["bigCPUns"] {
// fmt.Printf("%d -> %s\n", k, v)
// }
return conf
}
func handleMutate(namespacesLister corev1listers.NamespaceLister) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Decode the request
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
admissionReview := v1beta1.AdmissionReview{}
if err := json.Unmarshal(body, &admissionReview); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
response, err := mutate(namespacesLister, *admissionReview.Request, UnmarshalConf())
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
reviewResponse := v1beta1.AdmissionReview{
Response: &response,
}
if body, err = json.Marshal(reviewResponse); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(body)
}
}
func main() {
klog.InitFlags(nil)
flag.Parse()
stopCh := signals.SetupSignalHandler()
// We need to setup a Kubernetes client in order to maintain a list of namespaces.
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
klog.Fatalf("error building kubeconfig: %v", err)
}
kubeclient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("error building kubernetes client: %v", err)
}
informerFactory := informers.NewSharedInformerFactory(kubeclient, time.Second*30)
namespacesInformer := informerFactory.Core().V1().Namespaces()
namespacesLister := namespacesInformer.Lister()
informerFactory.Start(stopCh)
// Wait for caches to sync
klog.Infof("waiting for synchronization of informer caches")
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
if ok := cache.WaitForCacheSync(ctx.Done(), namespacesInformer.Informer().HasSynced); !ok {
klog.Fatalf("failed to synchronize informer caches")
}
klog.Infof("informer caches have synchronized")
// Start the webserver
mux := http.NewServeMux()
mux.HandleFunc("/", handleRoot)
mux.HandleFunc("/_healthz", handleHealthz)
mux.HandleFunc("/mutate", handleMutate(namespacesLister))
s := &http.Server{
Addr: ":8443",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Println("Listening on :8443")
log.Fatal(s.ListenAndServeTLS("./certs/tls.crt", "./certs/tls.key"))
}
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
}