Skip to content

Commit

Permalink
Use TA CRD only if it's available
Browse files Browse the repository at this point in the history
This way we won't break installations which don't have the new CRD yet
for whatever reason.
  • Loading branch information
swiatekm committed Jan 17, 2025
1 parent 1d00cb1 commit 59452da
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 25 deletions.
25 changes: 17 additions & 8 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
"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 @@ -101,26 +102,27 @@ type mockAutoDetect struct {
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
RBACPermissionsFunc func(ctx context.Context) (autoRBAC.Availability, error)
CertManagerAvailabilityFunc func(ctx context.Context) (certmanager.Availability, error)
TargetAllocatorAvailabilityFunc func() (targetallocator.Availability, error)
}

func (m *mockAutoDetect) FIPSEnabled(ctx context.Context) bool {
func (m *mockAutoDetect) FIPSEnabled(_ context.Context) bool {
return false
}

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) {
if m.OpenShiftRoutesAvailabilityFunc != nil {
return m.OpenShiftRoutesAvailabilityFunc()
}
return openshift.RoutesNotAvailable, nil
}

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

func (m *mockAutoDetect) RBACPermissions(ctx context.Context) (autoRBAC.Availability, error) {
if m.RBACPermissionsFunc != nil {
return m.RBACPermissionsFunc(ctx)
Expand All @@ -135,6 +137,13 @@ func (m *mockAutoDetect) CertManagerAvailability(ctx context.Context) (certmanag
return certmanager.NotAvailable, nil
}

func (m *mockAutoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
if m.TargetAllocatorAvailabilityFunc != nil {
return m.TargetAllocatorAvailabilityFunc()
}
return targetallocator.NotAvailable, nil
}

func TestMain(m *testing.M) {
var err error
ctx, cancel = context.WithCancel(context.TODO())
Expand Down
36 changes: 36 additions & 0 deletions internal/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package autodetect
import (
"context"
"fmt"
"slices"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"

Expand All @@ -27,6 +29,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
)

Expand All @@ -38,6 +41,7 @@ type AutoDetect interface {
PrometheusCRsAvailability() (prometheus.Availability, error)
RBACPermissions(ctx context.Context) (autoRBAC.Availability, error)
CertManagerAvailability(ctx context.Context) (certmanager.Availability, error)
TargetAllocatorAvailability() (targetallocator.Availability, error)
FIPSEnabled(ctx context.Context) bool
}

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

// TargetAllocatorAvailability checks if OpenShift Route are available.
func (a *autoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
apiList, err := a.dcl.ServerGroups()
if err != nil {
return targetallocator.NotAvailable, err
}

apiGroups := apiList.Groups
otelGroupIndex := slices.IndexFunc(apiGroups, func(group metav1.APIGroup) bool {
return group.Name == "opentelemetry.io"
})
if otelGroupIndex == -1 {
return targetallocator.NotAvailable, nil
}

otelGroup := apiGroups[otelGroupIndex]
for _, groupVersion := range otelGroup.Versions {
resourceList, err := a.dcl.ServerResourcesForGroupVersion(groupVersion.GroupVersion)
if err != nil {
return targetallocator.NotAvailable, err
}
targetAllocatorIndex := slices.IndexFunc(resourceList.APIResources, func(group metav1.APIResource) bool {
return group.Kind == "TargetAllocator"
})
if targetAllocatorIndex >= 0 {
return targetallocator.Available, nil
}
}

return targetallocator.NotAvailable, nil
}

func (a *autoDetect) FIPSEnabled(_ context.Context) bool {
return fips.IsFipsEnabled()
}
30 changes: 30 additions & 0 deletions internal/autodetect/targetallocator/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 targetallocator

// Availability represents that the TargetAllocator CR is available in the cluster.
type Availability int

const (
// NotAvailable TargetAllocator CR is available in the cluster.
NotAvailable Availability = iota

// Available TargetAllocator CR is available in the cluster.
Available
)

func (p Availability) String() string {
return [...]string{"NotAvailable", "Available"}[p]
}
16 changes: 16 additions & 0 deletions internal/config/main.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/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
)

Expand Down Expand Up @@ -67,6 +68,7 @@ type Config struct {
openshiftRoutesAvailability openshift.RoutesAvailability
prometheusCRAvailability prometheus.Availability
certManagerAvailability certmanager.Availability
targetAllocatorAvailability targetallocator.Availability
labelsFilter []string
annotationsFilter []string
}
Expand All @@ -79,6 +81,7 @@ func New(opts ...Option) Config {
openshiftRoutesAvailability: openshift.RoutesNotAvailable,
createRBACPermissions: autoRBAC.NotAvailable,
certManagerAvailability: certmanager.NotAvailable,
targetAllocatorAvailability: targetallocator.NotAvailable,
collectorConfigMapEntry: defaultCollectorConfigMapEntry,
targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry,
operatorOpAMPBridgeConfigMapEntry: defaultOperatorOpAMPBridgeConfigMapEntry,
Expand Down Expand Up @@ -112,6 +115,7 @@ func New(opts ...Option) Config {
openshiftRoutesAvailability: o.openshiftRoutesAvailability,
prometheusCRAvailability: o.prometheusCRAvailability,
certManagerAvailability: o.certManagerAvailability,
targetAllocatorAvailability: o.targetAllocatorAvailability,
autoInstrumentationJavaImage: o.autoInstrumentationJavaImage,
autoInstrumentationNodeJSImage: o.autoInstrumentationNodeJSImage,
autoInstrumentationPythonImage: o.autoInstrumentationPythonImage,
Expand Down Expand Up @@ -157,6 +161,13 @@ func (c *Config) AutoDetect() error {
c.certManagerAvailability = cmAvl
c.logger.V(2).Info("the cert manager crd and permissions are set for the operator", "availability", cmAvl)

taAvl, err := c.autoDetect.TargetAllocatorAvailability()
if err != nil {
return err
}
c.targetAllocatorAvailability = taAvl
c.logger.V(2).Info("determined TargetAllocator CRD availability", "availability", cmAvl)

return nil
}

Expand Down Expand Up @@ -250,6 +261,11 @@ func (c *Config) CertManagerAvailability() certmanager.Availability {
return c.certManagerAvailability
}

// TargetAllocatorAvailability represents the availability of the TargetAllocator CRD.
func (c *Config) TargetAllocatorAvailability() targetallocator.Availability {
return c.targetAllocatorAvailability
}

// AutoInstrumentationJavaImage returns OpenTelemetry Java auto-instrumentation container image.
func (c *Config) AutoInstrumentationJavaImage() string {
return c.autoInstrumentationJavaImage
Expand Down
18 changes: 18 additions & 0 deletions internal/config/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"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/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
)

Expand Down Expand Up @@ -60,6 +61,9 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
CertManagerAvailabilityFunc: func(ctx context.Context) (certmanager.Availability, error) {
return certmanager.Available, nil
},
TargetAllocatorAvailabilityFunc: func() (targetallocator.Availability, error) {
return targetallocator.Available, nil
},
}
cfg := config.New(
config.WithAutoDetect(mock),
Expand All @@ -68,6 +72,9 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
// sanity check
require.Equal(t, openshift.RoutesNotAvailable, cfg.OpenShiftRoutesAvailability())
require.Equal(t, prometheus.NotAvailable, cfg.PrometheusCRAvailability())
require.Equal(t, rbac.NotAvailable, cfg.CreateRBACPermissions())
require.Equal(t, certmanager.NotAvailable, cfg.CertManagerAvailability())
require.Equal(t, targetallocator.NotAvailable, cfg.TargetAllocatorAvailability())

// test
err := cfg.AutoDetect()
Expand All @@ -76,6 +83,9 @@ func TestConfigChangesOnAutoDetect(t *testing.T) {
// verify
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenShiftRoutesAvailability())
require.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability())
require.Equal(t, rbac.Available, cfg.CreateRBACPermissions())
require.Equal(t, certmanager.Available, cfg.CertManagerAvailability())
require.Equal(t, targetallocator.Available, cfg.TargetAllocatorAvailability())
}

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

func (m *mockAutoDetect) FIPSEnabled(_ context.Context) bool {
Expand Down Expand Up @@ -118,3 +129,10 @@ func (m *mockAutoDetect) CertManagerAvailability(ctx context.Context) (certmanag
}
return certmanager.NotAvailable, nil
}

func (m *mockAutoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
if m.TargetAllocatorAvailabilityFunc != nil {
return m.TargetAllocatorAvailabilityFunc()
}
return targetallocator.NotAvailable, nil
}
2 changes: 2 additions & 0 deletions internal/config/options.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/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
)

Expand Down Expand Up @@ -58,6 +59,7 @@ type options struct {
openshiftRoutesAvailability openshift.RoutesAvailability
prometheusCRAvailability prometheus.Availability
certManagerAvailability certmanager.Availability
targetAllocatorAvailability targetallocator.Availability
labelsFilter []string
annotationsFilter []string
}
Expand Down
30 changes: 17 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/certmanager"
"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/autodetect/targetallocator"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/fips"
collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
Expand All @@ -68,7 +69,6 @@ import (
"github.com/open-telemetry/opentelemetry-operator/pkg/instrumentation"
instrumentationupgrade "github.com/open-telemetry/opentelemetry-operator/pkg/instrumentation/upgrade"
"github.com/open-telemetry/opentelemetry-operator/pkg/sidecar"
// +kubebuilder:scaffold:imports
)

var (
Expand Down Expand Up @@ -400,15 +400,17 @@ func main() {
os.Exit(1)
}

if err = controllers.NewTargetAllocatorReconciler(
mgr.GetClient(),
mgr.GetScheme(),
mgr.GetEventRecorderFor("targetallocator"),
cfg,
ctrl.Log.WithName("controllers").WithName("TargetAllocator"),
).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TargetAllocator")
os.Exit(1)
if cfg.TargetAllocatorAvailability() == targetallocator.Available {
if err = controllers.NewTargetAllocatorReconciler(
mgr.GetClient(),
mgr.GetScheme(),
mgr.GetEventRecorderFor("targetallocator"),
cfg,
ctrl.Log.WithName("controllers").WithName("TargetAllocator"),
).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TargetAllocator")
os.Exit(1)
}
}

if err = controllers.NewOpAMPBridgeReconciler(controllers.OpAMPBridgeReconcilerParams{
Expand Down Expand Up @@ -475,9 +477,11 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "OpenTelemetryCollector")
os.Exit(1)
}
if err = otelv1alpha1.SetupTargetAllocatorWebhook(mgr, cfg, reviewer); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "TargetAllocator")
os.Exit(1)
if cfg.TargetAllocatorAvailability() == targetallocator.Available {
if err = otelv1alpha1.SetupTargetAllocatorWebhook(mgr, cfg, reviewer); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "TargetAllocator")
os.Exit(1)
}
}
if err = otelv1alpha1.SetupInstrumentationWebhook(mgr, cfg); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Instrumentation")
Expand Down
3 changes: 1 addition & 2 deletions tests/test-e2e-apps/bridge-server/data/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"time"

"github.com/google/uuid"
"google.golang.org/protobuf/proto"

"github.com/open-telemetry/opamp-go/protobufs"
"github.com/open-telemetry/opamp-go/server/types"
"google.golang.org/protobuf/proto"
)

var _ json.Marshaler = &Agent{}
Expand Down
3 changes: 1 addition & 2 deletions tests/test-e2e-apps/bridge-server/opampsrv/opampsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (

"github.com/google/uuid"
"github.com/oklog/ulid/v2"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"

"github.com/open-telemetry/opamp-go/protobufs"
"github.com/open-telemetry/opamp-go/server"
"github.com/open-telemetry/opamp-go/server/types"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"

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

0 comments on commit 59452da

Please sign in to comment.