Skip to content

Commit

Permalink
Update condition to select right pvc as child for statefulset (#550)
Browse files Browse the repository at this point in the history
* Update if condition to select right pvc as child for statefulset

Signed-off-by: hanzala <muhammad.hanzala@waltlabs.io>

* fix indentation

Signed-off-by: hanzala <muhammad.hanzala@waltlabs.io>

* test(cache): Add tests for isStatefulSetChild function

* test(pkg/cache): Replace JSON unmarshalling with structured approach in tests

---------

Signed-off-by: hanzala <muhammad.hanzala@waltlabs.io>
Co-authored-by: hanzala <muhammad.hanzala@waltlabs.io>
Co-authored-by: Obinna Odirionye <odirionye@gmail.com>
  • Loading branch information
3 people committed May 14, 2024
1 parent 0aecd43 commit 8a3ce6d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pkg/cache/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package cache
import (
"encoding/json"
"fmt"
"strings"

"regexp"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -75,7 +74,7 @@ func isStatefulSetChild(un *unstructured.Unstructured) (func(kube.ResourceKey) b
return func(key kube.ResourceKey) bool {
if key.Kind == kube.PersistentVolumeClaimKind && key.GroupKind().Group == "" {
for _, templ := range templates {
if strings.HasPrefix(key.Name, fmt.Sprintf("%s-%s-", templ.Name, un.GetName())) {
if match, _ := regexp.MatchString(fmt.Sprintf(`%s-%s-\d+$`, templ.Name, un.GetName()), key.Name); match {
return true
}
}
Expand Down
105 changes: 105 additions & 0 deletions pkg/cache/references_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cache

import (
"github.com/argoproj/gitops-engine/pkg/utils/kube"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"testing"
)

func Test_isStatefulSetChild(t *testing.T) {
type args struct {
un *unstructured.Unstructured
}

statefulSet := &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "StatefulSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "sw-broker",
},
Spec: appsv1.StatefulSetSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: "emqx-data",
},
},
},
},
}

// Create a new unstructured object from the JSON string
un, err := kube.ToUnstructured(statefulSet)
if err != nil {
t.Errorf("Failed to convert StatefulSet to unstructured: %v", err)
}

tests := []struct {
name string
args args
wantErr bool
checkFunc func(func(kube.ResourceKey) bool) bool
}{
{
name: "Valid PVC for sw-broker",
args: args{un: un},
wantErr: false,
checkFunc: func(fn func(kube.ResourceKey) bool) bool {
// Check a valid PVC name for "sw-broker"
return fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-0"})
},
},
{
name: "Invalid PVC for sw-broker",
args: args{un: un},
wantErr: false,
checkFunc: func(fn func(kube.ResourceKey) bool) bool {
// Check an invalid PVC name that should belong to "sw-broker-internal"
return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-internal-0"})
},
},
{
name: "Mismatch PVC for sw-broker",
args: args{un: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "StatefulSet",
"metadata": map[string]interface{}{
"name": "sw-broker",
},
"spec": map[string]interface{}{
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
"name": "volume-2",
},
},
},
},
},
}},
wantErr: false,
checkFunc: func(fn func(kube.ResourceKey) bool) bool {
// Check an invalid PVC name for "api-test"
return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "volume-2"})
},
},
}

// Execute test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := isStatefulSetChild(tt.args.un)
assert.Equal(t, tt.wantErr, err != nil, "isStatefulSetChild() error = %v, wantErr %v", err, tt.wantErr)
if err == nil {
assert.True(t, tt.checkFunc(got), "Check function failed for %v", tt.name)
}
})
}
}

0 comments on commit 8a3ce6d

Please sign in to comment.