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

Emit an event if overwriting PodTemplate affinity #2859

Merged
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,13 @@ func (c *Reconciler) createPod(ctx context.Context, tr *v1beta1.TaskRun, rtr *re
return nil, fmt.Errorf("translating TaskSpec to Pod: %w", err)
}

return c.KubeClientSet.CoreV1().Pods(tr.Namespace).Create(pod)

pod, err = c.KubeClientSet.CoreV1().Pods(tr.Namespace).Create(pod)
if err == nil && willOverwritePodSetAffinity(tr) {
if recorder := controller.GetEventRecorder(ctx); recorder != nil {
recorder.Eventf(tr, corev1.EventTypeWarning, "PodAffinityOverwrite", "Pod template affinity is overwritten by affinity assistant for pod %q", pod.Name)
Copy link
Member

@pritidesai pritidesai Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have introduced const here for PodAffinityOverwrite but going through the reviews from @jlpettersson on the same 🙃 , looks good for now, can be converted to const if needed in future

}
}
return pod, err
}

type DeletePod func(podName string, options *metav1.DeleteOptions) error
Expand Down Expand Up @@ -731,3 +736,13 @@ func storeTaskSpec(ctx context.Context, tr *v1beta1.TaskRun, ts *v1beta1.TaskSpe
}
return nil
}

// willOverwritePodSetAffinity returns a bool indicating whether the
// affinity for pods will be overwritten with affinity assistant.
func willOverwritePodSetAffinity(taskRun *v1beta1.TaskRun) bool {
var podTemplate v1beta1.PodTemplate
if taskRun.Spec.PodTemplate != nil {
podTemplate = *taskRun.Spec.PodTemplate
}
return taskRun.Annotations[workspace.AnnotationAffinityAssistantName] != "" && podTemplate.Affinity != nil
}
65 changes: 65 additions & 0 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3026,3 +3026,68 @@ func Test_storeTaskSpec(t *testing.T) {
t.Fatalf(diff.PrintWantGot(d))
}
}

func TestWillOverwritePodAffinity(t *testing.T) {
affinity := &corev1.Affinity{
PodAffinity: &corev1.PodAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
Namespaces: []string{"tekton-pipelines"},
},
},
},
}
affinityAssistantName := "pipeline.tekton.dev/affinity-assistant"

tcs := []struct {
name string
hasTemplateAffinity bool
annotations map[string]string
expected bool
}{
{
name: "no settings",
expected: false,
},
{
name: "no PodTemplate affinity set",
annotations: map[string]string{
affinityAssistantName: "affinity-assistant",
},
expected: false,
},
{
name: "affinity assistant not set",
hasTemplateAffinity: true,
expected: false,
},
{
name: "PodTemplate affinity will be overwritten with affinity assistant",
hasTemplateAffinity: true,
annotations: map[string]string{
affinityAssistantName: "affinity-assistant",
},
expected: true,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
tr := &v1beta1.TaskRun{
Spec: v1beta1.TaskRunSpec{
PodTemplate: &v1beta1.PodTemplate{},
},
ObjectMeta: metav1.ObjectMeta{
Annotations: tc.annotations,
},
}
if tc.hasTemplateAffinity {
tr.Spec.PodTemplate.Affinity = affinity
}

if got := willOverwritePodSetAffinity(tr); got != tc.expected {
t.Errorf("expected: %t got: %t", tc.expected, got)
}
})
}
}