Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate k8s client only once #750

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions controllers/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
return ctrl.Result{}, err
}

err = k8sutils.CreateStandaloneRedis(instance)
err = k8sutils.CreateStandaloneRedis(instance, r.K8sClient)

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
}
err = k8sutils.CreateStandaloneService(instance)
err = k8sutils.CreateStandaloneService(instance, r.K8sClient)

Check warning on line 72 in controllers/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redis_controller.go#L72

Added line #L72 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down
16 changes: 8 additions & 8 deletions controllers/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,22 @@
}

if leaderReplicas != 0 {
err = k8sutils.CreateRedisLeaderService(instance)
err = k8sutils.CreateRedisLeaderService(instance, r.K8sClient)

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.K8sClient)

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
}

err = k8sutils.ReconcileRedisPodDisruptionBudget(instance, "leader", instance.Spec.RedisLeader.PodDisruptionBudget)
err = k8sutils.ReconcileRedisPodDisruptionBudget(instance, "leader", instance.Spec.RedisLeader.PodDisruptionBudget, r.K8sClient)

Check warning on line 130 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L130

Added line #L130 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}

redisLeaderInfo, err := k8sutils.GetStatefulSet(instance.Namespace, instance.ObjectMeta.Name+"-leader")
redisLeaderInfo, err := k8sutils.GetStatefulSet(instance.Namespace, instance.ObjectMeta.Name+"-leader", r.K8sClient)

Check warning on line 135 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L135

Added line #L135 was not covered by tests
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{RequeueAfter: time.Second * 60}, nil
Expand All @@ -151,21 +151,21 @@
}
// if we have followers create their service.
if followerReplicas != 0 {
err = k8sutils.CreateRedisFollowerService(instance)
err = k8sutils.CreateRedisFollowerService(instance, r.K8sClient)

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.K8sClient)

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
}
err = k8sutils.ReconcileRedisPodDisruptionBudget(instance, "follower", instance.Spec.RedisFollower.PodDisruptionBudget)
err = k8sutils.ReconcileRedisPodDisruptionBudget(instance, "follower", instance.Spec.RedisFollower.PodDisruptionBudget, r.K8sClient)

Check warning on line 163 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L163

Added line #L163 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
}
redisFollowerInfo, err := k8sutils.GetStatefulSet(instance.Namespace, instance.ObjectMeta.Name+"-follower")
redisFollowerInfo, err := k8sutils.GetStatefulSet(instance.Namespace, instance.ObjectMeta.Name+"-follower", r.K8sClient)

Check warning on line 168 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L168

Added line #L168 was not covered by tests
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{RequeueAfter: time.Second * 60}, nil
Expand Down
6 changes: 3 additions & 3 deletions controllers/redisreplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@
return ctrl.Result{}, err
}

err = k8sutils.CreateReplicationRedis(instance)
err = k8sutils.CreateReplicationRedis(instance, r.K8sClient)

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
}
err = k8sutils.CreateReplicationService(instance)
err = k8sutils.CreateReplicationService(instance, r.K8sClient)

Check warning on line 63 in controllers/redisreplication_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redisreplication_controller.go#L63

Added line #L63 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}

// Set Pod distruptiuon Budget Later

redisReplicationInfo, err := k8sutils.GetStatefulSet(instance.Namespace, instance.ObjectMeta.Name)
redisReplicationInfo, err := k8sutils.GetStatefulSet(instance.Namespace, instance.ObjectMeta.Name, r.K8sClient)

Check warning on line 70 in controllers/redisreplication_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redisreplication_controller.go#L70

Added line #L70 was not covered by tests
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 60}, err
}
Expand Down
6 changes: 3 additions & 3 deletions controllers/redissentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@
}

// Create Redis Sentinel
err = k8sutils.CreateRedisSentinel(ctx, r.K8sClient, r.Log, instance)
err = k8sutils.CreateRedisSentinel(ctx, r.K8sClient, r.Log, instance, r.K8sClient)

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
}

err = k8sutils.ReconcileSentinelPodDisruptionBudget(instance, instance.Spec.PodDisruptionBudget)
err = k8sutils.ReconcileSentinelPodDisruptionBudget(instance, instance.Spec.PodDisruptionBudget, r.K8sClient)

Check warning on line 63 in controllers/redissentinel_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redissentinel_controller.go#L63

Added line #L63 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}

// Create the Service for Redis Sentinel
err = k8sutils.CreateRedisSentinelService(instance)
err = k8sutils.CreateRedisSentinelService(instance, r.K8sClient)

Check warning on line 69 in controllers/redissentinel_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redissentinel_controller.go#L69

Added line #L69 was not covered by tests
if err != nil {
return ctrl.Result{}, err
}
Expand Down
65 changes: 23 additions & 42 deletions k8sutils/poddisruption.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
)

// CreateRedisLeaderPodDisruptionBudget check and create a PodDisruptionBudget for Leaders
func ReconcileRedisPodDisruptionBudget(cr *redisv1beta2.RedisCluster, role string, pdbParams *commonapi.RedisPodDisruptionBudget) error {
func ReconcileRedisPodDisruptionBudget(cr *redisv1beta2.RedisCluster, role string, pdbParams *commonapi.RedisPodDisruptionBudget, cl kubernetes.Interface) error {

Check warning on line 19 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L19

Added line #L19 was not covered by tests
pdbName := cr.ObjectMeta.Name + "-" + role
logger := pdbLogger(cr.Namespace, pdbName)
if pdbParams != nil && pdbParams.Enabled {
labels := getRedisLabels(cr.ObjectMeta.Name, cluster, role, cr.ObjectMeta.GetLabels())
annotations := generateStatefulSetsAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.IgnoreAnnotations)
pdbMeta := generateObjectMetaInformation(pdbName, cr.Namespace, labels, annotations)
pdbDef := generatePodDisruptionBudgetDef(cr, role, pdbMeta, cr.Spec.RedisLeader.PodDisruptionBudget)
return CreateOrUpdatePodDisruptionBudget(pdbDef)
return CreateOrUpdatePodDisruptionBudget(pdbDef, cl)

Check warning on line 27 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L27

Added line #L27 was not covered by tests
} else {
// Check if one exists, and delete it.
_, err := GetPodDisruptionBudget(cr.Namespace, pdbName)
_, err := GetPodDisruptionBudget(cr.Namespace, pdbName, cl)

Check warning on line 30 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L30

Added line #L30 was not covered by tests
if err == nil {
return deletePodDisruptionBudget(cr.Namespace, pdbName)
return deletePodDisruptionBudget(cr.Namespace, pdbName, cl)

Check warning on line 32 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L32

Added line #L32 was not covered by tests
} else if err != nil && errors.IsNotFound(err) {
logger.V(1).Info("Reconciliation Successful, no PodDisruptionBudget Found.")
// Its ok if its not found, as we're deleting anyway
Expand All @@ -38,20 +39,20 @@
}
}

func ReconcileSentinelPodDisruptionBudget(cr *redisv1beta2.RedisSentinel, pdbParams *commonapi.RedisPodDisruptionBudget) error {
func ReconcileSentinelPodDisruptionBudget(cr *redisv1beta2.RedisSentinel, pdbParams *commonapi.RedisPodDisruptionBudget, cl kubernetes.Interface) error {

Check warning on line 42 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L42

Added line #L42 was not covered by tests
pdbName := cr.ObjectMeta.Name + "-sentinel"
logger := pdbLogger(cr.Namespace, pdbName)
if pdbParams != nil && pdbParams.Enabled {
labels := getRedisLabels(cr.ObjectMeta.Name, sentinel, "sentinel", cr.ObjectMeta.GetLabels())
annotations := generateStatefulSetsAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.IgnoreAnnotations)
pdbMeta := generateObjectMetaInformation(pdbName, cr.Namespace, labels, annotations)
pdbDef := generateSentinelPodDisruptionBudgetDef(cr, "sentinel", pdbMeta, pdbParams)
return CreateOrUpdatePodDisruptionBudget(pdbDef)
return CreateOrUpdatePodDisruptionBudget(pdbDef, cl)

Check warning on line 50 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L50

Added line #L50 was not covered by tests
} else {
// Check if one exists, and delete it.
_, err := GetPodDisruptionBudget(cr.Namespace, pdbName)
_, err := GetPodDisruptionBudget(cr.Namespace, pdbName, cl)

Check warning on line 53 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L53

Added line #L53 was not covered by tests
if err == nil {
return deletePodDisruptionBudget(cr.Namespace, pdbName)
return deletePodDisruptionBudget(cr.Namespace, pdbName, cl)

Check warning on line 55 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L55

Added line #L55 was not covered by tests
} else if err != nil && errors.IsNotFound(err) {
logger.V(1).Info("Reconciliation Successful, no PodDisruptionBudget Found.")
// Its ok if its not found, as we're deleting anyway
Expand Down Expand Up @@ -116,24 +117,24 @@
}

// CreateOrUpdateService method will create or update Redis service
func CreateOrUpdatePodDisruptionBudget(pdbDef *policyv1.PodDisruptionBudget) error {
func CreateOrUpdatePodDisruptionBudget(pdbDef *policyv1.PodDisruptionBudget, cl kubernetes.Interface) error {

Check warning on line 120 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L120

Added line #L120 was not covered by tests
logger := pdbLogger(pdbDef.Namespace, pdbDef.Name)
storedPDB, err := GetPodDisruptionBudget(pdbDef.Namespace, pdbDef.Name)
storedPDB, err := GetPodDisruptionBudget(pdbDef.Namespace, pdbDef.Name, cl)

Check warning on line 122 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L122

Added line #L122 was not covered by tests
if err != nil {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(pdbDef); err != nil { //nolint
logger.Error(err, "Unable to patch redis PodDisruptionBudget with comparison object")
return err
}
if errors.IsNotFound(err) {
return createPodDisruptionBudget(pdbDef.Namespace, pdbDef)
return createPodDisruptionBudget(pdbDef.Namespace, pdbDef, cl)

Check warning on line 129 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L129

Added line #L129 was not covered by tests
}
return err
}
return patchPodDisruptionBudget(storedPDB, pdbDef, pdbDef.Namespace)
return patchPodDisruptionBudget(storedPDB, pdbDef, pdbDef.Namespace, cl)

Check warning on line 133 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L133

Added line #L133 was not covered by tests
}

// patchPodDisruptionBudget will patch Redis Kubernetes PodDisruptionBudgets
func patchPodDisruptionBudget(storedPdb *policyv1.PodDisruptionBudget, newPdb *policyv1.PodDisruptionBudget, namespace string) error {
func patchPodDisruptionBudget(storedPdb *policyv1.PodDisruptionBudget, newPdb *policyv1.PodDisruptionBudget, namespace string, cl kubernetes.Interface) error {

Check warning on line 137 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L137

Added line #L137 was not covered by tests
logger := pdbLogger(namespace, storedPdb.Name)
// We want to try and keep this atomic as possible.
newPdb.ResourceVersion = storedPdb.ResourceVersion
Expand Down Expand Up @@ -169,20 +170,15 @@
logger.Error(err, "Unable to patch redis PodDisruptionBudget with comparison object")
return err
}
return updatePodDisruptionBudget(namespace, newPdb)
return updatePodDisruptionBudget(namespace, newPdb, cl)

Check warning on line 173 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L173

Added line #L173 was not covered by tests
}
return nil
}

// createPodDisruptionBudget is a method to create PodDisruptionBudgets in Kubernetes
func createPodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudget) error {
func createPodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudget, cl kubernetes.Interface) error {

Check warning on line 179 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L179

Added line #L179 was not covered by tests
logger := pdbLogger(namespace, pdb.Name)
client, err := GenerateK8sClient(GenerateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
_, err = client.PolicyV1().PodDisruptionBudgets(namespace).Create(context.TODO(), pdb, metav1.CreateOptions{})
_, err := cl.PolicyV1().PodDisruptionBudgets(namespace).Create(context.TODO(), pdb, metav1.CreateOptions{})

Check warning on line 181 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L181

Added line #L181 was not covered by tests
if err != nil {
logger.Error(err, "Redis PodDisruptionBudget creation failed")
return err
Expand All @@ -192,14 +188,9 @@
}

// updatePodDisruptionBudget is a method to update PodDisruptionBudgets in Kubernetes
func updatePodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudget) error {
func updatePodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudget, cl kubernetes.Interface) error {

Check warning on line 191 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L191

Added line #L191 was not covered by tests
logger := pdbLogger(namespace, pdb.Name)
client, err := GenerateK8sClient(GenerateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
_, err = client.PolicyV1().PodDisruptionBudgets(namespace).Update(context.TODO(), pdb, metav1.UpdateOptions{})
_, err := cl.PolicyV1().PodDisruptionBudgets(namespace).Update(context.TODO(), pdb, metav1.UpdateOptions{})

Check warning on line 193 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L193

Added line #L193 was not covered by tests
if err != nil {
logger.Error(err, "Redis PodDisruptionBudget update failed")
return err
Expand All @@ -209,14 +200,9 @@
}

// deletePodDisruptionBudget is a method to delete PodDisruptionBudgets in Kubernetes
func deletePodDisruptionBudget(namespace string, pdbName string) error {
func deletePodDisruptionBudget(namespace string, pdbName string, cl kubernetes.Interface) error {

Check warning on line 203 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L203

Added line #L203 was not covered by tests
logger := pdbLogger(namespace, pdbName)
client, err := GenerateK8sClient(GenerateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
err = client.PolicyV1().PodDisruptionBudgets(namespace).Delete(context.TODO(), pdbName, metav1.DeleteOptions{})
err := cl.PolicyV1().PodDisruptionBudgets(namespace).Delete(context.TODO(), pdbName, metav1.DeleteOptions{})

Check warning on line 205 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L205

Added line #L205 was not covered by tests
if err != nil {
logger.Error(err, "Redis PodDisruption deletion failed")
return err
Expand All @@ -226,17 +212,12 @@
}

// GetPodDisruptionBudget is a method to get PodDisruptionBudgets in Kubernetes
func GetPodDisruptionBudget(namespace string, pdb string) (*policyv1.PodDisruptionBudget, error) {
func GetPodDisruptionBudget(namespace string, pdb string, cl kubernetes.Interface) (*policyv1.PodDisruptionBudget, error) {

Check warning on line 215 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L215

Added line #L215 was not covered by tests
logger := pdbLogger(namespace, pdb)
client, err := GenerateK8sClient(GenerateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return nil, err
}
getOpts := metav1.GetOptions{
TypeMeta: generateMetaInformation("PodDisruptionBudget", "policy/v1"),
}
pdbInfo, err := client.PolicyV1().PodDisruptionBudgets(namespace).Get(context.TODO(), pdb, getOpts)
pdbInfo, err := cl.PolicyV1().PodDisruptionBudgets(namespace).Get(context.TODO(), pdb, getOpts)

Check warning on line 220 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L220

Added line #L220 was not covered by tests
if err != nil {
logger.V(1).Info("Redis PodDisruptionBudget get action failed")
return nil, err
Expand Down
Loading
Loading