Skip to content

Commit b769b56

Browse files
QuanZhang-Williamtekton-robot
authored andcommitted
[TEP-0135] Revert PVC creation
Part of [#6740] and unblocks [#6635]. `PVCs` are created if a user specifies `VolumeClaimTemplate` as the source of a `pipelinerun workspace`. Prior to this change, such `pvcs` are created via `affinity assistant statefulset` when `affinity assistant` is enabled (in both `workspaces` or `pipelineruns` coschedule mode). To delete such pvcs when the owning `pipelinerun` is completed, we must explicitly delete those pvcs in the reconciler since the owner of such pvcs is the `affinity assistant statefulset` instead of the `pipelinerun`. The problem of such deletion mechanism is that such `pvcs` are left in `terminating` state when the owning `pipelinerun` is `completed` but not `deleted`. This is because the pvcs are protected by `kubernetes.io/pvc-protection` `finalizer`, which does not allow the `pvcs` to be deleted until the `pipelinerun` consuming the `pvc` is deleted. Leaving pvcs in `terminating` state is confusing to cluster operators. Instead of changing the pvc deletion behavior in such backward incompatible way, it is better to make it configurable so that it is backward compatible, as prototyped in [#6635]. This commit reverts the pvc creation behavior `per-workspace` coschedule mode, which changes the owner of the `pvcs` back to the `pipelinerun` instead of the `affinity assistant statefulset`. After the commit, the pvcs created by specifying `VolumeClaimTemplate` are left in `bounded` state instead of `terminating`. This commit is the prerequisite of [#6635]. This commit does NOT reverts the pvc creation behavior `per-pipelinerun` coschedule mode as we will enforce the deletion of pvcs when the owning `pipelinerun` is completed. This is a better practice and there is no backward compatability concern since `per-pipelinerun` coschedule mode is a new feature. [#6740]: #6740 [#6635]: #6635
1 parent 4f6c74a commit b769b56

File tree

8 files changed

+285
-129
lines changed

8 files changed

+285
-129
lines changed

pkg/reconciler/pipelinerun/affinity_assistant.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,19 @@ import (
4141

4242
const (
4343
// ReasonCouldntCreateOrUpdateAffinityAssistantStatefulSetPerWorkspace indicates that a PipelineRun uses workspaces with PersistentVolumeClaim
44-
// as a volume source and expect an Assistant StatefulSet, but couldn't create a StatefulSet.
44+
// as a volume source and expect an Assistant StatefulSet in AffinityAssistantPerWorkspace behavior, but couldn't create a StatefulSet.
4545
ReasonCouldntCreateOrUpdateAffinityAssistantStatefulSetPerWorkspace = "ReasonCouldntCreateOrUpdateAffinityAssistantStatefulSetPerWorkspace"
4646

4747
featureFlagDisableAffinityAssistantKey = "disable-affinity-assistant"
4848
)
4949

50-
// createOrUpdateAffinityAssistantsPerAABehavior creates Affinity Assistant StatefulSets based on AffinityAssistantBehavior.
50+
// createOrUpdateAffinityAssistantsAndPVCs creates Affinity Assistant StatefulSets and PVCs based on AffinityAssistantBehavior.
5151
// This is done to achieve Node Affinity for taskruns in a pipelinerun, and make it possible for the taskruns to execute parallel while sharing volume.
5252
// If the AffinityAssitantBehavior is AffinityAssistantPerWorkspace, it creates an Affinity Assistant for
5353
// every taskrun in the pipelinerun that use the same PVC based volume.
5454
// If the AffinityAssitantBehavior is AffinityAssistantPerPipelineRun or AffinityAssistantPerPipelineRunWithIsolation,
5555
// it creates one Affinity Assistant for the pipelinerun.
56-
// Other AffinityAssitantBehaviors are invalid.
57-
func (c *Reconciler) createOrUpdateAffinityAssistantsPerAABehavior(ctx context.Context, pr *v1.PipelineRun, aaBehavior aa.AffinityAssitantBehavior) error {
56+
func (c *Reconciler) createOrUpdateAffinityAssistantsAndPVCs(ctx context.Context, pr *v1.PipelineRun, aaBehavior aa.AffinityAssitantBehavior) error {
5857
var errs []error
5958
var unschedulableNodes sets.Set[string] = nil
6059

@@ -79,26 +78,40 @@ func (c *Reconciler) createOrUpdateAffinityAssistantsPerAABehavior(ctx context.C
7978
}
8079
}
8180

81+
// create PVCs from PipelineRun's VolumeClaimTemplate when aaBehavior is AffinityAssistantPerWorkspace or AffinityAssistantDisabled before creating
82+
// affinity assistant so that the OwnerReference of the PVCs are the pipelineruns, which is used to achieve PVC auto deletion at PipelineRun deletion time
83+
if (aaBehavior == aa.AffinityAssistantPerWorkspace || aaBehavior == aa.AffinityAssistantDisabled) && pr.HasVolumeClaimTemplate() {
84+
if err := c.pvcHandler.CreatePVCsForWorkspaces(ctx, pr.Spec.Workspaces, *kmeta.NewControllerRef(pr), pr.Namespace); err != nil {
85+
return fmt.Errorf("failed to create PVC for PipelineRun %s: %w", pr.Name, err)
86+
}
87+
}
88+
8289
switch aaBehavior {
8390
case aa.AffinityAssistantPerWorkspace:
8491
for claim, workspaceName := range claimToWorkspace {
85-
aaName := getAffinityAssistantName(workspaceName, pr.Name)
92+
aaName := GetAffinityAssistantName(workspaceName, pr.Name)
8693
err := c.createOrUpdateAffinityAssistant(ctx, aaName, pr, nil, []corev1.PersistentVolumeClaimVolumeSource{*claim}, unschedulableNodes)
8794
errs = append(errs, err...)
8895
}
8996
for claimTemplate, workspaceName := range claimTemplatesToWorkspace {
90-
aaName := getAffinityAssistantName(workspaceName, pr.Name)
91-
err := c.createOrUpdateAffinityAssistant(ctx, aaName, pr, []corev1.PersistentVolumeClaim{*claimTemplate}, nil, unschedulableNodes)
97+
aaName := GetAffinityAssistantName(workspaceName, pr.Name)
98+
// To support PVC auto deletion at pipelinerun deletion time, the OwnerReference of the PVCs should be set to the owning pipelinerun.
99+
// In AffinityAssistantPerWorkspace mode, the reconciler has created PVCs (owned by pipelinerun) from pipelinerun's VolumeClaimTemplate at this point,
100+
// so the VolumeClaimTemplates are pass in as PVCs when creating affinity assistant StatefulSet for volume scheduling.
101+
// If passed in as VolumeClaimTemplates, the PVCs are owned by Affinity Assistant StatefulSet instead of the pipelinerun.
102+
err := c.createOrUpdateAffinityAssistant(ctx, aaName, pr, nil, []corev1.PersistentVolumeClaimVolumeSource{{ClaimName: claimTemplate.Name}}, unschedulableNodes)
92103
errs = append(errs, err...)
93104
}
94105
case aa.AffinityAssistantPerPipelineRun, aa.AffinityAssistantPerPipelineRunWithIsolation:
95106
if claims != nil || claimTemplates != nil {
96-
aaName := getAffinityAssistantName("", pr.Name)
107+
aaName := GetAffinityAssistantName("", pr.Name)
108+
// In AffinityAssistantPerPipelineRun or AffinityAssistantPerPipelineRunWithIsolation modes, the PVCs are created via StatefulSet for volume scheduling.
109+
// PVCs from pipelinerun's VolumeClaimTemplate are enforced to be deleted at pipelinerun completion time,
110+
// so we don't need to worry the OwnerReference of the PVCs
97111
err := c.createOrUpdateAffinityAssistant(ctx, aaName, pr, claimTemplates, claims, unschedulableNodes)
98112
errs = append(errs, err...)
99113
}
100114
case aa.AffinityAssistantDisabled:
101-
return fmt.Errorf("unexpected Affinity Assistant behavior %v", aa.AffinityAssistantDisabled)
102115
}
103116

104117
return errorutils.NewAggregate(errs)
@@ -175,18 +188,10 @@ func (c *Reconciler) cleanupAffinityAssistants(ctx context.Context, pr *v1.Pipel
175188
var errs []error
176189
for _, w := range pr.Spec.Workspaces {
177190
if w.PersistentVolumeClaim != nil || w.VolumeClaimTemplate != nil {
178-
affinityAssistantStsName := getAffinityAssistantName(w.Name, pr.Name)
191+
affinityAssistantStsName := GetAffinityAssistantName(w.Name, pr.Name)
179192
if err := c.KubeClientSet.AppsV1().StatefulSets(pr.Namespace).Delete(ctx, affinityAssistantStsName, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
180193
errs = append(errs, fmt.Errorf("failed to delete StatefulSet %s: %w", affinityAssistantStsName, err))
181194
}
182-
183-
// cleanup PVCs created by Affinity Assistants
184-
if w.VolumeClaimTemplate != nil {
185-
pvcName := getPersistentVolumeClaimNameWithAffinityAssistant(w.Name, pr.Name, w, *kmeta.NewControllerRef(pr))
186-
if err := c.KubeClientSet.CoreV1().PersistentVolumeClaims(pr.Namespace).Delete(ctx, pvcName, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
187-
errs = append(errs, fmt.Errorf("failed to delete PersistentVolumeClaim %s: %w", pvcName, err))
188-
}
189-
}
190195
}
191196
}
192197
return errorutils.NewAggregate(errs)
@@ -195,13 +200,15 @@ func (c *Reconciler) cleanupAffinityAssistants(ctx context.Context, pr *v1.Pipel
195200
// getPersistentVolumeClaimNameWithAffinityAssistant returns the PersistentVolumeClaim name that is
196201
// created by the Affinity Assistant StatefulSet VolumeClaimTemplate when Affinity Assistant is enabled.
197202
// The PVCs created by StatefulSet VolumeClaimTemplates follow the format `<pvcName>-<affinityAssistantName>-0`
203+
// TODO(#6740)(WIP): use this function when adding end-to-end support for AffinityAssistantPerPipelineRun mode
198204
func getPersistentVolumeClaimNameWithAffinityAssistant(pipelineWorkspaceName, prName string, wb v1.WorkspaceBinding, owner metav1.OwnerReference) string {
199205
pvcName := volumeclaim.GetPVCNameWithoutAffinityAssistant(wb.VolumeClaimTemplate.Name, wb, owner)
200-
affinityAssistantName := getAffinityAssistantName(pipelineWorkspaceName, prName)
206+
affinityAssistantName := GetAffinityAssistantName(pipelineWorkspaceName, prName)
201207
return fmt.Sprintf("%s-%s-0", pvcName, affinityAssistantName)
202208
}
203209

204-
func getAffinityAssistantName(pipelineWorkspaceName string, pipelineRunName string) string {
210+
// GetAffinityAssistantName returns the Affinity Assistant name based on pipelineWorkspaceName and pipelineRunName
211+
func GetAffinityAssistantName(pipelineWorkspaceName string, pipelineRunName string) string {
205212
hashBytes := sha256.Sum256([]byte(pipelineWorkspaceName + pipelineRunName))
206213
hashString := fmt.Sprintf("%x", hashBytes)
207214
return fmt.Sprintf("%s-%s", workspace.ComponentNameAffinityAssistant, hashString[:10])

pkg/reconciler/pipelinerun/affinity_assistant_test.go

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package pipelinerun
1919
import (
2020
"context"
2121
"errors"
22-
"fmt"
2322
"testing"
2423

2524
"github.com/google/go-cmp/cmp"
@@ -29,9 +28,11 @@ import (
2928
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
3029
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
3130
aa "github.com/tektoncd/pipeline/pkg/internal/affinityassistant"
31+
"github.com/tektoncd/pipeline/pkg/reconciler/volumeclaim"
3232
"github.com/tektoncd/pipeline/pkg/workspace"
3333
"github.com/tektoncd/pipeline/test/diff"
3434
"github.com/tektoncd/pipeline/test/parse"
35+
"go.uber.org/zap"
3536
appsv1 "k8s.io/api/apps/v1"
3637
corev1 "k8s.io/api/core/v1"
3738
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -168,30 +169,32 @@ func TestCreateAndDeleteOfAffinityAssistantPerPipelineRun(t *testing.T) {
168169
KubeClientSet: fakek8s.NewSimpleClientset(),
169170
}
170171

171-
err := c.createOrUpdateAffinityAssistantsPerAABehavior(ctx, tc.pr, aa.AffinityAssistantPerPipelineRun)
172+
err := c.createOrUpdateAffinityAssistantsAndPVCs(ctx, tc.pr, aa.AffinityAssistantPerPipelineRun)
172173
if err != nil {
173174
t.Errorf("unexpected error from createOrUpdateAffinityAssistantsPerPipelineRun: %v", err)
174175
}
175176

176177
// validate StatefulSets from Affinity Assistant
177-
expectAAName := getAffinityAssistantName("", tc.pr.Name)
178+
expectAAName := GetAffinityAssistantName("", tc.pr.Name)
178179
validateStatefulSetSpec(t, ctx, c, expectAAName, tc.expectStatefulSetSpec)
179180

180181
// TODO(#6740)(WIP): test cleanupAffinityAssistants for coscheduling-pipelinerun mode when fully implemented
181182
})
182183
}
183184
}
184185

185-
// TestCreateAndDeleteOfAffinityAssistantPerWorkspace tests to create and delete an Affinity Assistant
186+
// TestCreateAndDeleteOfAffinityAssistantPerWorkspaceOrDisabled tests to create and delete an Affinity Assistant
186187
// per workspace for a given PipelineRun
187-
func TestCreateAndDeleteOfAffinityAssistantPerWorkspace(t *testing.T) {
188+
func TestCreateAndDeleteOfAffinityAssistantPerWorkspaceOrDisabled(t *testing.T) {
188189
tests := []struct {
189-
name string
190+
name, expectedPVCName string
190191
pr *v1.PipelineRun
191192
expectStatefulSetSpec []*appsv1.StatefulSetSpec
193+
aaBehavior aa.AffinityAssitantBehavior
192194
}{{
193-
name: "PersistentVolumeClaim Workspace type",
194-
pr: testPRWithPVC,
195+
name: "PersistentVolumeClaim Workspace type",
196+
aaBehavior: aa.AffinityAssistantPerWorkspace,
197+
pr: testPRWithPVC,
195198
expectStatefulSetSpec: []*appsv1.StatefulSetSpec{{
196199
Template: corev1.PodTemplateSpec{
197200
Spec: corev1.PodSpec{
@@ -205,20 +208,43 @@ func TestCreateAndDeleteOfAffinityAssistantPerWorkspace(t *testing.T) {
205208
},
206209
}},
207210
}, {
208-
name: "VolumeClaimTemplate Workspace type",
209-
pr: testPRWithVolumeClaimTemplate,
211+
name: "VolumeClaimTemplate Workspace type",
212+
aaBehavior: aa.AffinityAssistantPerWorkspace,
213+
pr: testPRWithVolumeClaimTemplate,
214+
expectedPVCName: "pvc-b9eea16dce",
210215
expectStatefulSetSpec: []*appsv1.StatefulSetSpec{{
211-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{{
212-
ObjectMeta: metav1.ObjectMeta{Name: "pvc-b9eea16dce"},
213-
}},
216+
Template: corev1.PodTemplateSpec{
217+
Spec: corev1.PodSpec{
218+
Volumes: []corev1.Volume{{
219+
Name: "workspace-0",
220+
VolumeSource: corev1.VolumeSource{
221+
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: "pvc-b9eea16dce"},
222+
},
223+
}},
224+
},
225+
},
214226
}},
215227
}, {
216-
name: "VolumeClaimTemplate and PersistentVolumeClaim Workspaces",
217-
pr: testPRWithVolumeClaimTemplateAndPVC,
228+
name: "VolumeClaimTemplate Workspace type - AA disabled",
229+
aaBehavior: aa.AffinityAssistantDisabled,
230+
pr: testPRWithVolumeClaimTemplate,
231+
expectedPVCName: "pvc-b9eea16dce",
232+
}, {
233+
name: "VolumeClaimTemplate and PersistentVolumeClaim Workspaces",
234+
aaBehavior: aa.AffinityAssistantPerWorkspace,
235+
pr: testPRWithVolumeClaimTemplateAndPVC,
236+
expectedPVCName: "pvc-b9eea16dce",
218237
expectStatefulSetSpec: []*appsv1.StatefulSetSpec{{
219-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{{
220-
ObjectMeta: metav1.ObjectMeta{Name: "pvc-b9eea16dce"},
221-
}}}, {
238+
Template: corev1.PodTemplateSpec{
239+
Spec: corev1.PodSpec{
240+
Volumes: []corev1.Volume{{
241+
Name: "workspace-0",
242+
VolumeSource: corev1.VolumeSource{
243+
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: "pvc-b9eea16dce"},
244+
},
245+
}},
246+
},
247+
}}, {
222248
Template: corev1.PodTemplateSpec{
223249
Spec: corev1.PodSpec{
224250
Volumes: []corev1.Volume{{
@@ -232,6 +258,7 @@ func TestCreateAndDeleteOfAffinityAssistantPerWorkspace(t *testing.T) {
232258
}},
233259
}, {
234260
name: "other Workspace type",
261+
aaBehavior: aa.AffinityAssistantPerWorkspace,
235262
pr: testPRWithEmptyDir,
236263
expectStatefulSetSpec: nil,
237264
}}
@@ -240,23 +267,33 @@ func TestCreateAndDeleteOfAffinityAssistantPerWorkspace(t *testing.T) {
240267
tc := tc
241268
t.Run(tc.name, func(t *testing.T) {
242269
ctx := context.Background()
270+
kubeClientSet := fakek8s.NewSimpleClientset()
243271
c := Reconciler{
244-
KubeClientSet: fakek8s.NewSimpleClientset(),
272+
KubeClientSet: kubeClientSet,
273+
pvcHandler: volumeclaim.NewPVCHandler(kubeClientSet, zap.NewExample().Sugar()),
245274
}
246275

247-
err := c.createOrUpdateAffinityAssistantsPerAABehavior(ctx, tc.pr, aa.AffinityAssistantPerWorkspace)
276+
err := c.createOrUpdateAffinityAssistantsAndPVCs(ctx, tc.pr, tc.aaBehavior)
248277
if err != nil {
249-
t.Errorf("unexpected error from createOrUpdateAffinityAssistantsPerWorkspace: %v", err)
278+
t.Fatalf("unexpected error from createOrUpdateAffinityAssistantsPerWorkspace: %v", err)
250279
}
251280

252281
// validate StatefulSets from Affinity Assistant
253282
for i, w := range tc.pr.Spec.Workspaces {
254283
if tc.expectStatefulSetSpec != nil {
255-
expectAAName := getAffinityAssistantName(w.Name, tc.pr.Name)
284+
expectAAName := GetAffinityAssistantName(w.Name, tc.pr.Name)
256285
validateStatefulSetSpec(t, ctx, c, expectAAName, tc.expectStatefulSetSpec[i])
257286
}
258287
}
259288

289+
// validate PVCs from VolumeClaimTemplate
290+
if tc.expectedPVCName != "" {
291+
_, err = c.KubeClientSet.CoreV1().PersistentVolumeClaims("").Get(ctx, tc.expectedPVCName, metav1.GetOptions{})
292+
if err != nil {
293+
t.Errorf("unexpected error when retrieving PVC: %v", err)
294+
}
295+
}
296+
260297
// clean up Affinity Assistant
261298
c.cleanupAffinityAssistants(ctx, tc.pr)
262299
if err != nil {
@@ -267,7 +304,7 @@ func TestCreateAndDeleteOfAffinityAssistantPerWorkspace(t *testing.T) {
267304
continue
268305
}
269306

270-
expectAAName := getAffinityAssistantName(w.Name, tc.pr.Name)
307+
expectAAName := GetAffinityAssistantName(w.Name, tc.pr.Name)
271308
_, err = c.KubeClientSet.AppsV1().StatefulSets(tc.pr.Namespace).Get(ctx, expectAAName, metav1.GetOptions{})
272309
if !apierrors.IsNotFound(err) {
273310
t.Errorf("expected a NotFound response, got: %v", err)
@@ -277,28 +314,10 @@ func TestCreateAndDeleteOfAffinityAssistantPerWorkspace(t *testing.T) {
277314
}
278315
}
279316

280-
func TestCreateAndDeleteOfAffinityAssistantDisabled_Failure(t *testing.T) {
281-
ctx := context.Background()
282-
c := Reconciler{
283-
KubeClientSet: fakek8s.NewSimpleClientset(),
284-
}
285-
286-
wantErr := fmt.Errorf("unexpected Affinity Assistant behavior %s", aa.AffinityAssistantDisabled)
287-
288-
err := c.createOrUpdateAffinityAssistantsPerAABehavior(ctx, testPRWithPVC, aa.AffinityAssistantDisabled)
289-
if err == nil {
290-
t.Fatalf("expecting error: %v, but got nil", wantErr)
291-
}
292-
293-
if diff := cmp.Diff(wantErr.Error(), err.Error()); diff != "" {
294-
t.Errorf("expected error mismatch: %v", diff)
295-
}
296-
}
297-
298317
// TestCreateAffinityAssistantWhenNodeIsCordoned tests an existing Affinity Assistant can identify the node failure and
299318
// can migrate the affinity assistant pod to a healthy node so that the existing pipelineRun runs to competition
300319
func TestCreateOrUpdateAffinityAssistantWhenNodeIsCordoned(t *testing.T) {
301-
expectedAffinityAssistantName := getAffinityAssistantName(workspacePVCName, testPRWithPVC.Name)
320+
expectedAffinityAssistantName := GetAffinityAssistantName(workspacePVCName, testPRWithPVC.Name)
302321

303322
ss := []*appsv1.StatefulSet{{
304323
TypeMeta: metav1.TypeMeta{
@@ -398,7 +417,7 @@ func TestCreateOrUpdateAffinityAssistantWhenNodeIsCordoned(t *testing.T) {
398417
return true, &corev1.Pod{}, errors.New("error listing/deleting pod")
399418
})
400419
}
401-
err := c.createOrUpdateAffinityAssistantsPerAABehavior(ctx, testPRWithPVC, aa.AffinityAssistantPerWorkspace)
420+
err := c.createOrUpdateAffinityAssistantsAndPVCs(ctx, testPRWithPVC, aa.AffinityAssistantPerWorkspace)
402421
if !tt.expectedError && err != nil {
403422
t.Errorf("expected no error from createOrUpdateAffinityAssistantsPerWorkspace for the test \"%s\", but got: %v", tt.name, err)
404423
}
@@ -603,7 +622,7 @@ func TestThatTheAffinityAssistantIsWithoutNodeSelectorAndTolerations(t *testing.
603622
// plus 10 chars for a hash. Labels in Kubernetes can not be longer than 63 chars.
604623
// Typical output from the example below is affinity-assistant-0384086f62
605624
func TestThatAffinityAssistantNameIsNoLongerThan53(t *testing.T) {
606-
affinityAssistantName := getAffinityAssistantName(
625+
affinityAssistantName := GetAffinityAssistantName(
607626
"pipeline-workspace-name-that-is-quite-long",
608627
"pipelinerun-with-a-long-custom-name")
609628

@@ -628,7 +647,7 @@ func TestCleanupAffinityAssistants_Success(t *testing.T) {
628647
}
629648

630649
// seed data to create StatefulSets and PVCs
631-
expectedAffinityAssistantName := getAffinityAssistantName(workspace.Name, pr.Name)
650+
expectedAffinityAssistantName := GetAffinityAssistantName(workspace.Name, pr.Name)
632651
aa := []*appsv1.StatefulSet{{
633652
TypeMeta: metav1.TypeMeta{
634653
Kind: "StatefulSet",
@@ -642,14 +661,12 @@ func TestCleanupAffinityAssistants_Success(t *testing.T) {
642661
ReadyReplicas: 1,
643662
},
644663
}}
645-
646664
expectedPVCName := getPersistentVolumeClaimNameWithAffinityAssistant(workspace.Name, pr.Name, workspace, *kmeta.NewControllerRef(pr))
647665
pvc := []*corev1.PersistentVolumeClaim{{
648666
ObjectMeta: metav1.ObjectMeta{
649667
Name: expectedPVCName,
650668
}},
651669
}
652-
653670
data := Data{
654671
StatefulSets: aa,
655672
PVCs: pvc,
@@ -667,9 +684,11 @@ func TestCleanupAffinityAssistants_Success(t *testing.T) {
667684
if !apierrors.IsNotFound(err) {
668685
t.Errorf("expected a NotFound response of StatefulSet, got: %v", err)
669686
}
687+
688+
// the PVCs are NOT expected to be deleted at Affinity Assistant cleanup time
670689
_, err = c.KubeClientSet.CoreV1().PersistentVolumeClaims(pr.Namespace).Get(ctx, expectedPVCName, metav1.GetOptions{})
671-
if !apierrors.IsNotFound(err) {
672-
t.Errorf("expected a NotFound response of PersistentVolumeClaims, got: %v", err)
690+
if err != nil {
691+
t.Errorf("unexpected err when getting PersistentVolumeClaims, err: %v", err)
673692
}
674693
}
675694

@@ -692,14 +711,9 @@ func TestCleanupAffinityAssistants_Failure(t *testing.T) {
692711
func(action testing2.Action) (handled bool, ret runtime.Object, err error) {
693712
return true, &corev1.NodeList{}, errors.New("error deleting statefulsets")
694713
})
695-
c.KubeClientSet.CoreV1().(*fake.FakeCoreV1).PrependReactor("delete", "persistentvolumeclaims",
696-
func(action testing2.Action) (handled bool, ret runtime.Object, err error) {
697-
return true, &corev1.Pod{}, errors.New("error deleting persistentvolumeclaims")
698-
})
699714

700715
expectedErrs := errorutils.NewAggregate([]error{
701716
errors.New("failed to delete StatefulSet affinity-assistant-e3b0c44298: error deleting statefulsets"),
702-
errors.New("failed to delete PersistentVolumeClaim pvc-e3b0c44298-affinity-assistant-e3b0c44298-0: error deleting persistentvolumeclaims"),
703717
})
704718

705719
errs := c.cleanupAffinityAssistants(ctx, pr)

0 commit comments

Comments
 (0)