Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YUNIKORN-1351] alway prefer annotations above labels #677

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 49 additions & 17 deletions pkg/admission/admission_controller.go
Expand Up @@ -196,7 +196,7 @@
patch = updateSchedulerName(patch)

if c.shouldLabelNamespace(namespace) {
patch = c.updateLabels(namespace, &pod, patch)
patch = c.updateApplicationInfo(namespace, &pod, patch)
patch = c.updatePreemptionInfo(&pod, patch)
} else {
patch = disableYuniKorn(namespace, &pod, patch)
Expand Down Expand Up @@ -369,8 +369,8 @@
func updateSchedulerName(patch []common.PatchOperation) []common.PatchOperation {
log.Log(log.Admission).Info("updating scheduler name")
return append(patch, common.PatchOperation{
Op: "add",
Path: "/spec/schedulerName",
Op: constants.AddPatchOp,
Path: constants.SchedulerNamePatchPath,
Value: constants.SchedulerName,
})
}
Expand All @@ -397,7 +397,7 @@

// check for an existing patch on annotations and update it
for _, p := range patch {
if p.Op == "add" && p.Path == "/metadata/annotations" {
if p.Op == constants.AddPatchOp && p.Path == constants.AnnotationPatchPath {
if annotations, ok := p.Value.(map[string]string); ok {
annotations[constants.AnnotationAllowPreemption] = value
return patch
Expand All @@ -407,29 +407,61 @@

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

Check warning on line 411 in pkg/admission/admission_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/admission/admission_controller.go#L410-L411

Added lines #L410 - L411 were not covered by tests
Value: result,
})

return patch
}

func (c *AdmissionController) updateLabels(namespace string, pod *v1.Pod, patch []common.PatchOperation) []common.PatchOperation {
log.Log(log.Admission).Info("updating pod labels",
func (c *AdmissionController) updateApplicationInfo(namespace string, pod *v1.Pod, patch []common.PatchOperation) []common.PatchOperation {
log.Log(log.Admission).Info("updating pod application labels and annotations",
zap.String("podName", pod.Name),
zap.String("generateName", pod.GenerateName),
zap.String("namespace", namespace),
zap.Any("labels", pod.Labels))
zap.Any("labels", pod.Labels),
zap.Any("annotations", pod.Annotations))

result := updatePodLabel(pod, namespace, c.conf.GetGenerateUniqueAppIds(), c.conf.GetDefaultQueueName())
newLabels, newAnnotations := getNewApplicationInfo(pod, namespace, c.conf.GetGenerateUniqueAppIds(), c.conf.GetDefaultQueueName())

patch = append(patch, common.PatchOperation{
Op: "add",
Path: "/metadata/labels",
Value: result,
})
patch = updatePatch(pod, newLabels, constants.LabelPatchPath, patch)
patch = updatePatch(pod, newAnnotations, constants.AnnotationPatchPath, patch)

return patch
}

func updatePatch(pod *v1.Pod, newValues map[string]string, patchPath string, patch []common.PatchOperation) []common.PatchOperation {
// if found an existing patch, add new values to it
for _, p := range patch {
if p.Op == constants.AddPatchOp && p.Path == patchPath {
if existingPatchValues, ok := p.Value.(map[string]string); ok {
for k, v := range newValues {
existingPatchValues[k] = v
}
return patch
}
}
}

// Add a new patch
if len(newValues) != 0 {
// combine new values with existing values in pod to create first patch
var existingPodValues map[string]string
if patchPath == constants.LabelPatchPath {
// newly add labels patch should include existing labels in pod
existingPodValues = pod.Labels
} else if patchPath == constants.AnnotationPatchPath {
// newly add annotations patch should include existing annotations in pod
existingPodValues = pod.Annotations
}
patchValue := utils.MergeMaps(existingPodValues, newValues)
patch = append(patch, common.PatchOperation{
Op: constants.AddPatchOp,
Path: patchPath,
Value: patchValue,
})
}
return patch
}

Expand All @@ -442,8 +474,8 @@
result := updatePodAnnotation(pod, constants.AnnotationIgnoreApplication, constants.True)

patch = append(patch, common.PatchOperation{
Op: "add",
Path: "/metadata/annotations",
Op: constants.AddPatchOp,
Path: constants.AnnotationPatchPath,
Value: result,
})

Expand Down