-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutate.go
75 lines (64 loc) · 2.01 KB
/
mutate.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/go-logr/logr"
v1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
)
type MutateContainerImage struct {
logger logr.Logger
}
func (m *MutateContainerImage) MutateContainerImages(reqBody []byte, imageRules map[string]string) ([]byte, error) {
review := &v1.AdmissionReview{}
resp := &v1.AdmissionResponse{}
if errUnmarshalling := json.Unmarshal(reqBody, review); errUnmarshalling != nil {
return nil, errUnmarshalling
}
podObj := &corev1.Pod{}
if errUnmarshallingToPod := json.Unmarshal(review.Request.Object.Raw, podObj); errUnmarshallingToPod != nil {
return nil, errUnmarshallingToPod
}
if podObj.GetDeletionTimestamp() != nil {
return nil, errors.New("ErrPodIsInDeletingState")
}
var namespacedName string = fmt.Sprintf("%s/%s", podObj.GetNamespace(), podObj.GetName())
jsonPatch := v1.PatchTypeJSONPatch
resp.PatchType = &jsonPatch
resp.Allowed = true
resp.AuditAnnotations = map[string]string{"mutatedBy": "pod-mutating-webhook"}
var patches []map[string]string
for idx, cont := range podObj.Spec.Containers {
if val, ok := imageRules[cont.Image]; ok && val != "" {
p := map[string]string{
"op": "replace",
"path": fmt.Sprintf("/spec/containers/%d/image", idx),
"value": val,
}
m.logger.Info(fmt.Sprintf("%v changing image from %v to %v", namespacedName, cont.Image, val))
patches = append(patches, p)
}
}
for idx, initCont := range podObj.Spec.InitContainers {
if val, ok := imageRules[initCont.Image]; ok && val != "" {
p := map[string]string{
"op": "replace",
"path": fmt.Sprintf("/spec/initContainers/%d/image", idx),
"value": val,
}
m.logger.Info(fmt.Sprintf("%v changing image from %v to %v", namespacedName, initCont.Image, val))
patches = append(patches, p)
}
}
if len(patches) > 0 {
respPatch, err := json.Marshal(patches)
if err != nil {
return nil, err
}
resp.Patch = respPatch
review.Response = resp
return json.Marshal(review)
}
return nil, nil
}