Skip to content

Commit

Permalink
Generate k8s client only once
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuCesbron committed Jan 13, 2024
1 parent e57531a commit c22de5d
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion controllers/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 68 in controllers/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redis_controller.go#L68

Added line #L68 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 120 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L120

Added line #L120 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
}
err = k8sutils.CreateRedisLeader(instance)
err = k8sutils.CreateRedisLeader(instance, r.Client)

Check warning on line 125 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L125

Added line #L125 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -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)

Check warning on line 154 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L154

Added line #L154 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
}
err = k8sutils.CreateRedisFollower(instance)
err = k8sutils.CreateRedisFollower(instance, r.Client)

Check warning on line 159 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L159

Added line #L159 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/redisreplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 59 in controllers/redisreplication_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redisreplication_controller.go#L59

Added line #L59 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/redissentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 58 in controllers/redissentinel_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redissentinel_controller.go#L58

Added line #L58 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down
22 changes: 12 additions & 10 deletions k8sutils/redis-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {

Check warning on line 209 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L209

Added line #L209 was not covered by tests
prop := RedisClusterSTS{
RedisStateFulType: "leader",
SecurityContext: cr.Spec.RedisLeader.SecurityContext,
Expand All @@ -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)

Check warning on line 223 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L223

Added line #L223 was not covered by tests
}

// CreateRedisFollower will create a follower redis setup
func CreateRedisFollower(cr *redisv1beta2.RedisCluster) error {
func CreateRedisFollower(cr *redisv1beta2.RedisCluster, cl client.Client) error {

Check warning on line 227 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L227

Added line #L227 was not covered by tests
prop := RedisClusterSTS{
RedisStateFulType: "follower",
SecurityContext: cr.Spec.RedisFollower.SecurityContext,
Expand All @@ -237,31 +238,31 @@ 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)

Check warning on line 241 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L241

Added line #L241 was not covered by tests
}

// CreateRedisLeaderService method will create service for Redis Leader
func CreateRedisLeaderService(cr *redisv1beta2.RedisCluster) error {
func CreateRedisLeaderService(cr *redisv1beta2.RedisCluster, cl client.Client) error {

Check warning on line 245 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L245

Added line #L245 was not covered by tests
prop := RedisClusterService{
RedisServiceRole: "leader",
}
return prop.CreateRedisClusterService(cr)
return prop.CreateRedisClusterService(cr, cl)

Check warning on line 249 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L249

Added line #L249 was not covered by tests
}

// CreateRedisFollowerService method will create service for Redis Follower
func CreateRedisFollowerService(cr *redisv1beta2.RedisCluster) error {
func CreateRedisFollowerService(cr *redisv1beta2.RedisCluster, cl client.Client) error {

Check warning on line 253 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L253

Added line #L253 was not covered by tests
prop := RedisClusterService{
RedisServiceRole: "follower",
}
return prop.CreateRedisClusterService(cr)
return prop.CreateRedisClusterService(cr, cl)

Check warning on line 257 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L257

Added line #L257 was not covered by tests
}

func (service RedisClusterSTS) getReplicaCount(cr *redisv1beta2.RedisCluster) int32 {
return cr.Spec.GetReplicaCounts(service.RedisStateFulType)
}

// 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 {

Check warning on line 265 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L265

Added line #L265 was not covered by tests
stateFulName := cr.ObjectMeta.Name + "-" + service.RedisStateFulType
logger := statefulSetLogger(cr.Namespace, stateFulName)
labels := getRedisLabels(stateFulName, cluster, service.RedisStateFulType, cr.ObjectMeta.Labels)
Expand All @@ -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,

Check warning on line 279 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L279

Added line #L279 was not covered by tests
)
if err != nil {
logger.Error(err, "Cannot create statefulset for Redis", "Setup.Type", service.RedisStateFulType)
Expand All @@ -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 {

Check warning on line 289 in k8sutils/redis-cluster.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-cluster.go#L289

Added line #L289 was not covered by tests
serviceName := cr.ObjectMeta.Name + "-" + service.RedisServiceRole
logger := serviceLogger(cr.Namespace, serviceName)
labels := getRedisLabels(serviceName, cluster, service.RedisServiceRole, cr.ObjectMeta.Labels)
Expand Down
4 changes: 3 additions & 1 deletion k8sutils/redis-replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Check warning on line 54 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L54

Added line #L54 was not covered by tests
stateFulName := cr.ObjectMeta.Name
logger := statefulSetLogger(cr.Namespace, cr.ObjectMeta.Name)
labels := getRedisLabels(cr.ObjectMeta.Name, replication, "replication", cr.ObjectMeta.Labels)
Expand All @@ -63,6 +64,7 @@ func CreateReplicationRedis(cr *redisv1beta2.RedisReplication) error {
generateRedisReplicationInitContainerParams(cr),
generateRedisReplicationContainerParams(cr),
cr.Spec.Sidecars,
cl,

Check warning on line 67 in k8sutils/redis-replication.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-replication.go#L67

Added line #L67 was not covered by tests
)
if err != nil {
logger.Error(err, "Cannot create replication statefulset for Redis")
Expand Down
8 changes: 5 additions & 3 deletions k8sutils/redis-sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {

Check warning on line 41 in k8sutils/redis-sentinel.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-sentinel.go#L41

Added line #L41 was not covered by tests
prop := RedisSentinelSTS{
RedisStateFulType: "sentinel",
Affinity: cr.Spec.Affinity,
Expand All @@ -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)

Check warning on line 54 in k8sutils/redis-sentinel.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-sentinel.go#L54

Added line #L54 was not covered by tests

}

Expand All @@ -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 {

Check warning on line 68 in k8sutils/redis-sentinel.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-sentinel.go#L68

Added line #L68 was not covered by tests
stateFulName := cr.ObjectMeta.Name + "-" + service.RedisStateFulType
labels := getRedisLabels(stateFulName, sentinel, service.RedisStateFulType, cr.ObjectMeta.Labels)
annotations := generateStatefulSetsAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.IgnoreAnnotations)
Expand All @@ -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,

Check warning on line 81 in k8sutils/redis-sentinel.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-sentinel.go#L81

Added line #L81 was not covered by tests
)

if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion k8sutils/redis-standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {

Check warning on line 58 in k8sutils/redis-standalone.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-standalone.go#L58

Added line #L58 was not covered by tests
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)
Expand All @@ -66,6 +67,7 @@ func CreateStandaloneRedis(cr *redisv1beta2.Redis) error {
generateRedisStandaloneInitContainerParams(cr),
generateRedisStandaloneContainerParams(cr),
cr.Spec.Sidecars,
cl,

Check warning on line 70 in k8sutils/redis-standalone.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-standalone.go#L70

Added line #L70 was not covered by tests
)
if err != nil {
logger.Error(err, "Cannot create standalone statefulset for Redis")
Expand Down
19 changes: 8 additions & 11 deletions k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {

Check warning on line 100 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L100

Added line #L100 was not covered by tests
logger := statefulSetLogger(namespace, stsMeta.Name)
storedStateful, err := GetStatefulSet(namespace, stsMeta.Name)
statefulSetDef := generateStatefulSetsDef(stsMeta, params, ownerDef, initcontainerParams, containerParams, getSidecars(sidecars))
Expand All @@ -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)

Check warning on line 110 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L110

Added line #L110 was not covered by tests
}
return err
}
Expand Down Expand Up @@ -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)

Check warning on line 697 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L695-L697

Added lines #L695 - L697 were not covered by tests
if err != nil {
logger.Error(err, "Redis stateful creation failed")
return err
Expand Down
2 changes: 1 addition & 1 deletion tests/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c22de5d

Please sign in to comment.