diff --git a/controllers/redis_controller.go b/controllers/redis_controller.go index ee7224ade..4cfb43601 100644 --- a/controllers/redis_controller.go +++ b/controllers/redis_controller.go @@ -65,7 +65,7 @@ func (r *RedisReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl return ctrl.Result{}, err } - err = k8sutils.CreateStandaloneRedis(instance) + err = k8sutils.CreateStandaloneRedis(instance, r.Client) if err != nil { return ctrl.Result{}, err } diff --git a/controllers/rediscluster_controller.go b/controllers/rediscluster_controller.go index d49230b51..beb409f50 100644 --- a/controllers/rediscluster_controller.go +++ b/controllers/rediscluster_controller.go @@ -117,12 +117,12 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request } if leaderReplicas != 0 { - err = k8sutils.CreateRedisLeaderService(instance) + err = k8sutils.CreateRedisLeaderService(instance, r.Client) if err != nil { return ctrl.Result{}, err } } - err = k8sutils.CreateRedisLeader(instance) + err = k8sutils.CreateRedisLeader(instance, r.Client) if err != nil { return ctrl.Result{}, err } @@ -151,12 +151,12 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request } // if we have followers create their service. if followerReplicas != 0 { - err = k8sutils.CreateRedisFollowerService(instance) + err = k8sutils.CreateRedisFollowerService(instance, r.Client) if err != nil { return ctrl.Result{}, err } } - err = k8sutils.CreateRedisFollower(instance) + err = k8sutils.CreateRedisFollower(instance, r.Client) if err != nil { return ctrl.Result{}, err } diff --git a/controllers/redisreplication_controller.go b/controllers/redisreplication_controller.go index c424c85d5..8bd716cb7 100644 --- a/controllers/redisreplication_controller.go +++ b/controllers/redisreplication_controller.go @@ -56,7 +56,7 @@ func (r *RedisReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req return ctrl.Result{}, err } - err = k8sutils.CreateReplicationRedis(instance) + err = k8sutils.CreateReplicationRedis(instance, r.Client) if err != nil { return ctrl.Result{}, err } diff --git a/controllers/redissentinel_controller.go b/controllers/redissentinel_controller.go index 3a88dcc74..d88daca2b 100644 --- a/controllers/redissentinel_controller.go +++ b/controllers/redissentinel_controller.go @@ -55,7 +55,7 @@ func (r *RedisSentinelReconciler) Reconcile(ctx context.Context, req ctrl.Reques } // Create Redis Sentinel - err = k8sutils.CreateRedisSentinel(ctx, r.K8sClient, r.Log, instance) + err = k8sutils.CreateRedisSentinel(ctx, r.K8sClient, r.Log, instance, r.Client) if err != nil { return ctrl.Result{}, err } diff --git a/k8sutils/redis-cluster.go b/k8sutils/redis-cluster.go index 92bd9f64c..4fd44c716 100644 --- a/k8sutils/redis-cluster.go +++ b/k8sutils/redis-cluster.go @@ -5,6 +5,7 @@ import ( "strings" "k8s.io/apimachinery/pkg/util/intstr" + "sigs.k8s.io/controller-runtime/pkg/client" commonapi "github.com/OT-CONTAINER-KIT/redis-operator/api" redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" @@ -205,7 +206,7 @@ func generateRedisClusterContainerParams(cr *redisv1beta2.RedisCluster, security } // CreateRedisLeader will create a leader redis setup -func CreateRedisLeader(cr *redisv1beta2.RedisCluster) error { +func CreateRedisLeader(cr *redisv1beta2.RedisCluster, cl client.Client) error { prop := RedisClusterSTS{ RedisStateFulType: "leader", SecurityContext: cr.Spec.RedisLeader.SecurityContext, @@ -219,11 +220,11 @@ func CreateRedisLeader(cr *redisv1beta2.RedisCluster) error { if cr.Spec.RedisLeader.RedisConfig != nil { prop.ExternalConfig = cr.Spec.RedisLeader.RedisConfig.AdditionalRedisConfig } - return prop.CreateRedisClusterSetup(cr) + return prop.CreateRedisClusterSetup(cr, cl) } // CreateRedisFollower will create a follower redis setup -func CreateRedisFollower(cr *redisv1beta2.RedisCluster) error { +func CreateRedisFollower(cr *redisv1beta2.RedisCluster, cl client.Client) error { prop := RedisClusterSTS{ RedisStateFulType: "follower", SecurityContext: cr.Spec.RedisFollower.SecurityContext, @@ -237,23 +238,23 @@ func CreateRedisFollower(cr *redisv1beta2.RedisCluster) error { if cr.Spec.RedisFollower.RedisConfig != nil { prop.ExternalConfig = cr.Spec.RedisFollower.RedisConfig.AdditionalRedisConfig } - return prop.CreateRedisClusterSetup(cr) + return prop.CreateRedisClusterSetup(cr, cl) } // CreateRedisLeaderService method will create service for Redis Leader -func CreateRedisLeaderService(cr *redisv1beta2.RedisCluster) error { +func CreateRedisLeaderService(cr *redisv1beta2.RedisCluster, cl client.Client) error { prop := RedisClusterService{ RedisServiceRole: "leader", } - return prop.CreateRedisClusterService(cr) + return prop.CreateRedisClusterService(cr, cl) } // CreateRedisFollowerService method will create service for Redis Follower -func CreateRedisFollowerService(cr *redisv1beta2.RedisCluster) error { +func CreateRedisFollowerService(cr *redisv1beta2.RedisCluster, cl client.Client) error { prop := RedisClusterService{ RedisServiceRole: "follower", } - return prop.CreateRedisClusterService(cr) + return prop.CreateRedisClusterService(cr, cl) } func (service RedisClusterSTS) getReplicaCount(cr *redisv1beta2.RedisCluster) int32 { @@ -261,7 +262,7 @@ func (service RedisClusterSTS) getReplicaCount(cr *redisv1beta2.RedisCluster) in } // CreateRedisClusterSetup will create Redis Setup for leader and follower -func (service RedisClusterSTS) CreateRedisClusterSetup(cr *redisv1beta2.RedisCluster) error { +func (service RedisClusterSTS) CreateRedisClusterSetup(cr *redisv1beta2.RedisCluster, cl client.Client) error { stateFulName := cr.ObjectMeta.Name + "-" + service.RedisStateFulType logger := statefulSetLogger(cr.Namespace, stateFulName) labels := getRedisLabels(stateFulName, cluster, service.RedisStateFulType, cr.ObjectMeta.Labels) @@ -275,6 +276,7 @@ func (service RedisClusterSTS) CreateRedisClusterSetup(cr *redisv1beta2.RedisClu generateRedisClusterInitContainerParams(cr), generateRedisClusterContainerParams(cr, service.SecurityContext, service.ReadinessProbe, service.LivenessProbe, service.RedisStateFulType), cr.Spec.Sidecars, + cl, ) if err != nil { logger.Error(err, "Cannot create statefulset for Redis", "Setup.Type", service.RedisStateFulType) @@ -284,7 +286,7 @@ func (service RedisClusterSTS) CreateRedisClusterSetup(cr *redisv1beta2.RedisClu } // CreateRedisClusterService method will create service for Redis -func (service RedisClusterService) CreateRedisClusterService(cr *redisv1beta2.RedisCluster) error { +func (service RedisClusterService) CreateRedisClusterService(cr *redisv1beta2.RedisCluster, cl client.Client) error { serviceName := cr.ObjectMeta.Name + "-" + service.RedisServiceRole logger := serviceLogger(cr.Namespace, serviceName) labels := getRedisLabels(serviceName, cluster, service.RedisServiceRole, cr.ObjectMeta.Labels) diff --git a/k8sutils/redis-replication.go b/k8sutils/redis-replication.go index a0f0c7fd9..f240c7d2a 100644 --- a/k8sutils/redis-replication.go +++ b/k8sutils/redis-replication.go @@ -4,6 +4,7 @@ import ( redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/util" "k8s.io/utils/pointer" + "sigs.k8s.io/controller-runtime/pkg/client" ) // CreateReplicationService method will create replication service for Redis @@ -50,7 +51,7 @@ func CreateReplicationService(cr *redisv1beta2.RedisReplication) error { } // CreateReplicationRedis will create a replication redis setup -func CreateReplicationRedis(cr *redisv1beta2.RedisReplication) error { +func CreateReplicationRedis(cr *redisv1beta2.RedisReplication, cl client.Client) error { stateFulName := cr.ObjectMeta.Name logger := statefulSetLogger(cr.Namespace, cr.ObjectMeta.Name) labels := getRedisLabels(cr.ObjectMeta.Name, replication, "replication", cr.ObjectMeta.Labels) @@ -63,6 +64,7 @@ func CreateReplicationRedis(cr *redisv1beta2.RedisReplication) error { generateRedisReplicationInitContainerParams(cr), generateRedisReplicationContainerParams(cr), cr.Spec.Sidecars, + cl, ) if err != nil { logger.Error(err, "Cannot create replication statefulset for Redis") diff --git a/k8sutils/redis-sentinel.go b/k8sutils/redis-sentinel.go index fdb3dcb84..04f54eee3 100644 --- a/k8sutils/redis-sentinel.go +++ b/k8sutils/redis-sentinel.go @@ -7,6 +7,7 @@ import ( "github.com/OT-CONTAINER-KIT/redis-operator/pkg/util" "k8s.io/utils/pointer" + "sigs.k8s.io/controller-runtime/pkg/client" commonapi "github.com/OT-CONTAINER-KIT/redis-operator/api" redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" @@ -37,7 +38,7 @@ type RedisReplicationObject struct { } // Redis Sentinel Create the Redis Sentinel Setup -func CreateRedisSentinel(ctx context.Context, client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisSentinel) error { +func CreateRedisSentinel(ctx context.Context, client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisSentinel, cl client.Client) error { prop := RedisSentinelSTS{ RedisStateFulType: "sentinel", Affinity: cr.Spec.Affinity, @@ -50,7 +51,7 @@ func CreateRedisSentinel(ctx context.Context, client kubernetes.Interface, logge prop.ExternalConfig = cr.Spec.RedisSentinelConfig.AdditionalSentinelConfig } - return prop.CreateRedisSentinelSetup(ctx, client, logger, cr) + return prop.CreateRedisSentinelSetup(ctx, client, logger, cr, cl) } @@ -64,7 +65,7 @@ func CreateRedisSentinelService(cr *redisv1beta2.RedisSentinel) error { } // Create Redis Sentinel Cluster Setup -func (service RedisSentinelSTS) CreateRedisSentinelSetup(ctx context.Context, client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisSentinel) error { +func (service RedisSentinelSTS) CreateRedisSentinelSetup(ctx context.Context, client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisSentinel, cl client.Client) error { stateFulName := cr.ObjectMeta.Name + "-" + service.RedisStateFulType labels := getRedisLabels(stateFulName, sentinel, service.RedisStateFulType, cr.ObjectMeta.Labels) annotations := generateStatefulSetsAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.IgnoreAnnotations) @@ -77,6 +78,7 @@ func (service RedisSentinelSTS) CreateRedisSentinelSetup(ctx context.Context, cl generateRedisSentinelInitContainerParams(cr), generateRedisSentinelContainerParams(ctx, client, logger, cr, service.ReadinessProbe, service.LivenessProbe), cr.Spec.Sidecars, + cl, ) if err != nil { diff --git a/k8sutils/redis-standalone.go b/k8sutils/redis-standalone.go index 4addf77be..89af2730f 100644 --- a/k8sutils/redis-standalone.go +++ b/k8sutils/redis-standalone.go @@ -4,6 +4,7 @@ import ( redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/util" "k8s.io/utils/pointer" + "sigs.k8s.io/controller-runtime/pkg/client" ) //var ( @@ -54,7 +55,7 @@ func CreateStandaloneService(cr *redisv1beta2.Redis) error { } // CreateStandaloneRedis will create a standalone redis setup -func CreateStandaloneRedis(cr *redisv1beta2.Redis) error { +func CreateStandaloneRedis(cr *redisv1beta2.Redis, cl client.Client) error { logger := statefulSetLogger(cr.Namespace, cr.ObjectMeta.Name) labels := getRedisLabels(cr.ObjectMeta.Name, standalone, "standalone", cr.ObjectMeta.Labels) annotations := generateStatefulSetsAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.IgnoreAnnotations) @@ -66,6 +67,7 @@ func CreateStandaloneRedis(cr *redisv1beta2.Redis) error { generateRedisStandaloneInitContainerParams(cr), generateRedisStandaloneContainerParams(cr), cr.Spec.Sidecars, + cl, ) if err != nil { logger.Error(err, "Cannot create standalone statefulset for Redis") diff --git a/k8sutils/statefulset.go b/k8sutils/statefulset.go index f1caa2891..cedb8862f 100644 --- a/k8sutils/statefulset.go +++ b/k8sutils/statefulset.go @@ -3,12 +3,14 @@ package k8sutils import ( "context" "fmt" - "k8s.io/utils/env" "path" "sort" "strconv" "strings" + "k8s.io/utils/env" + "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/OT-CONTAINER-KIT/redis-operator/pkg/util" "k8s.io/utils/pointer" @@ -95,7 +97,7 @@ type initContainerParameters struct { } // CreateOrUpdateStateFul method will create or update Redis service -func CreateOrUpdateStateFul(namespace string, stsMeta metav1.ObjectMeta, params statefulSetParameters, ownerDef metav1.OwnerReference, initcontainerParams initContainerParameters, containerParams containerParameters, sidecars *[]redisv1beta2.Sidecar) error { +func CreateOrUpdateStateFul(namespace string, stsMeta metav1.ObjectMeta, params statefulSetParameters, ownerDef metav1.OwnerReference, initcontainerParams initContainerParameters, containerParams containerParameters, sidecars *[]redisv1beta2.Sidecar, cl client.Client) error { logger := statefulSetLogger(namespace, stsMeta.Name) storedStateful, err := GetStatefulSet(namespace, stsMeta.Name) statefulSetDef := generateStatefulSetsDef(stsMeta, params, ownerDef, initcontainerParams, containerParams, getSidecars(sidecars)) @@ -105,7 +107,7 @@ func CreateOrUpdateStateFul(namespace string, stsMeta metav1.ObjectMeta, params return err } if apierrors.IsNotFound(err) { - return createStatefulSet(namespace, statefulSetDef) + return createStatefulSet(statefulSetDef, cl) } return err } @@ -690,14 +692,9 @@ func getEnvironmentVariables(role string, enabledPassword *bool, secretName *str } // createStatefulSet is a method to create statefulset in Kubernetes -func createStatefulSet(namespace string, stateful *appsv1.StatefulSet) error { - logger := statefulSetLogger(namespace, stateful.Name) - client, err := GenerateK8sClient(GenerateK8sConfig) - if err != nil { - logger.Error(err, "Could not generate kubernetes client") - return err - } - _, err = client.AppsV1().StatefulSets(namespace).Create(context.TODO(), stateful, metav1.CreateOptions{}) +func createStatefulSet(stateful *appsv1.StatefulSet, cl client.Client) error { + logger := statefulSetLogger(stateful.Namespace, stateful.Name) + err := cl.Create(context.TODO(), stateful, nil) if err != nil { logger.Error(err, "Redis stateful creation failed") return err diff --git a/tests/readme.md b/tests/readme.md index f4057b698..c4c91cd86 100644 --- a/tests/readme.md +++ b/tests/readme.md @@ -35,7 +35,7 @@ Please refer to the repository's README for detailed instructions on installing Execute the kuttl test using the following command: -To run all default tests ( _config/kuttl-test.yaml is the default config file ) +To run all default tests ( \_config/kuttl-test.yaml is the default config file ) ```bash kubectl kuttl test --config tests/_config/kuttl-test.yaml