|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + admissionv1 "k8s.io/api/admission/v1" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + "github.com/mattbaird/jsonpatch" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + "k8s.io/client-go/rest" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + AnnotationRoutes = "ovn.kubernetes.io/routes" |
| 22 | + AnnotationPortSecurity = "ovn.kubernetes.io/port_security" |
| 23 | +) |
| 24 | + |
| 25 | +func HandleMutatePods(w http.ResponseWriter, r *http.Request) { |
| 26 | + body, err := io.ReadAll(r.Body) |
| 27 | + if err != nil { |
| 28 | + http.Error(w, "could not read request body", http.StatusBadRequest) |
| 29 | + return |
| 30 | + } |
| 31 | + defer r.Body.Close() |
| 32 | + |
| 33 | + var admissionReviewReq admissionv1.AdmissionReview |
| 34 | + if err := json.Unmarshal(body, &admissionReviewReq); err != nil { |
| 35 | + http.Error(w, "could not unmarshal request", http.StatusBadRequest) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + admissionReviewResp := admissionv1.AdmissionReview{ |
| 40 | + TypeMeta: admissionReviewReq.TypeMeta, |
| 41 | + } |
| 42 | + admissionResponse := &admissionv1.AdmissionResponse{ |
| 43 | + UID: admissionReviewReq.Request.UID, |
| 44 | + } |
| 45 | + admissionReviewResp.Response = admissionResponse |
| 46 | + |
| 47 | + if admissionReviewReq.Request.Operation != admissionv1.Create || |
| 48 | + admissionReviewReq.Request.Kind.Kind != "Pod" { |
| 49 | + admissionResponse.Allowed = true |
| 50 | + writeResponse(w, admissionReviewResp) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + raw := admissionReviewReq.Request.Object.Raw |
| 55 | + var pod corev1.Pod |
| 56 | + if err := json.Unmarshal(raw, &pod); err != nil { |
| 57 | + admissionResponse.Allowed = true |
| 58 | + writeResponse(w, admissionReviewResp) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + ns := admissionReviewReq.Request.Namespace |
| 63 | + nsAnnotations, err := getNamespaceAnnotations(ns) |
| 64 | + if err != nil { |
| 65 | + log.Printf("Failed to get namespace %q: %v", ns, err) |
| 66 | + admissionResponse.Allowed = true |
| 67 | + writeResponse(w, admissionReviewResp) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + mergedAnnotations := map[string]string{} |
| 72 | + if pod.Annotations != nil { |
| 73 | + for k, v := range pod.Annotations { |
| 74 | + mergedAnnotations[k] = v |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + nsRoutes, nsHasRoutes := nsAnnotations[AnnotationRoutes] |
| 79 | + if nsHasRoutes { |
| 80 | + if _, alreadySet := mergedAnnotations[AnnotationRoutes]; !alreadySet { |
| 81 | + mergedAnnotations[AnnotationRoutes] = nsRoutes |
| 82 | + } |
| 83 | + } else if RoutesGlobal != "" { |
| 84 | + if _, alreadySet := mergedAnnotations[AnnotationRoutes]; !alreadySet { |
| 85 | + mergedAnnotations[AnnotationRoutes] = RoutesGlobal |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + nsPortSec, nsHasPortSec := nsAnnotations[AnnotationPortSecurity] |
| 90 | + finalPortSec := "" |
| 91 | + if nsHasPortSec { |
| 92 | + finalPortSec = nsPortSec |
| 93 | + } else if PortSecurityGlobal { |
| 94 | + finalPortSec = "true" |
| 95 | + } |
| 96 | + |
| 97 | + if finalPortSec != "" { |
| 98 | + if _, alreadySet := mergedAnnotations[AnnotationPortSecurity]; !alreadySet { |
| 99 | + mergedAnnotations[AnnotationPortSecurity] = finalPortSec |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if len(mergedAnnotations) == len(pod.Annotations) { |
| 104 | + admissionResponse.Allowed = true |
| 105 | + writeResponse(w, admissionReviewResp) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + op := "replace" |
| 110 | + if pod.Annotations == nil { |
| 111 | + op = "add" |
| 112 | + } |
| 113 | + |
| 114 | + patches := []jsonpatch.JsonPatchOperation{{ |
| 115 | + Operation: op, |
| 116 | + Path: "/metadata/annotations", |
| 117 | + Value: mergedAnnotations, |
| 118 | + }} |
| 119 | + |
| 120 | + patchBytes, err := json.Marshal(patches) |
| 121 | + if err != nil { |
| 122 | + log.Printf("Failed to marshal patch: %v", err) |
| 123 | + admissionResponse.Allowed = true |
| 124 | + writeResponse(w, admissionReviewResp) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + admissionResponse.Allowed = true |
| 129 | + admissionResponse.Patch = patchBytes |
| 130 | + pt := admissionv1.PatchTypeJSONPatch |
| 131 | + admissionResponse.PatchType = &pt |
| 132 | + |
| 133 | + writeResponse(w, admissionReviewResp) |
| 134 | +} |
| 135 | + |
| 136 | +func getNamespaceAnnotations(namespace string) (map[string]string, error) { |
| 137 | + config, err := rest.InClusterConfig() |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + clientset, err := kubernetes.NewForConfig(config) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + ns, err := clientset.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + return ns.Annotations, nil |
| 151 | +} |
| 152 | + |
| 153 | +func writeResponse(w http.ResponseWriter, review admissionv1.AdmissionReview) { |
| 154 | + resp, err := json.Marshal(review) |
| 155 | + if err != nil { |
| 156 | + http.Error(w, fmt.Sprintf("could not marshal response: %v", err), http.StatusInternalServerError) |
| 157 | + return |
| 158 | + } |
| 159 | + w.Header().Set("Content-Type", "application/json") |
| 160 | + _, _ = w.Write(resp) |
| 161 | +} |
0 commit comments