func()

in pkg/admission/admission_controller.go [378:416]


func (c *AdmissionController) updatePreemptionInfo(pod *v1.Pod, patch []common.PatchOperation) []common.PatchOperation {
	value := utils.GetPodAnnotationValue(pod, constants.AnnotationAllowPreemption)

	// return if already set to a valid value
	switch value {
	case constants.True:
		return patch
	case constants.False:
		return patch
	}

	value = constants.False
	if c.pcCache.isPreemptSelfAllowed(pod.Spec.PriorityClassName) {
		value = constants.True
	}

	log.Log(log.Admission).Info("updating pod preemption annotations",
		zap.String("podName", pod.Name),
		zap.String("allowPreemption", value))

	// check for an existing patch on annotations and update it
	for _, p := range patch {
		if p.Op == "add" && p.Path == "/metadata/annotations" {
			if annotations, ok := p.Value.(map[string]string); ok {
				annotations[constants.AnnotationAllowPreemption] = value
				return patch
			}
		}
	}

	result := updatePodAnnotation(pod, constants.AnnotationAllowPreemption, value)
	patch = append(patch, common.PatchOperation{
		Op:    "add",
		Path:  "/metadata/annotations",
		Value: result,
	})

	return patch
}