Skip to content

Commit 34b8e86

Browse files
committed
Use TA CRD only if it's available
This way we won't break installations which don't have the new CRD yet for whatever reason.
1 parent 37ea35e commit 34b8e86

File tree

9 files changed

+138
-25
lines changed

9 files changed

+138
-25
lines changed

controllers/suite_test.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import (
6060
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
6161
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
6262
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
63+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
6364
"github.com/open-telemetry/opentelemetry-operator/internal/config"
6465
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
6566
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/testdata"
@@ -101,26 +102,27 @@ type mockAutoDetect struct {
101102
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
102103
RBACPermissionsFunc func(ctx context.Context) (autoRBAC.Availability, error)
103104
CertManagerAvailabilityFunc func(ctx context.Context) (certmanager.Availability, error)
105+
TargetAllocatorAvailabilityFunc func() (targetallocator.Availability, error)
104106
}
105107

106-
func (m *mockAutoDetect) FIPSEnabled(ctx context.Context) bool {
108+
func (m *mockAutoDetect) FIPSEnabled(_ context.Context) bool {
107109
return false
108110
}
109111

110-
func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
111-
if m.PrometheusCRsAvailabilityFunc != nil {
112-
return m.PrometheusCRsAvailabilityFunc()
113-
}
114-
return prometheus.NotAvailable, nil
115-
}
116-
117112
func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
118113
if m.OpenShiftRoutesAvailabilityFunc != nil {
119114
return m.OpenShiftRoutesAvailabilityFunc()
120115
}
121116
return openshift.RoutesNotAvailable, nil
122117
}
123118

119+
func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
120+
if m.PrometheusCRsAvailabilityFunc != nil {
121+
return m.PrometheusCRsAvailabilityFunc()
122+
}
123+
return prometheus.NotAvailable, nil
124+
}
125+
124126
func (m *mockAutoDetect) RBACPermissions(ctx context.Context) (autoRBAC.Availability, error) {
125127
if m.RBACPermissionsFunc != nil {
126128
return m.RBACPermissionsFunc(ctx)
@@ -135,6 +137,13 @@ func (m *mockAutoDetect) CertManagerAvailability(ctx context.Context) (certmanag
135137
return certmanager.NotAvailable, nil
136138
}
137139

140+
func (m *mockAutoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
141+
if m.TargetAllocatorAvailabilityFunc != nil {
142+
return m.TargetAllocatorAvailabilityFunc()
143+
}
144+
return targetallocator.NotAvailable, nil
145+
}
146+
138147
func TestMain(m *testing.M) {
139148
var err error
140149
ctx, cancel = context.WithCancel(context.TODO())

internal/autodetect/main.go

+36
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package autodetect
1818
import (
1919
"context"
2020
"fmt"
21+
"slices"
2122

23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
"k8s.io/client-go/discovery"
2325
"k8s.io/client-go/rest"
2426

@@ -27,6 +29,7 @@ import (
2729
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2830
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2931
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
32+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
3033
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
3134
)
3235

@@ -38,6 +41,7 @@ type AutoDetect interface {
3841
PrometheusCRsAvailability() (prometheus.Availability, error)
3942
RBACPermissions(ctx context.Context) (autoRBAC.Availability, error)
4043
CertManagerAvailability(ctx context.Context) (certmanager.Availability, error)
44+
TargetAllocatorAvailability() (targetallocator.Availability, error)
4145
FIPSEnabled(ctx context.Context) bool
4246
}
4347

@@ -157,6 +161,38 @@ func (a *autoDetect) CertManagerAvailability(ctx context.Context) (certmanager.A
157161
return certmanager.Available, nil
158162
}
159163

164+
// TargetAllocatorAvailability checks if OpenShift Route are available.
165+
func (a *autoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
166+
apiList, err := a.dcl.ServerGroups()
167+
if err != nil {
168+
return targetallocator.NotAvailable, err
169+
}
170+
171+
apiGroups := apiList.Groups
172+
otelGroupIndex := slices.IndexFunc(apiGroups, func(group metav1.APIGroup) bool {
173+
return group.Name == "opentelemetry.io"
174+
})
175+
if otelGroupIndex == -1 {
176+
return targetallocator.NotAvailable, nil
177+
}
178+
179+
otelGroup := apiGroups[otelGroupIndex]
180+
for _, groupVersion := range otelGroup.Versions {
181+
resourceList, err := a.dcl.ServerResourcesForGroupVersion(groupVersion.GroupVersion)
182+
if err != nil {
183+
return targetallocator.NotAvailable, err
184+
}
185+
targetAllocatorIndex := slices.IndexFunc(resourceList.APIResources, func(group metav1.APIResource) bool {
186+
return group.Kind == "TargetAllocator"
187+
})
188+
if targetAllocatorIndex >= 0 {
189+
return targetallocator.Available, nil
190+
}
191+
}
192+
193+
return targetallocator.NotAvailable, nil
194+
}
195+
160196
func (a *autoDetect) FIPSEnabled(_ context.Context) bool {
161197
return fips.IsFipsEnabled()
162198
}
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 targetallocator
16+
17+
// Availability represents that the TargetAllocator CR is available in the cluster.
18+
type Availability int
19+
20+
const (
21+
// NotAvailable TargetAllocator CR is available in the cluster.
22+
NotAvailable Availability = iota
23+
24+
// Available TargetAllocator CR is available in the cluster.
25+
Available
26+
)
27+
28+
func (p Availability) String() string {
29+
return [...]string{"NotAvailable", "Available"}[p]
30+
}

internal/config/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2828
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2929
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
30+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
3031
"github.com/open-telemetry/opentelemetry-operator/internal/version"
3132
)
3233

@@ -67,6 +68,7 @@ type Config struct {
6768
openshiftRoutesAvailability openshift.RoutesAvailability
6869
prometheusCRAvailability prometheus.Availability
6970
certManagerAvailability certmanager.Availability
71+
targetAllocatorAvailability targetallocator.Availability
7072
labelsFilter []string
7173
annotationsFilter []string
7274
}
@@ -79,6 +81,7 @@ func New(opts ...Option) Config {
7981
openshiftRoutesAvailability: openshift.RoutesNotAvailable,
8082
createRBACPermissions: autoRBAC.NotAvailable,
8183
certManagerAvailability: certmanager.NotAvailable,
84+
targetAllocatorAvailability: targetallocator.NotAvailable,
8285
collectorConfigMapEntry: defaultCollectorConfigMapEntry,
8386
targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry,
8487
operatorOpAMPBridgeConfigMapEntry: defaultOperatorOpAMPBridgeConfigMapEntry,
@@ -112,6 +115,7 @@ func New(opts ...Option) Config {
112115
openshiftRoutesAvailability: o.openshiftRoutesAvailability,
113116
prometheusCRAvailability: o.prometheusCRAvailability,
114117
certManagerAvailability: o.certManagerAvailability,
118+
targetAllocatorAvailability: o.targetAllocatorAvailability,
115119
autoInstrumentationJavaImage: o.autoInstrumentationJavaImage,
116120
autoInstrumentationNodeJSImage: o.autoInstrumentationNodeJSImage,
117121
autoInstrumentationPythonImage: o.autoInstrumentationPythonImage,
@@ -157,6 +161,13 @@ func (c *Config) AutoDetect() error {
157161
c.certManagerAvailability = cmAvl
158162
c.logger.V(2).Info("the cert manager crd and permissions are set for the operator", "availability", cmAvl)
159163

164+
taAvl, err := c.autoDetect.TargetAllocatorAvailability()
165+
if err != nil {
166+
return err
167+
}
168+
c.targetAllocatorAvailability = taAvl
169+
c.logger.V(2).Info("determined TargetAllocator CRD availability", "availability", cmAvl)
170+
160171
return nil
161172
}
162173

@@ -250,6 +261,11 @@ func (c *Config) CertManagerAvailability() certmanager.Availability {
250261
return c.certManagerAvailability
251262
}
252263

264+
// TargetAllocatorAvailability represents the availability of the TargetAllocator CRD.
265+
func (c *Config) TargetAllocatorAvailability() targetallocator.Availability {
266+
return c.targetAllocatorAvailability
267+
}
268+
253269
// AutoInstrumentationJavaImage returns OpenTelemetry Java auto-instrumentation container image.
254270
func (c *Config) AutoInstrumentationJavaImage() string {
255271
return c.autoInstrumentationJavaImage

internal/config/main_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2727
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2828
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
29+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
2930
"github.com/open-telemetry/opentelemetry-operator/internal/config"
3031
)
3132

@@ -60,6 +61,9 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
6061
CertManagerAvailabilityFunc: func(ctx context.Context) (certmanager.Availability, error) {
6162
return certmanager.Available, nil
6263
},
64+
TargetAllocatorAvailabilityFunc: func() (targetallocator.Availability, error) {
65+
return targetallocator.Available, nil
66+
},
6367
}
6468
cfg := config.New(
6569
config.WithAutoDetect(mock),
@@ -68,6 +72,9 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
6872
// sanity check
6973
require.Equal(t, openshift.RoutesNotAvailable, cfg.OpenShiftRoutesAvailability())
7074
require.Equal(t, prometheus.NotAvailable, cfg.PrometheusCRAvailability())
75+
require.Equal(t, rbac.NotAvailable, cfg.CreateRBACPermissions())
76+
require.Equal(t, certmanager.NotAvailable, cfg.CertManagerAvailability())
77+
require.Equal(t, targetallocator.NotAvailable, cfg.TargetAllocatorAvailability())
7178

7279
// test
7380
err := cfg.AutoDetect()
@@ -76,6 +83,9 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
7683
// verify
7784
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenShiftRoutesAvailability())
7885
require.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability())
86+
require.Equal(t, rbac.Available, cfg.CreateRBACPermissions())
87+
require.Equal(t, certmanager.Available, cfg.CertManagerAvailability())
88+
require.Equal(t, targetallocator.Available, cfg.TargetAllocatorAvailability())
7989
}
8090

8191
var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)
@@ -85,6 +95,7 @@ type mockAutoDetect struct {
8595
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
8696
RBACPermissionsFunc func(ctx context.Context) (rbac.Availability, error)
8797
CertManagerAvailabilityFunc func(ctx context.Context) (certmanager.Availability, error)
98+
TargetAllocatorAvailabilityFunc func() (targetallocator.Availability, error)
8899
}
89100

90101
func (m *mockAutoDetect) FIPSEnabled(_ context.Context) bool {
@@ -118,3 +129,10 @@ func (m *mockAutoDetect) CertManagerAvailability(ctx context.Context) (certmanag
118129
}
119130
return certmanager.NotAvailable, nil
120131
}
132+
133+
func (m *mockAutoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
134+
if m.TargetAllocatorAvailabilityFunc != nil {
135+
return m.TargetAllocatorAvailabilityFunc()
136+
}
137+
return targetallocator.NotAvailable, nil
138+
}

internal/config/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2424
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2525
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
26+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
2627
"github.com/open-telemetry/opentelemetry-operator/internal/version"
2728
)
2829

@@ -58,6 +59,7 @@ type options struct {
5859
openshiftRoutesAvailability openshift.RoutesAvailability
5960
prometheusCRAvailability prometheus.Availability
6061
certManagerAvailability certmanager.Availability
62+
targetAllocatorAvailability targetallocator.Availability
6163
labelsFilter []string
6264
annotationsFilter []string
6365
}

main.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/certmanager"
5555
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
5656
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
57+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
5758
"github.com/open-telemetry/opentelemetry-operator/internal/config"
5859
"github.com/open-telemetry/opentelemetry-operator/internal/fips"
5960
collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
@@ -68,7 +69,6 @@ import (
6869
"github.com/open-telemetry/opentelemetry-operator/pkg/instrumentation"
6970
instrumentationupgrade "github.com/open-telemetry/opentelemetry-operator/pkg/instrumentation/upgrade"
7071
"github.com/open-telemetry/opentelemetry-operator/pkg/sidecar"
71-
// +kubebuilder:scaffold:imports
7272
)
7373

7474
var (
@@ -400,15 +400,17 @@ func main() {
400400
os.Exit(1)
401401
}
402402

403-
if err = controllers.NewTargetAllocatorReconciler(
404-
mgr.GetClient(),
405-
mgr.GetScheme(),
406-
mgr.GetEventRecorderFor("targetallocator"),
407-
cfg,
408-
ctrl.Log.WithName("controllers").WithName("TargetAllocator"),
409-
).SetupWithManager(mgr); err != nil {
410-
setupLog.Error(err, "unable to create controller", "controller", "TargetAllocator")
411-
os.Exit(1)
403+
if cfg.TargetAllocatorAvailability() == targetallocator.Available {
404+
if err = controllers.NewTargetAllocatorReconciler(
405+
mgr.GetClient(),
406+
mgr.GetScheme(),
407+
mgr.GetEventRecorderFor("targetallocator"),
408+
cfg,
409+
ctrl.Log.WithName("controllers").WithName("TargetAllocator"),
410+
).SetupWithManager(mgr); err != nil {
411+
setupLog.Error(err, "unable to create controller", "controller", "TargetAllocator")
412+
os.Exit(1)
413+
}
412414
}
413415

414416
if err = controllers.NewOpAMPBridgeReconciler(controllers.OpAMPBridgeReconcilerParams{
@@ -475,9 +477,11 @@ func main() {
475477
setupLog.Error(err, "unable to create webhook", "webhook", "OpenTelemetryCollector")
476478
os.Exit(1)
477479
}
478-
if err = otelv1alpha1.SetupTargetAllocatorWebhook(mgr, cfg, reviewer); err != nil {
479-
setupLog.Error(err, "unable to create webhook", "webhook", "TargetAllocator")
480-
os.Exit(1)
480+
if cfg.TargetAllocatorAvailability() == targetallocator.Available {
481+
if err = otelv1alpha1.SetupTargetAllocatorWebhook(mgr, cfg, reviewer); err != nil {
482+
setupLog.Error(err, "unable to create webhook", "webhook", "TargetAllocator")
483+
os.Exit(1)
484+
}
481485
}
482486
if err = otelv1alpha1.SetupInstrumentationWebhook(mgr, cfg); err != nil {
483487
setupLog.Error(err, "unable to create webhook", "webhook", "Instrumentation")

tests/test-e2e-apps/bridge-server/data/agent.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
"time"
1010

1111
"github.com/google/uuid"
12-
"google.golang.org/protobuf/proto"
13-
1412
"github.com/open-telemetry/opamp-go/protobufs"
1513
"github.com/open-telemetry/opamp-go/server/types"
14+
"google.golang.org/protobuf/proto"
1615
)
1716

1817
var _ json.Marshaler = &Agent{}

tests/test-e2e-apps/bridge-server/opampsrv/opampsrv.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import (
1010

1111
"github.com/google/uuid"
1212
"github.com/oklog/ulid/v2"
13-
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
14-
1513
"github.com/open-telemetry/opamp-go/protobufs"
1614
"github.com/open-telemetry/opamp-go/server"
1715
"github.com/open-telemetry/opamp-go/server/types"
16+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1817

1918
"github.com/open-telemetry/opentelemetry-operator/tests/test-e2e-apps/bridge-server/data"
2019
)

0 commit comments

Comments
 (0)