Skip to content

Commit

Permalink
Set replicas to default value (open-telemetry#814)
Browse files Browse the repository at this point in the history
* Set replicas to default value

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* Fix

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* Fix

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* Fix

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
  • Loading branch information
pavolloffay authored Apr 12, 2022
1 parent 2dfbe14 commit 9ebf5c7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
14 changes: 8 additions & 6 deletions apis/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var _ webhook.Defaulter = &OpenTelemetryCollector{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *OpenTelemetryCollector) Default() {
opentelemetrycollectorlog.Info("default", "name", r.Name)

if len(r.Spec.Mode) == 0 {
r.Spec.Mode = ModeDeployment
}
Expand All @@ -54,7 +56,12 @@ func (r *OpenTelemetryCollector) Default() {
r.Labels["app.kubernetes.io/managed-by"] = "opentelemetry-operator"
}

opentelemetrycollectorlog.Info("default", "name", r.Name)
if r.Spec.Replicas == nil {
// We can default to one because dependent objects Deployment and HorizontalPodAutoScaler
// default to 1 as well.
one := int32(1)
r.Spec.Replicas = &one
}
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-opentelemetry-io-v1alpha1-opentelemetrycollector,mutating=false,failurePolicy=fail,groups=opentelemetry.io,resources=opentelemetrycollectors,versions=v1alpha1,name=vopentelemetrycollectorcreateupdate.kb.io,sideEffects=none,admissionReviewVersions=v1
Expand Down Expand Up @@ -86,11 +93,6 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the attribute 'volumeClaimTemplates'", r.Spec.Mode)
}

// validate replicas
if (r.Spec.Mode == ModeSidecar || r.Spec.Mode == ModeDaemonSet) && r.Spec.Replicas != nil {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the attribute 'replicas'", r.Spec.Mode)
}

// validate tolerations
if r.Spec.Mode == ModeSidecar && len(r.Spec.Tolerations) > 0 {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the attribute 'tolerations'", r.Spec.Mode)
Expand Down
78 changes: 78 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestOTELColDefaultingWebhook(t *testing.T) {
one := int32(1)
five := int32(5)
tests := []struct {
name string
otelcol OpenTelemetryCollector
expected OpenTelemetryCollector
}{
{
name: "all fields default",
otelcol: OpenTelemetryCollector{},
expected: OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "opentelemetry-operator",
},
},
Spec: OpenTelemetryCollectorSpec{
Mode: ModeDeployment,
Replicas: &one,
UpgradeStrategy: UpgradeStrategyAutomatic,
},
},
},
{
name: "provided values in spec",
otelcol: OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
Mode: ModeSidecar,
Replicas: &five,
UpgradeStrategy: "adhoc",
},
},
expected: OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app.kubernetes.io/managed-by": "opentelemetry-operator",
},
},
Spec: OpenTelemetryCollectorSpec{
Mode: ModeSidecar,
Replicas: &five,
UpgradeStrategy: "adhoc",
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.otelcol.Default()
assert.Equal(t, test.expected, test.otelcol)
})
}
}

0 comments on commit 9ebf5c7

Please sign in to comment.