Skip to content

Commit ee3159f

Browse files
authored
Only add to scheme when prometheus is available (#2811)
* Only add to scheme when prometheus is available * fix the bad stuff * logs and better changelog
1 parent 47f78af commit ee3159f

17 files changed

+230
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Introduces ability to detect presence of Prometheus CRDs to dynamically add to scheme to prevent startup issues.
9+
10+
# One or more tracking issues related to the change
11+
issues: [2180]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

controllers/opampbridge_controller_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
3737
"github.com/open-telemetry/opentelemetry-operator/controllers"
3838
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
39+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
3940
"github.com/open-telemetry/opentelemetry-operator/internal/config"
4041
)
4142

@@ -44,6 +45,9 @@ var opampBridgeMockAutoDetector = &mockAutoDetect{
4445
OpenShiftRoutesAvailabilityFunc: func() (openshift.RoutesAvailability, error) {
4546
return openshift.RoutesAvailable, nil
4647
},
48+
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
49+
return prometheus.Available, nil
50+
},
4751
}
4852

4953
func TestNewObjectsOnReconciliation_OpAMPBridge(t *testing.T) {

controllers/opentelemetrycollector_controller.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
4040
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
4141
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
42+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
4243
"github.com/open-telemetry/opentelemetry-operator/internal/config"
4344
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
4445
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
@@ -80,7 +81,7 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
8081
for i := range hpaList.Items {
8182
ownedObjects[hpaList.Items[i].GetUID()] = &hpaList.Items[i]
8283
}
83-
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
84+
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability() == prometheus.Available {
8485
servicemonitorList := &monitoringv1.ServiceMonitorList{}
8586
err = r.List(ctx, servicemonitorList, listOps)
8687
if err != nil {
@@ -249,7 +250,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er
249250
builder.Owns(&rbacv1.ClusterRole{})
250251
}
251252

252-
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
253+
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability() == prometheus.Available {
253254
builder.Owns(&monitoringv1.ServiceMonitor{})
254255
builder.Owns(&monitoringv1.PodMonitor{})
255256
}

controllers/reconcile_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
4343
"github.com/open-telemetry/opentelemetry-operator/controllers"
4444
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
45+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
4546
"github.com/open-telemetry/opentelemetry-operator/internal/config"
4647
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
4748
ta "github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator/adapters"
@@ -556,6 +557,7 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
556557
config.WithCollectorImage("default-collector"),
557558
config.WithTargetAllocatorImage("default-ta-allocator"),
558559
config.WithOpenShiftRoutesAvailability(openshift.RoutesAvailable),
560+
config.WithPrometheusCRAvailability(prometheus.Available),
559561
),
560562
})
561563

controllers/suite_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
5555
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
5656
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
57+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
5758
"github.com/open-telemetry/opentelemetry-operator/internal/config"
5859
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
5960
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/testdata"
@@ -92,6 +93,14 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)
9293

9394
type mockAutoDetect struct {
9495
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
96+
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
97+
}
98+
99+
func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
100+
if m.PrometheusCRsAvailabilityFunc != nil {
101+
return m.PrometheusCRsAvailabilityFunc()
102+
}
103+
return prometheus.NotAvailable, nil
95104
}
96105

97106
func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {

internal/autodetect/main.go

+19
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"k8s.io/client-go/rest"
2121

2222
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
23+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2324
)
2425

2526
var _ AutoDetect = (*autoDetect)(nil)
2627

2728
// AutoDetect provides an assortment of routines that auto-detect traits based on the runtime.
2829
type AutoDetect interface {
2930
OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error)
31+
PrometheusCRsAvailability() (prometheus.Availability, error)
3032
}
3133

3234
type autoDetect struct {
@@ -48,6 +50,23 @@ func New(restConfig *rest.Config) (AutoDetect, error) {
4850
}, nil
4951
}
5052

53+
// PrometheusCRsAvailability checks if Prometheus CRDs are available.
54+
func (a *autoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
55+
apiList, err := a.dcl.ServerGroups()
56+
if err != nil {
57+
return prometheus.NotAvailable, err
58+
}
59+
60+
apiGroups := apiList.Groups
61+
for i := 0; i < len(apiGroups); i++ {
62+
if apiGroups[i].Name == "monitoring.coreos.com" {
63+
return prometheus.Available, nil
64+
}
65+
}
66+
67+
return prometheus.NotAvailable, nil
68+
}
69+
5170
// OpenShiftRoutesAvailability checks if OpenShift Route are available.
5271
func (a *autoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
5372
apiList, err := a.dcl.ServerGroups()

internal/autodetect/main_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2929
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
30+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
3031
)
3132

3233
func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
@@ -71,3 +72,46 @@ func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
7172
assert.Equal(t, tt.expected, ora)
7273
}
7374
}
75+
76+
func TestDetectPlatformBasedOnAvailableAPIGroupsPrometheus(t *testing.T) {
77+
for _, tt := range []struct {
78+
apiGroupList *metav1.APIGroupList
79+
expected prometheus.Availability
80+
}{
81+
{
82+
&metav1.APIGroupList{},
83+
prometheus.NotAvailable,
84+
},
85+
{
86+
&metav1.APIGroupList{
87+
Groups: []metav1.APIGroup{
88+
{
89+
Name: "monitoring.coreos.com",
90+
},
91+
},
92+
},
93+
prometheus.Available,
94+
},
95+
} {
96+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
97+
output, err := json.Marshal(tt.apiGroupList)
98+
require.NoError(t, err)
99+
100+
w.Header().Set("Content-Type", "application/json")
101+
w.WriteHeader(http.StatusOK)
102+
_, err = w.Write(output)
103+
require.NoError(t, err)
104+
}))
105+
defer server.Close()
106+
107+
autoDetect, err := autodetect.New(&rest.Config{Host: server.URL})
108+
require.NoError(t, err)
109+
110+
// test
111+
ora, err := autoDetect.PrometheusCRsAvailability()
112+
113+
// verify
114+
assert.NoError(t, err)
115+
assert.Equal(t, tt.expected, ora)
116+
}
117+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package prometheus
16+
17+
// Availability represents what CRDs are available from the prometheus operator.
18+
type Availability int
19+
20+
const (
21+
// NotAvailable represents the monitoring.coreos.com is not available.
22+
NotAvailable Availability = iota
23+
24+
// Available represents the monitoring.coreos.com is available.
25+
Available
26+
)
27+
28+
func (p Availability) String() string {
29+
return [...]string{"NotAvailable", "Available"}[p]
30+
}

internal/config/main.go

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2525
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
26+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2627
"github.com/open-telemetry/opentelemetry-operator/internal/version"
2728
)
2829

@@ -57,6 +58,7 @@ type Config struct {
5758
autoInstrumentationNodeJSImage string
5859
autoInstrumentationJavaImage string
5960
openshiftRoutesAvailability openshift.RoutesAvailability
61+
prometheusCRAvailability prometheus.Availability
6062
labelsFilter []string
6163
annotationsFilter []string
6264
}
@@ -65,6 +67,7 @@ type Config struct {
6567
func New(opts ...Option) Config {
6668
// initialize with the default values
6769
o := options{
70+
prometheusCRAvailability: prometheus.NotAvailable,
6871
openshiftRoutesAvailability: openshift.RoutesNotAvailable,
6972
collectorConfigMapEntry: defaultCollectorConfigMapEntry,
7073
targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry,
@@ -92,6 +95,7 @@ func New(opts ...Option) Config {
9295
operatorOpAMPBridgeConfigMapEntry: o.operatorOpAMPBridgeConfigMapEntry,
9396
logger: o.logger,
9497
openshiftRoutesAvailability: o.openshiftRoutesAvailability,
98+
prometheusCRAvailability: o.prometheusCRAvailability,
9599
autoInstrumentationJavaImage: o.autoInstrumentationJavaImage,
96100
autoInstrumentationNodeJSImage: o.autoInstrumentationNodeJSImage,
97101
autoInstrumentationPythonImage: o.autoInstrumentationPythonImage,
@@ -113,6 +117,12 @@ func (c *Config) AutoDetect() error {
113117
return err
114118
}
115119
c.openshiftRoutesAvailability = ora
120+
121+
pcrd, err := c.autoDetect.PrometheusCRsAvailability()
122+
if err != nil {
123+
return err
124+
}
125+
c.prometheusCRAvailability = pcrd
116126
return nil
117127
}
118128

@@ -181,6 +191,11 @@ func (c *Config) OpenShiftRoutesAvailability() openshift.RoutesAvailability {
181191
return c.openshiftRoutesAvailability
182192
}
183193

194+
// PrometheusCRAvailability represents the availability of the Prometheus Operator CRDs.
195+
func (c *Config) PrometheusCRAvailability() prometheus.Availability {
196+
return c.prometheusCRAvailability
197+
}
198+
184199
// AutoInstrumentationJavaImage returns OpenTelemetry Java auto-instrumentation container image.
185200
func (c *Config) AutoInstrumentationJavaImage() string {
186201
return c.autoInstrumentationJavaImage

internal/config/main_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2424
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
25+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2526
"github.com/open-telemetry/opentelemetry-operator/internal/config"
2627
)
2728

@@ -31,12 +32,14 @@ func TestNewConfig(t *testing.T) {
3132
config.WithCollectorImage("some-image"),
3233
config.WithCollectorConfigMapEntry("some-config.yaml"),
3334
config.WithOpenShiftRoutesAvailability(openshift.RoutesAvailable),
35+
config.WithPrometheusCRAvailability(prometheus.Available),
3436
)
3537

3638
// test
3739
assert.Equal(t, "some-image", cfg.CollectorImage())
3840
assert.Equal(t, "some-config.yaml", cfg.CollectorConfigMapEntry())
3941
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenShiftRoutesAvailability())
42+
assert.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability())
4043
}
4144

4245
func TestConfigChangesOnAutoDetect(t *testing.T) {
@@ -45,26 +48,32 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
4548
OpenShiftRoutesAvailabilityFunc: func() (openshift.RoutesAvailability, error) {
4649
return openshift.RoutesAvailable, nil
4750
},
51+
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
52+
return prometheus.Available, nil
53+
},
4854
}
4955
cfg := config.New(
5056
config.WithAutoDetect(mock),
5157
)
5258

5359
// sanity check
5460
require.Equal(t, openshift.RoutesNotAvailable, cfg.OpenShiftRoutesAvailability())
61+
require.Equal(t, prometheus.NotAvailable, cfg.PrometheusCRAvailability())
5562

5663
// test
5764
err := cfg.AutoDetect()
5865
require.NoError(t, err)
5966

6067
// verify
6168
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenShiftRoutesAvailability())
69+
require.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability())
6270
}
6371

6472
var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)
6573

6674
type mockAutoDetect struct {
6775
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
76+
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
6877
}
6978

7079
func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
@@ -73,3 +82,10 @@ func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailabi
7382
}
7483
return openshift.RoutesNotAvailable, nil
7584
}
85+
86+
func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
87+
if m.PrometheusCRsAvailabilityFunc != nil {
88+
return m.PrometheusCRsAvailabilityFunc()
89+
}
90+
return prometheus.NotAvailable, nil
91+
}

internal/config/options.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2424
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
25+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2526
"github.com/open-telemetry/opentelemetry-operator/internal/version"
2627
)
2728

@@ -52,6 +53,7 @@ type options struct {
5253
targetAllocatorImage string
5354
operatorOpAMPBridgeImage string
5455
openshiftRoutesAvailability openshift.RoutesAvailability
56+
prometheusCRAvailability prometheus.Availability
5557
labelsFilter []string
5658
annotationsFilter []string
5759
}
@@ -180,6 +182,12 @@ func WithOpenShiftRoutesAvailability(os openshift.RoutesAvailability) Option {
180182
}
181183
}
182184

185+
func WithPrometheusCRAvailability(pcrd prometheus.Availability) Option {
186+
return func(o *options) {
187+
o.prometheusCRAvailability = pcrd
188+
}
189+
}
190+
183191
func WithLabelFilters(labelFilters []string) Option {
184192
return func(o *options) {
185193

internal/manifests/collector/podmonitor.go

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323

2424
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
25+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2526
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
2627
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters"
2728
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils"
@@ -36,6 +37,12 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) {
3637
"params.OtelCol.namespace", params.OtelCol.Namespace,
3738
)
3839
return nil, nil
40+
} else if params.Config.PrometheusCRAvailability() == prometheus.NotAvailable {
41+
params.Log.V(1).Info("Cannot enable PodMonitor when prometheus CRDs are unavailable",
42+
"params.OtelCol.name", params.OtelCol.Name,
43+
"params.OtelCol.namespace", params.OtelCol.Namespace,
44+
)
45+
return nil, nil
3946
}
4047
var pm monitoringv1.PodMonitor
4148

0 commit comments

Comments
 (0)