Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set replicas to default value #814

Merged
merged 4 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion 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)
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved

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
64 changes: 64 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package v1alpha1

import (
"testing"

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

func TestOELColDefaultingWebhook(t *testing.T) {
pavolloffay marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
}