-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (121 loc) · 3.97 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
// TODO: try this library to see if it generates correct json patch
// https://github.com/mattbaird/jsonpatch
)
const (
// MonbanKey is state of lock
MonbanKey = "koudaiii/monban"
)
// you need clientset to get Namespace from deployments
var clientset *kubernetes.Clientset
// toAdmissionResponse decide response Result and Message
func toAdmissionResponse(allowed bool, err error) *v1beta1.AdmissionResponse {
response := &v1beta1.AdmissionResponse{
Allowed: allowed,
}
if err != nil {
response.Result = &metav1.Status{
Message: err.Error(),
}
}
return response
}
// Disallow edit deployments from specific annotations.
func admitDeployments(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
log.Println("admitting deployments")
if expect, actual := "deployments", ar.Request.Resource.Resource; expect != actual {
err := fmt.Errorf("unexpected resource: expect %s, actual %s", expect, actual)
log.Println(err)
return toAdmissionResponse(true, nil)
}
name, namespace, operation := ar.Request.Name, ar.Request.Namespace, ar.Request.Operation
log.Printf("operation: %s, name: %s, namespace: %s\n", operation, name, namespace)
if expect, actual := "UPDATE", string(operation); expect != actual {
err := fmt.Errorf("unexpected operation: expect %s, actual %s", expect, actual)
log.Println(err)
return toAdmissionResponse(true, nil)
}
// Get namespace with Annotations.
ns, err := clientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{})
if err != nil {
log.Println(err)
return toAdmissionResponse(true, nil)
}
if ns.ObjectMeta.Annotations[MonbanKey] == "enabled" {
var msg string
msg = "%s is locked in %s\n"
msg = msg + "If you want to unlock, please run command `kubectl annotate namespace/%s %s-`"
err := fmt.Errorf(msg, name, namespace, namespace, MonbanKey)
log.Println(err)
return toAdmissionResponse(false, err)
}
return toAdmissionResponse(true, nil)
}
type admitFunc func(v1beta1.AdmissionReview) *v1beta1.AdmissionResponse
func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
}
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
log.Printf("contentType=%s, expect application/json\n", contentType)
return
}
var reviewResponse *v1beta1.AdmissionResponse
ar := v1beta1.AdmissionReview{}
deserializer := codecs.UniversalDeserializer()
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
log.Fatal(err)
reviewResponse = toAdmissionResponse(false, err)
} else {
reviewResponse = admit(ar)
}
response := v1beta1.AdmissionReview{}
if reviewResponse != nil {
response.Response = reviewResponse
response.Response.UID = ar.Request.UID
}
// reset the Object and OldObject, they are not needed in a response.
ar.Request.Object = runtime.RawExtension{}
ar.Request.OldObject = runtime.RawExtension{}
resp, err := json.Marshal(response)
if err != nil {
log.Println(err)
}
if _, err := w.Write(resp); err != nil {
log.Println(err)
}
}
func serveDeployments(w http.ResponseWriter, r *http.Request) {
serve(w, r, admitDeployments)
}
func main() {
var config Config
flag.StringVar(&config.CertFile, "tls-cert-file", config.CertFile, "Destination of cert file")
flag.StringVar(&config.KeyFile, "tls-key-file", config.KeyFile, "Destination of key file")
flag.StringVar(&config.CAFile, "tls-ca-file", config.CAFile, "Destination of CA file")
flag.Parse()
clientset = getClient()
http.HandleFunc("/deployments", serveDeployments)
log.Println("Starting monban...")
server := &http.Server{
Addr: ":443",
TLSConfig: configTLS(config, clientset),
}
server.ListenAndServeTLS("", "")
}