-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
192 lines (164 loc) · 5.26 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/json"
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"io/ioutil"
"k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
kubeClient *kubernetes.Clientset
runtimeScheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(runtimeScheme)
deserializer = codecs.UniversalDeserializer()
)
// WhSvrParameters ...
// Webhook Server parameters
type WhSvrParameters struct {
port int // webhook server port
certFile string // path to the x509 certificate for https
keyFile string // path to the x509 private key matching `CertFile`
alsoLogToStderr bool
}
type WebhookServer struct {
server *http.Server
}
type patchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
func main() {
var parameters WhSvrParameters
// get command line parameters
flag.IntVar(¶meters.port, "port", 443, "Webhook server port.")
flag.BoolVar(¶meters.alsoLogToStderr, "alsologtostderr", true, "Flag that controls sending logs to stderr")
flag.StringVar(¶meters.certFile, "tlsCertFile", "/etc/webhook/certs/cert.pem", "File containing the x509 Certificate for HTTPS.")
flag.StringVar(¶meters.keyFile, "tlsKeyFile", "/etc/webhook/certs/key.pem", "File containing the x509 private key to --tlsCertFile.")
flag.Parse()
// creates the in-cluster config
cfg, err := rest.InClusterConfig()
if err != nil {
panic(err)
}
kubeClient = kubernetes.NewForConfigOrDie(cfg)
pair, err := tls.LoadX509KeyPair(parameters.certFile, parameters.keyFile)
if err != nil {
panic(fmt.Errorf("Failed to load key pair: %v", err))
}
whsvr := &WebhookServer{
server: &http.Server{
Addr: fmt.Sprintf(":%v", parameters.port),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{pair}},
},
}
// define http server and server handler
mux := http.NewServeMux()
mux.HandleFunc("/mutate", whsvr.serve)
whsvr.server.Handler = mux
// start webhook server in new routine
go func() {
if err := whsvr.server.ListenAndServeTLS("", ""); err != nil {
panic(fmt.Errorf("Failed to listen and serve webhook server: %v", err))
}
}()
fmt.Println("Server started")
// listening OS shutdown singal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
fmt.Println("Got OS shutdown signal, shutting down webhook server gracefully...")
whsvr.server.Shutdown(context.Background())
}
// Serve method for webhook server
func (whsvr *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
fmt.Print("## Received request ##")
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
}
}
if len(body) == 0 {
fmt.Println("empty body")
http.Error(w, "empty body", http.StatusBadRequest)
return
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
fmt.Printf("Content-Type=%s, expect application/json", contentType)
http.Error(w, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
return
}
var admissionResponse *v1.AdmissionResponse
ar := v1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
fmt.Printf("Can't decode body: %v", err)
admissionResponse = &v1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
} else {
fmt.Printf("%v\n", ar.Request)
fmt.Printf("####### METHOD:%s #######\n", ar.Request.Operation)
fmt.Println(r.URL.Path)
if r.URL.Path == "/mutate" {
method := string(ar.Request.Operation)
admissionResponse = whsvr.mutate(&ar, method)
}
}
admissionReview := v1.AdmissionReview{}
if admissionResponse != nil {
admissionReview.Response = admissionResponse
if ar.Request != nil {
admissionReview.Response.UID = ar.Request.UID
}
resp, err := json.Marshal(admissionReview)
if err != nil {
fmt.Printf("Can't encode response: %v", err)
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
}
//fmt.Println("Ready to write reponse ...")
if _, err := w.Write(resp); err != nil {
fmt.Printf("Can't write response: %v", err)
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
}
}
}
func (whsvr *WebhookServer) mutate(ar *v1.AdmissionReview, httpMethod string) *v1.AdmissionResponse {
req := ar.Request
fmt.Println("=== Request ===")
fmt.Println(req.Kind.Kind)
fmt.Println(req.Name)
fmt.Println(req.Namespace)
fmt.Println(httpMethod)
fmt.Println("=== Request ===")
fmt.Println("=== User ===")
fmt.Println(req.UserInfo.Username)
fmt.Println("=== User ===")
var patchOperations []patchOperation
patchOperations = make([]patchOperation, 0)
fmt.Printf("PatchOperations:%v\n", patchOperations)
patchBytes, _ := json.Marshal(patchOperations)
return &v1.AdmissionResponse{
Allowed: true,
Patch: patchBytes,
PatchType: func() *v1.PatchType {
pt := v1.PatchTypeJSONPatch
return &pt
}(),
}
}