-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add observability controller alerts (#3272)
* Refactor hyperconverged monitoring into separate package Signed-off-by: João Vilaça <[email protected]> * Add base for observability controller Prometheus rules Signed-off-by: João Vilaça <[email protected]> * Add HighCPUWorkload alert Signed-off-by: João Vilaça <[email protected]> * Add HAControlPlaneDown alert Signed-off-by: João Vilaça <[email protected]> * Add NodeNetworkInterfaceDown alert Signed-off-by: João Vilaça <[email protected]> * Update observability func tests Signed-off-by: João Vilaça <[email protected]> * Add observability controller unit tests Signed-off-by: João Vilaça <[email protected]> --------- Signed-off-by: João Vilaça <[email protected]>
- Loading branch information
1 parent
c4ab624
commit fbf6052
Showing
43 changed files
with
673 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package observability | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/monitoring/observability/rules" | ||
) | ||
|
||
func (r *Reconciler) ReconcileAlerts(ctx context.Context) error { | ||
desiredPromRule, err := rules.BuildPrometheusRule(r.namespace, r.owner) | ||
if err != nil { | ||
return fmt.Errorf("failed to build PrometheusRule: %v", err) | ||
} | ||
|
||
existingPromRule := &promv1.PrometheusRule{} | ||
err = r.Get(ctx, types.NamespacedName{ | ||
Name: desiredPromRule.Name, | ||
Namespace: desiredPromRule.Namespace, | ||
}, existingPromRule) | ||
|
||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
// if it doesn't exist, create it | ||
if createErr := r.Create(ctx, desiredPromRule); createErr != nil { | ||
return fmt.Errorf("failed to create PrometheusRule: %v", createErr) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return fmt.Errorf("failed to get PrometheusRule: %v", err) | ||
} | ||
|
||
// if it does exist, compare specs and update if different | ||
if !reflect.DeepEqual(existingPromRule.Spec, desiredPromRule.Spec) { | ||
existingPromRule.Spec = desiredPromRule.Spec | ||
if updateErr := r.Update(ctx, existingPromRule); updateErr != nil { | ||
return fmt.Errorf("failed to update PrometheusRule: %v", updateErr) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package observability_test | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/commontestutils" | ||
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/observability" | ||
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/monitoring/observability/rules" | ||
) | ||
|
||
const namespace = "observability_test" | ||
|
||
var logger = logf.Log.WithName("observability-controller") | ||
|
||
var _ = Describe("Reconcile Alerts", func() { | ||
var ( | ||
reconciler *observability.Reconciler | ||
cl client.Client | ||
promRules *promv1.PrometheusRule | ||
) | ||
|
||
BeforeEach(func() { | ||
err := rules.SetupRules() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
promRules, err = rules.BuildPrometheusRule(namespace, &metav1.OwnerReference{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
cl = commontestutils.InitClient([]client.Object{}) | ||
mgr, err := commontestutils.NewManagerMock(&rest.Config{}, manager.Options{}, cl, logger) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
reconciler = observability.NewReconciler(mgr, namespace, &appsv1.Deployment{}) | ||
}) | ||
|
||
It("Should create new PrometheusRules", func() { | ||
Expect(reconciler.ReconcileAlerts(context.TODO())).To(Succeed()) | ||
|
||
var foundPromRules promv1.PrometheusRule | ||
err := cl.Get(context.TODO(), client.ObjectKeyFromObject(promRules), &foundPromRules) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(foundPromRules.Spec).To(Equal(promRules.Spec)) | ||
}) | ||
|
||
It("Should update PrometheusRules with diffs", func() { | ||
diffPromRules := promRules.DeepCopy() | ||
diffPromRules.Spec.Groups[0].Rules[0].Expr = intstr.FromString("1") | ||
err := cl.Create(context.TODO(), diffPromRules) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(reconciler.ReconcileAlerts(context.TODO())).To(Succeed()) | ||
|
||
var foundPromRules promv1.PrometheusRule | ||
err = cl.Get(context.TODO(), client.ObjectKeyFromObject(promRules), &foundPromRules) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(foundPromRules.Spec).To(Equal(promRules.Spec)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.