@@ -25,6 +25,22 @@ import (
25
25
"strings"
26
26
"time"
27
27
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"
28
44
admissionv1 "k8s.io/api/admissionregistration/v1"
29
45
appsv1 "k8s.io/api/apps/v1"
30
46
v1 "k8s.io/api/core/v1"
@@ -52,23 +68,6 @@ import (
52
68
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
53
69
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
54
70
"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"
72
71
)
73
72
74
73
const (
@@ -650,6 +649,9 @@ func (c *Client) GetAlertingRule(ctx context.Context, namespace, name string) (*
650
649
return c .osmclient .MonitoringV1 ().AlertingRules (namespace ).Get (ctx , name , metav1.GetOptions {})
651
650
}
652
651
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.
653
655
func (c * Client ) CreateOrUpdatePrometheus (ctx context.Context , structuredRequiredPrometheus * monv1.Prometheus ) (bool , error ) {
654
656
unstructuredRequiredPrometheusObject , err := runtime .DefaultUnstructuredConverter .ToUnstructured (structuredRequiredPrometheus )
655
657
if err != nil {
@@ -676,7 +678,7 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
676
678
unstructuredRequiredPrometheus ,
677
679
c .resourceCache ,
678
680
prometheusGVR ,
679
- prometheusDefaultingFunc ,
681
+ nil ,
680
682
nil ,
681
683
)
682
684
if err != nil {
@@ -703,7 +705,7 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
703
705
unstructuredRequiredPrometheus ,
704
706
c .resourceCache ,
705
707
prometheusGVR ,
706
- prometheusDefaultingFunc ,
708
+ nil ,
707
709
nil ,
708
710
)
709
711
if err != nil {
@@ -713,52 +715,6 @@ func (c *Client) CreateOrUpdatePrometheus(ctx context.Context, structuredRequire
713
715
return didUpdate , nil
714
716
}
715
717
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
-
762
718
func (c * Client ) CreateOrUpdatePrometheusRule (ctx context.Context , p * monv1.PrometheusRule ) error {
763
719
pclient := c .mclient .MonitoringV1 ().PrometheusRules (p .GetNamespace ())
764
720
existing , err := pclient .Get (ctx , p .GetName (), metav1.GetOptions {})
0 commit comments