Skip to content

Commit

Permalink
NETOBSERV-927 Improve servicemonitor/prometheusrule checking changes … (
Browse files Browse the repository at this point in the history
#302)

* NETOBSERV-927 Improve servicemonitor/prometheusrule checking changes perf

Only check for spec change, not meta
NB: namespace or name changes follow a different process; it is
safe to not check for name/namespace changes there; you cannot anyway
update a resource for a namespace or name change

* Check labels change, add labels to SM
  • Loading branch information
jotak authored Mar 22, 2023
1 parent 460d0b0 commit 1b56f8e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions controllers/flowlogspipeline/flp_common_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ func (b *builder) serviceMonitor() *monitoringv1.ServiceMonitor {
ObjectMeta: metav1.ObjectMeta{
Name: b.serviceMonitorName(),
Namespace: b.namespace,
Labels: b.labels,
},
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
Expand Down
29 changes: 22 additions & 7 deletions controllers/flowlogspipeline/flp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var resources = corev1.ResourceRequirements{
},
}
var image = "quay.io/netobserv/flowlogs-pipeline:dev"
var image2 = "quay.io/netobserv/flowlogs-pipeline:dev2"
var pullPolicy = corev1.PullIfNotPresent
var minReplicas = int32(1)
var maxReplicas = int32(5)
Expand Down Expand Up @@ -494,13 +495,20 @@ func TestServiceMonitorChanged(t *testing.T) {
first := b.generic.serviceMonitor()

// Check namespace change
cfg.Processor.Metrics.Server.Port = 9999
b = newMonolithBuilder("namespace2", image, &cfg, true, &certWatcher)
second := b.generic.serviceMonitor()

report := helper.NewChangeReport("")
assert.True(helper.ServiceMonitorChanged(first, second, &report))
assert.Contains(report.String(), "ServiceMonitor meta changed")
assert.Contains(report.String(), "ServiceMonitor spec changed")

// Check labels change
b = newMonolithBuilder("namespace2", image2, &cfg, true, &certWatcher)
third := b.generic.serviceMonitor()

report = helper.NewChangeReport("")
assert.True(helper.ServiceMonitorChanged(second, third, &report))
assert.Contains(report.String(), "ServiceMonitor labels changed")
}

func TestPrometheusRuleNoChange(t *testing.T) {
Expand All @@ -524,19 +532,26 @@ func TestPrometheusRuleChanged(t *testing.T) {
assert := assert.New(t)

// Get first
ns := "namespace"
cfg := getConfig()
b := newMonolithBuilder(ns, image, &cfg, true, &certWatcher)
b := newMonolithBuilder("namespace", image, &cfg, true, &certWatcher)
first := b.generic.prometheusRule()

// Check namespace change
cfg.Processor.Metrics.Server.Port = 9999
b = newMonolithBuilder("namespace2", image, &cfg, true, &certWatcher)
cfg.Processor.Metrics.DisableAlerts = []flowslatest.FLPAlert{flowslatest.AlertNoFlows}
b = newMonolithBuilder("namespace", image, &cfg, true, &certWatcher)
second := b.generic.prometheusRule()

report := helper.NewChangeReport("")
assert.True(helper.PrometheusRuleChanged(first, second, &report))
assert.Contains(report.String(), "PrometheusRule meta changed")
assert.Contains(report.String(), "PrometheusRule spec changed")

// Check labels change
b = newMonolithBuilder("namespace2", image2, &cfg, true, &certWatcher)
third := b.generic.prometheusRule()

report = helper.NewChangeReport("")
assert.True(helper.PrometheusRuleChanged(second, third, &report))
assert.Contains(report.String(), "PrometheusRule labels changed")
}

func TestConfigMapShouldDeserializeAsJSON(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/helper/comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func ServiceChanged(old, new *corev1.Service, report *ChangeReport) bool {
}

func ServiceMonitorChanged(old, new *monitoringv1.ServiceMonitor, report *ChangeReport) bool {
return report.Check("ServiceMonitor meta changed", !equality.Semantic.DeepDerivative(new.ObjectMeta, old.ObjectMeta)) ||
report.Check("ServiceMonitor spec changed", !equality.Semantic.DeepDerivative(new.Spec, old.Spec))
return report.Check("ServiceMonitor spec changed", !equality.Semantic.DeepDerivative(new.Spec, old.Spec)) ||
report.Check("ServiceMonitor labels changed", !IsSubSet(old.Labels, new.Labels))
}

func PrometheusRuleChanged(old, new *monitoringv1.PrometheusRule, report *ChangeReport) bool {
return report.Check("PrometheusRule meta changed", !equality.Semantic.DeepDerivative(new.ObjectMeta, old.ObjectMeta)) ||
report.Check("PrometheusRule spec changed", !equality.Semantic.DeepDerivative(new.Spec, old.Spec))
return report.Check("PrometheusRule spec changed", !equality.Semantic.DeepDerivative(new.Spec, old.Spec)) ||
report.Check("PrometheusRule labels changed", !IsSubSet(old.Labels, new.Labels))
}

// FindContainer searches in pod containers one that matches the provided name
Expand Down

0 comments on commit 1b56f8e

Please sign in to comment.