Browse Source

labels

pull/4/head
Vladimir Smagin 1 year ago
parent
commit
c7d88c2e96
3 changed files with 35 additions and 113 deletions
  1. +15
    -7
      pkg/controller/rabbitmq/rabbitmq_controller.go
  2. +0
    -86
      pkg/controller/rabbitmq/rabbitmq_rbac.go.off
  3. +20
    -20
      pkg/controller/rabbitmq/rabbitmq_services.go

+ 15
- 7
pkg/controller/rabbitmq/rabbitmq_controller.go View File

@@ -87,19 +87,19 @@ func add(mgr manager.Manager, reconciler reconcile.Reconciler) error {
mapFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: a.Meta.GetLabels()["rabbitmq.improvado.io/name"], Namespace: a.Meta.GetNamespace()}},
{NamespacedName: types.NamespacedName{Name: a.Meta.GetLabels()["app.improvado.io/instance"], Namespace: a.Meta.GetNamespace()}},
}
})

p := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
if _, ok := e.MetaOld.GetLabels()["rabbitmq.improvado.io/name"]; !ok {
if _, ok := e.MetaOld.GetLabels()["app.improvado.io/instance"]; !ok {
return false
}
return e.ObjectOld != e.ObjectNew
},
CreateFunc: func(e event.CreateEvent) bool {
if _, ok := e.Meta.GetLabels()["rabbitmq.improvado.io/name"]; !ok {
if _, ok := e.Meta.GetLabels()["app.improvado.io/instance"]; !ok {
return false
}
return true
@@ -150,8 +150,8 @@ func mergeMaps(itermaps ...map[string]string) map[string]string {

func returnLabels(cr *rabbitmqv1.Rabbitmq) map[string]string {
labels := map[string]string{
"application": "rabbitmq",
"instance": cr.Name,
"app.improvado.io/application": "rabbitmq",
"app.improvado.io/instance": cr.Name,
}
return labels
}
@@ -227,6 +227,14 @@ func (r *ReconcileRabbitmq) Reconcile(request reconcile.Request) (reconcile.Resu
found.Spec.Template = statefulset.Spec.Template
}

if !reflect.DeepEqual(found.Annotations, statefulset.Annotations) {
found.Annotations = statefulset.Annotations
}

if !reflect.DeepEqual(found.Labels, statefulset.Labels) {
found.Labels = statefulset.Labels
}

reqLogger.Info("Reconcile statefulset", "statefulset.Namespace", found.Namespace, "statefulset.Name", found.Name)
if err = r.client.Update(context.TODO(), found); err != nil {
reqLogger.Info("Reconcile statefulset error", "statefulset.Namespace", found.Namespace, "statefulset.Name", found.Name)
@@ -417,7 +425,7 @@ func newStatefulSet(cr *rabbitmqv1.Rabbitmq, secretNames secretResouces) *v1.Sta
podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: mergeMaps(returnLabels(cr),
map[string]string{"component": "messaging"},
map[string]string{"app.improvado.io/component": "messaging"},
),
Annotations: returnAnnotations(cr),
},
@@ -485,7 +493,7 @@ func newStatefulSet(cr *rabbitmqv1.Rabbitmq, secretNames secretResouces) *v1.Sta
Name: cr.Name,
Namespace: cr.Namespace,
Labels: mergeMaps(returnLabels(cr),
map[string]string{"component": "messaging"},
map[string]string{"app.improvado.io/component": "messaging"},
),
},
Spec: v1.StatefulSetSpec{


+ 0
- 86
pkg/controller/rabbitmq/rabbitmq_rbac.go.off View File

@@ -1,86 +0,0 @@
// JUST A FUTURE FEATURE
// FORGET ABOUT THIS FILE

package rabbitmq

import (
"context"
"github.com/go-logr/logr"
"k8s.io/api/rbac/v1beta1"
"reflect"
"time"

rabbitmqv1 "github.com/tekliner/rabbitmq-operator/pkg/apis/rabbitmq/v1"
"k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
func (r *ReconcileRabbitmq) reconcileRBAC(reqLogger logr.Logger, cr *rabbitmqv1.Rabbitmq) (reconcile.Result, error) {
reqLogger.Info("Started reconciling RBAC", "RBAC.Namespace", cr.Namespace, "RBAC.Name", cr.Name)

rbac_sa:= &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: returnLabels(cr),
},
}

rbac_role := &v1beta1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: returnLabels(cr),
},
}

if err := controllerutil.SetControllerReference(cr, configmap, r.scheme); err != nil {
reqLogger.Info("Configmap can't set controller reference", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
return reconcile.Result{}, err
}

found := &corev1.ConfigMap{}
reqLogger.Info("Trying to receive configmap", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
err = r.client.Get(context.TODO(), types.NamespacedName{Name: configmap.Name, Namespace: configmap.Namespace}, found)
if err != nil && apierrors.IsNotFound(err) {
reqLogger.Info("Creating ConfigMap", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
err = r.client.Create(context.TODO(), configmap)
found = configmap

if err != nil {
reqLogger.Info("Creating ConfigMap error", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
return reconcile.Result{}, err
}

} else if err != nil {
reqLogger.Info("Unknown error while getting ConfigMap", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
return reconcile.Result{}, err
}

if !reflect.DeepEqual(found.Data, configmap.Data) {
reqLogger.Info("Configmap not equal to received", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
found.Data = configmap.Data
}

if err = r.client.Update(context.TODO(), found); err != nil {
reqLogger.Info("Configmap can't be updated", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
return reconcile.Result{}, err
}

reqLogger.Info("Configmap successfuly reconciled", "ConfigMap.Namespace", configmap.Namespace, "ConfigMap.Name", configmap.Name)
return reconcile.Result{}, nil
}

+ 20
- 20
pkg/controller/rabbitmq/rabbitmq_services.go View File

@@ -2,9 +2,10 @@ package rabbitmq

import (
"context"
v12 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"reflect"

v12 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"

"github.com/go-logr/logr"
rabbitmqv1 "github.com/tekliner/rabbitmq-operator/pkg/apis/rabbitmq/v1"
corev1 "k8s.io/api/core/v1"
@@ -84,16 +85,15 @@ func (r *ReconcileRabbitmq) reconcileServiceMonitor(reqLogger logr.Logger, cr *r
return reconcile.Result{}, nil
}


func (r *ReconcileRabbitmq) reconcileDiscoveryService(reqLogger logr.Logger, cr *rabbitmqv1.Rabbitmq) (reconcile.Result, error) {

service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-discovery",
Namespace: cr.Namespace,
Labels: mergeMaps(returnLabels(cr),
map[string]string{"service": "discovery"},
map[string]string{"component": "networking"},
Labels: mergeMaps(returnLabels(cr),
map[string]string{"app.improvado.io/service": "discovery"},
map[string]string{"app.improvado.io/component": "networking"},
),
},
Spec: corev1.ServiceSpec{
@@ -126,9 +126,9 @@ func (r *ReconcileRabbitmq) reconcileHAService(reqLogger logr.Logger, cr *rabbit
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: mergeMaps(returnLabels(cr),
map[string]string{"service": "general"},
map[string]string{"component": "networking"},
Labels: mergeMaps(returnLabels(cr),
map[string]string{"app.improvado.io/service": "general"},
map[string]string{"app.improvado.io/component": "networking"},
),
},
Spec: corev1.ServiceSpec{
@@ -168,9 +168,9 @@ func (r *ReconcileRabbitmq) reconcileHTTPService(reqLogger logr.Logger, cr *rabb
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-api",
Namespace: cr.Namespace,
Labels: mergeMaps(returnLabels(cr),
map[string]string{"service": "api"},
map[string]string{"component": "networking"},
Labels: mergeMaps(returnLabels(cr),
map[string]string{"app.improvado.io/service": "api"},
map[string]string{"app.improvado.io/component": "networking"},
),
},
Spec: corev1.ServiceSpec{
@@ -198,10 +198,10 @@ func (r *ReconcileRabbitmq) reconcilePrometheusExporterService(reqLogger logr.Lo
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-exporter",
Namespace: cr.Namespace,
Labels: mergeMaps(returnLabels(cr),
map[string]string{"service": "prometheus-exporter"},
map[string]string{"component": "monitoring"},
map[string]string{"component": "networking"},
Labels: mergeMaps(returnLabels(cr),
map[string]string{"app.improvado.io/service": "prometheus-exporter"},
map[string]string{"app.improvado.io/component": "monitoring"},
map[string]string{"app.improvado.io/component": "networking"},
),
},
Spec: corev1.ServiceSpec{
@@ -229,23 +229,23 @@ func (r *ReconcileRabbitmq) reconcilePrometheusExporterServiceMonitor(reqLogger
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: mergeMaps(returnLabels(cr), map[string]string{"rabbitmq.improvado.io/component": "monitoring"}),
Labels: mergeMaps(returnLabels(cr), map[string]string{"app.improvado.io/component": "monitoring"}),
},
Spec: v12.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: mergeMaps(returnLabels(cr),
map[string]string{"service": "prometheus-exporter"},
map[string]string{"component": "monitoring"},
map[string]string{"app.improvado.io/service": "prometheus-exporter"},
map[string]string{"app.improvado.io/component": "monitoring"},
),
},
Endpoints: []v12.Endpoint{
{
Port: "exporter",
Port: "exporter",
Interval: "10s",
},
},
NamespaceSelector: v12.NamespaceSelector{
Any:true,
Any: true,
},
},
}


Loading…
Cancel
Save