Skip to content

Commit f2b5e3c

Browse files
authored
[chore] Refactor Webhooks to their own packages (#2210)
* lots of moving things around and simplifcation * moves webhook manifest * rename * generate * Add back annotations * Fix tests, simplify code * naming * fix borked test * FIX TESTS * oops * move things back * update manifests * fix a miss * fix tests * Rename
1 parent 16c3cd7 commit f2b5e3c

17 files changed

+469
-415
lines changed

apis/v1alpha1/opentelemetrycollector_webhook.go renamed to apis/v1alpha1/collector_webhook.go

Lines changed: 93 additions & 74 deletions
Large diffs are not rendered by default.

apis/v1alpha1/opentelemetrycollector_webhook_test.go renamed to apis/v1alpha1/collector_webhook_test.go

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,38 @@
1515
package v1alpha1
1616

1717
import (
18+
"context"
1819
"fmt"
20+
"os"
1921
"testing"
2022

23+
"github.com/go-logr/logr"
2124
"github.com/stretchr/testify/assert"
2225
autoscalingv2 "k8s.io/api/autoscaling/v2"
2326
v1 "k8s.io/api/core/v1"
2427
"k8s.io/apimachinery/pkg/api/resource"
2528
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/runtime"
2630
"k8s.io/apimachinery/pkg/util/intstr"
31+
"k8s.io/client-go/kubernetes/scheme"
32+
33+
"github.com/open-telemetry/opentelemetry-operator/internal/config"
34+
)
35+
36+
var (
37+
testScheme *runtime.Scheme = scheme.Scheme
2738
)
2839

2940
func TestOTELColDefaultingWebhook(t *testing.T) {
3041
one := int32(1)
3142
five := int32(5)
3243
defaultCPUTarget := int32(90)
3344

45+
if err := AddToScheme(testScheme); err != nil {
46+
fmt.Printf("failed to register scheme: %v", err)
47+
os.Exit(1)
48+
}
49+
3450
tests := []struct {
3551
name string
3652
otelcol OpenTelemetryCollector
@@ -261,7 +277,17 @@ func TestOTELColDefaultingWebhook(t *testing.T) {
261277

262278
for _, test := range tests {
263279
t.Run(test.name, func(t *testing.T) {
264-
test.otelcol.Default()
280+
cvw := &CollectorWebhook{
281+
logger: logr.Discard(),
282+
scheme: testScheme,
283+
cfg: config.New(
284+
config.WithCollectorImage("collector:v0.0.0"),
285+
config.WithTargetAllocatorImage("ta:v0.0.0"),
286+
),
287+
}
288+
ctx := context.Background()
289+
err := cvw.Default(ctx, &test.otelcol)
290+
assert.NoError(t, err)
265291
assert.Equal(t, test.expected, test.otelcol)
266292
})
267293
}
@@ -279,9 +305,10 @@ func TestOTELColValidatingWebhook(t *testing.T) {
279305
five := int32(5)
280306

281307
tests := []struct { //nolint:govet
282-
name string
283-
otelcol OpenTelemetryCollector
284-
expectedErr string
308+
name string
309+
otelcol OpenTelemetryCollector
310+
expectedErr string
311+
expectedWarnings []string
285312
}{
286313
{
287314
name: "valid empty spec",
@@ -336,12 +363,6 @@ func TestOTELColValidatingWebhook(t *testing.T) {
336363
},
337364
TargetCPUUtilization: &five,
338365
},
339-
PodDisruptionBudget: &PodDisruptionBudgetSpec{
340-
MinAvailable: &intstr.IntOrString{
341-
Type: intstr.Int,
342-
IntVal: 1,
343-
},
344-
},
345366
},
346367
},
347368
},
@@ -440,7 +461,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
440461
MaxReplicas: &zero,
441462
},
442463
},
443-
expectedErr: "maxReplicas should be defined and one or more",
464+
expectedErr: "maxReplicas should be defined and one or more",
465+
expectedWarnings: []string{"MaxReplicas is deprecated"},
444466
},
445467
{
446468
name: "invalid replicas, greater than max",
@@ -450,7 +472,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
450472
Replicas: &five,
451473
},
452474
},
453-
expectedErr: "replicas must not be greater than maxReplicas",
475+
expectedErr: "replicas must not be greater than maxReplicas",
476+
expectedWarnings: []string{"MaxReplicas is deprecated"},
454477
},
455478
{
456479
name: "invalid min replicas, greater than max",
@@ -460,7 +483,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
460483
MinReplicas: &five,
461484
},
462485
},
463-
expectedErr: "minReplicas must not be greater than maxReplicas",
486+
expectedErr: "minReplicas must not be greater than maxReplicas",
487+
expectedWarnings: []string{"MaxReplicas is deprecated", "MinReplicas is deprecated"},
464488
},
465489
{
466490
name: "invalid min replicas, lesser than 1",
@@ -470,7 +494,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
470494
MinReplicas: &zero,
471495
},
472496
},
473-
expectedErr: "minReplicas should be one or more",
497+
expectedErr: "minReplicas should be one or more",
498+
expectedWarnings: []string{"MaxReplicas is deprecated", "MinReplicas is deprecated"},
474499
},
475500
{
476501
name: "invalid autoscaler scale down",
@@ -486,7 +511,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
486511
},
487512
},
488513
},
489-
expectedErr: "scaleDown should be one or more",
514+
expectedErr: "scaleDown should be one or more",
515+
expectedWarnings: []string{"MaxReplicas is deprecated"},
490516
},
491517
{
492518
name: "invalid autoscaler scale up",
@@ -502,7 +528,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
502528
},
503529
},
504530
},
505-
expectedErr: "scaleUp should be one or more",
531+
expectedErr: "scaleUp should be one or more",
532+
expectedWarnings: []string{"MaxReplicas is deprecated"},
506533
},
507534
{
508535
name: "invalid autoscaler target cpu utilization",
@@ -514,7 +541,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
514541
},
515542
},
516543
},
517-
expectedErr: "targetCPUUtilization should be greater than 0 and less than 100",
544+
expectedErr: "targetCPUUtilization should be greater than 0 and less than 100",
545+
expectedWarnings: []string{"MaxReplicas is deprecated"},
518546
},
519547
{
520548
name: "autoscaler minReplicas is less than maxReplicas",
@@ -542,7 +570,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
542570
},
543571
},
544572
},
545-
expectedErr: "the OpenTelemetry Spec autoscale configuration is incorrect, metric type unsupported. Expected metric of source type Pod",
573+
expectedErr: "the OpenTelemetry Spec autoscale configuration is incorrect, metric type unsupported. Expected metric of source type Pod",
574+
expectedWarnings: []string{"MaxReplicas is deprecated"},
546575
},
547576
{
548577
name: "invalid pod metric average value",
@@ -567,7 +596,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
567596
},
568597
},
569598
},
570-
expectedErr: "the OpenTelemetry Spec autoscale configuration is incorrect, average value should be greater than 0",
599+
expectedErr: "the OpenTelemetry Spec autoscale configuration is incorrect, average value should be greater than 0",
600+
expectedWarnings: []string{"MaxReplicas is deprecated"},
571601
},
572602
{
573603
name: "utilization target is not valid with pod metrics",
@@ -592,26 +622,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
592622
},
593623
},
594624
},
595-
expectedErr: "the OpenTelemetry Spec autoscale configuration is incorrect, invalid pods target type",
596-
},
597-
{
598-
name: "pdb minAvailable and maxUnavailable have been set together",
599-
otelcol: OpenTelemetryCollector{
600-
Spec: OpenTelemetryCollectorSpec{
601-
MaxReplicas: &three,
602-
PodDisruptionBudget: &PodDisruptionBudgetSpec{
603-
MinAvailable: &intstr.IntOrString{
604-
Type: intstr.Int,
605-
IntVal: 1,
606-
},
607-
MaxUnavailable: &intstr.IntOrString{
608-
Type: intstr.Int,
609-
IntVal: 1,
610-
},
611-
},
612-
},
613-
},
614-
expectedErr: "the OpenTelemetry Spec podDisruptionBudget configuration is incorrect, minAvailable and maxUnavailable are mutually exclusive",
625+
expectedErr: "the OpenTelemetry Spec autoscale configuration is incorrect, invalid pods target type",
626+
expectedWarnings: []string{"MaxReplicas is deprecated"},
615627
},
616628
{
617629
name: "invalid deployment mode incompabible with ingress settings",
@@ -758,11 +770,25 @@ func TestOTELColValidatingWebhook(t *testing.T) {
758770

759771
for _, test := range tests {
760772
t.Run(test.name, func(t *testing.T) {
761-
err := test.otelcol.validateCRDSpec()
773+
cvw := &CollectorWebhook{
774+
logger: logr.Discard(),
775+
scheme: testScheme,
776+
cfg: config.New(
777+
config.WithCollectorImage("collector:v0.0.0"),
778+
config.WithTargetAllocatorImage("ta:v0.0.0"),
779+
),
780+
}
781+
ctx := context.Background()
782+
warnings, err := cvw.ValidateCreate(ctx, &test.otelcol)
762783
if test.expectedErr == "" {
763784
assert.NoError(t, err)
764785
return
765786
}
787+
if len(test.expectedWarnings) == 0 {
788+
assert.Empty(t, warnings, test.expectedWarnings)
789+
} else {
790+
assert.ElementsMatch(t, warnings, test.expectedWarnings)
791+
}
766792
assert.ErrorContains(t, err, test.expectedErr)
767793
})
768794
}

0 commit comments

Comments
 (0)