func mutatePod()

in webhooks/alloydb-nodeselector-mwh/handlers/pod_nodeselector_handler.go [104:161]


func mutatePod(ar *v1beta1.AdmissionReview, selectors map[string]string) *v1beta1.AdmissionResponse {

	log.Info("handlers.mutatePod():Starting to add AlloyDB specific node selectors 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(selectors) == 0 {
		return &v1beta1.AdmissionResponse{
			UID:     ar.Request.UID,
			Allowed: true,
			Result: &metav1.Status{
				Status: "Success",
			},
		}

	}
	existing := pod.Spec.NodeSelector
	combined := mergeMaps(existing, selectors)
	patch, err := constructPatch(combined)
	if err != nil {
		log.Errorf("handlers.mutatePod():Could not create a patch for adding node selectors 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 node selectors 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
		}(),
	}
}