Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Reconcile for owned resources" #189

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions controllers/alertmanagerconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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 @@ -15,15 +17,11 @@ 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 @@ -56,7 +54,8 @@ func (r *AlertmanagerConfigReconciler) SetupWithManager(mgr ctrl.Manager) error
}

return ctrl.NewControllerManagedBy(mgr).
For(&prometheus.AlertmanagerConfig{}, builder.WithPredicates(predicate.Funcs{
For(&prometheus.AlertmanagerConfig{}).
WithEventFilter(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return shouldTrackAlertmanagerConfigs(e.Object.GetLabels())
},
Expand All @@ -65,9 +64,8 @@ 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 @@ -104,13 +102,16 @@ 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: 30 additions & 18 deletions controllers/prometheusrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import (
"strings"
"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/v1"
"go.uber.org/zap/zapcore"

prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"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 @@ -93,14 +92,18 @@ 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 @@ -172,8 +175,13 @@ func (r *PrometheusRuleReconciler) convertPrometheusRuleAlertToCxAlert(ctx conte
alertCRD.Spec = prometheusRuleToCoralogixAlertSpec(rule)
alertCRD.Namespace = prometheusRule.Namespace
alertCRD.Name = alertCRDName
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.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: prometheusRule.APIVersion,
Kind: prometheusRule.Kind,
Name: prometheusRule.Name,
UID: prometheusRule.UID,
},
}
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 @@ -190,8 +198,13 @@ func (r *PrometheusRuleReconciler) convertPrometheusRuleAlertToCxAlert(ctx conte

//Converting the PrometheusRule to the desired Alert.
alertCRD.Spec = prometheusRuleToCoralogixAlertSpec(rule)
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.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: prometheusRule.APIVersion,
Kind: prometheusRule.Kind,
Name: prometheusRule.Name,
UID: prometheusRule.UID,
},
}
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 @@ -378,7 +391,8 @@ func (r *PrometheusRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

return ctrl.NewControllerManagedBy(mgr).
For(&prometheus.PrometheusRule{}, builder.WithPredicates(predicate.Funcs{
For(&prometheus.PrometheusRule{}).
WithEventFilter(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return shouldTrackPrometheusRules(e.Object.GetLabels())
},
Expand All @@ -387,9 +401,7 @@ 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: 17 additions & 38 deletions tests/e2e/prometheus_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ import (

var _ = Describe("PrometheusRule", Ordered, func() {
var (
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"
crClient client.Client
promRule *prometheus.PrometheusRule
promRuleName = "prometheus-rules"
alertName = "test-alert"
alertResourceName = promRuleName + "-" + alertName + "-0"
newAlertName = "test-alert-updated"
newAlertResourceName = promRuleName + "-" + newAlertName + "-0"
)

BeforeAll(func() {
Expand Down Expand Up @@ -90,39 +88,17 @@ 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}, alert)
return crClient.Get(ctx, types.NamespacedName{Name: alertResourceName, Namespace: testNamespace}, fetchedAlert)
}, time.Minute, time.Second).Should(Succeed())

fetchedRecordingRuleGroupSet := &coralogixv1alpha1.RecordingRuleGroupSet{}
Eventually(func() error {
return crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, recordingRuleGroupSet)
return crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, fetchedRecordingRuleGroupSet)
}, 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 @@ -131,7 +107,8 @@ var _ = Describe("PrometheusRule", Ordered, func() {

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

Expand All @@ -140,13 +117,15 @@ 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}, alert)
err := crClient.Get(ctx, types.NamespacedName{Name: newAlertResourceName, Namespace: testNamespace}, fetchedAlert)
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}, recordingRuleGroupSet)
err := crClient.Get(ctx, types.NamespacedName{Name: promRuleName, Namespace: testNamespace}, fetchedRecordingRuleGroupSet)
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
})
Expand Down
Loading