Skip to content

Commit eb40c55

Browse files
committed
fixup! fixup! OCPBUGS-17113: Lazy updates for Prometheus
1 parent b140a5e commit eb40c55

File tree

4 files changed

+77
-73
lines changed

4 files changed

+77
-73
lines changed

Diff for: Makefile

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ GOPROXY?=http://proxy.golang.org
1212
export GO111MODULE
1313
export GOPROXY
1414

15-
# go pakages for unit tests, excluding e2e tests
15+
# go packages for unit tests, excluding e2e tests
1616
PKGS=$(shell go list ./... | grep -v /test/e2e)
1717
GOLANG_FILES:=$(shell find . -name \*.go -print)
1818
# NOTE: grep -v %.yaml is needed because "%s-policy.yaml" is used
@@ -249,17 +249,21 @@ check-runbooks:
249249
# Testing #
250250
###########
251251

252+
.PHONY: bench
253+
bench:
254+
go test -run NONE -bench ^Bench -benchmem $(PKGS)
255+
252256
.PHONY: test
253257
test: test-unit test-rules test-e2e
254258

255259
.PHONY: test-unit
256260
test-unit:
257-
go test -race -short $(PKGS) -count=1
261+
go test -run ^Test -race -short $(PKGS) -count=1
258262

259263
.PHONY: test-e2e
260264
test-e2e: KUBECONFIG?=$(HOME)/.kube/config
261265
test-e2e:
262-
go test -v -timeout=150m ./test/e2e/ --kubeconfig $(KUBECONFIG)
266+
go test -run ^Test -v -timeout=150m ./test/e2e/ --kubeconfig $(KUBECONFIG)
263267

264268
$(BIN_DIR):
265269
mkdir -p $(BIN_DIR)

Diff for: pkg/client/client.go

+21-65
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/imdario/mergo"
29+
configv1 "github.com/openshift/api/config/v1"
30+
consolev1 "github.com/openshift/api/console/v1"
31+
osmv1 "github.com/openshift/api/monitoring/v1"
32+
routev1 "github.com/openshift/api/route/v1"
33+
secv1 "github.com/openshift/api/security/v1"
34+
openshiftconfigclientset "github.com/openshift/client-go/config/clientset/versioned"
35+
openshiftconsoleclientset "github.com/openshift/client-go/console/clientset/versioned"
36+
openshiftmonitoringclientset "github.com/openshift/client-go/monitoring/clientset/versioned"
37+
openshiftoperatorclientset "github.com/openshift/client-go/operator/clientset/versioned"
38+
openshiftrouteclientset "github.com/openshift/client-go/route/clientset/versioned"
39+
openshiftsecurityclientset "github.com/openshift/client-go/security/clientset/versioned"
40+
"github.com/openshift/library-go/pkg/operator/events"
41+
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
42+
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
43+
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
2844
admissionv1 "k8s.io/api/admissionregistration/v1"
2945
appsv1 "k8s.io/api/apps/v1"
3046
v1 "k8s.io/api/core/v1"
@@ -52,23 +68,6 @@ import (
5268
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
5369
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
5470
"k8s.io/utils/ptr"
55-
56-
"github.com/imdario/mergo"
57-
configv1 "github.com/openshift/api/config/v1"
58-
consolev1 "github.com/openshift/api/console/v1"
59-
osmv1 "github.com/openshift/api/monitoring/v1"
60-
routev1 "github.com/openshift/api/route/v1"
61-
secv1 "github.com/openshift/api/security/v1"
62-
openshiftconfigclientset "github.com/openshift/client-go/config/clientset/versioned"
63-
openshiftconsoleclientset "github.com/openshift/client-go/console/clientset/versioned"
64-
openshiftmonitoringclientset "github.com/openshift/client-go/monitoring/clientset/versioned"
65-
openshiftoperatorclientset "github.com/openshift/client-go/operator/clientset/versioned"
66-
openshiftrouteclientset "github.com/openshift/client-go/route/clientset/versioned"
67-
openshiftsecurityclientset "github.com/openshift/client-go/security/clientset/versioned"
68-
"github.com/openshift/library-go/pkg/operator/events"
69-
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
70-
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
71-
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
7271
)
7372

7473
const (
@@ -650,6 +649,9 @@ func (c *Client) GetAlertingRule(ctx context.Context, namespace, name string) (*
650649
return c.osmclient.MonitoringV1().AlertingRules(namespace).Get(ctx, name, metav1.GetOptions{})
651650
}
652651

652+
// NOTE: We don't use a defaulting function here, and resort only to the caching mechanism, since,
653+
// * defaults for Prometheus should be introduced from its operator level, and,
654+
// * the defaulting approach generally requires hardcoding the default values, which adds on to the maintenance overhead.
653655
func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequiredPrometheus *monv1.Prometheus) (bool, error) {
654656
unstructuredRequiredPrometheusObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(structuredRequiredPrometheus)
655657
if err != nil {
@@ -676,7 +678,7 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
676678
unstructuredRequiredPrometheus,
677679
c.resourceCache,
678680
prometheusGVR,
679-
prometheusDefaultingFunc,
681+
nil,
680682
nil,
681683
)
682684
if err != nil {
@@ -703,7 +705,7 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
703705
unstructuredRequiredPrometheus,
704706
c.resourceCache,
705707
prometheusGVR,
706-
prometheusDefaultingFunc,
708+
nil,
707709
nil,
708710
)
709711
if err != nil {
@@ -713,52 +715,6 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
713715
return didUpdate, nil
714716
}
715717

716-
func prometheusDefaultingFunc(unstructuredPrometheus *unstructured.Unstructured) {
717-
// Cast to the corresponding structured representation.
718-
structuredPrometheus := &monv1.Prometheus{}
719-
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredPrometheus.UnstructuredContent(), structuredPrometheus); err != nil {
720-
klog.ErrorS(err, "failed to convert unstructured to structured Prometheus spec")
721-
return
722-
}
723-
724-
// Set defaults.
725-
if structuredPrometheus.Spec.CommonPrometheusFields.ScrapeInterval == "" {
726-
structuredPrometheus.Spec.CommonPrometheusFields.ScrapeInterval = "30s"
727-
}
728-
if len(structuredPrometheus.Spec.CommonPrometheusFields.ExternalLabels) == 0 {
729-
structuredPrometheus.Spec.CommonPrometheusFields.ExternalLabels = nil
730-
}
731-
if len(structuredPrometheus.Spec.CommonPrometheusFields.EnableFeatures) == 0 {
732-
structuredPrometheus.Spec.CommonPrometheusFields.EnableFeatures = nil
733-
}
734-
for i, container := range structuredPrometheus.Spec.CommonPrometheusFields.Containers {
735-
for j, port := range container.Ports {
736-
if port.Protocol == "" {
737-
structuredPrometheus.Spec.CommonPrometheusFields.Containers[i].Ports[j].Protocol = "TCP"
738-
}
739-
}
740-
}
741-
if structuredPrometheus.Spec.CommonPrometheusFields.PortName == "" {
742-
structuredPrometheus.Spec.CommonPrometheusFields.PortName = "web"
743-
}
744-
if structuredPrometheus.Spec.Thanos == nil {
745-
structuredPrometheus.Spec.Thanos = &monv1.ThanosSpec{}
746-
}
747-
if structuredPrometheus.Spec.Thanos.BlockDuration == "" {
748-
structuredPrometheus.Spec.Thanos.BlockDuration = "2h"
749-
}
750-
if structuredPrometheus.Spec.EvaluationInterval == "" {
751-
structuredPrometheus.Spec.EvaluationInterval = "30s"
752-
}
753-
754-
// Convert back to the corresponding unstructured representation and inject.
755-
var err error
756-
unstructuredPrometheus.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(structuredPrometheus)
757-
if err != nil {
758-
klog.ErrorS(err, "failed to convert structured to unstructured Prometheus")
759-
}
760-
}
761-
762718
func (c *Client) CreateOrUpdatePrometheusRule(ctx context.Context, p *monv1.PrometheusRule) error {
763719
pclient := c.mclient.MonitoringV1().PrometheusRules(p.GetNamespace())
764720
existing, err := pclient.Get(ctx, p.GetName(), metav1.GetOptions{})

Diff for: pkg/client/client_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1856,9 +1856,8 @@ func TestCreateOrUpdatePrometheus(t *testing.T) {
18561856
shouldUpdate bool
18571857
}{
18581858
{
1859-
description: "initially, apply all default values to the existing prometheus",
1860-
prometheus: initialPrometheus,
1861-
shouldUpdate: true,
1859+
description: "initially, apply all default values to the existing prometheus",
1860+
prometheus: initialPrometheus,
18621861
},
18631862
{
18641863
description: "apply a default value to the existing prometheus",
@@ -1871,13 +1870,15 @@ func TestCreateOrUpdatePrometheus(t *testing.T) {
18711870
},
18721871
},
18731872
},
1873+
shouldUpdate: true,
18741874
},
18751875
{
18761876
description: "apply no value to the existing prometheus",
18771877
prometheus: &monv1.Prometheus{
18781878
TypeMeta: initialPrometheus.TypeMeta,
18791879
ObjectMeta: initialPrometheus.ObjectMeta,
18801880
},
1881+
shouldUpdate: true,
18811882
},
18821883
{
18831884
description: "apply a non-default value to the existing prometheus",
@@ -2005,7 +2006,7 @@ func BenchmarkCreateOrUpdatePrometheus(b *testing.B) {
20052006
},
20062007
},
20072008
{
2008-
// Benchmark the custom-defined defaulting behavior.
2009+
// Benchmark the library-go `spec` behavior.
20092010
description: "apply the same default value to the existing prometheus",
20102011
prometheus: func() *monv1.Prometheus {
20112012
return &monv1.Prometheus{
@@ -2021,7 +2022,7 @@ func BenchmarkCreateOrUpdatePrometheus(b *testing.B) {
20212022
requiresInitialApply: true,
20222023
},
20232024
{
2024-
// Benchmark the caching behavior.
2025+
// Benchmark the library-go `metadata` behavior.
20252026
description: "apply the same custom label to the existing prometheus",
20262027
prometheus: func() *monv1.Prometheus {
20272028
return &monv1.Prometheus{

Diff for: vendor/github.com/openshift/library-go/pkg/operator/resource/resourcehelper/event_helpers.go

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)