Skip to content

Commit 9efb249

Browse files
authored
Merge pull request #57 from gnufied/make-claimref-fix
Verify claimref associated with PVs before resizing
2 parents e964992 + 70a38b2 commit 9efb249

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

pkg/controller/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ func (ctrl *resizeController) pvNeedResize(pvc *v1.PersistentVolumeClaim, pv *v1
283283
return false
284284
}
285285

286+
if (pv.Spec.ClaimRef == nil) || (pvc.Namespace != pv.Spec.ClaimRef.Namespace) || (pvc.UID != pv.Spec.ClaimRef.UID) {
287+
klog.V(4).Infof("persistent volume is not bound to PVC being updated: %s", util.PVCKey(pvc))
288+
return false
289+
}
290+
286291
pvSize := pv.Spec.Capacity[v1.ResourceStorage]
287292
requestSize := pvc.Spec.Resources.Requests[v1.ResourceStorage]
288293
if pvSize.Cmp(requestSize) >= 0 {

pkg/controller/controller_test.go

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"k8s.io/apimachinery/pkg/api/resource"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
"k8s.io/apimachinery/pkg/runtime"
16+
"k8s.io/apimachinery/pkg/types"
1617
"k8s.io/client-go/informers"
1718
"k8s.io/client-go/kubernetes"
1819
"k8s.io/client-go/kubernetes/fake"
@@ -26,43 +27,62 @@ func TestController(t *testing.T) {
2627

2728
CreateObjects bool
2829
NodeResize bool
30+
CallCSIExpand bool
2931
}{
3032
{
31-
Name: "Invalid key",
32-
PVC: invalidPVC(),
33+
Name: "Invalid key",
34+
PVC: invalidPVC(),
35+
CallCSIExpand: false,
3336
},
3437
{
35-
Name: "PVC not found",
36-
PVC: createPVC(1, 1),
38+
Name: "PVC not found",
39+
PVC: createPVC(1, 1),
40+
CallCSIExpand: false,
3741
},
3842
{
3943
Name: "PVC doesn't need resize",
4044
PVC: createPVC(1, 1),
4145
CreateObjects: true,
46+
CallCSIExpand: false,
4247
},
4348
{
4449
Name: "PV not found",
4550
PVC: createPVC(2, 1),
4651
CreateObjects: true,
52+
CallCSIExpand: false,
4753
},
4854
{
49-
Name: "PV doesn't need resize",
55+
Name: "pv claimref does not have pvc UID",
5056
PVC: createPVC(2, 1),
51-
PV: createPV(2),
52-
CreateObjects: true,
57+
PV: createPV(1, "testPVC" /*pvcName*/, "test" /*pvcNamespace*/, "foobaz" /*pvcUID*/),
58+
CallCSIExpand: false,
59+
},
60+
{
61+
Name: "pv claimref does not have PVC namespace",
62+
PVC: createPVC(2, 1),
63+
PV: createPV(1, "testPVC" /*pvcName*/, "test1" /*pvcNamespace*/, "foobar" /*pvcUID*/),
64+
CallCSIExpand: false,
65+
},
66+
{
67+
Name: "pv claimref is nil",
68+
PVC: createPVC(2, 1),
69+
PV: createPV(1, "" /*pvcName*/, "test1" /*pvcNamespace*/, "foobar" /*pvcUID*/),
70+
CallCSIExpand: false,
5371
},
5472
{
5573
Name: "Resize PVC, no FS resize",
5674
PVC: createPVC(2, 1),
57-
PV: createPV(1),
75+
PV: createPV(1, "testPVC", "test", "foobar"),
5876
CreateObjects: true,
77+
CallCSIExpand: true,
5978
},
6079
{
6180
Name: "Resize PVC with FS resize",
6281
PVC: createPVC(2, 1),
63-
PV: createPV(1),
82+
PV: createPV(1, "testPVC", "test", "foobar"),
6483
CreateObjects: true,
6584
NodeResize: true,
85+
CallCSIExpand: true,
6686
},
6787
} {
6888
client := csi.NewMockClient("mock", test.NodeResize, true, true)
@@ -104,6 +124,15 @@ func TestController(t *testing.T) {
104124
if err != nil {
105125
t.Fatalf("Test %s: Unexpected error: %v", test.Name, err)
106126
}
127+
128+
expandCallCount := client.GetExpandCount()
129+
if test.CallCSIExpand && expandCallCount == 0 {
130+
t.Fatalf("for %s: expected csi expand call, no csi expand call was made", test.Name)
131+
}
132+
133+
if !test.CallCSIExpand && expandCallCount > 0 {
134+
t.Fatalf("for %s: expected no csi expand call, received csi expansion request", test.Name)
135+
}
107136
}
108137
}
109138

@@ -128,6 +157,7 @@ func createPVC(requestGB, capacityGB int) *v1.PersistentVolumeClaim {
128157
ObjectMeta: metav1.ObjectMeta{
129158
Name: "testPVC",
130159
Namespace: "test",
160+
UID: "foobar",
131161
},
132162
Spec: v1.PersistentVolumeClaimSpec{
133163
Resources: v1.ResourceRequirements{
@@ -146,10 +176,10 @@ func createPVC(requestGB, capacityGB int) *v1.PersistentVolumeClaim {
146176
}
147177
}
148178

149-
func createPV(capacityGB int) *v1.PersistentVolume {
179+
func createPV(capacityGB int, pvcName, pvcNamespace string, pvcUID types.UID) *v1.PersistentVolume {
150180
capacity := quantityGB(capacityGB)
151181

152-
return &v1.PersistentVolume{
182+
pv := &v1.PersistentVolume{
153183
ObjectMeta: metav1.ObjectMeta{
154184
Name: "testPV",
155185
},
@@ -165,6 +195,14 @@ func createPV(capacityGB int) *v1.PersistentVolume {
165195
},
166196
},
167197
}
198+
if len(pvcName) > 0 {
199+
pv.Spec.ClaimRef = &v1.ObjectReference{
200+
Namespace: pvcNamespace,
201+
Name: pvcName,
202+
UID: pvcUID,
203+
}
204+
}
205+
return pv
168206
}
169207

170208
func fakeK8s(objs []runtime.Object) (kubernetes.Interface, informers.SharedInformerFactory) {

pkg/csi/mock_client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func NewMockClient(
1111
name: name,
1212
supportsNodeResize: supportsNodeResize,
1313
supportsControllerResize: supportsControllerResize,
14+
expandCalled: 0,
1415
supportsPluginControllerService: supportsPluginControllerService,
1516
}
1617
}
@@ -20,6 +21,7 @@ type MockClient struct {
2021
supportsNodeResize bool
2122
supportsControllerResize bool
2223
supportsPluginControllerService bool
24+
expandCalled int
2325
usedSecrets map[string]string
2426
}
2527

@@ -45,10 +47,15 @@ func (c *MockClient) Expand(
4547
requestBytes int64,
4648
secrets map[string]string) (int64, bool, error) {
4749
// TODO: Determine whether the operation succeeds or fails by parameters.
50+
c.expandCalled++
4851
c.usedSecrets = secrets
4952
return requestBytes, c.supportsNodeResize, nil
5053
}
5154

55+
func (c *MockClient) GetExpandCount() int {
56+
return c.expandCalled
57+
}
58+
5259
// GetSecrets returns secrets used for volume expansion
5360
func (c *MockClient) GetSecrets() map[string]string {
5461
return c.usedSecrets

0 commit comments

Comments
 (0)