diff --git a/controllers/redis_controller_test.go b/controllers/redis_controller_test.go index 3b9ff9870..dbc90cffc 100644 --- a/controllers/redis_controller_test.go +++ b/controllers/redis_controller_test.go @@ -3,7 +3,6 @@ package controllers import ( "context" "fmt" - "time" appsv1 "k8s.io/api/apps/v1" @@ -17,13 +16,6 @@ import ( "k8s.io/apimachinery/pkg/types" ) -const ( - ns = "default" - - timeout = time.Second * 5 - interval = time.Millisecond * 250 -) - var _ = Describe("Redis standalone test", func() { var ( redisCR redisv1beta2.Redis diff --git a/controllers/rediscluster_controller_test.go b/controllers/rediscluster_controller_test.go new file mode 100644 index 000000000..9a3c7aed3 --- /dev/null +++ b/controllers/rediscluster_controller_test.go @@ -0,0 +1,145 @@ +package controllers + +import ( + "context" + "fmt" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + + redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +var _ = Describe("Redis cluster test", func() { + var ( + redisClusterCR redisv1beta2.RedisCluster + redisClusterCRName string + size int32 + // version string + // Used to create unique name for each test + testCount int + ) + + JustBeforeEach(func() { + size = 3 + // version = "v7" + redisClusterCR = redisv1beta2.RedisCluster{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "redis.redis.opstreelabs.in/v1beta2", + Kind: "RedisCluster", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: redisClusterCRName, + Namespace: ns, + }, + Spec: redisv1beta2.RedisClusterSpec{ + Size: &size, + // ClusterVersion: &version, + Storage: &redisv1beta2.ClusterStorage{}, + }, + } + Expect(k8sClient.Create(context.TODO(), &redisClusterCR)).Should(Succeed()) + testCount++ + }) + + BeforeEach(func() { + redisClusterCRName = fmt.Sprintf("redis-cluster-%d", testCount) + }) + + Context("When creating a redis cluster CR", func() { + It("should create a statefulset", func() { + + sts := &appsv1.StatefulSet{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisClusterCRName + "-leader", + Namespace: ns, + }, sts) + }, timeout, interval).Should(BeNil()) + + Expect(sts.Labels).To(Equal(map[string]string{ + "app": redisClusterCRName + "-leader", + "redis_setup_type": "cluster", + "role": "leader", + })) + + Expect(*sts.Spec.Replicas).To(BeEquivalentTo(3)) + Expect(sts.Spec.ServiceName).To(Equal(redisClusterCRName + "-leader-headless")) + }) + + It("should create a service", func() { + svc := &corev1.Service{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisClusterCRName + "-leader", + Namespace: ns, + }, svc) + }, timeout, interval).Should(BeNil()) + + Expect(svc.Labels).To(Equal(map[string]string{ + "app": redisClusterCRName + "-leader", + "redis_setup_type": "cluster", + "role": "leader", + })) + }) + + It("should create a headless service", func() { + svc := &corev1.Service{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisClusterCRName + "-leader-headless", + Namespace: ns, + }, svc) + }, timeout, interval).Should(BeNil()) + + Expect(svc.Labels).To(Equal(map[string]string{ + "app": redisClusterCRName + "-leader", + "redis_setup_type": "cluster", + "role": "leader", + })) + }) + + It("should create additional service", func() { + svc := &corev1.Service{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisClusterCRName + "-leader-additional", + Namespace: ns, + }, svc) + }, timeout, interval).Should(BeNil()) + + Expect(svc.Labels).To(Equal(map[string]string{ + "app": redisClusterCRName + "-leader", + "redis_setup_type": "cluster", + "role": "leader", + })) + }) + + Context("then deleting the redis cluster CR", func() { + It("should delete the statefulset", func() { + redisClusterCR := &redisv1beta2.RedisCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: redisClusterCRName, + Namespace: ns, + }, + } + Expect(k8sClient.Delete(context.TODO(), redisClusterCR)).To(BeNil()) + + Eventually(func() bool { + sts := &appsv1.StatefulSet{} + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisClusterCRName, + Namespace: ns, + }, sts) + return errors.IsNotFound(err) + }, timeout, interval).Should(BeTrue()) + }) + }) + }) +}) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 3bc2b13c7..150022568 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -27,6 +27,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" + "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" @@ -41,6 +42,13 @@ import ( var k8sClient client.Client var testEnv *envtest.Environment +const ( + ns = "default" + + timeout = time.Second * 10 + interval = time.Millisecond * 250 +) + func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) @@ -83,10 +91,22 @@ var _ = BeforeSuite(func() { k8sClient, err := kubernetes.NewForConfig(cfg) Expect(err).ToNot(HaveOccurred()) + dk8sClient, err := dynamic.NewForConfig(cfg) + Expect(err).ToNot(HaveOccurred()) + err = (&RedisReconciler{ - Client: k8sManager.GetClient(), - K8sClient: k8sClient, - Scheme: k8sManager.GetScheme(), + Client: k8sManager.GetClient(), + K8sClient: k8sClient, + Dk8sClient: dk8sClient, + Scheme: k8sManager.GetScheme(), + }).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + + err = (&RedisClusterReconciler{ + Client: k8sManager.GetClient(), + K8sClient: k8sClient, + Dk8sClient: dk8sClient, + Scheme: k8sManager.GetScheme(), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/k8sutils/redis-cluster.go b/k8sutils/redis-cluster.go index cfa2748d2..ae6a26247 100644 --- a/k8sutils/redis-cluster.go +++ b/k8sutils/redis-cluster.go @@ -1,6 +1,7 @@ package k8sutils import ( + "fmt" "strconv" "strings" @@ -34,6 +35,7 @@ type RedisClusterService struct { // generateRedisClusterParams generates Redis cluster information func generateRedisClusterParams(cr *redisv1beta2.RedisCluster, replicas int32, externalConfig *string, params RedisClusterSTS) statefulSetParameters { + fmt.Println(cr.Spec.Storage.NodeConfVolume) res := statefulSetParameters{ Replicas: &replicas, ClusterMode: true,