Skip to content

Commit

Permalink
Only add to scheme when prometheus is available (open-telemetry#2811)
Browse files Browse the repository at this point in the history
* Only add to scheme when prometheus is available

* fix the bad stuff

* logs and better changelog
  • Loading branch information
jaronoff97 authored Apr 5, 2024
1 parent 47f78af commit ee3159f
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Introduces ability to detect presence of Prometheus CRDs to dynamically add to scheme to prevent startup issues.

# One or more tracking issues related to the change
issues: [2180]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
4 changes: 4 additions & 0 deletions controllers/opampbridge_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/controllers"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
)

Expand All @@ -44,6 +45,9 @@ var opampBridgeMockAutoDetector = &mockAutoDetect{
OpenShiftRoutesAvailabilityFunc: func() (openshift.RoutesAvailability, error) {
return openshift.RoutesAvailable, nil
},
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
return prometheus.Available, nil
},
}

func TestNewObjectsOnReconciliation_OpAMPBridge(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
for i := range hpaList.Items {
ownedObjects[hpaList.Items[i].GetUID()] = &hpaList.Items[i]
}
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability() == prometheus.Available {
servicemonitorList := &monitoringv1.ServiceMonitorList{}
err = r.List(ctx, servicemonitorList, listOps)
if err != nil {
Expand Down Expand Up @@ -249,7 +250,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er
builder.Owns(&rbacv1.ClusterRole{})
}

if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability() == prometheus.Available {
builder.Owns(&monitoringv1.ServiceMonitor{})
builder.Owns(&monitoringv1.PodMonitor{})
}
Expand Down
2 changes: 2 additions & 0 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/controllers"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
ta "github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator/adapters"
Expand Down Expand Up @@ -556,6 +557,7 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
config.WithCollectorImage("default-collector"),
config.WithTargetAllocatorImage("default-ta-allocator"),
config.WithOpenShiftRoutesAvailability(openshift.RoutesAvailable),
config.WithPrometheusCRAvailability(prometheus.Available),
),
})

Expand Down
9 changes: 9 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/testdata"
Expand Down Expand Up @@ -92,6 +93,14 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)

type mockAutoDetect struct {
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
}

func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
if m.PrometheusCRsAvailabilityFunc != nil {
return m.PrometheusCRsAvailabilityFunc()
}
return prometheus.NotAvailable, nil
}

func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
Expand Down
19 changes: 19 additions & 0 deletions internal/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"k8s.io/client-go/rest"

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
)

var _ AutoDetect = (*autoDetect)(nil)

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

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

// PrometheusCRsAvailability checks if Prometheus CRDs are available.
func (a *autoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
apiList, err := a.dcl.ServerGroups()
if err != nil {
return prometheus.NotAvailable, err
}

apiGroups := apiList.Groups
for i := 0; i < len(apiGroups); i++ {
if apiGroups[i].Name == "monitoring.coreos.com" {
return prometheus.Available, nil
}
}

return prometheus.NotAvailable, nil
}

// OpenShiftRoutesAvailability checks if OpenShift Route are available.
func (a *autoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
apiList, err := a.dcl.ServerGroups()
Expand Down
44 changes: 44 additions & 0 deletions internal/autodetect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
)

func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
Expand Down Expand Up @@ -71,3 +72,46 @@ func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
assert.Equal(t, tt.expected, ora)
}
}

func TestDetectPlatformBasedOnAvailableAPIGroupsPrometheus(t *testing.T) {
for _, tt := range []struct {
apiGroupList *metav1.APIGroupList
expected prometheus.Availability
}{
{
&metav1.APIGroupList{},
prometheus.NotAvailable,
},
{
&metav1.APIGroupList{
Groups: []metav1.APIGroup{
{
Name: "monitoring.coreos.com",
},
},
},
prometheus.Available,
},
} {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
output, err := json.Marshal(tt.apiGroupList)
require.NoError(t, err)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(output)
require.NoError(t, err)
}))
defer server.Close()

autoDetect, err := autodetect.New(&rest.Config{Host: server.URL})
require.NoError(t, err)

// test
ora, err := autoDetect.PrometheusCRsAvailability()

// verify
assert.NoError(t, err)
assert.Equal(t, tt.expected, ora)
}
}
30 changes: 30 additions & 0 deletions internal/autodetect/prometheus/operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package prometheus

// Availability represents what CRDs are available from the prometheus operator.
type Availability int

const (
// NotAvailable represents the monitoring.coreos.com is not available.
NotAvailable Availability = iota

// Available represents the monitoring.coreos.com is available.
Available
)

func (p Availability) String() string {
return [...]string{"NotAvailable", "Available"}[p]
}
15 changes: 15 additions & 0 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
)

Expand Down Expand Up @@ -57,6 +58,7 @@ type Config struct {
autoInstrumentationNodeJSImage string
autoInstrumentationJavaImage string
openshiftRoutesAvailability openshift.RoutesAvailability
prometheusCRAvailability prometheus.Availability
labelsFilter []string
annotationsFilter []string
}
Expand All @@ -65,6 +67,7 @@ type Config struct {
func New(opts ...Option) Config {
// initialize with the default values
o := options{
prometheusCRAvailability: prometheus.NotAvailable,
openshiftRoutesAvailability: openshift.RoutesNotAvailable,
collectorConfigMapEntry: defaultCollectorConfigMapEntry,
targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry,
Expand Down Expand Up @@ -92,6 +95,7 @@ func New(opts ...Option) Config {
operatorOpAMPBridgeConfigMapEntry: o.operatorOpAMPBridgeConfigMapEntry,
logger: o.logger,
openshiftRoutesAvailability: o.openshiftRoutesAvailability,
prometheusCRAvailability: o.prometheusCRAvailability,
autoInstrumentationJavaImage: o.autoInstrumentationJavaImage,
autoInstrumentationNodeJSImage: o.autoInstrumentationNodeJSImage,
autoInstrumentationPythonImage: o.autoInstrumentationPythonImage,
Expand All @@ -113,6 +117,12 @@ func (c *Config) AutoDetect() error {
return err
}
c.openshiftRoutesAvailability = ora

pcrd, err := c.autoDetect.PrometheusCRsAvailability()
if err != nil {
return err
}
c.prometheusCRAvailability = pcrd
return nil
}

Expand Down Expand Up @@ -181,6 +191,11 @@ func (c *Config) OpenShiftRoutesAvailability() openshift.RoutesAvailability {
return c.openshiftRoutesAvailability
}

// PrometheusCRAvailability represents the availability of the Prometheus Operator CRDs.
func (c *Config) PrometheusCRAvailability() prometheus.Availability {
return c.prometheusCRAvailability
}

// AutoInstrumentationJavaImage returns OpenTelemetry Java auto-instrumentation container image.
func (c *Config) AutoInstrumentationJavaImage() string {
return c.autoInstrumentationJavaImage
Expand Down
16 changes: 16 additions & 0 deletions internal/config/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
)

Expand All @@ -31,12 +32,14 @@ func TestNewConfig(t *testing.T) {
config.WithCollectorImage("some-image"),
config.WithCollectorConfigMapEntry("some-config.yaml"),
config.WithOpenShiftRoutesAvailability(openshift.RoutesAvailable),
config.WithPrometheusCRAvailability(prometheus.Available),
)

// test
assert.Equal(t, "some-image", cfg.CollectorImage())
assert.Equal(t, "some-config.yaml", cfg.CollectorConfigMapEntry())
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenShiftRoutesAvailability())
assert.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability())
}

func TestConfigChangesOnAutoDetect(t *testing.T) {
Expand All @@ -45,26 +48,32 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
OpenShiftRoutesAvailabilityFunc: func() (openshift.RoutesAvailability, error) {
return openshift.RoutesAvailable, nil
},
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
return prometheus.Available, nil
},
}
cfg := config.New(
config.WithAutoDetect(mock),
)

// sanity check
require.Equal(t, openshift.RoutesNotAvailable, cfg.OpenShiftRoutesAvailability())
require.Equal(t, prometheus.NotAvailable, cfg.PrometheusCRAvailability())

// test
err := cfg.AutoDetect()
require.NoError(t, err)

// verify
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenShiftRoutesAvailability())
require.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability())
}

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

type mockAutoDetect struct {
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
}

func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
Expand All @@ -73,3 +82,10 @@ func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailabi
}
return openshift.RoutesNotAvailable, nil
}

func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
if m.PrometheusCRsAvailabilityFunc != nil {
return m.PrometheusCRsAvailabilityFunc()
}
return prometheus.NotAvailable, nil
}
8 changes: 8 additions & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
)

Expand Down Expand Up @@ -52,6 +53,7 @@ type options struct {
targetAllocatorImage string
operatorOpAMPBridgeImage string
openshiftRoutesAvailability openshift.RoutesAvailability
prometheusCRAvailability prometheus.Availability
labelsFilter []string
annotationsFilter []string
}
Expand Down Expand Up @@ -180,6 +182,12 @@ func WithOpenShiftRoutesAvailability(os openshift.RoutesAvailability) Option {
}
}

func WithPrometheusCRAvailability(pcrd prometheus.Availability) Option {
return func(o *options) {
o.prometheusCRAvailability = pcrd
}
}

func WithLabelFilters(labelFilters []string) Option {
return func(o *options) {

Expand Down
7 changes: 7 additions & 0 deletions internal/manifests/collector/podmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils"
Expand All @@ -36,6 +37,12 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) {
"params.OtelCol.namespace", params.OtelCol.Namespace,
)
return nil, nil
} else if params.Config.PrometheusCRAvailability() == prometheus.NotAvailable {
params.Log.V(1).Info("Cannot enable PodMonitor when prometheus CRDs are unavailable",
"params.OtelCol.name", params.OtelCol.Name,
"params.OtelCol.namespace", params.OtelCol.Namespace,
)
return nil, nil
}
var pm monitoringv1.PodMonitor

Expand Down
Loading

0 comments on commit ee3159f

Please sign in to comment.