Skip to content

Commit 8a3ce6d

Browse files
hanzala1234hanzala-waltlabsnerdeveloper
authored
Update condition to select right pvc as child for statefulset (#550)
* Update if condition to select right pvc as child for statefulset Signed-off-by: hanzala <[email protected]> * fix indentation Signed-off-by: hanzala <[email protected]> * test(cache): Add tests for isStatefulSetChild function * test(pkg/cache): Replace JSON unmarshalling with structured approach in tests --------- Signed-off-by: hanzala <[email protected]> Co-authored-by: hanzala <[email protected]> Co-authored-by: Obinna Odirionye <[email protected]>
1 parent 0aecd43 commit 8a3ce6d

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

Diff for: pkg/cache/references.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package cache
33
import (
44
"encoding/json"
55
"fmt"
6-
"strings"
7-
6+
"regexp"
87
v1 "k8s.io/api/apps/v1"
98
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -75,7 +74,7 @@ func isStatefulSetChild(un *unstructured.Unstructured) (func(kube.ResourceKey) b
7574
return func(key kube.ResourceKey) bool {
7675
if key.Kind == kube.PersistentVolumeClaimKind && key.GroupKind().Group == "" {
7776
for _, templ := range templates {
78-
if strings.HasPrefix(key.Name, fmt.Sprintf("%s-%s-", templ.Name, un.GetName())) {
77+
if match, _ := regexp.MatchString(fmt.Sprintf(`%s-%s-\d+$`, templ.Name, un.GetName()), key.Name); match {
7978
return true
8079
}
8180
}

Diff for: pkg/cache/references_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package cache
2+
3+
import (
4+
"github.com/argoproj/gitops-engine/pkg/utils/kube"
5+
"github.com/stretchr/testify/assert"
6+
appsv1 "k8s.io/api/apps/v1"
7+
corev1 "k8s.io/api/core/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"testing"
11+
)
12+
13+
func Test_isStatefulSetChild(t *testing.T) {
14+
type args struct {
15+
un *unstructured.Unstructured
16+
}
17+
18+
statefulSet := &appsv1.StatefulSet{
19+
TypeMeta: metav1.TypeMeta{
20+
APIVersion: "apps/v1",
21+
Kind: "StatefulSet",
22+
},
23+
ObjectMeta: metav1.ObjectMeta{
24+
Name: "sw-broker",
25+
},
26+
Spec: appsv1.StatefulSetSpec{
27+
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
28+
{
29+
ObjectMeta: metav1.ObjectMeta{
30+
Name: "emqx-data",
31+
},
32+
},
33+
},
34+
},
35+
}
36+
37+
// Create a new unstructured object from the JSON string
38+
un, err := kube.ToUnstructured(statefulSet)
39+
if err != nil {
40+
t.Errorf("Failed to convert StatefulSet to unstructured: %v", err)
41+
}
42+
43+
tests := []struct {
44+
name string
45+
args args
46+
wantErr bool
47+
checkFunc func(func(kube.ResourceKey) bool) bool
48+
}{
49+
{
50+
name: "Valid PVC for sw-broker",
51+
args: args{un: un},
52+
wantErr: false,
53+
checkFunc: func(fn func(kube.ResourceKey) bool) bool {
54+
// Check a valid PVC name for "sw-broker"
55+
return fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-0"})
56+
},
57+
},
58+
{
59+
name: "Invalid PVC for sw-broker",
60+
args: args{un: un},
61+
wantErr: false,
62+
checkFunc: func(fn func(kube.ResourceKey) bool) bool {
63+
// Check an invalid PVC name that should belong to "sw-broker-internal"
64+
return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-internal-0"})
65+
},
66+
},
67+
{
68+
name: "Mismatch PVC for sw-broker",
69+
args: args{un: &unstructured.Unstructured{
70+
Object: map[string]interface{}{
71+
"apiVersion": "apps/v1",
72+
"kind": "StatefulSet",
73+
"metadata": map[string]interface{}{
74+
"name": "sw-broker",
75+
},
76+
"spec": map[string]interface{}{
77+
"volumeClaimTemplates": []interface{}{
78+
map[string]interface{}{
79+
"metadata": map[string]interface{}{
80+
"name": "volume-2",
81+
},
82+
},
83+
},
84+
},
85+
},
86+
}},
87+
wantErr: false,
88+
checkFunc: func(fn func(kube.ResourceKey) bool) bool {
89+
// Check an invalid PVC name for "api-test"
90+
return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "volume-2"})
91+
},
92+
},
93+
}
94+
95+
// Execute test cases
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
got, err := isStatefulSetChild(tt.args.un)
99+
assert.Equal(t, tt.wantErr, err != nil, "isStatefulSetChild() error = %v, wantErr %v", err, tt.wantErr)
100+
if err == nil {
101+
assert.True(t, tt.checkFunc(got), "Check function failed for %v", tt.name)
102+
}
103+
})
104+
}
105+
}

0 commit comments

Comments
 (0)