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

feat: add kubebuilders validations for name length to be <= 63 #218

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ test: manifests generate fmt vet local-unit-test
local-unit-test: $(ENVTEST) ## Run tests.
CGO_ENABLED=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./pkg/... -race -coverprofile=coverage.xml -covermode=atomic -v

.PHONY: integration-test
integration-test: $(ENVTEST) ## Run integration tests.
CGO_ENABLED=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" \
go test -tags=integration -v ./test/apis/v1alpha1/api_validation_integration_test.go

.PHONY: e2e-setup
e2e-setup:
bash test/scripts/bootstrap.sh
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/multiclusterservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
// +kubebuilder:printcolumn:JSONPath=`.metadata.creationTimestamp`,name="Age",type=date

// MultiClusterService is the Schema for creating north-south L4 load balancer to consume services across clusters.
// +kubebuilder:validation:XValidation:rule="size(self.metadata.name) < 64",message="metadata.name max length is 63"
type MultiClusterService struct {
metav1.TypeMeta `json:",inline"`
// +optional
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/serviceexport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ServiceExportStatus struct {
// +kubebuilder:printcolumn:JSONPath=`.metadata.creationTimestamp`,name="Age",type=date

// ServiceExport declares that the associated service should be exported to other clusters.
// +kubebuilder:validation:XValidation:rule="size(self.metadata.name) < 64",message="metadata.name max length is 63"
type ServiceExport struct {
metav1.TypeMeta `json:",inline"`
// +optional
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/serviceimport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// +kubebuilder:subresource:status

// ServiceImport describes a service imported from clusters in a ClusterSet.
// +kubebuilder:validation:XValidation:rule="size(self.metadata.name) < 64",message="metadata.name max length is 63"
type ServiceImport struct {
metav1.TypeMeta `json:",inline"`
// +optional
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/trafficmanagerbackend_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
// A backend contains one or more endpoints. Therefore, the controller may create multiple endpoints under the Traffic
// Manager Profile.
// https://learn.microsoft.com/en-us/azure/traffic-manager/traffic-manager-endpoint-types
// +kubebuilder:validation:XValidation:rule="size(self.metadata.name) < 64",message="metadata.name max length is 63"
type TrafficManagerBackend struct {
metav1.TypeMeta `json:",inline"`
// +optional
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/trafficmanagerprofile_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (

// TrafficManagerProfile is used to manage a simple Azure Traffic Manager Profile using cloud native way.
// https://learn.microsoft.com/en-us/azure/traffic-manager/traffic-manager-overview
// +kubebuilder:validation:XValidation:rule="size(self.metadata.name) < 64",message="metadata.name max length is 63"
type TrafficManagerProfile struct {
metav1.TypeMeta `json:",inline"`
// +optional
Expand All @@ -40,6 +41,7 @@ type TrafficManagerProfileSpec struct {

// MonitorConfig defines the endpoint monitoring settings of the Traffic Manager profile.
// https://learn.microsoft.com/en-us/azure/traffic-manager/traffic-manager-monitoring
// +kubebuilder:validation:XValidation:rule="(self.intervalInSeconds == 30 && (self.timeoutInSeconds >= 5 && self.timeoutInSeconds <= 10)) || (!has(self.intervalInSeconds) && self.timeoutInSeconds == 10) || (self.intervalInSeconds == 10 && (self.timeoutInSeconds >= 5 && self.timeoutInSeconds <= 9)) || (has(self.intervalInSeconds) && self.intervalInSeconds != 30 && self.intervalInSeconds != 10)",message="allowed timeoutInSeconds value is dependent on intervalInSeconds value"
type MonitorConfig struct {
// The monitor interval for endpoints in this profile. This is the interval at which Traffic Manager will check the health
// of each endpoint in this profile.
Expand Down Expand Up @@ -74,6 +76,7 @@ type MonitorConfig struct {
// If no Timeout value is specified, it uses a default value of 9 seconds.
// +kubebuilder:validation:Minimum=5
// +kubebuilder:validation:Maximum=10
// +kubebuilder:default:=9
TimeoutInSeconds *int64 `json:"timeoutInSeconds,omitempty"`

// The number of consecutive failed health check that Traffic Manager tolerates before declaring an endpoint in this profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ spec:
required:
- spec
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ spec:
x-kubernetes-list-type: map
type: object
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ spec:
type: string
type: object
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ spec:
required:
- spec
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ spec:
- TCP
type: string
timeoutInSeconds:
default: 9
description: |-
The monitor timeout for endpoints in this profile. This is the time that Traffic Manager allows endpoints in this profile
to response to the health check.
Expand All @@ -111,6 +112,14 @@ spec:
minimum: 0
type: integer
type: object
x-kubernetes-validations:
- message: allowed timeoutInSeconds value is dependent on intervalInSeconds
value
rule: (self.intervalInSeconds == 30 && (self.timeoutInSeconds >=
5 && self.timeoutInSeconds <= 10)) || (self.intervalInSeconds
== null && self.timeoutInSeconds == 10) || (self.intervalInSeconds
== 10 && (self.timeoutInSeconds >= 5 && self.timeoutInSeconds
<= 9))
type: object
status:
description: The observed status of TrafficManagerProfile.
Expand Down Expand Up @@ -186,6 +195,9 @@ spec:
required:
- spec
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
3 changes: 3 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ rules:
- multiclusterservices
- serviceexports
- serviceimports
- trafficmanagerbackends
- trafficmanagerprofiles
verbs:
- create
Expand All @@ -86,6 +87,7 @@ rules:
- multiclusterservices/status
- serviceexports/status
- serviceimports/status
- trafficmanagerbackends/status
- trafficmanagerprofiles/status
verbs:
- get
Expand All @@ -96,6 +98,7 @@ rules:
resources:
- multiclusterservices/finalizers
- serviceimports/finalizers
- trafficmanagerbackends/finalizers
- trafficmanagerprofiles/finalizers
verbs:
- get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var _ = Describe("Test TrafficManagerProfile Controller", func() {
It("Update the trafficManagerProfile spec", func() {
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: testNamespace, Name: name}, profile)).Should(Succeed(), "failed to get the trafficManagerProfile")
profile.Spec.MonitorConfig.IntervalInSeconds = ptr.To[int64](10)
profile.Spec.MonitorConfig.TimeoutInSeconds = ptr.To[int64](10)
profile.Spec.MonitorConfig.TimeoutInSeconds = ptr.To[int64](9)
Expect(k8sClient.Update(ctx, profile)).Should(Succeed(), "failed to update the trafficManagerProfile")
})

Expand Down
Loading
Loading