1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 11:48:53 +00:00

Merge pull request from simonpasquier/add-optional-markers

chore: add +optional markers
This commit is contained in:
Simon Pasquier 2023-06-06 14:47:19 +02:00 committed by GitHub
commit bbad6fd85a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 3 deletions

View file

@ -10917,6 +10917,7 @@ Duration
</em>
</td>
<td>
<em>(Optional)</em>
<p>Alerts are considered firing once they have been returned for this long.</p>
</td>
</tr>
@ -10982,6 +10983,7 @@ Duration
</em>
</td>
<td>
<em>(Optional)</em>
<p>Interval determines how often rules in the group are evaluated.</p>
</td>
</tr>
@ -10995,6 +10997,7 @@ Duration
</em>
</td>
<td>
<em>(Optional)</em>
<p>List of alerting and recording rules.</p>
</td>
</tr>

View file

@ -62,8 +62,10 @@ type RuleGroup struct {
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`
// Interval determines how often rules in the group are evaluated.
Interval Duration `json:"interval,omitempty"`
// +optional
Interval *Duration `json:"interval,omitempty"`
// List of alerting and recording rules.
// +optional
Rules []Rule `json:"rules,omitempty"`
// PartialResponseStrategy is only used by ThanosRuler and will
// be ignored by Prometheus instances.
@ -90,7 +92,8 @@ type Rule struct {
// PromQL expression to evaluate.
Expr intstr.IntOrString `json:"expr"`
// Alerts are considered firing once they have been returned for this long.
For Duration `json:"for,omitempty"`
// +optional
For *Duration `json:"for,omitempty"`
// Labels to add or overwrite.
Labels map[string]string `json:"labels,omitempty"`
// Annotations to add to each alert.

View file

@ -2088,6 +2088,11 @@ func (in *RemoteWriteSpec) DeepCopy() *RemoteWriteSpec {
func (in *Rule) DeepCopyInto(out *Rule) {
*out = *in
out.Expr = in.Expr
if in.For != nil {
in, out := &in.For, &out.For
*out = new(Duration)
**out = **in
}
if in.Labels != nil {
in, out := &in.Labels, &out.Labels
*out = make(map[string]string, len(*in))
@ -2117,6 +2122,11 @@ func (in *Rule) DeepCopy() *Rule {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RuleGroup) DeepCopyInto(out *RuleGroup) {
*out = *in
if in.Interval != nil {
in, out := &in.Interval, &out.Interval
*out = new(Duration)
**out = **in
}
if in.Rules != nil {
in, out := &in.Rules, &out.Rules
*out = make([]Rule, len(*in))

View file

@ -114,6 +114,18 @@ func ValidateRule(promRule monitoringv1.PrometheusRuleSpec) []error {
// The upstream Prometheus rule validator doesn't support the
// partial_response_strategy field.
promRule.Groups[i].PartialResponseStrategy = ""
// Empty durations need to be translated to nil to be omitted from the
// YAML ouptut otherwise the generated configuration will not be valid.
if promRule.Groups[i].Interval != nil && *promRule.Groups[i].Interval == "" {
promRule.Groups[i].Interval = nil
}
for j := range promRule.Groups[i].Rules {
if promRule.Groups[i].Rules[j].For != nil && *promRule.Groups[i].Rules[j].For == "" {
promRule.Groups[i].Rules[j].For = nil
}
}
}
content, err := yaml.Marshal(promRule)

View file

@ -29,6 +29,7 @@ import (
func TestMakeRulesConfigMaps(t *testing.T) {
t.Run("shouldAcceptRuleWithValidPartialResponseStrategyValue", shouldAcceptRuleWithValidPartialResponseStrategyValue)
t.Run("shouldAcceptValidRule", shouldAcceptValidRule)
t.Run("shouldAcceptRulesWithEmptyDurations", shouldAcceptRulesWithEmptyDurations)
t.Run("shouldRejectRuleWithInvalidLabels", shouldRejectRuleWithInvalidLabels)
t.Run("shouldRejectRuleWithInvalidExpression", shouldRejectRuleWithInvalidExpression)
t.Run("shouldResetRuleWithPartialResponseStrategySet", shouldResetRuleWithPartialResponseStrategySet)
@ -67,7 +68,6 @@ func shouldAcceptRuleWithValidPartialResponseStrategyValue(t *testing.T) {
content, _ := pr.generateRulesConfiguration(rules)
if !strings.Contains(content, "partial_response_strategy: warn") {
t.Fatalf("expected `partial_response_strategy` to be set in PrometheusRule as `warn`")
}
}
@ -96,6 +96,38 @@ func shouldAcceptValidRule(t *testing.T) {
}
}
func shouldAcceptRulesWithEmptyDurations(t *testing.T) {
durationPtr := func(d string) *monitoringv1.Duration {
v := monitoringv1.Duration(d)
return &v
}
rules := &monitoringv1.PrometheusRule{
Spec: monitoringv1.PrometheusRuleSpec{Groups: []monitoringv1.RuleGroup{
{
Name: "group",
Interval: durationPtr(""),
Rules: []monitoringv1.Rule{
{
Alert: "alert",
Expr: intstr.FromString("vector(1)"),
Labels: map[string]string{
"valid_label": "valid_value",
},
For: durationPtr(""),
},
},
},
}},
}
promVersion, _ := semver.ParseTolerant(DefaultPrometheusVersion)
pr := newRuleSelectorForConfigGeneration(PrometheusFormat, promVersion)
_, err := pr.generateRulesConfiguration(rules)
if err != nil {
t.Fatalf("expected no errors when parsing valid rule")
}
}
func shouldRejectRuleWithInvalidLabels(t *testing.T) {
rules := &monitoringv1.PrometheusRule{
Spec: monitoringv1.PrometheusRuleSpec{Groups: []monitoringv1.RuleGroup{