@@ -24,6 +24,7 @@ import (
2424 "github.com/google/go-cmp/cmp"
2525 "github.com/google/go-cmp/cmp/cmpopts"
2626 "github.com/tektoncd/pipeline/pkg/apis/config"
27+ cfgtesting "github.com/tektoncd/pipeline/pkg/apis/config/testing"
2728 "github.com/tektoncd/pipeline/pkg/apis/pipeline"
2829 "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
2930 v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
@@ -178,7 +179,7 @@ func TestCreateAndDeleteOfAffinityAssistantPerPipelineRun(t *testing.T) {
178179 expectAAName := GetAffinityAssistantName ("" , tc .pr .Name )
179180 validateStatefulSetSpec (t , ctx , c , expectAAName , tc .expectStatefulSetSpec )
180181
181- // TODO(#6740)(WIP): test cleanupAffinityAssistants for coscheduling-pipelinerun mode when fully implemented
182+ // TODO(#6740)(WIP): test cleanupAffinityAssistantsAndPVCs for coscheduling-pipelinerun mode when fully implemented
182183 })
183184 }
184185}
@@ -295,9 +296,9 @@ func TestCreateAndDeleteOfAffinityAssistantPerWorkspaceOrDisabled(t *testing.T)
295296 }
296297
297298 // clean up Affinity Assistant
298- c .cleanupAffinityAssistants (ctx , tc .pr )
299+ c .cleanupAffinityAssistantsAndPVCs (ctx , tc .pr )
299300 if err != nil {
300- t .Errorf ("unexpected error from cleanupAffinityAssistants : %v" , err )
301+ t .Errorf ("unexpected error from cleanupAffinityAssistantsAndPVCs : %v" , err )
301302 }
302303 for _ , w := range tc .pr .Spec .Workspaces {
303304 if w .PersistentVolumeClaim == nil && w .VolumeClaimTemplate == nil {
@@ -630,7 +631,6 @@ func TestThatAffinityAssistantNameIsNoLongerThan53(t *testing.T) {
630631 t .Errorf ("affinity assistant name can not be longer than 53 chars" )
631632 }
632633}
633-
634634func TestCleanupAffinityAssistants_Success (t * testing.T ) {
635635 workspace := v1.WorkspaceBinding {
636636 Name : "volumeClaimTemplate workspace" ,
@@ -646,53 +646,94 @@ func TestCleanupAffinityAssistants_Success(t *testing.T) {
646646 },
647647 }
648648
649- // seed data to create StatefulSets and PVCs
650- expectedAffinityAssistantName := GetAffinityAssistantName ( workspace . Name , pr . Name )
651- aa := [] * appsv1. StatefulSet {{
652- TypeMeta : metav1. TypeMeta {
653- Kind : "StatefulSet" ,
654- APIVersion : "apps/v1 " ,
655- } ,
656- ObjectMeta : metav1. ObjectMeta {
657- Name : expectedAffinityAssistantName ,
658- Labels : getStatefulSetLabels ( pr , expectedAffinityAssistantName ) ,
649+ testCases := [] struct {
650+ name string
651+ aaBehavior aa. AffinityAssistantBehavior
652+ cfgMap map [ string ] string
653+ }{{
654+ name : "Affinity Assistant Cleanup - per workspace " ,
655+ aaBehavior : aa . AffinityAssistantPerWorkspace ,
656+ cfgMap : map [ string ] string {
657+ "disable-affinity-assistant" : "false" ,
658+ "coschedule" : "workspaces" ,
659659 },
660- Status : appsv1.StatefulSetStatus {
661- ReadyReplicas : 1 ,
660+ }, {
661+ name : "Affinity Assistant Cleanup - per pipelinerun" ,
662+ aaBehavior : aa .AffinityAssistantPerPipelineRun ,
663+ cfgMap : map [string ]string {
664+ "disable-affinity-assistant" : "true" ,
665+ "coschedule" : "pipelineruns" ,
662666 },
663667 }}
664- expectedPVCName := getPersistentVolumeClaimNameWithAffinityAssistant (workspace .Name , pr .Name , workspace , * kmeta .NewControllerRef (pr ))
665- pvc := []* corev1.PersistentVolumeClaim {{
666- ObjectMeta : metav1.ObjectMeta {
667- Name : expectedPVCName ,
668- }},
669- }
670- data := Data {
671- StatefulSets : aa ,
672- PVCs : pvc ,
673- }
674- ctx , c , _ := seedTestData (data )
675668
676- // call clean up
677- err := c .cleanupAffinityAssistants (ctx , pr )
678- if err != nil {
679- t .Fatalf ("unexpected err when clean up Affinity Assistant: %v" , err )
680- }
669+ for _ , tc := range testCases {
670+ expectedAffinityAssistantName := ""
671+ expectedPVCName := ""
672+ if tc .aaBehavior == aa .AffinityAssistantPerPipelineRun {
673+ expectedAffinityAssistantName = GetAffinityAssistantName ("" , pr .Name )
674+ expectedPVCName = getPersistentVolumeClaimNameWithAffinityAssistant ("" , pr .Name , workspace , * kmeta .NewControllerRef (pr ))
675+ } else if tc .aaBehavior == aa .AffinityAssistantPerWorkspace {
676+ expectedAffinityAssistantName = GetAffinityAssistantName (workspace .Name , pr .Name )
677+ expectedPVCName = volumeclaim .GetPVCNameWithoutAffinityAssistant ("" , workspace , * kmeta .NewControllerRef (pr ))
678+ }
681679
682- // validate the cleanup result
683- _ , err = c .KubeClientSet .AppsV1 ().StatefulSets (pr .Namespace ).Get (ctx , expectedAffinityAssistantName , metav1.GetOptions {})
684- if ! apierrors .IsNotFound (err ) {
685- t .Errorf ("expected a NotFound response of StatefulSet, got: %v" , err )
686- }
680+ // seed data to create StatefulSets and PVCs
681+ ss := []* appsv1.StatefulSet {{
682+ TypeMeta : metav1.TypeMeta {
683+ Kind : "StatefulSet" ,
684+ APIVersion : "apps/v1" ,
685+ },
686+ ObjectMeta : metav1.ObjectMeta {
687+ Name : expectedAffinityAssistantName ,
688+ Labels : getStatefulSetLabels (pr , expectedAffinityAssistantName ),
689+ },
690+ Status : appsv1.StatefulSetStatus {
691+ ReadyReplicas : 1 ,
692+ },
693+ }}
694+
695+ pvc := []* corev1.PersistentVolumeClaim {{
696+ ObjectMeta : metav1.ObjectMeta {
697+ Name : expectedPVCName ,
698+ }},
699+ }
700+ data := Data {
701+ StatefulSets : ss ,
702+ PVCs : pvc ,
703+ }
687704
688- // the PVCs are NOT expected to be deleted at Affinity Assistant cleanup time
689- _ , err = c .KubeClientSet .CoreV1 ().PersistentVolumeClaims (pr .Namespace ).Get (ctx , expectedPVCName , metav1.GetOptions {})
690- if err != nil {
691- t .Errorf ("unexpected err when getting PersistentVolumeClaims, err: %v" , err )
705+ _ , c , _ := seedTestData (data )
706+ ctx := cfgtesting .SetFeatureFlags (context .Background (), t , tc .cfgMap )
707+
708+ // call clean up
709+ err := c .cleanupAffinityAssistantsAndPVCs (ctx , pr )
710+ if err != nil {
711+ t .Fatalf ("unexpected err when clean up Affinity Assistant: %v" , err )
712+ }
713+
714+ // validate the cleanup result
715+ _ , err = c .KubeClientSet .AppsV1 ().StatefulSets (pr .Namespace ).Get (ctx , expectedAffinityAssistantName , metav1.GetOptions {})
716+ if ! apierrors .IsNotFound (err ) {
717+ t .Errorf ("expected a NotFound response of StatefulSet, got: %v" , err )
718+ }
719+
720+ // validate pvcs
721+ if tc .aaBehavior == aa .AffinityAssistantPerWorkspace {
722+ // the PVCs are NOT expected to be deleted at Affinity Assistant cleanup time in AffinityAssistantPerWorkspace mode
723+ _ , err = c .KubeClientSet .CoreV1 ().PersistentVolumeClaims (pr .Namespace ).Get (ctx , expectedPVCName , metav1.GetOptions {})
724+ if err != nil {
725+ t .Errorf ("unexpected err when getting PersistentVolumeClaims, err: %v" , err )
726+ }
727+ } else {
728+ _ , err = c .KubeClientSet .CoreV1 ().PersistentVolumeClaims (pr .Namespace ).Get (ctx , expectedPVCName , metav1.GetOptions {})
729+ if ! apierrors .IsNotFound (err ) {
730+ t .Errorf ("failed to clean up PersistentVolumeClaim" )
731+ }
732+ }
692733 }
693734}
694735
695- func TestCleanupAffinityAssistants_Failure (t * testing.T ) {
736+ func TestCleanupAffinityAssistantsAndPVCs_Failure (t * testing.T ) {
696737 pr := & v1.PipelineRun {
697738 Spec : v1.PipelineRunSpec {
698739 Workspaces : []v1.WorkspaceBinding {{
@@ -716,7 +757,7 @@ func TestCleanupAffinityAssistants_Failure(t *testing.T) {
716757 errors .New ("failed to delete StatefulSet affinity-assistant-e3b0c44298: error deleting statefulsets" ),
717758 })
718759
719- errs := c .cleanupAffinityAssistants (ctx , pr )
760+ errs := c .cleanupAffinityAssistantsAndPVCs (ctx , pr )
720761 if errs == nil {
721762 t .Fatalf ("expecting Affinity Assistant cleanup error but got nil" )
722763 }
@@ -747,7 +788,7 @@ func TestThatCleanupIsAvoidedIfAssistantIsDisabled(t *testing.T) {
747788 store := config .NewStore (logtesting .TestLogger (t ))
748789 store .OnConfigChanged (configMap )
749790
750- _ = c .cleanupAffinityAssistants (store .ToContext (context .Background ()), testPRWithPVC )
791+ _ = c .cleanupAffinityAssistantsAndPVCs (store .ToContext (context .Background ()), testPRWithPVC )
751792
752793 if len (fakeClientSet .Actions ()) != 0 {
753794 t .Errorf ("Expected 0 k8s client requests, did %d request" , len (fakeClientSet .Actions ()))
@@ -958,8 +999,10 @@ func seedTestData(d Data) (context.Context, Reconciler, func()) {
958999 ctx := context .Background ()
9591000 ctx , cancel := context .WithCancel (ctx )
9601001
1002+ kubeClientSet := fakek8s .NewSimpleClientset ()
9611003 c := Reconciler {
962- KubeClientSet : fakek8s .NewSimpleClientset (),
1004+ KubeClientSet : kubeClientSet ,
1005+ pvcHandler : volumeclaim .NewPVCHandler (kubeClientSet , zap .NewExample ().Sugar ()),
9631006 }
9641007 for _ , s := range d .StatefulSets {
9651008 c .KubeClientSet .AppsV1 ().StatefulSets (s .Namespace ).Create (ctx , s , metav1.CreateOptions {})
0 commit comments