in webhooks/alloydb-mutating-wh/handlers/pod_tolerations_handler.go [106:174]
func mutatePod(ar *v1beta1.AdmissionReview, tols []corev1.Toleration) *v1beta1.AdmissionResponse {
log.Info("handlers.mutatePod():Starting to add AlloyDB Omninodepool specific tolerations to the pod")
raw := ar.Request.Object.Raw
pod := corev1.Pod{}
if err := json.Unmarshal(raw, &pod); err != nil {
return &v1beta1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: false,
Result: &metav1.Status{
Message: err.Error(),
},
}
}
if pod.TypeMeta.Kind != "Pod" {
return &v1beta1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: false,
Result: &metav1.Status{
Message: "Invalid Kind for the request, only pods are supported for mutation",
},
}
}
if len(tols) == 0 {
return &v1beta1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: true,
Result: &metav1.Status{
Status: "Success",
},
}
}
existing := pod.Spec.Tolerations // Existing tolerations
combined := []corev1.Toleration{} // Existing & newly added combined
if len(existing) == 0 { // When no existing tolerations, combined = newly added only
combined = tols
} else {
for _, t := range tols {
if !exists(t, existing) {
combined = append(combined, t)
}
}
combined = append(combined, existing...)
}
patch, err := constructPatch(combined)
if err != nil {
log.Errorf("handlers.mutatePod():Could not create a patch for adding tolerations to the pod:: %v", err)
return &v1beta1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: false,
Result: &metav1.Status{
Message: err.Error(),
},
}
}
log.Info("handlers.mutatePod():Added the AlloyDB Omni nodepool specific tolerations to the pod & returning the patch")
return &v1beta1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: true,
Patch: patch,
PatchType: func() *v1beta1.PatchType {
pt := v1beta1.PatchTypeJSONPatch
return &pt
}(),
}
}