Skip to content

Commit

Permalink
Reconcile for owned resources (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 authored Oct 16, 2024
1 parent 1c3ac4c commit 3e400e3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 61 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)
}
55 changes: 38 additions & 17 deletions tests/e2e/prometheus_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ import (

var _ = Describe("PrometheusRule", Ordered, func() {
var (
crClient client.Client
promRule *prometheus.PrometheusRule
promRuleName = "prometheus-rules"
alertName = "test-alert"
alertResourceName = promRuleName + "-" + alertName + "-0"
newAlertName = "test-alert-updated"
newAlertResourceName = promRuleName + "-" + newAlertName + "-0"
crClient client.Client
promRule *prometheus.PrometheusRule
promRuleName = "prometheus-rules"
alert = &coralogixv1alpha1.Alert{}
recordingRuleGroupSet = &coralogixv1alpha1.RecordingRuleGroupSet{}
alertName = "test-alert"
alertResourceName = promRuleName + "-" + alertName + "-0"
newAlertName = "test-alert-updated"
newAlertResourceName = promRuleName + "-" + newAlertName + "-0"
)

BeforeAll(func() {
Expand Down Expand Up @@ -88,17 +90,39 @@ var _ = Describe("PrometheusRule", Ordered, func() {
Expect(crClient.Create(ctx, promRule)).To(Succeed())

By("Verifying underlying Alert and RecordingRuleGroupSet were created")
fetchedAlert := &coralogixv1alpha1.Alert{}
Eventually(func() error {
return crClient.Get(ctx, types.NamespacedName{Name: alertResourceName, Namespace: testNamespace}, fetchedAlert)
return crClient.Get(ctx, types.NamespacedName{Name: alertResourceName, Namespace: testNamespace}, alert)
}, time.Minute, time.Second).Should(Succeed())

fetchedRecordingRuleGroupSet := &coralogixv1alpha1.RecordingRuleGroupSet{}
Eventually(func() error {
return crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, fetchedRecordingRuleGroupSet)
return crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, recordingRuleGroupSet)
}, time.Minute, time.Second).Should(Succeed())
})

It("Should recreate underlying resources when they are deleted", func(ctx context.Context) {
By("Deleting underlying Alert")
alertInitialUID := alert.GetUID()
Expect(crClient.Delete(ctx, alert)).To(Succeed())

By("Verifying underlying Alert was recreated")
Eventually(func(g Gomega) bool {
g.Expect(crClient.Get(ctx, types.NamespacedName{Name: alertResourceName, Namespace: testNamespace}, alert)).To(Succeed())
return alert.GetUID() != alertInitialUID && alert.GetUID() != ""
}, time.Minute, time.Second).Should(BeTrue())

By("Deleting underlying RecordingRuleGroupSet")
recordingRuleGroupSetInitialUID := recordingRuleGroupSet.GetUID()
Expect(crClient.Delete(ctx, recordingRuleGroupSet)).To(Succeed())

By("Verifying underlying RecordingRuleGroupSet was recreated")
Eventually(func(g Gomega) bool {
g.Expect(crClient.Get(ctx,
types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, recordingRuleGroupSet)).To(Succeed())
return recordingRuleGroupSet.GetUID() != recordingRuleGroupSetInitialUID &&
recordingRuleGroupSet.GetUID() != ""
}, time.Minute, time.Second).Should(BeTrue())
})

It("Should be updated successfully", func(ctx context.Context) {
By("Patching the PrometheusRule")
modifiedPromRule := promRule.DeepCopy()
Expand All @@ -107,8 +131,7 @@ var _ = Describe("PrometheusRule", Ordered, func() {

By("Verifying underlying Alert was updated")
Eventually(func() error {
fetchedAlert := &coralogixv1alpha1.Alert{}
return crClient.Get(ctx, types.NamespacedName{Name: newAlertResourceName, Namespace: testNamespace}, fetchedAlert)
return crClient.Get(ctx, types.NamespacedName{Name: newAlertResourceName, Namespace: testNamespace}, alert)
}, time.Minute, time.Second).Should(Succeed())
})

Expand All @@ -117,15 +140,13 @@ var _ = Describe("PrometheusRule", Ordered, func() {
Expect(crClient.Delete(ctx, promRule)).To(Succeed())

By("Verifying underlying Alert and RecordingRuleGroupSet were deleted")
fetchedAlert := &coralogixv1alpha1.Alert{}
Eventually(func() bool {
err := crClient.Get(ctx, types.NamespacedName{Name: newAlertResourceName, Namespace: testNamespace}, fetchedAlert)
err := crClient.Get(ctx, types.NamespacedName{Name: newAlertResourceName, Namespace: testNamespace}, alert)
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())

fetchedRecordingRuleGroupSet := &coralogixv1alpha1.RecordingRuleGroupSet{}
Eventually(func() bool {
err := crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, fetchedRecordingRuleGroupSet)
err := crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, recordingRuleGroupSet)
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
})
Expand Down

0 comments on commit 3e400e3

Please sign in to comment.