-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutate.go
123 lines (104 loc) · 3.08 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
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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
admission "k8s.io/api/admission/v1"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/json"
)
const LinkerdInjectAnnotation = "linkerd.io/inject"
func MutateCronjobs(body []byte) ([]byte, error) {
admReview := admission.AdmissionReview{}
if err := json.Unmarshal(body, &admReview); err != nil {
return nil, fmt.Errorf("unmarshaling request failed with %s", err)
}
var err error
var cronjob *batchv1.CronJob
var responseBody []byte
ar := admReview.Request
resp := admission.AdmissionResponse{}
if ar != nil {
if err := json.Unmarshal(ar.Object.Raw, &cronjob); err != nil {
return nil, fmt.Errorf("unable unmarshal cronjob json object %v", err)
}
resp.Allowed = true
resp.UID = ar.UID
pT := admission.PatchTypeJSONPatch
resp.PatchType = &pT
var op map[string]interface{}
if _, present := cronjob.Spec.JobTemplate.Spec.Template.Annotations[LinkerdInjectAnnotation]; !present {
log.WithField("cronjob", cronjob.Name).WithField("namespace", cronjob.Namespace).Info("Patch Cronjob")
op = map[string]interface{}{
"op": "add",
"path": "/spec/jobTemplate/spec/template/metadata/annotations",
"value": map[string]string{LinkerdInjectAnnotation: "disabled"},
}
resp.Patch, err = json.Marshal([]interface{}{op})
if err != nil {
return nil, err
}
} else {
resp.Patch, err = json.Marshal([]interface{}{})
if err != nil {
return nil, err
}
}
resp.Result = &metav1.Status{
Status: "Success",
}
admReview.Response = &resp
responseBody, err = json.Marshal(admReview)
if err != nil {
return nil, err
}
}
return responseBody, nil
}
func MutateJobs(body []byte) ([]byte, error) {
admReview := admission.AdmissionReview{}
if err := json.Unmarshal(body, &admReview); err != nil {
return nil, fmt.Errorf("unmarshaling request failed with %s", err)
}
var err error
var job *batchv1.Job
var responseBody []byte
ar := admReview.Request
resp := admission.AdmissionResponse{}
if ar != nil {
if err := json.Unmarshal(ar.Object.Raw, &job); err != nil {
return nil, fmt.Errorf("unable unmarshal job json object %v", err)
}
resp.Allowed = true
resp.UID = ar.UID
pT := admission.PatchTypeJSONPatch
resp.PatchType = &pT
var op map[string]interface{}
if _, present := job.Spec.Template.Annotations[LinkerdInjectAnnotation]; !present {
log.WithField("job", job.Name).WithField("namespace", job.Namespace).Info("Patch Job")
op = map[string]interface{}{
"op": "add",
"path": "/spec/template/metadata/annotations",
"value": map[string]string{LinkerdInjectAnnotation: "disabled"},
}
resp.Patch, err = json.Marshal([]interface{}{op})
if err != nil {
return nil, err
}
} else {
resp.Patch, err = json.Marshal([]interface{}{})
if err != nil {
return nil, err
}
}
resp.Result = &metav1.Status{
Status: "Success",
}
admReview.Response = &resp
responseBody, err = json.Marshal(admReview)
if err != nil {
return nil, err
}
}
return responseBody, nil
}