Skip to content
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
8 changes: 7 additions & 1 deletion pkg/reconciler/pipelinerun/affinity_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ func getClaimName(w v1beta1.WorkspaceBinding, ownerReference metav1.OwnerReferen
return ""
}

func (c *Reconciler) cleanupAffinityAssistants(pr *v1beta1.PipelineRun) error {
func (c *Reconciler) cleanupAffinityAssistants(ctx context.Context, pr *v1beta1.PipelineRun) error {

// omit cleanup if the feature is disabled
if c.isAffinityAssistantDisabled(ctx) {
return nil
}

var errs []error
for _, w := range pr.Spec.Workspaces {
if w.PersistentVolumeClaim != nil || w.VolumeClaimTemplate != nil {
Expand Down
46 changes: 45 additions & 1 deletion pkg/reconciler/pipelinerun/affinity_assistant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestCreateAndDeleteOfAffinityAssistant(t *testing.T) {
t.Errorf("unexpected error when retrieving StatefulSet: %v", err)
}

err = c.cleanupAffinityAssistants(testPipelineRun)
err = c.cleanupAffinityAssistants(context.Background(), testPipelineRun)
if err != nil {
t.Errorf("unexpected error from cleanupAffinityAssistants: %v", err)
}
Expand Down Expand Up @@ -146,6 +146,50 @@ func TestThatAffinityAssistantNameIsNoLongerThan53(t *testing.T) {
}
}

// TestThatCleanupIsAvoidedIfAssistantIsDisabled tests that
// cleanup of Affinity Assistants is omitted when the
// Affinity Assistant is disabled
func TestThatCleanupIsAvoidedIfAssistantIsDisabled(t *testing.T) {
testPipelineRun := &v1beta1.PipelineRun{
TypeMeta: metav1.TypeMeta{Kind: "PipelineRun"},
ObjectMeta: metav1.ObjectMeta{
Name: "test-pipelinerun",
},
Spec: v1beta1.PipelineRunSpec{
Workspaces: []v1beta1.WorkspaceBinding{{
Name: "test-workspace",
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "myclaim",
},
}},
},
}

configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName(), Namespace: system.GetNamespace()},
Data: map[string]string{
featureFlagDisableAffinityAssistantKey: "true",
},
}

fakeClientSet := fakek8s.NewSimpleClientset(
configMap,
)

c := Reconciler{
KubeClientSet: fakeClientSet,
Images: pipeline.Images{},
}
store := config.NewStore(logtesting.TestLogger(t))
store.OnConfigChanged(configMap)

_ = c.cleanupAffinityAssistants(store.ToContext(context.Background()), testPipelineRun)

if len(fakeClientSet.Actions()) != 0 {
t.Errorf("Expected 0 k8s client requests, did %d request", len(fakeClientSet.Actions()))
}
}

func TestDisableAffinityAssistant(t *testing.T) {
for _, tc := range []struct {
description string
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (c *Reconciler) ReconcileKind(ctx context.Context, pr *v1beta1.PipelineRun)
logger.Errorf("Failed to delete PVC for PipelineRun %s: %v", pr.Name, err)
return c.finishReconcileUpdateEmitEvents(ctx, pr, before, err)
}
if err := c.cleanupAffinityAssistants(pr); err != nil {
if err := c.cleanupAffinityAssistants(ctx, pr); err != nil {
logger.Errorf("Failed to delete StatefulSet for PipelineRun %s: %v", pr.Name, err)
return c.finishReconcileUpdateEmitEvents(ctx, pr, before, err)
}
Expand Down