diff --git a/api/v1alpha1/nats_types.go b/api/v1alpha1/nats_types.go index 09b62888..0933e9b4 100644 --- a/api/v1alpha1/nats_types.go +++ b/api/v1alpha1/nats_types.go @@ -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"` } diff --git a/config/crd/bases/operator.kyma-project.io_nats.yaml b/config/crd/bases/operator.kyma-project.io_nats.yaml index 47ae0baf..dc54fabb 100644 --- a/config/crd/bases/operator.kyma-project.io_nats.yaml +++ b/config/crd/bases/operator.kyma-project.io_nats.yaml @@ -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: diff --git a/internal/controller/nats/integrationtests/validation/integration_test.go b/internal/controller/nats/integrationtests/validation/integration_test.go index 288d4ae9..1410a852 100644 --- a/internal/controller/nats/integrationtests/validation/integration_test.go +++ b/internal/controller/nats/integrationtests/validation/integration_test.go @@ -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 { diff --git a/internal/controller/nats/mocks/controller.go b/internal/controller/nats/mocks/controller.go index a2360b69..7cc68336 100644 --- a/internal/controller/nats/mocks/controller.go +++ b/internal/controller/nats/mocks/controller.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.3. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mocks diff --git a/internal/controller/nats/mocks/manager.go b/internal/controller/nats/mocks/manager.go index d53f4ff7..5f61310d 100644 --- a/internal/controller/nats/mocks/manager.go +++ b/internal/controller/nats/mocks/manager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.3. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mocks @@ -129,49 +129,6 @@ func (_c *Manager_AddHealthzCheck_Call) RunAndReturn(run func(string, healthz.Ch return _c } -// AddMetricsExtraHandler provides a mock function with given fields: path, handler -func (_m *Manager) AddMetricsExtraHandler(path string, handler http.Handler) error { - ret := _m.Called(path, handler) - - var r0 error - if rf, ok := ret.Get(0).(func(string, http.Handler) error); ok { - r0 = rf(path, handler) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Manager_AddMetricsExtraHandler_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddMetricsExtraHandler' -type Manager_AddMetricsExtraHandler_Call struct { - *mock.Call -} - -// AddMetricsExtraHandler is a helper method to define mock.On call -// - path string -// - handler http.Handler -func (_e *Manager_Expecter) AddMetricsExtraHandler(path interface{}, handler interface{}) *Manager_AddMetricsExtraHandler_Call { - return &Manager_AddMetricsExtraHandler_Call{Call: _e.mock.On("AddMetricsExtraHandler", path, handler)} -} - -func (_c *Manager_AddMetricsExtraHandler_Call) Run(run func(path string, handler http.Handler)) *Manager_AddMetricsExtraHandler_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(http.Handler)) - }) - return _c -} - -func (_c *Manager_AddMetricsExtraHandler_Call) Return(_a0 error) *Manager_AddMetricsExtraHandler_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *Manager_AddMetricsExtraHandler_Call) RunAndReturn(run func(string, http.Handler) error) *Manager_AddMetricsExtraHandler_Call { - _c.Call.Return(run) - return _c -} - // AddReadyzCheck provides a mock function with given fields: name, check func (_m *Manager) AddReadyzCheck(name string, check healthz.Checker) error { ret := _m.Called(name, check) diff --git a/pkg/k8s/chart/mocks/renderer.go b/pkg/k8s/chart/mocks/renderer.go index 436f307d..09317e12 100644 --- a/pkg/k8s/chart/mocks/renderer.go +++ b/pkg/k8s/chart/mocks/renderer.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.3. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mocks diff --git a/pkg/k8s/mocks/client.go b/pkg/k8s/mocks/client.go index 7729484b..53d61583 100644 --- a/pkg/k8s/mocks/client.go +++ b/pkg/k8s/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.3. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mocks diff --git a/pkg/manager/mocks/manager.go b/pkg/manager/mocks/manager.go index 94ba82af..9b6af3f5 100644 --- a/pkg/manager/mocks/manager.go +++ b/pkg/manager/mocks/manager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.3. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mocks diff --git a/pkg/nats/mocks/client.go b/pkg/nats/mocks/client.go index 9262a275..9dedba9e 100644 --- a/pkg/nats/mocks/client.go +++ b/pkg/nats/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.3. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mocks diff --git a/testutils/options.go b/testutils/options.go index 4f0e6cb3..5bb422c7 100644 --- a/testutils/options.go +++ b/testutils/options.go @@ -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