diff --git a/lifecycle-operator/controllers/common/config/config.go b/lifecycle-operator/controllers/common/config/config.go index 3d194b3f05c..3c641ad2645 100644 --- a/lifecycle-operator/controllers/common/config/config.go +++ b/lifecycle-operator/controllers/common/config/config.go @@ -15,12 +15,15 @@ type IConfig interface { GetCloudEventsEndpoint() string SetDefaultNamespace(namespace string) GetDefaultNamespace() string + SetBlockDeployment(value bool) + GetBlockDeployment() bool } type ControllerConfig struct { keptnAppCreationRequestTimeout time.Duration cloudEventsEndpoint string defaultNamespace string + blockDeployment bool } var instance *ControllerConfig @@ -28,7 +31,10 @@ var once = sync.Once{} func Instance() *ControllerConfig { once.Do(func() { - instance = &ControllerConfig{keptnAppCreationRequestTimeout: defaultKeptnAppCreationRequestTimeout} + instance = &ControllerConfig{ + keptnAppCreationRequestTimeout: defaultKeptnAppCreationRequestTimeout, + blockDeployment: true, + } }) return instance } @@ -56,3 +62,11 @@ func (o *ControllerConfig) SetDefaultNamespace(ns string) { func (o *ControllerConfig) GetDefaultNamespace() string { return o.defaultNamespace } + +func (o *ControllerConfig) SetBlockDeployment(value bool) { + o.blockDeployment = value +} + +func (o *ControllerConfig) GetBlockDeployment() bool { + return o.blockDeployment +} diff --git a/lifecycle-operator/controllers/common/config/config_test.go b/lifecycle-operator/controllers/common/config/config_test.go index 40b4c9787ae..97c9616d222 100644 --- a/lifecycle-operator/controllers/common/config/config_test.go +++ b/lifecycle-operator/controllers/common/config/config_test.go @@ -55,3 +55,12 @@ func TestConfig_SetAndGetCloudEventEndpoint(t *testing.T) { i.SetCloudEventsEndpoint("mytestendpoint") require.Equal(t, "mytestendpoint", i.GetCloudEventsEndpoint()) } + +func TestConfig_SetAndGetBlockDeployment(t *testing.T) { + i := Instance() + + blocked := i.GetBlockDeployment() + require.True(t, blocked) + i.SetBlockDeployment(false) + require.False(t, i.GetBlockDeployment()) +} diff --git a/lifecycle-operator/controllers/common/config/fake/config_mock.go b/lifecycle-operator/controllers/common/config/fake/config_mock.go index ef66114648d..5d0c38141d8 100644 --- a/lifecycle-operator/controllers/common/config/fake/config_mock.go +++ b/lifecycle-operator/controllers/common/config/fake/config_mock.go @@ -14,6 +14,9 @@ import ( // // // make and configure a mocked config.IConfig // mockedIConfig := &MockConfig{ +// GetBlockDeploymentFunc: func() bool { +// panic("mock out the GetBlockDeployment method") +// }, // GetCloudEventsEndpointFunc: func() string { // panic("mock out the GetCloudEventsEndpoint method") // }, @@ -23,6 +26,9 @@ import ( // GetDefaultNamespaceFunc: func() string { // panic("mock out the GetDefaultNamespace method") // }, +// SetBlockDeploymentFunc: func(value bool) { +// panic("mock out the SetBlockDeployment method") +// }, // SetCloudEventsEndpointFunc: func(endpoint string) { // panic("mock out the SetCloudEventsEndpoint method") // }, @@ -39,6 +45,9 @@ import ( // // } type MockConfig struct { + // GetBlockDeploymentFunc mocks the GetBlockDeployment method. + GetBlockDeploymentFunc func() bool + // GetCloudEventsEndpointFunc mocks the GetCloudEventsEndpoint method. GetCloudEventsEndpointFunc func() string @@ -48,6 +57,9 @@ type MockConfig struct { // GetDefaultNamespaceFunc mocks the GetDefaultNamespace method. GetDefaultNamespaceFunc func() string + // SetBlockDeploymentFunc mocks the SetBlockDeployment method. + SetBlockDeploymentFunc func(value bool) + // SetCloudEventsEndpointFunc mocks the SetCloudEventsEndpoint method. SetCloudEventsEndpointFunc func(endpoint string) @@ -59,6 +71,9 @@ type MockConfig struct { // calls tracks calls to the methods. calls struct { + // GetBlockDeployment holds details about calls to the GetBlockDeployment method. + GetBlockDeployment []struct { + } // GetCloudEventsEndpoint holds details about calls to the GetCloudEventsEndpoint method. GetCloudEventsEndpoint []struct { } @@ -68,6 +83,11 @@ type MockConfig struct { // GetDefaultNamespace holds details about calls to the GetDefaultNamespace method. GetDefaultNamespace []struct { } + // SetBlockDeployment holds details about calls to the SetBlockDeployment method. + SetBlockDeployment []struct { + // Value is the value argument value. + Value bool + } // SetCloudEventsEndpoint holds details about calls to the SetCloudEventsEndpoint method. SetCloudEventsEndpoint []struct { // Endpoint is the endpoint argument value. @@ -84,14 +104,43 @@ type MockConfig struct { Namespace string } } + lockGetBlockDeployment sync.RWMutex lockGetCloudEventsEndpoint sync.RWMutex lockGetCreationRequestTimeout sync.RWMutex lockGetDefaultNamespace sync.RWMutex + lockSetBlockDeployment sync.RWMutex lockSetCloudEventsEndpoint sync.RWMutex lockSetCreationRequestTimeout sync.RWMutex lockSetDefaultNamespace sync.RWMutex } +// GetBlockDeployment calls GetBlockDeploymentFunc. +func (mock *MockConfig) GetBlockDeployment() bool { + if mock.GetBlockDeploymentFunc == nil { + panic("MockConfig.GetBlockDeploymentFunc: method is nil but IConfig.GetBlockDeployment was just called") + } + callInfo := struct { + }{} + mock.lockGetBlockDeployment.Lock() + mock.calls.GetBlockDeployment = append(mock.calls.GetBlockDeployment, callInfo) + mock.lockGetBlockDeployment.Unlock() + return mock.GetBlockDeploymentFunc() +} + +// GetBlockDeploymentCalls gets all the calls that were made to GetBlockDeployment. +// Check the length with: +// +// len(mockedIConfig.GetBlockDeploymentCalls()) +func (mock *MockConfig) GetBlockDeploymentCalls() []struct { +} { + var calls []struct { + } + mock.lockGetBlockDeployment.RLock() + calls = mock.calls.GetBlockDeployment + mock.lockGetBlockDeployment.RUnlock() + return calls +} + // GetCloudEventsEndpoint calls GetCloudEventsEndpointFunc. func (mock *MockConfig) GetCloudEventsEndpoint() string { if mock.GetCloudEventsEndpointFunc == nil { @@ -173,6 +222,38 @@ func (mock *MockConfig) GetDefaultNamespaceCalls() []struct { return calls } +// SetBlockDeployment calls SetBlockDeploymentFunc. +func (mock *MockConfig) SetBlockDeployment(value bool) { + if mock.SetBlockDeploymentFunc == nil { + panic("MockConfig.SetBlockDeploymentFunc: method is nil but IConfig.SetBlockDeployment was just called") + } + callInfo := struct { + Value bool + }{ + Value: value, + } + mock.lockSetBlockDeployment.Lock() + mock.calls.SetBlockDeployment = append(mock.calls.SetBlockDeployment, callInfo) + mock.lockSetBlockDeployment.Unlock() + mock.SetBlockDeploymentFunc(value) +} + +// SetBlockDeploymentCalls gets all the calls that were made to SetBlockDeployment. +// Check the length with: +// +// len(mockedIConfig.SetBlockDeploymentCalls()) +func (mock *MockConfig) SetBlockDeploymentCalls() []struct { + Value bool +} { + var calls []struct { + Value bool + } + mock.lockSetBlockDeployment.RLock() + calls = mock.calls.SetBlockDeployment + mock.lockSetBlockDeployment.RUnlock() + return calls +} + // SetCloudEventsEndpoint calls SetCloudEventsEndpointFunc. func (mock *MockConfig) SetCloudEventsEndpoint(endpoint string) { if mock.SetCloudEventsEndpointFunc == nil { @@ -267,4 +348,4 @@ func (mock *MockConfig) SetDefaultNamespaceCalls() []struct { calls = mock.calls.SetDefaultNamespace mock.lockSetDefaultNamespace.RUnlock() return calls -} +} \ No newline at end of file diff --git a/lifecycle-operator/controllers/options/keptnconfig_controller.go b/lifecycle-operator/controllers/options/keptnconfig_controller.go index fe50e286589..58aac343015 100644 --- a/lifecycle-operator/controllers/options/keptnconfig_controller.go +++ b/lifecycle-operator/controllers/options/keptnconfig_controller.go @@ -76,6 +76,7 @@ func (r *KeptnConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) // reconcile config values r.config.SetCreationRequestTimeout(time.Duration(cfg.Spec.KeptnAppCreationRequestTimeoutSeconds) * time.Second) r.config.SetCloudEventsEndpoint(cfg.Spec.CloudEventsEndpoint) + r.config.SetBlockDeployment(cfg.Spec.BlockDeployment) result, err := r.reconcileOtelCollectorUrl(cfg) if err != nil { return result, err diff --git a/lifecycle-operator/controllers/options/keptnconfig_controller_test.go b/lifecycle-operator/controllers/options/keptnconfig_controller_test.go index f32acc321ff..10e441bd128 100644 --- a/lifecycle-operator/controllers/options/keptnconfig_controller_test.go +++ b/lifecycle-operator/controllers/options/keptnconfig_controller_test.go @@ -39,6 +39,8 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { wantErr bool wantCreationRequestTimeoutConfig time.Duration wantCloudEventsEndpointConfig string + wantBlockDeployment bool + blockDeploymentCalls int }{ { name: "test 1", @@ -58,11 +60,14 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { }, Spec: optionsv1alpha1.KeptnConfigSpec{ OTelCollectorUrl: "", + BlockDeployment: true, }, }, - lastAppliedConfig: &optionsv1alpha1.KeptnConfigSpec{}, - want: ctrl.Result{}, - wantErr: false, + lastAppliedConfig: &optionsv1alpha1.KeptnConfigSpec{}, + want: ctrl.Result{}, + wantErr: false, + wantBlockDeployment: true, + blockDeploymentCalls: 1, }, { name: "test 2", @@ -82,10 +87,13 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { }, Spec: optionsv1alpha1.KeptnConfigSpec{ OTelCollectorUrl: "", + BlockDeployment: true, }, }, - want: ctrl.Result{}, - wantErr: false, + want: ctrl.Result{}, + wantErr: false, + wantBlockDeployment: true, + blockDeploymentCalls: 1, }, { name: "test 3", @@ -103,15 +111,47 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { Name: "empty-config", Namespace: "keptn-system", }, + }, + want: ctrl.Result{}, + wantErr: false, + blockDeploymentCalls: 0, + }, + { + name: "test 4", + args: args{ + ctx: context.TODO(), + req: ctrl.Request{ + NamespacedName: types.NamespacedName{ + Namespace: "keptn-system", + Name: "config1", + }, + }, + }, + lastAppliedConfig: &optionsv1alpha1.KeptnConfigSpec{ + OTelCollectorUrl: "some-url", + BlockDeployment: true, + }, + reconcileConfig: &optionsv1alpha1.KeptnConfig{ + ObjectMeta: metav1.ObjectMeta{ + Name: "config1", + Namespace: "keptn-system", + }, Spec: optionsv1alpha1.KeptnConfigSpec{ - OTelCollectorUrl: "", + OTelCollectorUrl: "url1", + KeptnAppCreationRequestTimeoutSeconds: 10, + CloudEventsEndpoint: "ce-endpoint", + BlockDeployment: false, }, }, - want: ctrl.Result{}, - wantErr: false, + want: ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, + wantCloudEventsEndpointConfig: "ce-endpoint", + wantCreationRequestTimeoutConfig: 10 * time.Second, + wantErr: true, + wantBlockDeployment: false, + blockDeploymentCalls: 1, }, { - name: "test 4", + name: "test 5", args: args{ ctx: context.TODO(), req: ctrl.Request{ @@ -133,12 +173,15 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { OTelCollectorUrl: "url1", KeptnAppCreationRequestTimeoutSeconds: 10, CloudEventsEndpoint: "ce-endpoint", + BlockDeployment: false, }, }, want: ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, wantCloudEventsEndpointConfig: "ce-endpoint", wantCreationRequestTimeoutConfig: 10 * time.Second, wantErr: true, + wantBlockDeployment: false, + blockDeploymentCalls: 1, }, } for _, tt := range tests { @@ -164,6 +207,10 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { require.Len(t, mockConfig.SetCloudEventsEndpointCalls(), 1) require.Equal(t, tt.wantCloudEventsEndpointConfig, mockConfig.SetCloudEventsEndpointCalls()[0].Endpoint) } + require.Len(t, mockConfig.SetBlockDeploymentCalls(), tt.blockDeploymentCalls) + if tt.blockDeploymentCalls > 0 { + require.Equal(t, tt.wantBlockDeployment, mockConfig.SetBlockDeploymentCalls()[0].Value) + } }) } } @@ -305,6 +352,7 @@ func setupReconciler(withConfig *optionsv1alpha1.KeptnConfig) *KeptnConfigReconc r.config = &fakeconfig.MockConfig{ SetCloudEventsEndpointFunc: func(endpoint string) {}, SetCreationRequestTimeoutFunc: func(value time.Duration) {}, + SetBlockDeploymentFunc: func(value bool) {}, } return r }