-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutate.go
193 lines (163 loc) · 4.77 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"k8s.io/api/admission/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const injectionLabel = "data.statcan.gc.ca/inject-blob-volumes"
const automountLabel = "blob.aaw.statcan.gc.ca/automount"
const classificationLabel = "data.statcan.gc.ca/classification"
// TODO: make sure the PV & PVCs are not in terminating state.
func (s *server) getBinds(pod v1.Pod) ([]v1.PersistentVolumeClaim, error) {
// classificationLabel: "protected-b",
var selector metav1.LabelSelector
if classification, ok := pod.ObjectMeta.Labels[classificationLabel]; ok && classification == "protected-b" {
// automount && protected-b == true
selector = metav1.LabelSelector{
MatchLabels: map[string]string{
classificationLabel: classification,
automountLabel: "true",
},
}
} else {
// automount && !(protected-b == true)
selector = metav1.LabelSelector{
MatchLabels: map[string]string{
automountLabel: "true",
},
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: classificationLabel,
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{
"protected-b",
},
},
},
}
}
//
selectorStr, _ := metav1.LabelSelectorAsSelector(&selector)
pvcs, err := s.client.CoreV1().PersistentVolumeClaims(pod.Namespace).List(
context.Background(),
metav1.ListOptions{
LabelSelector: selectorStr.String(),
},
)
if err != nil {
return []v1.PersistentVolumeClaim{}, err
}
return pvcs.Items, nil
}
func (s *server) addVolumeMount(name, mountPath string, readOnly bool, containerIndex int) []map[string]interface{} {
patches := make([]map[string]interface{}, 0)
// Add volume definition
patches = append(patches, map[string]interface{}{
"op": "add",
"path": "/spec/volumes/-",
"value": v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: name,
// ReadOnly: readOnly, // I think the underlying PV handles this. TODO: Check.
},
},
},
})
// Add VolumeMount
patches = append(patches, map[string]interface{}{
"op": "add",
"path": fmt.Sprintf("/spec/containers/%d/volumeMounts/-", containerIndex),
"value": v1.VolumeMount{
Name: name,
MountPath: mountPath,
},
})
return patches
}
func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
func (s *server) mutate(request v1beta1.AdmissionRequest) (v1beta1.AdmissionResponse, error) {
response := v1beta1.AdmissionResponse{}
log.Println(prettyPrint(request))
// Default response
response.Allowed = true
response.UID = request.UID
patch := v1beta1.PatchTypeJSONPatch
response.PatchType = &patch
patches := make([]map[string]interface{}, 0)
// Decode the pod object
var err error
pod := v1.Pod{}
if err = json.Unmarshal(request.Object.Raw, &pod); err != nil {
return response, fmt.Errorf("unable to decode Pod %w", err)
}
// We have to populate this from the
// AdmissionReview object?
pod.Name = request.Name
pod.Namespace = request.Namespace
log.Printf(
"Check pod for notebook %s/%s",
pod.Name,
pod.Namespace,
)
// Only inject when matching label
inject := false
containerIndex := 0
// If we have the right annotations
if val, ok := pod.ObjectMeta.Labels[injectionLabel]; ok {
bval, err := strconv.ParseBool(val)
if err != nil {
log.Printf("Failed to parse injection label for %s/%s", pod.Name, pod.Namespace)
return response, fmt.Errorf("unable to decode %s annotation %w", injectionLabel, err)
}
inject = bval
}
// If we have a Argo workflow, then lets run the logic
if _, ok := pod.ObjectMeta.Labels["workflows.argoproj.io/workflow"]; ok {
// Check the name of the first container in the pod.
// If it's called "wait", then we want to add the mount to the second container.
if pod.Spec.Containers[0].Name == "wait" {
containerIndex = 1
} else {
containerIndex = 0
}
}
log.Println(pod)
if inject {
log.Printf("Injecting pod %s/%s ...\n", pod.Name, pod.Namespace)
pvcs, err := s.getBinds(pod)
// Add all PVC patches
for _, pvc := range pvcs {
log.Println(fmt.Sprintf("/home/jovyan/buckets/%s", pvc.Name))
pvcpatches := s.addVolumeMount(
pvc.Name,
fmt.Sprintf("/home/jovyan/buckets/%s", pvc.Name),
false,
containerIndex,
)
patches = append(patches, pvcpatches...)
}
response.AuditAnnotations = map[string]string{
"blob-csi-injector": "Added Blob-CSI volume mounts",
}
response.Patch, err = json.Marshal(patches)
if err != nil {
return response, err
}
response.Result = &metav1.Status{
Status: metav1.StatusSuccess,
}
} else {
log.Printf("Skipping pod %s/%s.\n", pod.Name, pod.Namespace)
}
return response, nil
}