Fix: deepcopy before mutating shared objects

Signed-off-by: dkeven <keven@kubesphere.io>
This commit is contained in:
dkeven
2022-01-07 18:00:26 +08:00
parent e1df8bc5bb
commit d1b0beffd6
7 changed files with 81 additions and 32 deletions

View File

@@ -53,6 +53,8 @@ func (p *persistentVolumeClaimGetter) Get(namespace, name string) (runtime.Objec
if err != nil {
return pvc, err
}
// we should never mutate the shared objects from informers
pvc = pvc.DeepCopy()
p.annotatePVC(pvc)
return pvc, nil
}
@@ -65,6 +67,7 @@ func (p *persistentVolumeClaimGetter) List(namespace string, query *query.Query)
var result []runtime.Object
for _, pvc := range all {
pvc = pvc.DeepCopy()
p.annotatePVC(pvc)
result = append(result, pvc)
}

View File

@@ -37,7 +37,7 @@ var (
testStorageClassName = "test-csi"
)
func TestListPods(t *testing.T) {
func TestListPVCs(t *testing.T) {
tests := []struct {
description string
namespace string
@@ -58,7 +58,7 @@ func TestListPods(t *testing.T) {
Filters: map[query.Field]query.Value{query.FieldNamespace: query.Value("default")},
},
&api.ListResult{
Items: []interface{}{pvc3, pvc2, pvc1},
Items: []interface{}{pvc3Expected, pvc2Expected, pvc1Expected},
TotalItems: len(persistentVolumeClaims),
},
nil,
@@ -79,7 +79,7 @@ func TestListPods(t *testing.T) {
},
},
&api.ListResult{
Items: []interface{}{pvc1},
Items: []interface{}{pvc1Expected},
TotalItems: 1,
},
nil,
@@ -100,7 +100,7 @@ func TestListPods(t *testing.T) {
},
},
&api.ListResult{
Items: []interface{}{pvcGet2},
Items: []interface{}{pvc2Expected},
TotalItems: 1,
},
nil,
@@ -121,7 +121,7 @@ func TestListPods(t *testing.T) {
},
},
&api.ListResult{
Items: []interface{}{pvcGet3},
Items: []interface{}{pvc3Expected},
TotalItems: 1,
},
nil,
@@ -154,6 +154,21 @@ var (
Phase: corev1.ClaimPending,
},
}
pvc1Expected = &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc-1",
Namespace: "default",
Annotations: map[string]string{
annotationInUse: "false",
annotationAllowSnapshot: "false",
},
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimPending,
},
}
pvc2 = &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc-2",
@@ -167,7 +182,7 @@ var (
},
}
pvcGet2 = &corev1.PersistentVolumeClaim{
pvc2Expected = &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc-2",
Namespace: "default",
@@ -189,7 +204,7 @@ var (
},
}
pvcGet3 = &corev1.PersistentVolumeClaim{
pvc3Expected = &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc-3",
Namespace: "default",