Skip to content

Commit b86f60d

Browse files
committed
prova fix
1 parent 21e4272 commit b86f60d

2 files changed

Lines changed: 95 additions & 50 deletions

File tree

operators/pkg/controller/pmp/provisioner.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package pmp
1818
import (
1919
"context"
2020
"errors"
21+
"strings"
2122

2223
"github.com/go-logr/logr"
2324
"google.golang.org/grpc/codes"
@@ -138,6 +139,9 @@ func (p *PvcMirrorProvisioner) Provision(ctx context.Context, options controller
138139
return nil, controller.ProvisioningFinished, &controller.IgnoredError{Reason: "PVC is in Lost phase"}
139140
case corev1.ClaimBound:
140141
// continue
142+
default:
143+
p.Logger.Error(errSlowRetry, strings.ReplaceAll(errPendingPVC.Error(), "pending", "no-phase"))
144+
return nil, controller.ProvisioningFinished, errPendingPVC
141145
}
142146

143147
// Check origin PV's CSI spec

operators/pkg/controller/pmp/provisioner_test.go

Lines changed: 91 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,37 @@ import (
2424
"google.golang.org/grpc/status"
2525
corev1 "k8s.io/api/core/v1"
2626
storagev1 "k8s.io/api/storage/v1"
27+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
"k8s.io/apimachinery/pkg/api/resource"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/utils/ptr"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
32+
ctrlUtil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3133
"sigs.k8s.io/sig-storage-lib-external-provisioner/v13/controller"
3234

3335
"github.com/netgroup-polito/CrownLabs/operators/pkg/controller/common"
3436
"github.com/netgroup-polito/CrownLabs/operators/pkg/controller/pmp"
3537
)
3638

39+
func UpdateObject[T interface {
40+
client.Object
41+
DeepCopy() T
42+
}](obj T) {
43+
Expect(pmprov.Client.Update(ctx, obj.DeepCopy())).To(Succeed())
44+
Expect(pmprov.Client.Status().Patch(ctx, obj, client.Merge)).To(Succeed())
45+
}
46+
47+
// func UpdatePVC(pvc *corev1.PersistentVolumeClaim, mutation func(corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim) {
48+
// var got corev1.PersistentVolumeClaim
49+
50+
// key := types.NamespacedName{Namespace: pvc.Namespace, Name: pvc.Name}
51+
// Expect(pmprov.Client.Get(ctx, key, &got)).To(Succeed())
52+
53+
// mutated := mutation(got)
54+
// Expect(pmprov.Client.Update(ctx, mutated.DeepCopy())).To(Succeed())
55+
// Expect(pmprov.Client.Status().Patch(ctx, &mutated, client.MergeFrom(&got))).To(Succeed())
56+
// }
57+
3758
// The following are unit test aimed to check the PVC Mirror Provisioner functions.
3859
var _ = Describe("The PVC Mirror Provisioner methods", func() {
3960
ns := corev1.Namespace{
@@ -43,13 +64,37 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
4364
}
4465

4566
AddObject := func(obj client.Object) {
46-
Expect(pmprov.Client.Create(ctx, obj)).To(Succeed())
67+
err := pmprov.Client.Create(ctx, obj)
68+
if apierrors.IsAlreadyExists(err) {
69+
Expect(pmprov.Client.Update(ctx, obj)).To(Succeed())
70+
} else {
71+
Expect(err).To(BeNil())
72+
}
4773
}
4874

4975
RemoveObject := func(obj client.Object) {
5076
_ = pmprov.Client.Delete(ctx, obj)
5177
}
5278

79+
RemovePVC := func(pvc *corev1.PersistentVolumeClaim) {
80+
ctrlUtil.RemoveFinalizer(pvc, "kubernetes.io/pvc-protection")
81+
UpdateObject(pvc)
82+
RemoveObject(pvc)
83+
}
84+
85+
ReplacePVC := func(pvc *corev1.PersistentVolumeClaim, mutation func(corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim) {
86+
mutated := mutation(*pvc)
87+
RemovePVC(pvc)
88+
AddObject(pvc)
89+
Expect(pmprov.Client.Status().Update(ctx, &mutated)).To(Succeed())
90+
}
91+
92+
// RemovePV := func(pv corev1.PersistentVolume) {
93+
// ctrlUtil.RemoveFinalizer(&pv, "kubernetes.io/pv-protection")
94+
// UpdateObject(&pv)
95+
// RemoveObject(&pv)
96+
// }
97+
5398
var _ = Describe("The PMP Provision method", Ordered, func() {
5499
var (
55100
originNs corev1.Namespace
@@ -148,10 +193,6 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
148193
AddObject(&originNs)
149194
})
150195

151-
AfterEach(func() {
152-
RemoveObject(&originNs)
153-
})
154-
155196
When("DataSourceRef is present", func() {
156197
BeforeEach(func() {
157198
pvOrig = corev1.PersistentVolume{
@@ -213,10 +254,11 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
213254

214255
When("the origin PVC is Bound", func() {
215256
BeforeEach(func() {
216-
pvcOrig.Status.Phase = corev1.ClaimBound
217-
pvcOrig.Spec.VolumeName = pvOrigName
218-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
219-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
257+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
258+
pvc.Spec.VolumeName = pvOrigName
259+
pvc.Status.Phase = corev1.ClaimBound
260+
return pvc
261+
})
220262
})
221263

222264
When("the origin PV has CSI spec", func() {
@@ -263,7 +305,7 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
263305
When("the origin PV does not have CSI spec", func() {
264306
BeforeEach(func() {
265307
pvOrig.Spec.CSI = nil
266-
Expect(pmprov.Client.Update(ctx, &pvOrig)).To(Succeed())
308+
UpdateObject(&pvOrig)
267309
})
268310

269311
JustBeforeEach(func() {
@@ -298,9 +340,10 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
298340

299341
When("the origin PVC is Pending", func() {
300342
BeforeEach(func() {
301-
pvcOrig.Status.Phase = corev1.ClaimPending
302-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
303-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
343+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
344+
pvc.Status.Phase = corev1.ClaimPending
345+
return pvc
346+
})
304347
})
305348

306349
JustBeforeEach(func() {
@@ -334,9 +377,10 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
334377

335378
When("the origin PVC is Lost", func() {
336379
BeforeEach(func() {
337-
pvcOrig.Status.Phase = corev1.ClaimLost
338-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
339-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
380+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
381+
pvc.Status.Phase = corev1.ClaimLost
382+
return pvc
383+
})
340384
})
341385

342386
JustBeforeEach(func() {
@@ -396,10 +440,11 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
396440

397441
When("the origin PVC is Bound", func() {
398442
BeforeEach(func() {
399-
pvcOrig.Status.Phase = corev1.ClaimBound
400-
pvcOrig.Spec.VolumeName = pvOrigName
401-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
402-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
443+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
444+
pvc.Status.Phase = corev1.ClaimBound
445+
pvc.Spec.VolumeName = pvOrigName
446+
return pvc
447+
})
403448
})
404449

405450
When("the origin PV has CSI spec", func() {
@@ -446,7 +491,7 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
446491
When("the origin PV does not have CSI spec", func() {
447492
BeforeEach(func() {
448493
pvOrig.Spec.CSI = nil
449-
Expect(pmprov.Client.Update(ctx, &pvOrig)).To(Succeed())
494+
UpdateObject(&pvOrig)
450495
})
451496

452497
JustBeforeEach(func() {
@@ -481,9 +526,10 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
481526

482527
When("the origin PVC is Pending", func() {
483528
BeforeEach(func() {
484-
pvcOrig.Status.Phase = corev1.ClaimPending
485-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
486-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
529+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
530+
pvc.Status.Phase = corev1.ClaimPending
531+
return pvc
532+
})
487533
})
488534

489535
JustBeforeEach(func() {
@@ -517,9 +563,10 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
517563

518564
When("the origin PVC is Lost", func() {
519565
BeforeEach(func() {
520-
pvcOrig.Status.Phase = corev1.ClaimLost
521-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
522-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
566+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
567+
pvc.Status.Phase = corev1.ClaimLost
568+
return pvc
569+
})
523570
})
524571

525572
JustBeforeEach(func() {
@@ -572,11 +619,12 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
572619
},
573620
},
574621
}
575-
pvcOrig.Status.Phase = corev1.ClaimBound
576-
pvcOrig.Spec.VolumeName = pvOrigName
577622
AddObject(&pvcOrig)
578-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
579-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
623+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
624+
pvc.Spec.VolumeName = pvOrigName
625+
pvc.Status.Phase = corev1.ClaimBound
626+
return pvc
627+
})
580628
})
581629

582630
JustBeforeEach(func() {
@@ -630,22 +678,23 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
630678
},
631679
},
632680
}
633-
pvcOrig.Status.Phase = corev1.ClaimBound
634-
pvcOrig.Spec.VolumeName = pvOrigName
635681
AddObject(&pvcOrig)
636-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
637-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
682+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
683+
pvc.Status.Phase = corev1.ClaimBound
684+
pvc.Spec.VolumeName = pvOrigName
685+
return pvc
686+
})
638687
// Remove label from mirror namespace
639688
targetNs.Labels = make(map[string]string)
640-
Expect(pmprov.Client.Update(ctx, &targetNs)).To(Succeed())
689+
UpdateObject(&targetNs)
641690
})
642691

643692
AfterEach(func() {
644693
// Restore label on mirror namespace
645694
targetNs.Labels = map[string]string{
646695
targetLabelKey: targetLabelVal,
647696
}
648-
Expect(pmprov.Client.Update(ctx, &targetNs)).To(Succeed())
697+
UpdateObject(&targetNs)
649698
})
650699

651700
JustBeforeEach(func() {
@@ -699,11 +748,12 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
699748
},
700749
},
701750
}
702-
pvcOrig.Status.Phase = corev1.ClaimBound
703-
pvcOrig.Spec.VolumeName = pvOrigName
704751
AddObject(&pvcOrig)
705-
Expect(pmprov.Client.Update(ctx, &pvcOrig)).To(Succeed())
706-
Expect(pmprov.Client.Status().Update(ctx, &pvcOrig)).To(Succeed())
752+
ReplacePVC(&pvcOrig, func(pvc corev1.PersistentVolumeClaim) corev1.PersistentVolumeClaim {
753+
pvc.Status.Phase = corev1.ClaimBound
754+
pvc.Spec.VolumeName = pvOrigName
755+
return pvc
756+
})
707757
})
708758

709759
JustBeforeEach(func() {
@@ -843,10 +893,6 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
843893
AddObject(&originNs)
844894
})
845895

846-
AfterEach(func() {
847-
RemoveObject(&originNs)
848-
})
849-
850896
JustBeforeEach(func() {
851897
pvcMirr = corev1.PersistentVolumeClaim{
852898
ObjectMeta: metav1.ObjectMeta{
@@ -888,10 +934,6 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
888934
AddObject(&originNs)
889935
})
890936

891-
AfterEach(func() {
892-
RemoveObject(&originNs)
893-
})
894-
895937
JustBeforeEach(func() {
896938
pvcMirr = corev1.PersistentVolumeClaim{
897939
ObjectMeta: metav1.ObjectMeta{
@@ -922,7 +964,6 @@ var _ = Describe("The PVC Mirror Provisioner methods", func() {
922964
})
923965

924966
AfterAll(func() {
925-
RemoveObject(&targetNs)
926967
RemoveObject(&sc)
927968
})
928969
})

0 commit comments

Comments
 (0)