-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (153 loc) · 5.25 KB
/
main.go
File metadata and controls
169 lines (153 loc) · 5.25 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/sirupsen/logrus"
admissionv1 "k8s.io/api/admission/v1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
)
func main() {
lvl, ok := os.LookupEnv("LOG_LEVEL")
if !ok {
lvl = "debug"
}
level, err := logrus.ParseLevel(lvl)
if err != nil {
level = logrus.DebugLevel
}
logrus.SetLevel(level)
http.HandleFunc("/mutate", mutateHandler)
port := 8443
cert := "/etc/image-annotator-webhook/tls/tls.crt"
if _, err := os.Stat(cert); os.IsNotExist(err) {
logrus.Fatal("TLS certificate not found. Please mount it to /etc/image-annotator-webhook/tls/tls.crt")
}
key := "/etc/image-annotator-webhook/tls/tls.key"
if _, err := os.Stat(key); os.IsNotExist(err) {
logrus.Fatal("TLS key not found. Please mount it to /etc/image-annotator-webhook/tls/tls.key")
}
logrus.Print("Listening on port 8443...")
http.ListenAndServeTLS(fmt.Sprintf(":%d", port), cert, key, nil)
}
func mutateHandler(w http.ResponseWriter, r *http.Request) {
// Resources with podSpec: replicasets, deployments,
// pods, cronjobs, jobs, statefulsets, daemonsets
var admissionResponse *admissionv1.AdmissionResponse
admissionReview := admissionv1.AdmissionReview{}
if err := json.NewDecoder(r.Body).Decode(&admissionReview); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Initialize deserializer
deserializer := scheme.Codecs.UniversalDeserializer()
raw := admissionReview.Request.Object.Raw
// Deserialize the raw data into the appropriate Kubernetes object
obj, _, err := deserializer.Decode(raw, nil, nil)
if err != nil {
admissionResponse = toAdmissionResponse(err)
} else {
logger := logrus.WithField("uri", r.RequestURI)
// Assert the object to metav1.Object to access its metadata
metaObj, ok := obj.(metav1.Object)
if !ok {
// Handle the error
logger.Error("Object is not a metav1.Object")
return
}
logger.Debug("received mutating request for ", obj.GetObjectKind().GroupVersionKind().Kind, " ", metaObj.GetName(), " in namespace ", metaObj.GetNamespace())
// Extract the PodSpec if available
var podSpec *v1.PodSpec
switch o := obj.(type) {
case *v1.Pod:
podSpec = &o.Spec
case *appsv1.Deployment:
podSpec = &o.Spec.Template.Spec
case *appsv1.StatefulSet:
podSpec = &o.Spec.Template.Spec
case *appsv1.ReplicaSet:
podSpec = &o.Spec.Template.Spec
case *appsv1.DaemonSet:
podSpec = &o.Spec.Template.Spec
case *batchv1.CronJob:
podSpec = &o.Spec.JobTemplate.Spec.Template.Spec
case *batchv1.Job:
podSpec = &o.Spec.Template.Spec
}
// If PodSpec is available, patch it
if podSpec != nil {
currentAnnotations := obj.(metav1.Object).GetAnnotations()
patch, err := patchPodSpec(podSpec, currentAnnotations)
// logrus.Debug("patch: ", string(patch))
if err != nil {
admissionResponse = toAdmissionResponse(err)
} else {
admissionResponse = &admissionv1.AdmissionResponse{
Allowed: true,
Patch: patch,
PatchType: func() *admissionv1.PatchType {
pt := admissionv1.PatchTypeJSONPatch
return &pt
}(),
}
}
} else {
// Object does not have a PodSpec, so allow it
admissionResponse = &admissionv1.AdmissionResponse{
Allowed: true,
}
}
}
// Return the AdmissionReview to the API server
// Log the object admissionResponse as JSON
jsonString, _ := json.Marshal(&admissionResponse)
logrus.Trace("admissionResponse: ", string(jsonString))
admissionReview.Response = admissionResponse
admissionReview.Response.UID = admissionReview.Request.UID
if err := json.NewEncoder(w).Encode(admissionReview); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func toAdmissionResponse(err error) *admissionv1.AdmissionResponse {
return &admissionv1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}
func patchPodSpec(podSpec *v1.PodSpec, currentAnnotations map[string]string) ([]byte, error) {
patch := []map[string]interface{}{}
if currentAnnotations == nil {
logrus.Debug("currentAnnotations is nil. Initializing it to an empty map")
patch = append(patch, map[string]interface{}{
"op": "add",
"path": "/metadata/annotations",
"value": map[string]string{},
})
}
// Extract and append container images
containers := append(podSpec.Containers, podSpec.InitContainers...)
for _, container := range containers {
// Skip if the annotation already exists and image has a '@' in it
// Reason is this is probably a digest from policyController and we don't want to overwrite the annotation
annotationKey := "image.clabs.co/" + container.Name
if _, ok := currentAnnotations[annotationKey]; ok && strings.Contains(container.Image, "@") {
logrus.Debug("Skipping container ", container.Name, " because it already has an annotation and image has a digest")
continue
}
logrus.Debug("Adding annotation for container ", container.Name, " with image ", container.Image)
patch = append(patch, map[string]interface{}{
"op": "add",
// ~1 is a JSON pointer escape for /
"path": "/metadata/annotations/image.clabs.co~1" + container.Name,
"value": container.Image,
})
}
return json.Marshal(patch)
}