-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
290 lines (266 loc) · 9.52 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
v1 "k8s.io/api/apps/v1"
runtime2 "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"log"
"net/http"
"os"
"strings"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
var (
codecs = serializer.NewCodecFactory(runtime.NewScheme())
logger = log.New(os.Stdout, "", log.LstdFlags)
clientset *kubernetes.Clientset
cloud = os.Getenv("CLOUD")
//namespace = os.Getenv("NAMESPACE")
namespaces = strings.Split(os.Getenv("NAMESPACE"), ",")
)
func main() {
// assume defaults
if cloud == "" {
cloud = "gcp"
}
tlsCert := flag.String("tls-cert", "", "Certificate for TLS")
tlsKey := flag.String("tls-key", "", "Private key file for TLS")
port := flag.Int("port", 443, "Port to listen on for HTTPS traffic")
flag.Parse()
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
stopCh := make(chan struct{})
//concurrently watch for pod creation and label the pod with the node locality
go watchAndLabelPods(stopCh)
//annotate existing deployments with stats tags
if err = annotateExistingDeployments(); err != nil {
log.Fatal(err)
}
if err = runWebhookServer(*tlsCert, *tlsKey, *port); err != nil {
log.Fatal(err)
}
}
// annotateExistingDeployments annotates existing deployments with stats tags.
func annotateExistingDeployments() error {
log.Println("fetching deployments...")
for _, ns := range namespaces {
depl, err := clientset.AppsV1().Deployments(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
log.Printf("annotating %v deployments in %v\n", len(depl.Items), ns)
for _, d := range depl.Items {
if d.Spec.Template.Annotations == nil {
d.Spec.Template.Annotations = make(map[string]string)
}
if v, ok := d.Spec.Template.Annotations["sidecar.istio.io/extraStatTags"]; ok && v == "destination_locality" {
log.Printf("skipping deployment %v/%v\n", d.Name, d.Namespace)
continue
}
log.Printf("annotating deployment %v/%v\n", d.Name, d.Namespace)
d.Spec.Template.Annotations["sidecar.istio.io/extraStatTags"] = "destination_locality"
_, err = clientset.AppsV1().Deployments(ns).Update(context.TODO(), &d, metav1.UpdateOptions{})
if err != nil {
logger.Printf("error in updating deployment, skipping...: %v\n", err)
}
}
}
return nil
}
func addPodEventHandler(obj interface{}) {
pod := obj.(*corev1.Pod)
log.Printf("updating pod %v\n", pod.Name)
// Get the node locality
locality, err := getNodeLocality(pod.Spec.NodeName, cloud)
if err != nil {
log.Printf("error in getting node locality: %v\n", err)
return
}
// Label the pod with the node locality
pod.ObjectMeta.Labels["locality"] = locality
// annotate the pod with destination_locality tag
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
pod.Annotations["sidecar.istio.io/extraStatTags"] = "destination_locality"
// Update the pod
_, err = clientset.CoreV1().Pods(pod.Namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
if err != nil {
log.Printf("error in updating pod: %v\n", err)
return
}
log.Printf("Pod %v updated\n", pod.Name)
}
// watchAndLabelPod watches for pod creation and labels the pod with the node locality.
func watchAndLabelPods(stopCh <-chan struct{}) {
log.Printf("labeling pods in namespaces %v...", namespaces)
informerIndex := map[string]cache.SharedInformer{}
if len(namespaces) == 0 {
informerIndex["_"] = informers.NewSharedInformerFactory(clientset, 0).Core().V1().Pods().Informer()
}
for _, ns := range namespaces {
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace(ns))
informerIndex[ns] = factory.Core().V1().Pods().Informer()
}
for _, informer := range informerIndex {
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: addPodEventHandler,
})
informer.Run(stopCh)
}
defer runtime2.HandleCrash()
}
var deserializer = codecs.UniversalDeserializer()
func mutatePod(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") != "application/json" {
http.Error(w, "invalid content type, want `application/json`", http.StatusBadRequest)
return
}
body, err := readBody(r)
if err != nil {
logger.Printf("reading body: %v", err)
http.Error(w, "failed to read request body", http.StatusInternalServerError)
return
}
// Decode the request body into
admissionReviewRequest := &admissionv1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, admissionReviewRequest); err != nil {
logger.Printf("decoding body: %v", err)
http.Error(w, "failed to read request body", http.StatusInternalServerError)
return
}
logger.Printf("got for %v", admissionReviewRequest.Request.Resource.Resource)
// Do server-side validation that we are only dealing with a pod resource. This
// should also be part of the MutatingWebhookConfiguration in the cluster, but
// we should verify here before continuing.
//podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
//deploymentResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "deployments"}
resourceType := admissionReviewRequest.Request.Resource
if resourceType.Resource != "deployments" && resourceType.Resource != "pods" {
logger.Printf("unexpected resource of type %q, expected a pod/deployment", admissionReviewRequest.Request.Resource.Resource)
http.Error(w, "unexpected resource", http.StatusBadRequest)
return
}
// Decode AdmissionReview to pod or deployment.
raw := admissionReviewRequest.Request.Object.Raw
var admissionReviewResponse admissionv1.AdmissionReview
admissionResponse := &admissionv1.AdmissionResponse{
Allowed: true,
}
var patch string
// todo this should probably be deleted at some point
if resourceType.Resource == "pods" {
pod := corev1.Pod{}
if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
logger.Printf("decoding raw pod: %v", err)
http.Error(w, "failed to decode pod", http.StatusInternalServerError)
return
}
// todo custom configure cloud
podLocality, err := getNodeLocality(pod.Spec.NodeName, "gcp")
if err != nil {
logger.Printf("unable to get locality from node info for pod %v, skipping patching locality\n", pod.Name)
}
log.Printf("editing pod %v for locality %v", pod.Name, podLocality)
patch = fmt.Sprintf(`[
{"op":"add",
"path":"/metadata/labels/locality","value": "%v"}
]`, podLocality)
} else {
// handle deployments
deployment := v1.Deployment{}
if _, _, err := deserializer.Decode(raw, nil, &deployment); err != nil {
logger.Printf("decoding raw deployment: %v", err)
http.Error(w, "failed to decode deployment", http.StatusInternalServerError)
}
log.Printf("editing deployment %v, adding destination_pod...", deployment.Name)
// we need to add the metadata/annotations key if it doesn't exist.
annotationPatch := ""
if len(deployment.Spec.Template.Annotations) == 0 {
annotationPatch = `{"op":"add","path":"/spec/template/metadata/annotations","value":{}},`
}
log.Printf("annotationPatch: %v", annotationPatch)
patch = fmt.Sprintf(`[%v
{"op":"add",
"path":"/spec/template/metadata/annotations/sidecar.istio.io~1extraStatTags","value": "destination_locality"}]`, annotationPatch)
}
// Construct response
patchType := admissionv1.PatchTypeJSONPatch
admissionResponse.PatchType = &patchType
admissionResponse.Patch = []byte(patch)
admissionReviewResponse.Response = admissionResponse
admissionReviewResponse.SetGroupVersionKind(admissionReviewRequest.GroupVersionKind())
admissionReviewResponse.Response.UID = admissionReviewRequest.Request.UID
res, err := json.Marshal(admissionReviewResponse)
if err != nil {
logger.Printf("marshaling response: %v", err)
http.Error(w, "failed to encode response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(res)
}
func readBody(r *http.Request) ([]byte, error) {
var body []byte
if r.Body != nil {
requestData, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
body = requestData
}
return body, nil
}
// getNodeLocality gets the locality given by topology.kubernetes.io.
func getNodeLocality(name, cloud string) (string, error) {
// if we are on AWS, we want to just get region, because availability zones
// are not supported yet.
if cloud == "aws" {
return getNodeLabel(name, "topology.kubernetes.io/region")
}
return getNodeLabel(name, "topology.kubernetes.io/zone")
}
// getNodeLabel returns the value of the label on the node with the given name.
func getNodeLabel(name, label string) (string, error) {
node, err := clientset.CoreV1().Nodes().Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
fmt.Printf("error in getting node %v: %v\n", name, err)
return "", err
}
return node.Labels[label], nil
}
func runWebhookServer(certFile, keyFile string, port int) error {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
http.HandleFunc("/mutate", mutatePod)
server := http.Server{
Addr: fmt.Sprintf(":%d", port),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
ErrorLog: logger,
}
return server.ListenAndServeTLS(certFile, keyFile)
}