Skip to content

Commit

Permalink
refactor configmap reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
tomp21 committed Dec 26, 2024
1 parent c2d4c44 commit b6fb88a
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 140 deletions.
5 changes: 0 additions & 5 deletions internal/controller/configmaps/conf/master.conf

This file was deleted.

6 changes: 0 additions & 6 deletions internal/controller/configmaps/conf/redis.conf

This file was deleted.

Empty file.
15 changes: 0 additions & 15 deletions internal/controller/configmaps/scripts/start-master.sh

This file was deleted.

50 changes: 0 additions & 50 deletions internal/controller/configmaps/scripts/start-replica.sh

This file was deleted.

66 changes: 2 additions & 64 deletions internal/controller/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ package controller

import (
"context"
_ "embed"
"fmt"
"strconv"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
Expand All @@ -37,7 +34,6 @@ import (

cachev1alpha1 "github.com/tomp21/yazio-challenge/api/v1alpha1"
"github.com/tomp21/yazio-challenge/internal/controller/reconcilers"
"github.com/tomp21/yazio-challenge/internal/util"
)

const (
Expand All @@ -54,21 +50,6 @@ var replicaLabels = map[string]string{
"app.kubernetes.io/component": "replica",
}

//go:embed configmaps/scripts/start-master.sh
var masterStartScript string

//go:embed configmaps/scripts/start-replica.sh
var replicaStartScript string

//go:embed configmaps/conf/redis.conf
var redisAllConf string

//go:embed configmaps/conf/master.conf
var redisMasterConf string

//go:embed configmaps/conf/master.conf
var redisReplicaConf string

// RedisReconciler reconciles a Redis object
type RedisReconciler struct {
client.Client
Expand Down Expand Up @@ -210,7 +191,8 @@ func (r *RedisReconciler) CreateOrUpdateRedis(ctx context.Context, redis *cachev
}

//ConfigMaps
if err = r.createOrUpdateConfigMaps(ctx, redis); err != nil {
cmReconciler := reconcilers.NewConfigMapReconciler(&r.Client, r.Scheme)
if err = cmReconciler.Reconcile(ctx, redis); err != nil {
return err
}

Expand All @@ -220,47 +202,3 @@ func (r *RedisReconciler) CreateOrUpdateRedis(ctx context.Context, redis *cachev
}
return nil
}

func (r *RedisReconciler) createOrUpdateConfigMaps(ctx context.Context, redis *cachev1alpha1.Redis) error {

cmStartScripts := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "start-scripts",
Namespace: redis.Namespace,
Labels: util.GetLabels(redis, nil),
},
Data: map[string]string{
"start-master.sh": masterStartScript,
"start-replica.sh": replicaStartScript,
},
}
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, cmStartScripts, func() error {
return controllerutil.SetControllerReference(redis, cmStartScripts, r.Scheme)
})
if err != nil {
return err
}

cmConfig := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-conf",
Namespace: redis.Namespace,
Labels: util.GetLabels(redis, nil),
},
Data: map[string]string{
"redis.conf": redisAllConf,
"master.conf": redisMasterConf,
"replica.conf": redisReplicaConf,
},
}

_, err = controllerutil.CreateOrUpdate(ctx, r.Client, cmConfig, func() error {
return controllerutil.SetControllerReference(redis, cmConfig, r.Scheme)
})

if err != nil {
return err
}

return nil
}

0 comments on commit b6fb88a

Please sign in to comment.