Skip to content

Commit

Permalink
Add cluster size validation rule (#230)
Browse files Browse the repository at this point in the history
* cluster size validation rule

* mockery update

* Add validation test case

* Set rule only for size integer value

* Improve test names

* Improve validation message
  • Loading branch information
grischperl authored Nov 21, 2023
1 parent b77368d commit 0940701
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 49 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/nats_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type Cluster struct {
// +kubebuilder:default:=3
// +kubebuilder:validation:Minimum:=1
// +kubebuilder:validation:XValidation:rule="(self%2) != 0", message="size only accepts odd numbers"
// +kubebuilder:validation:XValidation:rule="!(oldSelf > 1 && self == 1)", message="cannot be set to 1 if size was greater than 1"
Size int `json:"size,omitempty"`
}

Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/operator.kyma-project.io_nats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ spec:
x-kubernetes-validations:
- message: size only accepts odd numbers
rule: (self%2) != 0
- message: cannot be set to 1 if size was greater than 1
rule: '!(oldSelf > 1 && self == 1)'
type: object
jetStream:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,37 @@ func Test_Validate_UpdateNATS(t *testing.T) {
},
wantErrMsg: "fileStorage is immutable once it was set",
},
{
name: `validation of cluster fails, if cluster.size was set to a value >1 and now gets reduced to 1'`,
givenNATS: testutils.NewNATSCR(
testutils.WithNATSCluster(defaultCluster()),
),
wantMatches: gomega.And(
natsmatchers.HaveSpecCluster(defaultCluster()),
),
givenUpdates: []testutils.NATSOption{
testutils.WithNATSCluster(v1alpha1.Cluster{
Size: 1,
}),
},
wantErrMsg: "cannot be set to 1 if size was greater than 1",
},
{
name: `validation of cluster passes, if cluster.size was set to a value >1
and now gets set to another value >1'`,
givenNATS: testutils.NewNATSCR(
testutils.WithNATSCluster(defaultCluster()),
),
wantMatches: gomega.And(
natsmatchers.HaveSpecCluster(defaultCluster()),
),
givenUpdates: []testutils.NATSOption{
testutils.WithNATSCluster(v1alpha1.Cluster{
Size: 5,
}),
},
wantErrMsg: noError,
},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/nats/mocks/controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 1 addition & 44 deletions internal/controller/nats/mocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/k8s/chart/mocks/renderer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/k8s/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/manager/mocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/nats/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions testutils/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ func WithNATSFileStorage(fileStorage v1alpha1.FileStorage) NATSOption {
}
}

func WithNATSCluster(cluster v1alpha1.Cluster) NATSOption {
return func(nats *v1alpha1.NATS) error {
nats.Spec.Cluster = cluster
return nil
}
}

func WithNATSLabels(labels map[string]string) NATSOption {
return func(nats *v1alpha1.NATS) error {
nats.Spec.Labels = labels
Expand Down

0 comments on commit 0940701

Please sign in to comment.