From 84568db1576533c873d712fc1bad112d0914302c Mon Sep 17 00:00:00 2001 From: "assaf.admi" Date: Mon, 14 Oct 2024 16:36:26 +0300 Subject: [PATCH] Reconcile for owned resources --- controllers/alertmanagerconfig_controller.go | 27 ++++++----- controllers/prometheusrule_controller.go | 48 ++++++++------------ 2 files changed, 31 insertions(+), 44 deletions(-) diff --git a/controllers/alertmanagerconfig_controller.go b/controllers/alertmanagerconfig_controller.go index 379878e..0e7e0f4 100644 --- a/controllers/alertmanagerconfig_controller.go +++ b/controllers/alertmanagerconfig_controller.go @@ -6,8 +6,6 @@ import ( "regexp" "time" - coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1" - "github.com/coralogix/coralogix-operator/controllers/clientset" "github.com/go-logr/logr" prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1" "github.com/prometheus/common/model" @@ -17,11 +15,15 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/pointer" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1" + "github.com/coralogix/coralogix-operator/controllers/clientset" ) //+kubebuilder:rbac:groups=monitoring.coreos.com,resources=prometheusrules,verbs=get;list;watch @@ -54,8 +56,7 @@ func (r *AlertmanagerConfigReconciler) SetupWithManager(mgr ctrl.Manager) error } return ctrl.NewControllerManagedBy(mgr). - For(&prometheus.AlertmanagerConfig{}). - WithEventFilter(predicate.Funcs{ + For(&prometheus.AlertmanagerConfig{}, builder.WithPredicates(predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return shouldTrackAlertmanagerConfigs(e.Object.GetLabels()) }, @@ -64,8 +65,9 @@ func (r *AlertmanagerConfigReconciler) SetupWithManager(mgr ctrl.Manager) error }, DeleteFunc: func(e event.DeleteEvent) bool { return shouldTrackAlertmanagerConfigs(e.Object.GetLabels()) - }, - }). + }}, + )). + Owns(&coralogixv1alpha1.OutboundWebhook{}). Complete(r) } @@ -102,16 +104,13 @@ func (r *AlertmanagerConfigReconciler) convertAlertmanagerConfigToCxIntegrations outboundWebhook := &coralogixv1alpha1.OutboundWebhook{ ObjectMeta: metav1.ObjectMeta{ Namespace: alertmanagerConfig.Namespace, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: alertmanagerConfig.APIVersion, - Kind: alertmanagerConfig.Kind, - Name: alertmanagerConfig.Name, - UID: alertmanagerConfig.UID, - }, - }, }, } + + if err := ctrl.SetControllerReference(alertmanagerConfig, outboundWebhook, r.Scheme); err != nil { + log.Error(err, "Received an error while trying to create OutboundWebhook CRD from alertmanagerConfig") + } + for _, receiver := range alertmanagerConfig.Spec.Receivers { for i, opsGenieConfig := range receiver.OpsGenieConfigs { opsGenieWebhook := outboundWebhook.DeepCopy() diff --git a/controllers/prometheusrule_controller.go b/controllers/prometheusrule_controller.go index ecb5305..aca2999 100644 --- a/controllers/prometheusrule_controller.go +++ b/controllers/prometheusrule_controller.go @@ -7,23 +7,24 @@ import ( "strings" "time" - coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1" - "github.com/coralogix/coralogix-operator/controllers/clientset" "github.com/go-logr/logr" - "go.uber.org/zap/zapcore" - prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" + "go.uber.org/zap/zapcore" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1" + "github.com/coralogix/coralogix-operator/controllers/clientset" ) const ( @@ -92,18 +93,14 @@ func (r *PrometheusRuleReconciler) convertPrometheusRuleRecordingRuleToCxRecordi ObjectMeta: metav1.ObjectMeta{ Namespace: prometheusRule.Namespace, Name: prometheusRule.Name, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: prometheusRule.APIVersion, - Kind: prometheusRule.Kind, - Name: prometheusRule.Name, - UID: prometheusRule.UID, - }, - }, }, Spec: recordingRuleGroupSetSpec, } + if err := ctrl.SetControllerReference(prometheusRule, recordingRuleGroupSet, r.Scheme); err != nil { + return fmt.Errorf("received an error while trying to create RecordingRuleGroupSet CRD: %w", err) + } + if err := r.Client.Get(ctx, req.NamespacedName, recordingRuleGroupSet); err != nil { if errors.IsNotFound(err) { if err = r.Create(ctx, recordingRuleGroupSet); err != nil { @@ -175,13 +172,8 @@ func (r *PrometheusRuleReconciler) convertPrometheusRuleAlertToCxAlert(ctx conte alertCRD.Spec = prometheusRuleToCoralogixAlertSpec(rule) alertCRD.Namespace = prometheusRule.Namespace alertCRD.Name = alertCRDName - alertCRD.OwnerReferences = []metav1.OwnerReference{ - { - APIVersion: prometheusRule.APIVersion, - Kind: prometheusRule.Kind, - Name: prometheusRule.Name, - UID: prometheusRule.UID, - }, + if err := ctrl.SetControllerReference(prometheusRule, alertCRD, r.Scheme); err != nil { + return fmt.Errorf("received an error while trying to create Alert CRD: %w", err) } alertCRD.Labels = map[string]string{"app.kubernetes.io/managed-by": prometheusRule.Name} if val, ok := prometheusRule.Labels["app.coralogix.com/managed-by-alertmanger-config"]; ok { @@ -198,13 +190,8 @@ func (r *PrometheusRuleReconciler) convertPrometheusRuleAlertToCxAlert(ctx conte //Converting the PrometheusRule to the desired Alert. alertCRD.Spec = prometheusRuleToCoralogixAlertSpec(rule) - alertCRD.OwnerReferences = []metav1.OwnerReference{ - { - APIVersion: prometheusRule.APIVersion, - Kind: prometheusRule.Kind, - Name: prometheusRule.Name, - UID: prometheusRule.UID, - }, + if err := ctrl.SetControllerReference(prometheusRule, alertCRD, r.Scheme); err != nil { + return fmt.Errorf("received an error while trying to create Alert CRD: %w", err) } if val, ok := prometheusRule.Labels["app.coralogix.com/managed-by-alertmanger-config"]; ok { alertCRD.Labels["app.coralogix.com/managed-by-alertmanger-config"] = val @@ -391,8 +378,7 @@ func (r *PrometheusRuleReconciler) SetupWithManager(mgr ctrl.Manager) error { } return ctrl.NewControllerManagedBy(mgr). - For(&prometheus.PrometheusRule{}). - WithEventFilter(predicate.Funcs{ + For(&prometheus.PrometheusRule{}, builder.WithPredicates(predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return shouldTrackPrometheusRules(e.Object.GetLabels()) }, @@ -401,7 +387,9 @@ func (r *PrometheusRuleReconciler) SetupWithManager(mgr ctrl.Manager) error { }, DeleteFunc: func(e event.DeleteEvent) bool { return shouldTrackPrometheusRules(e.Object.GetLabels()) - }, - }). + }}, + )). + Owns(&coralogixv1alpha1.RecordingRuleGroupSet{}). + Owns(&coralogixv1alpha1.Alert{}). Complete(r) }