-
Notifications
You must be signed in to change notification settings - Fork 2
/
patch.go
156 lines (136 loc) · 3.41 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
package injector
import (
"strings"
"github.com/mattbaird/jsonpatch"
corev1 "k8s.io/api/core/v1"
)
func addVolumes(target, volumes []corev1.Volume, base string) []*jsonpatch.JsonPatchOperation {
result := make([]*jsonpatch.JsonPatchOperation, 0, len(volumes))
first := len(target) == 0
var value interface{}
for _, v := range volumes {
value = v
path := base
if first {
first = false
value = []corev1.Volume{v}
} else {
path += "/-"
}
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: path,
Value: value,
})
}
return result
}
func addVolumeMounts(target, mounts []corev1.VolumeMount, base string) []*jsonpatch.JsonPatchOperation {
result := make([]*jsonpatch.JsonPatchOperation, 0, len(mounts))
first := len(target) == 0
var value interface{}
for _, v := range mounts {
value = v
path := base
if first {
first = false
value = []corev1.VolumeMount{v}
} else {
path += "/-"
}
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: path,
Value: value,
})
}
return result
}
func addEnvs(target []corev1.EnvVar, vars []corev1.EnvVar, base string) []*jsonpatch.JsonPatchOperation {
result := make([]*jsonpatch.JsonPatchOperation, 0, len(vars))
first := len(target) == 0
var value interface{}
for _, v := range vars {
value = v
path := base
if first {
first = false
value = []corev1.EnvVar{v}
} else {
path += "/-"
}
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: path,
Value: value,
})
}
return result
}
func addContainers(target, containers []corev1.Container, base string) []*jsonpatch.JsonPatchOperation {
result := make([]*jsonpatch.JsonPatchOperation, 0, len(containers))
first := len(target) == 0
var value interface{}
for _, container := range containers {
value = container
path := base
if first {
first = false
value = []corev1.Container{container}
} else {
path += "/-"
}
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: path,
Value: value,
})
}
return result
}
func addInitContainers(target, containers []corev1.Container, base string) []*jsonpatch.JsonPatchOperation {
result := make([]*jsonpatch.JsonPatchOperation, 0, len(containers))
first := len(target) == 0
var value interface{}
for i := len(containers) - 1; i >= 0; i-- {
container := containers[i]
value = container
path := base
if first {
first = false
value = []corev1.Container{container}
} else {
path += "/0"
}
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: path,
Value: value,
})
}
return result
}
func updateAnnotations(target, annotations map[string]string) []*jsonpatch.JsonPatchOperation {
result := make([]*jsonpatch.JsonPatchOperation, 0, len(annotations))
if len(target) == 0 {
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/metadata/annotations",
Value: annotations,
})
return result
}
for key, value := range annotations {
result = append(result, &jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/metadata/annotations/" + EscapeJSONPointer(key),
Value: value,
})
}
return result
}
func EscapeJSONPointer(s string) string {
s = strings.ReplaceAll(s, "~", "~0")
s = strings.ReplaceAll(s, "/", "~1")
return s
}