Skip to content

Commit

Permalink
initial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
taniyourstruly committed Aug 7, 2024
1 parent b070c5f commit 895c802
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
15 changes: 6 additions & 9 deletions apis/v1beta1/collector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,35 +445,32 @@ func checkAutoscalerSpec(autoscaler *AutoscalerSpec) error {
return nil
}

// BuildValidator is mostly used for testing purposes.
// BuildValidator is //purpose and description
// Kubebuilder is used for testing
// +kubebuilder:object:generate=false

type BuildValidator func(c OpenTelemetryCollector) admission.Warnings

func NewCollectorWebhook(
logger logr.Logger,
scheme *runtime.Scheme,
cfg config.Config,
reviewer *rbac.Reviewer,
metrics *Metrics,
bv BuildValidator,
) *CollectorWebhook {
return &CollectorWebhook{
logger: logger,
scheme: scheme,
cfg: cfg,
reviewer: reviewer,
metrics: metrics,
bv: bv,
}
}

func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer, metrics *Metrics, bv BuildValidator) error {
cvw := &CollectorWebhook{
reviewer: reviewer,
logger: mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"),
scheme: mgr.GetScheme(),
cfg: cfg,
metrics: metrics,
bv: bv,
}
cvw := NewCollectorWebhook(mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"), mgr.GetScheme(), cfg, reviewer, metrics, bv)
return ctrl.NewWebhookManagedBy(mgr).
For(&OpenTelemetryCollector{}).
WithValidator(cvw).
Expand Down
18 changes: 11 additions & 7 deletions apis/v1beta1/collector_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ package v1beta1_test
import (
"context"
"fmt"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
"os"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"testing"

"github.com/go-logr/logr"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
Expand All @@ -35,13 +41,6 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
kubeTesting "k8s.io/client-go/testing"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
)

var (
Expand Down Expand Up @@ -127,6 +126,7 @@ func TestValidate(t *testing.T) {
config.WithTargetAllocatorImage("ta:v0.0.0"),
),
getReviewer(test.shouldFailSar),
nil,
bv,
)
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -587,6 +587,7 @@ func TestCollectorDefaultingWebhook(t *testing.T) {
config.WithTargetAllocatorImage("ta:v0.0.0"),
),
getReviewer(test.shouldFailSar),
nil,
bv,
)
ctx := context.Background()
Expand Down Expand Up @@ -1354,6 +1355,7 @@ func TestOTELColValidatingWebhook(t *testing.T) {
config.WithTargetAllocatorImage("ta:v0.0.0"),
),
getReviewer(test.shouldFailSar),
nil,
bv,
)
ctx := context.Background()
Expand Down Expand Up @@ -1389,6 +1391,7 @@ func TestOTELColValidateUpdateWebhook(t *testing.T) {
expectedErr: "which does not support modification",
},
}

bv := func(collector v1beta1.OpenTelemetryCollector) admission.Warnings {
var warnings admission.Warnings
cfg := config.New(
Expand Down Expand Up @@ -1419,6 +1422,7 @@ func TestOTELColValidateUpdateWebhook(t *testing.T) {
config.WithTargetAllocatorImage("ta:v0.0.0"),
),
getReviewer(test.shouldFailSar),
nil,
bv,
)
ctx := context.Background()
Expand Down
20 changes: 20 additions & 0 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sort"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -75,6 +76,25 @@ type Params struct {
Config config.Config
}

func (r *OpenTelemetryCollectorReconciler) Validate(otelcol v1beta1.OpenTelemetryCollector) admission.Warnings {
var warnings admission.Warnings
cfg := config.New(
config.WithCollectorImage("default-collector"),
config.WithTargetAllocatorImage("default-ta-allocator"),
)
params := manifests.Params{
Log: logr.Discard(),
Config: cfg,
OtelCol: otelcol,
}
_, err := collector.Build(params)
if err != nil {
warnings = append(warnings, err.Error())
return warnings
}
return nil
}

func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Context, params manifests.Params) (map[types.UID]client.Object, error) {
ownedObjects := map[types.UID]client.Object{}
ownedObjectTypes := []client.Object{
Expand Down
19 changes: 3 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ 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/config"
collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
openshiftDashboards "github.com/open-telemetry/opentelemetry-operator/internal/openshift/dashboards"
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/version"
Expand Down Expand Up @@ -407,22 +406,10 @@ func main() {

}

bv := func(collector otelv1beta1.OpenTelemetryCollector) admission.Warnings {
var warnings admission.Warnings
params, newErr := collectorReconciler.GetParams(collector)
if newErr != nil {
warnings = append(warnings, newErr.Error())
return warnings
}
_, err = collectorManifests.Build(params)
if err != nil {
warnings = append(warnings, err.Error())
return warnings
}
return warnings
}
reconciler := controllers.NewReconciler(controllers.Params{})
bv := otelv1beta1.SetupCollectorWebhook(mgr, cfg, reviewer, crdMetrics, reconciler.Validate)

if err = otelv1beta1.SetupCollectorWebhook(mgr, cfg, reviewer, crdMetrics, bv); err != nil {
if err = bv; err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OpenTelemetryCollector")
os.Exit(1)
}
Expand Down

0 comments on commit 895c802

Please sign in to comment.