Skip to content

Commit 7705144

Browse files
committed
Add unit tests for sharder config defaulting
1 parent 6fdb138 commit 7705144

File tree

5 files changed

+159
-3
lines changed

5 files changed

+159
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test-kyverno: $(KYVERNO) ## Run kyverno policy tests.
8383

8484
.PHONY: test-e2e
8585
test-e2e: $(GINKGO) ## Run e2e tests.
86-
ginkgo run --timeout=1h --poll-progress-after=60s --poll-progress-interval=30s --randomize-all --randomize-suites --keep-going --vv $(GINKGO_FLAGS) ./test/e2e/...
86+
$(GINKGO) run --timeout=1h --poll-progress-after=60s --poll-progress-interval=30s --randomize-all --randomize-suites --keep-going --vv $(GINKGO_FLAGS) ./test/e2e/...
8787

8888
.PHONY: skaffold-fix
8989
skaffold-fix: $(SKAFFOLD) ## Upgrade skaffold configuration to the latest apiVersion.

pkg/apis/config/v1alpha1/defaults.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ func SetDefaults_SharderConfig(obj *SharderConfig) {
4646
}
4747

4848
if obj.GracefulShutdownTimeout == nil {
49-
var defaultGracefulShutdownTimeout = 15 * time.Second
50-
obj.GracefulShutdownTimeout = &metav1.Duration{Duration: defaultGracefulShutdownTimeout}
49+
obj.GracefulShutdownTimeout = &metav1.Duration{Duration: 15 * time.Second}
5150
}
5251
}
5352

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1_test
18+
19+
import (
20+
"time"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1"
27+
"k8s.io/utils/ptr"
28+
29+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/config/v1alpha1"
30+
)
31+
32+
var _ = Describe("SharderConfig defaulting", func() {
33+
var obj *SharderConfig
34+
35+
BeforeEach(func() {
36+
obj = &SharderConfig{}
37+
})
38+
39+
Context("ClientConnectionConfiguration", func() {
40+
It("should set default values", func() {
41+
SetObjectDefaults_SharderConfig(obj)
42+
43+
Expect(obj.ClientConnection).To(Equal(&componentbaseconfigv1alpha1.ClientConnectionConfiguration{
44+
QPS: 100,
45+
Burst: 150,
46+
}))
47+
})
48+
})
49+
50+
Context("LeaderElectionConfiguration", func() {
51+
It("should set default values", func() {
52+
SetObjectDefaults_SharderConfig(obj)
53+
54+
Expect(obj.LeaderElection).To(Equal(&componentbaseconfigv1alpha1.LeaderElectionConfiguration{
55+
LeaderElect: ptr.To(true),
56+
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
57+
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
58+
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
59+
ResourceLock: "leases",
60+
ResourceName: "sharder",
61+
ResourceNamespace: "sharding-system",
62+
}))
63+
})
64+
})
65+
66+
Context("DebuggingConfiguration", func() {
67+
It("should set default values", func() {
68+
SetObjectDefaults_SharderConfig(obj)
69+
70+
Expect(obj.Debugging).To(Equal(&componentbaseconfigv1alpha1.DebuggingConfiguration{
71+
EnableProfiling: ptr.To(true),
72+
EnableContentionProfiling: ptr.To(false),
73+
}))
74+
})
75+
})
76+
77+
Context("manager settings", func() {
78+
It("should set default values", func() {
79+
SetObjectDefaults_SharderConfig(obj)
80+
81+
Expect(obj.Health).To(Equal(HealthEndpoint{
82+
BindAddress: ":8081",
83+
}))
84+
Expect(obj.Metrics).To(Equal(MetricsEndpoint{
85+
BindAddress: ":8080",
86+
}))
87+
Expect(obj.GracefulShutdownTimeout).To(Equal(&metav1.Duration{Duration: 15 * time.Second}))
88+
})
89+
})
90+
91+
Context("controller config", func() {
92+
It("should set default values", func() {
93+
SetObjectDefaults_SharderConfig(obj)
94+
95+
Expect(obj.Controller).To(Equal(Controller{
96+
Sharder: &SharderController{
97+
SyncPeriod: &metav1.Duration{Duration: 5 * time.Minute},
98+
},
99+
}))
100+
})
101+
})
102+
103+
Context("webhook config", func() {
104+
It("should set default values", func() {
105+
SetObjectDefaults_SharderConfig(obj)
106+
107+
Expect(obj.Webhook).To(Equal(Webhook{
108+
Server: &WebhookServer{},
109+
Config: &WebhookConfig{
110+
ClientConfig: &admissionregistrationv1.WebhookClientConfig{
111+
Service: &admissionregistrationv1.ServiceReference{
112+
Namespace: "sharding-system",
113+
Name: "sharder",
114+
},
115+
},
116+
NamespaceSelector: &metav1.LabelSelector{
117+
MatchExpressions: []metav1.LabelSelectorRequirement{{
118+
Key: "kubernetes.io/metadata.name",
119+
Operator: "NotIn",
120+
Values: []string{"kube-system", "sharding-system"},
121+
}},
122+
},
123+
},
124+
}))
125+
})
126+
})
127+
})

pkg/apis/config/v1alpha1/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type WebhookConfig struct {
121121
// +optional
122122
Annotations map[string]string `json:"annotations,omitempty"`
123123
// ClientConfig configures the webhook configs' target.
124+
// Defaults to a service reference to sharding-system/sharder.
124125
// +optional
125126
ClientConfig *admissionregistrationv1.WebhookClientConfig `json:"clientConfig,omitempty"`
126127
// NamespaceSelector overwrites the webhook configs' default namespaceSelector.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestV1alpha1(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Sharder Config API V1alpha1 Suite")
29+
}

0 commit comments

Comments
 (0)