Skip to content

Commit

Permalink
Reconcile for owned resources
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 committed Oct 14, 2024
1 parent 22e4f76 commit 84568db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 44 deletions.
27 changes: 13 additions & 14 deletions controllers/alertmanagerconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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())
},
Expand All @@ -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)
}

Expand Down Expand Up @@ -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()
Expand Down
48 changes: 18 additions & 30 deletions controllers/prometheusrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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())
},
Expand All @@ -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)
}

0 comments on commit 84568db

Please sign in to comment.