-
Notifications
You must be signed in to change notification settings - Fork 1
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
Parameterize absent pr name #153
Merged
talal
merged 4 commits into
sapcc:parametrize-promrule-name
from
conallprendergast:Parameterize-Absent-PR-Name
Sep 25, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,26 +21,43 @@ import ( | |
"reflect" | ||
"sort" | ||
"time" | ||
|
||
"text/template" | ||
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
const absencePromRuleNameSuffix = "-absent-metric-alert-rules" | ||
|
||
|
||
// AbsencePrometheusRuleName returns the name of an AbsencePrometheusRule resource that | ||
// holds the absence alert rules concerning a specific Prometheus server (e.g. openstack, kubernetes, etc.). | ||
func AbsencePrometheusRuleName(promServer string) string { | ||
return fmt.Sprintf("%s%s", promServer, absencePromRuleNameSuffix) | ||
func AbsencePrometheusRuleName(prometheusRule monitoringv1.PrometheusRule, prometheusRuleString string) string { | ||
|
||
t := template.Must(template.New("PrometheusRuleTemplate").Parse(prometheusRuleString)) | ||
b, err := json.Marshal(prometheusRule) | ||
|
||
m := make(map[string]interface{}) | ||
err = json.Unmarshal(b, &m) | ||
|
||
buf := &bytes.Buffer{} | ||
err = t.Execute(buf, m) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return "default-absent-metrics" | ||
} | ||
|
||
return buf.String() | ||
} | ||
|
||
func (r *PrometheusRuleReconciler) newAbsencePrometheusRule(namespace, promServer string) *monitoringv1.PrometheusRule { | ||
func (r *PrometheusRuleReconciler) newAbsencePrometheusRule(namespace, name string, promServer string) *monitoringv1.PrometheusRule { | ||
return &monitoringv1.PrometheusRule{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: AbsencePrometheusRuleName(promServer), | ||
Name: name, | ||
Namespace: namespace, | ||
Labels: map[string]string{ | ||
// Add a label that identifies that this PrometheusRule resource is | ||
|
@@ -55,11 +72,12 @@ func (r *PrometheusRuleReconciler) newAbsencePrometheusRule(namespace, promServe | |
|
||
func (r *PrometheusRuleReconciler) getExistingAbsencePrometheusRule( | ||
ctx context.Context, | ||
namespace, promServer string, | ||
namespace, prometheusRuleString string, | ||
rule monitoringv1.PrometheusRule, | ||
) (*monitoringv1.PrometheusRule, error) { | ||
|
||
var absencePromRule monitoringv1.PrometheusRule | ||
nsName := types.NamespacedName{Namespace: namespace, Name: AbsencePrometheusRuleName(promServer)} | ||
nsName := types.NamespacedName{Namespace: namespace, Name: AbsencePrometheusRuleName(rule, prometheusRuleString)} | ||
if err := r.Get(ctx, nsName, &absencePromRule); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -134,13 +152,20 @@ func (r *PrometheusRuleReconciler) cleanUpOrphanedAbsenceAlertRules( | |
ctx context.Context, | ||
promRule types.NamespacedName, | ||
promServer string, | ||
prometheusRuleString string, | ||
) error { | ||
|
||
var promRuleObj monitoringv1.PrometheusRule | ||
if err := r.Get(ctx, promRule, &promRuleObj); err != nil { | ||
return err | ||
} | ||
|
||
|
||
// Step 1: find the corresponding AbsencePrometheusRule that needs to be cleaned up. | ||
var aPRToClean *monitoringv1.PrometheusRule | ||
if promServer != "" { | ||
var err error | ||
if aPRToClean, err = r.getExistingAbsencePrometheusRule(ctx, promRule.Namespace, promServer); err != nil { | ||
if aPRToClean, err = r.getExistingAbsencePrometheusRule(ctx, promRule.Namespace, prometheusRuleString, promRuleObj); err != nil { | ||
return err | ||
} | ||
} else { | ||
|
@@ -204,9 +229,6 @@ func (r *PrometheusRuleReconciler) cleanUpAbsencePrometheusRule(ctx context.Cont | |
// concerning Prometheus server. | ||
var listOpts client.ListOptions | ||
client.InNamespace(absencePromRule.GetNamespace()).ApplyToList(&listOpts) | ||
client.MatchingLabels{ | ||
labelPrometheusServer: absencePromRule.Labels[labelPrometheusServer], | ||
}.ApplyToList(&listOpts) | ||
var promRules monitoringv1.PrometheusRuleList | ||
if err := r.List(ctx, &promRules, &listOpts); err != nil { | ||
return err | ||
|
@@ -242,7 +264,7 @@ func (r *PrometheusRuleReconciler) cleanUpAbsencePrometheusRule(ctx context.Cont | |
|
||
// updateAbsenceAlertRules generates absence alert rules for the given PrometheusRule and | ||
// adds them to the corresponding AbsencePrometheusRule. | ||
func (r *PrometheusRuleReconciler) updateAbsenceAlertRules(ctx context.Context, promRule *monitoringv1.PrometheusRule) error { | ||
func (r *PrometheusRuleReconciler) updateAbsenceAlertRules(ctx context.Context, promRule *monitoringv1.PrometheusRule, prometheusRuleString string) error { | ||
promRuleName := promRule.GetName() | ||
namespace := promRule.GetNamespace() | ||
log := r.Log.WithValues("name", promRuleName, "namespace", namespace) | ||
|
@@ -252,19 +274,21 @@ func (r *PrometheusRuleReconciler) updateAbsenceAlertRules(ctx context.Context, | |
promServer, ok := promRuleLabels["prometheus"] | ||
if !ok { | ||
// Normally this shouldn't happen but just in case that it does. | ||
return errors.New("no 'prometheus' label found") | ||
promServer = "default-prometheus" | ||
// return errors.New("no 'prometheus' label found") | ||
Comment on lines
+277
to
+278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like some leftover debugging code |
||
} | ||
|
||
// Step 2: get the corresponding AbsencePrometheusRule if it exists. We do this in | ||
// advance so that we can get suitable defaults for tier and service labels in the | ||
// next step. | ||
existingAbsencePrometheusRule := false | ||
absencePromRule, err := r.getExistingAbsencePrometheusRule(ctx, namespace, promServer) | ||
absencePromRule, err := r.getExistingAbsencePrometheusRule(ctx, namespace, prometheusRuleString, *promRule) | ||
switch { | ||
case err == nil: | ||
existingAbsencePrometheusRule = true | ||
case apierrors.IsNotFound(err): | ||
absencePromRule = r.newAbsencePrometheusRule(namespace, promServer) | ||
name := AbsencePrometheusRuleName(*promRule, prometheusRuleString) | ||
absencePromRule = r.newAbsencePrometheusRule(namespace, name, promServer) | ||
default: | ||
// This could have been caused by a temporary network failure, or any | ||
// other transient reason. | ||
|
@@ -310,7 +334,7 @@ func (r *PrometheusRuleReconciler) updateAbsenceAlertRules(ctx context.Context, | |
if len(absenceRuleGroups) == 0 { | ||
if existingAbsencePrometheusRule { | ||
key := types.NamespacedName{Namespace: namespace, Name: promRuleName} | ||
return r.cleanUpOrphanedAbsenceAlertRules(ctx, key, promServer) | ||
return r.cleanUpOrphanedAbsenceAlertRules(ctx, key, promServer, prometheusRuleString) | ||
} | ||
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the standard logging methods used elsewhere in the program