15
15
package v1alpha1
16
16
17
17
import (
18
+ "context"
18
19
"fmt"
20
+ "os"
19
21
"testing"
20
22
23
+ "github.com/go-logr/logr"
21
24
"github.com/stretchr/testify/assert"
22
25
autoscalingv2 "k8s.io/api/autoscaling/v2"
23
26
v1 "k8s.io/api/core/v1"
24
27
"k8s.io/apimachinery/pkg/api/resource"
25
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
+ "k8s.io/apimachinery/pkg/runtime"
26
30
"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
27
38
)
28
39
29
40
func TestOTELColDefaultingWebhook (t * testing.T ) {
30
41
one := int32 (1 )
31
42
five := int32 (5 )
32
43
defaultCPUTarget := int32 (90 )
33
44
45
+ if err := AddToScheme (testScheme ); err != nil {
46
+ fmt .Printf ("failed to register scheme: %v" , err )
47
+ os .Exit (1 )
48
+ }
49
+
34
50
tests := []struct {
35
51
name string
36
52
otelcol OpenTelemetryCollector
@@ -261,7 +277,17 @@ func TestOTELColDefaultingWebhook(t *testing.T) {
261
277
262
278
for _ , test := range tests {
263
279
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 )
265
291
assert .Equal (t , test .expected , test .otelcol )
266
292
})
267
293
}
@@ -279,9 +305,10 @@ func TestOTELColValidatingWebhook(t *testing.T) {
279
305
five := int32 (5 )
280
306
281
307
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
285
312
}{
286
313
{
287
314
name : "valid empty spec" ,
@@ -336,12 +363,6 @@ func TestOTELColValidatingWebhook(t *testing.T) {
336
363
},
337
364
TargetCPUUtilization : & five ,
338
365
},
339
- PodDisruptionBudget : & PodDisruptionBudgetSpec {
340
- MinAvailable : & intstr.IntOrString {
341
- Type : intstr .Int ,
342
- IntVal : 1 ,
343
- },
344
- },
345
366
},
346
367
},
347
368
},
@@ -440,7 +461,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
440
461
MaxReplicas : & zero ,
441
462
},
442
463
},
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" },
444
466
},
445
467
{
446
468
name : "invalid replicas, greater than max" ,
@@ -450,7 +472,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
450
472
Replicas : & five ,
451
473
},
452
474
},
453
- expectedErr : "replicas must not be greater than maxReplicas" ,
475
+ expectedErr : "replicas must not be greater than maxReplicas" ,
476
+ expectedWarnings : []string {"MaxReplicas is deprecated" },
454
477
},
455
478
{
456
479
name : "invalid min replicas, greater than max" ,
@@ -460,7 +483,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
460
483
MinReplicas : & five ,
461
484
},
462
485
},
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" },
464
488
},
465
489
{
466
490
name : "invalid min replicas, lesser than 1" ,
@@ -470,7 +494,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
470
494
MinReplicas : & zero ,
471
495
},
472
496
},
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" },
474
499
},
475
500
{
476
501
name : "invalid autoscaler scale down" ,
@@ -486,7 +511,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
486
511
},
487
512
},
488
513
},
489
- expectedErr : "scaleDown should be one or more" ,
514
+ expectedErr : "scaleDown should be one or more" ,
515
+ expectedWarnings : []string {"MaxReplicas is deprecated" },
490
516
},
491
517
{
492
518
name : "invalid autoscaler scale up" ,
@@ -502,7 +528,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
502
528
},
503
529
},
504
530
},
505
- expectedErr : "scaleUp should be one or more" ,
531
+ expectedErr : "scaleUp should be one or more" ,
532
+ expectedWarnings : []string {"MaxReplicas is deprecated" },
506
533
},
507
534
{
508
535
name : "invalid autoscaler target cpu utilization" ,
@@ -514,7 +541,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
514
541
},
515
542
},
516
543
},
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" },
518
546
},
519
547
{
520
548
name : "autoscaler minReplicas is less than maxReplicas" ,
@@ -542,7 +570,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
542
570
},
543
571
},
544
572
},
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" },
546
575
},
547
576
{
548
577
name : "invalid pod metric average value" ,
@@ -567,7 +596,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
567
596
},
568
597
},
569
598
},
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" },
571
601
},
572
602
{
573
603
name : "utilization target is not valid with pod metrics" ,
@@ -592,26 +622,8 @@ func TestOTELColValidatingWebhook(t *testing.T) {
592
622
},
593
623
},
594
624
},
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" },
615
627
},
616
628
{
617
629
name : "invalid deployment mode incompabible with ingress settings" ,
@@ -758,11 +770,25 @@ func TestOTELColValidatingWebhook(t *testing.T) {
758
770
759
771
for _ , test := range tests {
760
772
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 )
762
783
if test .expectedErr == "" {
763
784
assert .NoError (t , err )
764
785
return
765
786
}
787
+ if len (test .expectedWarnings ) == 0 {
788
+ assert .Empty (t , warnings , test .expectedWarnings )
789
+ } else {
790
+ assert .ElementsMatch (t , warnings , test .expectedWarnings )
791
+ }
766
792
assert .ErrorContains (t , err , test .expectedErr )
767
793
})
768
794
}
0 commit comments