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

Add cluster size validation rule #230

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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="size cannot be reduced to 1 once it was >1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message="size cannot be reduced to 1 once it was >1"
Wasn't this the other way around? Once it will be set to 1 it cannot be changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the rule is, once it was set to greater than 1 it can never be 1 again. I can try to make the message clearer like cannot be set to 1 if size was greater than 1?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, looks I got that wrong. Nevermind then.

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: size cannot be reduced to 1 once it was >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,36 @@ func Test_Validate_UpdateNATS(t *testing.T) {
},
wantErrMsg: "fileStorage is immutable once it was set",
},
{
name: `validation of cluster fails, if cluster.size>1 gets set to 1'`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is a little confusing. How about:

Suggested change
name: `validation of cluster fails, if cluster.size>1 gets set to 1'`,
name: `validation of cluster fails, if cluster.size was set to a value >1 and now gets changed to 1'`,

...or something similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

givenNATS: testutils.NewNATSCR(
testutils.WithNATSCluster(defaultCluster()),
),
wantMatches: gomega.And(
natsmatchers.HaveSpecCluster(defaultCluster()),
),
givenUpdates: []testutils.NATSOption{
testutils.WithNATSCluster(v1alpha1.Cluster{
Size: 1,
}),
},
wantErrMsg: "size cannot be reduced to 1 once it was >1",
},
{
name: `validation of cluster passes, if cluster.size>1 gets set to >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