Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions cmd/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var (

const (
admissionWebhookAnnotationInjectKey = "filer-injector-webhook.das-zone.statcan/inject"
admissionWebhookAnnotationStatusKey = "filer-injector-webhook.das-zone.statcan/status"
)

const existingShares = "existing-shares"
Expand Down Expand Up @@ -101,22 +100,16 @@ func mutationRequired(metadata *metav1.ObjectMeta) bool {
annotations = map[string]string{}
}

status := annotations[admissionWebhookAnnotationStatusKey]

// determine whether to perform mutation based on annotation for the target resource
var required bool
if strings.ToLower(status) == "injected" {
switch strings.ToLower(annotations[admissionWebhookAnnotationInjectKey]) {
default:
required = true
case "n", "not", "false", "off", "injected":
required = false
} else {
switch strings.ToLower(annotations[admissionWebhookAnnotationInjectKey]) {
default:
required = true
case "n", "not", "false", "off":
required = false
}
}

infoLogger.Printf("Mutation policy for %v/%v: status: %q required:%v", metadata.Namespace, metadata.Name, status, required)
infoLogger.Printf("Mutation policy for %v/%v: required:%v", metadata.Namespace, metadata.Name, required)
return required
}

Expand Down Expand Up @@ -162,25 +155,21 @@ func addVolume(target, added []corev1.Volume, basePath string) (patch []patchOpe
return patch
}

func updateAnnotation(target map[string]string, added map[string]string) (patch []patchOperation) {
for key, value := range added {
if target == nil || target[key] == "" {
target = map[string]string{}
patch = append(patch, patchOperation{
Op: "add",
Path: "/metadata/annotations",
Value: map[string]string{
key: value,
},
})
} else {
patch = append(patch, patchOperation{
Op: "replace",
Path: "/metadata/annotations/" + key,
Value: value,
})
}
func updateAnnotation(target map[string]string) (patch []patchOperation) {
if target == nil || target[admissionWebhookAnnotationInjectKey] == "" {
patch = append(patch, patchOperation{
Op: "add",
Path: "/metadata/annotations/filer-injector-webhook.das-zone.statcan~1inject",
Comment thread
Jose-Matsuda marked this conversation as resolved.
Value: "injected",
})
} else {
patch = append(patch, patchOperation{
Op: "replace",
Path: "/metadata/annotations/filer-injector-webhook.das-zone.statcan~1inject",
Value: "injected",
})
}

return patch
}

Expand Down Expand Up @@ -238,7 +227,7 @@ func updateUserEnvVars(targetContainerSpec []corev1.Container, variableName stri
}

// createPatch function handles the mutation patch creation
func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, annotations map[string]string, clientset *kubernetes.Clientset,
func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kubernetes.Clientset,
svmShareList *corev1.ConfigMap, svmInfoMap map[string]SvmInfo) ([]byte, error) {
var patch []patchOperation
resourceRequest := map[corev1.ResourceName]resource.Quantity{
Expand Down Expand Up @@ -323,7 +312,7 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, annotations map
// Add container and volume to the patch
patch = append(patch, addContainer(pod.Spec.Containers, sidecarConfig.Containers, "/spec/containers")...)
patch = append(patch, addVolume(pod.Spec.Volumes, sidecarConfig.Volumes, "/spec/volumes")...)
patch = append(patch, updateAnnotation(pod.Annotations, annotations)...)
patch = append(patch, updateAnnotation(pod.Annotations)...)
patch = append(patch, updateWorkingVolumeMounts(pod.Spec.Containers, csiEphemeralVolumeountName, bucketMount, svmName, isFirstVol)...)
// Add the environment variables
patch = append(patch, updateUserEnvVars(pod.Spec.Containers, svmName+"_"+cleanAndSanitizeName(bucketMount), hashedBucketName)...)
Expand Down Expand Up @@ -408,8 +397,7 @@ func (whsvr *WebhookServer) mutate(ar *admissionv1.AdmissionReview, clientset *k
}
}

annotations := map[string]string{admissionWebhookAnnotationStatusKey: "injected"}
patchBytes, err := createPatch(&pod, whsvr.sidecarConfig, annotations, clientset, svmShareList, svmInfoMap)
patchBytes, err := createPatch(&pod, whsvr.sidecarConfig, clientset, svmShareList, svmInfoMap)
if err != nil {
return &admissionv1.AdmissionResponse{
Result: &metav1.Status{
Expand Down