Skip to content

Commit b140a5e

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

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

Diff for: pkg/client/client.go

+35-27
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ 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"
4428
admissionv1 "k8s.io/api/admissionregistration/v1"
4529
appsv1 "k8s.io/api/apps/v1"
4630
v1 "k8s.io/api/core/v1"
@@ -68,6 +52,23 @@ import (
6852
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
6953
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
7054
"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"
7172
)
7273

7374
const (
@@ -649,10 +650,10 @@ func (c *Client) GetAlertingRule(ctx context.Context, namespace, name string) (*
649650
return c.osmclient.MonitoringV1().AlertingRules(namespace).Get(ctx, name, metav1.GetOptions{})
650651
}
651652

652-
func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequiredPrometheus *monv1.Prometheus) (*bool, error) {
653+
func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequiredPrometheus *monv1.Prometheus) (bool, error) {
653654
unstructuredRequiredPrometheusObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(structuredRequiredPrometheus)
654655
if err != nil {
655-
return nil, fmt.Errorf("converting Prometheus object to unstructured failed: %w", err)
656+
return false, fmt.Errorf("converting Prometheus object to unstructured failed: %w", err)
656657
}
657658
unstructuredRequiredPrometheus := &unstructured.Unstructured{}
658659
unstructuredRequiredPrometheus.SetUnstructuredContent(unstructuredRequiredPrometheusObject)
@@ -668,17 +669,24 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
668669
Namespace(structuredRequiredPrometheus.GetNamespace()).
669670
Get(ctx, structuredRequiredPrometheus.GetName(), metav1.GetOptions{})
670671
if apierrors.IsNotFound(err) {
671-
_, err = c.dclient.
672-
Resource(prometheusGVR).
673-
Namespace(structuredRequiredPrometheus.GetNamespace()).
674-
Create(ctx, unstructuredRequiredPrometheus, metav1.CreateOptions{})
672+
_, didUpdate, err := resourceapply.ApplyUnstructuredResourceImproved(
673+
ctx,
674+
c.dclient,
675+
c.eventRecorder,
676+
unstructuredRequiredPrometheus,
677+
c.resourceCache,
678+
prometheusGVR,
679+
prometheusDefaultingFunc,
680+
nil,
681+
)
675682
if err != nil {
676-
return nil, fmt.Errorf("creating Prometheus object failed: %w", err)
683+
return didUpdate, fmt.Errorf("creating Prometheus object failed: %w", err)
677684
}
678-
return ptr.To(true), nil
685+
686+
return true, nil
679687
}
680688
if err != nil {
681-
return nil, fmt.Errorf("retrieving Prometheus object failed: %w", err)
689+
return false, fmt.Errorf("retrieving Prometheus object failed: %w", err)
682690
}
683691
unstructuredRequiredPrometheusMetadataLabels := unstructuredRequiredPrometheus.GetLabels()
684692
unstructuredRequiredPrometheusMetadataAnnotations := unstructuredRequiredPrometheus.GetAnnotations()
@@ -699,10 +707,10 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
699707
nil,
700708
)
701709
if err != nil {
702-
return &didUpdate, fmt.Errorf("updating Prometheus object failed: %w", err)
710+
return didUpdate, fmt.Errorf("updating Prometheus object failed: %w", err)
703711
}
704712

705-
return &didUpdate, nil
713+
return didUpdate, nil
706714
}
707715

708716
func prometheusDefaultingFunc(unstructuredPrometheus *unstructured.Unstructured) {

Diff for: pkg/client/client_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1801,8 +1801,10 @@ func TestCreateOrUpdatePrometheus(t *testing.T) {
18011801
},
18021802
}
18031803

1804+
// Initialize the type registry for the fake client, and populate that with the necessary definition(s).
18041805
s := runtime.NewScheme()
18051806
_ = monv1.AddToScheme(s)
1807+
18061808
c := Client{
18071809
dclient: dynamicFake.NewSimpleDynamicClient(s, prometheus.DeepCopy()),
18081810
eventRecorder: events.NewInMemoryRecorder("test-create-or-update-prometheus"),
@@ -1928,14 +1930,14 @@ func TestCreateOrUpdatePrometheus(t *testing.T) {
19281930
if err != nil {
19291931
t.Fatalf("unexpected error: %v", err)
19301932
}
1931-
if *didUpdate != testcase.shouldUpdate {
1932-
t.Fatalf("expected update: %v, got %v", testcase.shouldUpdate, *didUpdate)
1933+
if didUpdate != testcase.shouldUpdate {
1934+
t.Fatalf("expected update: %v, got %v", testcase.shouldUpdate, didUpdate)
19331935
}
19341936
existingPostUpdate, err := c.dclient.Resource(prometheusGVR).Namespace(testcase.prometheus.GetNamespace()).Get(context.Background(), testcase.prometheus.GetName(), metav1.GetOptions{})
19351937
if err != nil {
19361938
t.Fatalf("unexpected error: %v", err)
19371939
}
1938-
if *didUpdate && reflect.DeepEqual(existingPreUpdate, existingPostUpdate) {
1940+
if didUpdate && reflect.DeepEqual(existingPreUpdate, existingPostUpdate) {
19391941
t.Fatalf("expected update: %s", cmp.Diff(existingPreUpdate, existingPostUpdate))
19401942
}
19411943
})

0 commit comments

Comments
 (0)