-
Notifications
You must be signed in to change notification settings - Fork 2
/
patch.go
196 lines (178 loc) · 5.56 KB
/
patch.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
package inject
import (
"encoding/json"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
type rfc6902PatchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
// JSONPatch `remove` is applied sequentially. Remove items in reverse
// order to avoid renumbering indices.
func removeContainers(containers []corev1.Container, removed []string, path string) (patch []rfc6902PatchOperation) {
names := map[string]bool{}
for _, name := range removed {
names[name] = true
}
for i := len(containers) - 1; i >= 0; i-- {
if _, ok := names[containers[i].Name]; ok {
patch = append(patch, rfc6902PatchOperation{
Op: "remove",
Path: fmt.Sprintf("%v/%v", path, i),
})
}
}
return patch
}
func removeVolumeMounts(volumeMounts []corev1.VolumeMount, removed []string, path string) (patch []rfc6902PatchOperation) {
names := map[string]bool{}
for _, name := range removed {
names[name] = true
}
for i := len(volumeMounts) - 1; i >= 0; i-- {
if _, ok := names[volumeMounts[i].Name]; ok {
patch = append(patch, rfc6902PatchOperation{
Op: "remove",
Path: fmt.Sprintf("%v/%v", path, i),
})
}
}
return patch
}
func removeAllVolumeMounts(containers []corev1.Container, volumeMounts []string) []rfc6902PatchOperation {
var patch []rfc6902PatchOperation
for i, container := range containers {
patch = append(patch, removeVolumeMounts(container.VolumeMounts, volumeMounts, fmt.Sprintf("/spec/containers/%d/volumeMounts", i))...)
}
return patch
}
func removeVolumes(volumes []corev1.Volume, removed []string, path string) (patch []rfc6902PatchOperation) {
names := map[string]bool{}
for _, name := range removed {
names[name] = true
}
for i := len(volumes) - 1; i >= 0; i-- {
if _, ok := names[volumes[i].Name]; ok {
patch = append(patch, rfc6902PatchOperation{
Op: "remove",
Path: fmt.Sprintf("%v/%v", path, i),
})
}
}
return patch
}
func insertContainer(target, added []corev1.Container, basePath string, position string) (patch []rfc6902PatchOperation) {
first := len(target) == 0
var value interface{}
for _, add := range added {
value = add
path := basePath
if first {
first = false
value = []corev1.Container{add}
} else {
path = path + "/" + position
}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: path,
Value: value,
})
}
return patch
}
func addVolumeMount(target, added []corev1.VolumeMount, basePath string) (patch []rfc6902PatchOperation) {
first := len(target) == 0
var value interface{}
for _, add := range added {
value = add
path := basePath
if first {
first = false
value = []corev1.VolumeMount{add}
} else {
path = path + "/-"
}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: path,
Value: value,
})
}
return patch
}
func addAllVolumeMounts(containers []corev1.Container, volumeMounts []corev1.VolumeMount, path string) []rfc6902PatchOperation {
var patch []rfc6902PatchOperation
for i, container := range containers {
patch = append(patch, addVolumeMount(container.VolumeMounts, volumeMounts, fmt.Sprintf(path, i))...)
}
return patch
}
func addVolume(target, added []corev1.Volume, basePath string) (patch []rfc6902PatchOperation) {
first := len(target) == 0
var value interface{}
for _, add := range added {
value = add
path := basePath
if first {
first = false
value = []corev1.Volume{add}
} else {
path = path + "/-"
}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: path,
Value: value,
})
}
return patch
}
func updateAnnotation(target map[string]string, added map[string]string) (patch []rfc6902PatchOperation) {
for key, value := range added {
if target == nil {
target = map[string]string{}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: "/metadata/annotations",
Value: map[string]string{
key: value,
},
})
} else {
op := "add"
if target[key] != "" {
op = "replace"
}
patch = append(patch, rfc6902PatchOperation{
Op: op,
Path: "/metadata/annotations/" + escapeJSONPointerValue(key),
Value: value,
})
}
}
return patch
}
// escape JSON Pointer value per https://tools.ietf.org/html/rfc6901.
func escapeJSONPointerValue(in string) string {
step := strings.Replace(in, "~", "~0", -1)
return strings.Replace(step, "/", "~1", -1)
}
// CreatePatch creates rfc6902Patch patching the pod.
func createPatch(pod *corev1.Pod, prevStatus *SidecarInjectionStatus, annotations map[string]string, sic *SidecarInjectionSpec) ([]byte, error) {
var patch []rfc6902PatchOperation
// Remove any containers previously injected by kube-inject using
// container and volume name as unique key for removal.
patch = append(patch, removeContainers(pod.Spec.InitContainers, prevStatus.InitContainers, "/spec/initContainers")...)
patch = append(patch, removeAllVolumeMounts(pod.Spec.Containers, prevStatus.VolumeMounts)...)
patch = append(patch, removeVolumes(pod.Spec.Volumes, prevStatus.Volumes, "/spec/volumes")...)
patch = append(patch, addAllVolumeMounts(pod.Spec.InitContainers, sic.VolumeMounts, "/spec/initContainers/%d/volumeMounts")...)
patch = append(patch, addAllVolumeMounts(pod.Spec.Containers, sic.VolumeMounts, "/spec/containers/%d/volumeMounts")...)
patch = append(patch, addVolume(pod.Spec.Volumes, sic.Volumes, "/spec/volumes")...)
patch = append(patch, insertContainer(pod.Spec.InitContainers, sic.InitContainers, "/spec/initContainers", "0")...)
patch = append(patch, updateAnnotation(pod.Annotations, annotations)...)
return json.Marshal(patch)
}