Browse Source

move manifests to separate controller

master
Vladimir Smagin 1 year ago
parent
commit
1fbc7b7b9b
Signed by: 21h GPG Key ID: 13BCE12E5618F071
8 changed files with 32 additions and 30 deletions
  1. +3
    -3
      pkg/controller/manifests/manifest_configmap.go
  2. +5
    -5
      pkg/controller/manifests/manifest_deployment.go
  3. +4
    -4
      pkg/controller/manifests/manifest_haproxy.go
  4. +4
    -4
      pkg/controller/manifests/manifest_service.go
  5. +5
    -5
      pkg/controller/manifests/manifest_statefulset.go
  6. +4
    -4
      pkg/controller/manifests/utils.go
  7. +3
    -2
      pkg/controller/redis/reconcile.go
  8. +4
    -3
      pkg/controller/redis/redis_controller.go

pkg/controller/redis/manifest_configmap.go → pkg/controller/manifests/manifest_configmap.go View File

@ -1,4 +1,4 @@
package redis
package manifests
import (
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
@ -7,13 +7,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func generateConfigmap(cr *blindagev1alpha1.Redis, name string, data map[string]string) corev1.ConfigMap {
func GenerateConfigmap(cr *blindagev1alpha1.Redis, name string, data map[string]string) corev1.ConfigMap {
configmap := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: cr.Namespace,
Labels: baseLabels(cr),
Labels: BaseLabels(cr),
},
Data: data,
}

pkg/controller/redis/manifest_deployment.go → pkg/controller/manifests/manifest_deployment.go View File

@ -1,4 +1,4 @@
package redis
package manifests
import (
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
@ -8,7 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func generateDeployment(cr *blindagev1alpha1.Redis) v1.Deployment {
func GenerateDeployment(cr *blindagev1alpha1.Redis) v1.Deployment {
// check affinity rules
affinity := &corev1.Affinity{}
@ -53,17 +53,17 @@ func generateDeployment(cr *blindagev1alpha1.Redis) v1.Deployment {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-sentinel",
Namespace: cr.ObjectMeta.Namespace,
Labels: baseLabels(cr),
Labels: BaseLabels(cr),
},
Spec: v1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: baseLabels(cr),
MatchLabels: BaseLabels(cr),
},
Replicas: &replicas,
Strategy: v1.DeploymentStrategy{Type: v1.RollingUpdateDeploymentStrategyType, RollingUpdate: nil},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: mergeLabels(baseLabels(cr),
Labels: MergeLabels(BaseLabels(cr),
map[string]string{"component": "sentinel"},
),
},

pkg/controller/redis/manifest_haproxy.go → pkg/controller/manifests/manifest_haproxy.go View File

@ -1,4 +1,4 @@
package redis
package manifests
import (
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
@ -8,7 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func generateHaproxyDeployment(cr *blindagev1alpha1.Redis) v1.Deployment {
func GenerateHaproxyDeployment(cr *blindagev1alpha1.Redis) v1.Deployment {
image := "haproxy:1.9.4-alpine"
if cr.Spec.HAProxyImage != "" {
image = cr.Spec.Image
@ -29,11 +29,11 @@ func generateHaproxyDeployment(cr *blindagev1alpha1.Redis) v1.Deployment {
Spec: v1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: mergeLabels(baseLabels(cr), map[string]string{"component": "haproxy"}),
MatchLabels: MergeLabels(BaseLabels(cr), map[string]string{"component": "haproxy"}),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: mergeLabels(baseLabels(cr), map[string]string{"component": "haproxy"}),
Labels: MergeLabels(BaseLabels(cr), map[string]string{"component": "haproxy"}),
},
Spec: corev1.PodSpec{
ShareProcessNamespace: &shareProcessNamespace,

pkg/controller/redis/manifest_service.go → pkg/controller/manifests/manifest_service.go View File

@ -1,4 +1,4 @@
package redis
package manifests
import (
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
@ -8,15 +8,15 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)
func generateService(cr *blindagev1alpha1.Redis, serviceName, portName string, portNum int32, labels map[string]string) corev1.Service {
func GenerateService(cr *blindagev1alpha1.Redis, serviceName, portName string, portNum int32, labels map[string]string) corev1.Service {
labelsComplete := mergeLabels(baseLabels(cr), labels)
labelsComplete := MergeLabels(BaseLabels(cr), labels)
service := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: cr.Namespace,
Labels: baseLabels(cr),
Labels: BaseLabels(cr),
},
}

pkg/controller/redis/manifest_statefulset.go → pkg/controller/manifests/manifest_statefulset.go View File

@ -1,4 +1,4 @@
package redis
package manifests
import (
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
@ -8,7 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func generateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
func GenerateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
// prepare containers for pod
podContainers := []corev1.Container{}
@ -103,7 +103,7 @@ func generateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: mergeLabels(baseLabels(cr),
Labels: MergeLabels(BaseLabels(cr),
map[string]string{"component": "redis"},
),
},
@ -150,14 +150,14 @@ func generateStatefulSet(cr *blindagev1alpha1.Redis) v1.StatefulSet {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-redis",
Namespace: cr.Namespace,
Labels: mergeLabels(baseLabels(cr),
Labels: MergeLabels(BaseLabels(cr),
map[string]string{"component": "redis"},
),
},
Spec: v1.StatefulSetSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: baseLabels(cr),
MatchLabels: BaseLabels(cr),
},
Template: podTemplate,
UpdateStrategy: v1.StatefulSetUpdateStrategy{

pkg/controller/redis/utils.go → pkg/controller/manifests/utils.go View File

@ -1,11 +1,11 @@
package redis
package manifests
import (
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
v1 "k8s.io/api/core/v1"
)
func mergeLabels(itermaps ...map[string]string) map[string]string {
func MergeLabels(itermaps ...map[string]string) map[string]string {
result := make(map[string]string)
for _, rv := range itermaps {
for k, v := range rv {
@ -15,14 +15,14 @@ func mergeLabels(itermaps ...map[string]string) map[string]string {
return result
}
func baseLabels(cr *blindagev1alpha1.Redis) map[string]string {
func BaseLabels(cr *blindagev1alpha1.Redis) map[string]string {
return map[string]string{
"operator": "redis-operator",
"instance": cr.Name,
}
}
func mergeENV(envs []v1.EnvVar) []v1.EnvVar {
func MergeENV(envs []v1.EnvVar) []v1.EnvVar {
return append([]v1.EnvVar{
v1.EnvVar{
Name: "POD_NAME",

+ 3
- 2
pkg/controller/redis/reconcile.go View File

@ -3,6 +3,7 @@ package redis
import (
"context"
"git.blindage.org/21h/redis-operator/pkg/controller/manifests"
v1 "k8s.io/api/apps/v1"
blindagev1alpha1 "git.blindage.org/21h/redis-operator/pkg/apis/blindage/v1alpha1"
@ -18,7 +19,7 @@ import (
// ReconcileConfigmap means full reconcile of configmap
func (r *ReconcileRedis) ReconcileConfigmap(reqLogger logr.Logger, cr *blindagev1alpha1.Redis, configmapName string, configmapData map[string]string) (reconcile.Result, error) {
newConfigmap := generateConfigmap(cr, configmapName, configmapData)
newConfigmap := manifests.GenerateConfigmap(cr, configmapName, configmapData)
if err := controllerutil.SetControllerReference(cr, &newConfigmap, r.scheme); err != nil {
raven.CaptureErrorAndWait(err, nil)
@ -57,7 +58,7 @@ func (r *ReconcileRedis) ReconcileConfigmap(reqLogger logr.Logger, cr *blindagev
func (r *ReconcileRedis) ReconcileService(reqLogger logr.Logger, cr *blindagev1alpha1.Redis, serviceName, portName string, portNum int32, selector map[string]string) (reconcile.Result, error) {
// reconcile Service
newService := generateService(cr, serviceName, portName, portNum, selector)
newService := manifests.GenerateService(cr, serviceName, portName, portNum, selector)
if err := controllerutil.SetControllerReference(cr, &newService, r.scheme); err != nil {
raven.CaptureErrorAndWait(err, nil)


+ 4
- 3
pkg/controller/redis/redis_controller.go View File

@ -6,6 +6,7 @@ import (
"os"
"sort"
"git.blindage.org/21h/redis-operator/pkg/controller/manifests"
rediscli "github.com/go-redis/redis"
"k8s.io/apimachinery/pkg/labels"
@ -141,13 +142,13 @@ fi
}
// reconcile Sentinel deployment
newSentinelDeployment := generateDeployment(instance)
newSentinelDeployment := manifests.GenerateDeployment(instance)
if _, err := r.ReconcileDeployment(reqLogger, instance, newSentinelDeployment); err != nil {
return reconcile.Result{}, err
}
// reconcile Redis StatefulSet
newRedisStatefulset := generateStatefulSet(instance)
newRedisStatefulset := manifests.GenerateStatefulSet(instance)
if _, err := r.ReconcileStatefulSet(reqLogger, instance, newRedisStatefulset); err != nil {
return reconcile.Result{}, err
}
@ -310,7 +311,7 @@ backend bk_redis
}
// reconcile HAProxy deployment
newHAProxyDeployment := generateHaproxyDeployment(instance)
newHAProxyDeployment := manifests.GenerateHaproxyDeployment(instance)
if _, err := r.ReconcileDeployment(reqLogger, instance, newHAProxyDeployment); err != nil {
return reconcile.Result{}, err
}


Loading…
Cancel
Save