From 007ceda2e4d546e0a512c737f0c61ffb0ae0a42f Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 29 Feb 2024 16:03:21 +0100 Subject: [PATCH 1/2] feat(lifecycle-operator): adapt KeptnConfig reconciler to set up observabilityTimeout parameter Signed-off-by: odubajDT --- .../controllers/common/config/config.go | 16 ++++ .../controllers/common/config/config_test.go | 17 ++++ .../common/config/fake/config_mock.go | 84 ++++++++++++++++++- .../options/keptnconfig_controller.go | 1 + .../options/keptnconfig_controller_test.go | 60 ++++++++++--- 5 files changed, 165 insertions(+), 13 deletions(-) diff --git a/lifecycle-operator/controllers/common/config/config.go b/lifecycle-operator/controllers/common/config/config.go index 3c641ad264..467fce739e 100644 --- a/lifecycle-operator/controllers/common/config/config.go +++ b/lifecycle-operator/controllers/common/config/config.go @@ -3,6 +3,8 @@ package config import ( "sync" "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const defaultKeptnAppCreationRequestTimeout = 30 * time.Second @@ -17,6 +19,8 @@ type IConfig interface { GetDefaultNamespace() string SetBlockDeployment(value bool) GetBlockDeployment() bool + SetObservabilityTimeout(timeout metav1.Duration) + GetObservabilityTimeout() metav1.Duration } type ControllerConfig struct { @@ -24,6 +28,7 @@ type ControllerConfig struct { cloudEventsEndpoint string defaultNamespace string blockDeployment bool + observabilityTimeout metav1.Duration } var instance *ControllerConfig @@ -34,6 +39,9 @@ func Instance() *ControllerConfig { instance = &ControllerConfig{ keptnAppCreationRequestTimeout: defaultKeptnAppCreationRequestTimeout, blockDeployment: true, + observabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, } }) return instance @@ -70,3 +78,11 @@ func (o *ControllerConfig) SetBlockDeployment(value bool) { func (o *ControllerConfig) GetBlockDeployment() bool { return o.blockDeployment } + +func (o *ControllerConfig) SetObservabilityTimeout(timeout metav1.Duration) { + o.observabilityTimeout = timeout +} + +func (o *ControllerConfig) GetObservabilityTimeout() metav1.Duration { + return o.observabilityTimeout +} diff --git a/lifecycle-operator/controllers/common/config/config_test.go b/lifecycle-operator/controllers/common/config/config_test.go index 97c9616d22..6f6314401c 100644 --- a/lifecycle-operator/controllers/common/config/config_test.go +++ b/lifecycle-operator/controllers/common/config/config_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/require" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestConfig_GetDefaultCreationRequestTimeout(t *testing.T) { @@ -64,3 +65,19 @@ func TestConfig_SetAndGetBlockDeployment(t *testing.T) { i.SetBlockDeployment(false) require.False(t, i.GetBlockDeployment()) } + +func TestConfig_SetAndGetObservabilityTimeout(t *testing.T) { + i := Instance() + + require.Equal(t, metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, i.GetObservabilityTimeout()) + + i.SetObservabilityTimeout(metav1.Duration{ + Duration: time.Duration(10 * time.Minute), + }) + + require.Equal(t, metav1.Duration{ + Duration: time.Duration(10 * time.Minute), + }, i.GetObservabilityTimeout()) +} diff --git a/lifecycle-operator/controllers/common/config/fake/config_mock.go b/lifecycle-operator/controllers/common/config/fake/config_mock.go index 5d0c38141d..0fe19e9df3 100644 --- a/lifecycle-operator/controllers/common/config/fake/config_mock.go +++ b/lifecycle-operator/controllers/common/config/fake/config_mock.go @@ -4,6 +4,7 @@ package fake import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sync" "time" ) @@ -26,6 +27,9 @@ import ( // GetDefaultNamespaceFunc: func() string { // panic("mock out the GetDefaultNamespace method") // }, +// GetObservabilityTimeoutFunc: func() metav1.Duration { +// panic("mock out the GetObservabilityTimeout method") +// }, // SetBlockDeploymentFunc: func(value bool) { // panic("mock out the SetBlockDeployment method") // }, @@ -38,6 +42,9 @@ import ( // SetDefaultNamespaceFunc: func(namespace string) { // panic("mock out the SetDefaultNamespace method") // }, +// SetObservabilityTimeoutFunc: func(timeout metav1.Duration) { +// panic("mock out the SetObservabilityTimeout method") +// }, // } // // // use mockedIConfig in code that requires config.IConfig @@ -57,6 +64,9 @@ type MockConfig struct { // GetDefaultNamespaceFunc mocks the GetDefaultNamespace method. GetDefaultNamespaceFunc func() string + // GetObservabilityTimeoutFunc mocks the GetObservabilityTimeout method. + GetObservabilityTimeoutFunc func() metav1.Duration + // SetBlockDeploymentFunc mocks the SetBlockDeployment method. SetBlockDeploymentFunc func(value bool) @@ -69,6 +79,9 @@ type MockConfig struct { // SetDefaultNamespaceFunc mocks the SetDefaultNamespace method. SetDefaultNamespaceFunc func(namespace string) + // SetObservabilityTimeoutFunc mocks the SetObservabilityTimeout method. + SetObservabilityTimeoutFunc func(timeout metav1.Duration) + // calls tracks calls to the methods. calls struct { // GetBlockDeployment holds details about calls to the GetBlockDeployment method. @@ -83,6 +96,9 @@ type MockConfig struct { // GetDefaultNamespace holds details about calls to the GetDefaultNamespace method. GetDefaultNamespace []struct { } + // GetObservabilityTimeout holds details about calls to the GetObservabilityTimeout method. + GetObservabilityTimeout []struct { + } // SetBlockDeployment holds details about calls to the SetBlockDeployment method. SetBlockDeployment []struct { // Value is the value argument value. @@ -103,15 +119,22 @@ type MockConfig struct { // Namespace is the namespace argument value. Namespace string } + // SetObservabilityTimeout holds details about calls to the SetObservabilityTimeout method. + SetObservabilityTimeout []struct { + // Timeout is the timeout argument value. + Timeout metav1.Duration + } } lockGetBlockDeployment sync.RWMutex lockGetCloudEventsEndpoint sync.RWMutex lockGetCreationRequestTimeout sync.RWMutex lockGetDefaultNamespace sync.RWMutex + lockGetObservabilityTimeout sync.RWMutex lockSetBlockDeployment sync.RWMutex lockSetCloudEventsEndpoint sync.RWMutex lockSetCreationRequestTimeout sync.RWMutex lockSetDefaultNamespace sync.RWMutex + lockSetObservabilityTimeout sync.RWMutex } // GetBlockDeployment calls GetBlockDeploymentFunc. @@ -222,6 +245,33 @@ func (mock *MockConfig) GetDefaultNamespaceCalls() []struct { return calls } +// GetObservabilityTimeout calls GetObservabilityTimeoutFunc. +func (mock *MockConfig) GetObservabilityTimeout() metav1.Duration { + if mock.GetObservabilityTimeoutFunc == nil { + panic("MockConfig.GetObservabilityTimeoutFunc: method is nil but IConfig.GetObservabilityTimeout was just called") + } + callInfo := struct { + }{} + mock.lockGetObservabilityTimeout.Lock() + mock.calls.GetObservabilityTimeout = append(mock.calls.GetObservabilityTimeout, callInfo) + mock.lockGetObservabilityTimeout.Unlock() + return mock.GetObservabilityTimeoutFunc() +} + +// GetObservabilityTimeoutCalls gets all the calls that were made to GetObservabilityTimeout. +// Check the length with: +// +// len(mockedIConfig.GetObservabilityTimeoutCalls()) +func (mock *MockConfig) GetObservabilityTimeoutCalls() []struct { +} { + var calls []struct { + } + mock.lockGetObservabilityTimeout.RLock() + calls = mock.calls.GetObservabilityTimeout + mock.lockGetObservabilityTimeout.RUnlock() + return calls +} + // SetBlockDeployment calls SetBlockDeploymentFunc. func (mock *MockConfig) SetBlockDeployment(value bool) { if mock.SetBlockDeploymentFunc == nil { @@ -348,4 +398,36 @@ func (mock *MockConfig) SetDefaultNamespaceCalls() []struct { calls = mock.calls.SetDefaultNamespace mock.lockSetDefaultNamespace.RUnlock() return calls -} \ No newline at end of file +} + +// SetObservabilityTimeout calls SetObservabilityTimeoutFunc. +func (mock *MockConfig) SetObservabilityTimeout(timeout metav1.Duration) { + if mock.SetObservabilityTimeoutFunc == nil { + panic("MockConfig.SetObservabilityTimeoutFunc: method is nil but IConfig.SetObservabilityTimeout was just called") + } + callInfo := struct { + Timeout metav1.Duration + }{ + Timeout: timeout, + } + mock.lockSetObservabilityTimeout.Lock() + mock.calls.SetObservabilityTimeout = append(mock.calls.SetObservabilityTimeout, callInfo) + mock.lockSetObservabilityTimeout.Unlock() + mock.SetObservabilityTimeoutFunc(timeout) +} + +// SetObservabilityTimeoutCalls gets all the calls that were made to SetObservabilityTimeout. +// Check the length with: +// +// len(mockedIConfig.SetObservabilityTimeoutCalls()) +func (mock *MockConfig) SetObservabilityTimeoutCalls() []struct { + Timeout metav1.Duration +} { + var calls []struct { + Timeout metav1.Duration + } + mock.lockSetObservabilityTimeout.RLock() + calls = mock.calls.SetObservabilityTimeout + mock.lockSetObservabilityTimeout.RUnlock() + return calls +} diff --git a/lifecycle-operator/controllers/options/keptnconfig_controller.go b/lifecycle-operator/controllers/options/keptnconfig_controller.go index 58aac34301..d6a04ce5a7 100644 --- a/lifecycle-operator/controllers/options/keptnconfig_controller.go +++ b/lifecycle-operator/controllers/options/keptnconfig_controller.go @@ -77,6 +77,7 @@ func (r *KeptnConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.config.SetCreationRequestTimeout(time.Duration(cfg.Spec.KeptnAppCreationRequestTimeoutSeconds) * time.Second) r.config.SetCloudEventsEndpoint(cfg.Spec.CloudEventsEndpoint) r.config.SetBlockDeployment(cfg.Spec.BlockDeployment) + r.config.SetBlockDeployment(cfg.Spec.ObservabilityTimeout) 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 10e441bd12..e41df9783a 100644 --- a/lifecycle-operator/controllers/options/keptnconfig_controller_test.go +++ b/lifecycle-operator/controllers/options/keptnconfig_controller_test.go @@ -41,6 +41,8 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { wantCloudEventsEndpointConfig string wantBlockDeployment bool blockDeploymentCalls int + wantObservabilityTimeout metav1.Duration + observabilityTimeoutCalls int }{ { name: "test 1", @@ -61,13 +63,20 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { Spec: optionsv1alpha1.KeptnConfigSpec{ OTelCollectorUrl: "", BlockDeployment: true, + ObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, }, }, - lastAppliedConfig: &optionsv1alpha1.KeptnConfigSpec{}, - want: ctrl.Result{}, - wantErr: false, - wantBlockDeployment: true, - blockDeploymentCalls: 1, + lastAppliedConfig: &optionsv1alpha1.KeptnConfigSpec{}, + want: ctrl.Result{}, + wantErr: false, + wantBlockDeployment: true, + blockDeploymentCalls: 1, + observabilityTimeoutCalls: 1, + wantObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, }, { name: "test 2", @@ -88,12 +97,19 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { Spec: optionsv1alpha1.KeptnConfigSpec{ OTelCollectorUrl: "", BlockDeployment: true, + ObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, }, }, - want: ctrl.Result{}, - wantErr: false, - wantBlockDeployment: true, - blockDeploymentCalls: 1, + want: ctrl.Result{}, + wantErr: false, + wantBlockDeployment: true, + blockDeploymentCalls: 1, + observabilityTimeoutCalls: 1, + wantObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, }, { name: "test 3", @@ -112,9 +128,10 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { Namespace: "keptn-system", }, }, - want: ctrl.Result{}, - wantErr: false, - blockDeploymentCalls: 0, + want: ctrl.Result{}, + wantErr: false, + blockDeploymentCalls: 0, + observabilityTimeoutCalls: 0, }, { name: "test 4", @@ -141,6 +158,9 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { KeptnAppCreationRequestTimeoutSeconds: 10, CloudEventsEndpoint: "ce-endpoint", BlockDeployment: false, + ObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, }, }, want: ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, @@ -149,6 +169,10 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { wantErr: true, wantBlockDeployment: false, blockDeploymentCalls: 1, + observabilityTimeoutCalls: 1, + wantObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(5 * time.Minute), + }, }, { name: "test 5", @@ -174,6 +198,9 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { KeptnAppCreationRequestTimeoutSeconds: 10, CloudEventsEndpoint: "ce-endpoint", BlockDeployment: false, + ObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(10 * time.Minute), + }, }, }, want: ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, @@ -182,6 +209,10 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { wantErr: true, wantBlockDeployment: false, blockDeploymentCalls: 1, + observabilityTimeoutCalls: 1, + wantObservabilityTimeout: metav1.Duration{ + Duration: time.Duration(10 * time.Minute), + }, }, } for _, tt := range tests { @@ -211,6 +242,10 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) { if tt.blockDeploymentCalls > 0 { require.Equal(t, tt.wantBlockDeployment, mockConfig.SetBlockDeploymentCalls()[0].Value) } + require.Len(t, mockConfig.SetObservabilityTimeoutCalls(), tt.observabilityTimeoutCalls) + if tt.observabilityTimeoutCalls > 0 { + require.Equal(t, tt.wantObservabilityTimeout, mockConfig.SetObservabilityTimeoutCalls()[0].Timeout) + } }) } } @@ -353,6 +388,7 @@ func setupReconciler(withConfig *optionsv1alpha1.KeptnConfig) *KeptnConfigReconc SetCloudEventsEndpointFunc: func(endpoint string) {}, SetCreationRequestTimeoutFunc: func(value time.Duration) {}, SetBlockDeploymentFunc: func(value bool) {}, + SetObservabilityTimeoutFunc: func(timeout metav1.Duration) {}, } return r } From 5887ed8bd87c7cddd4e8c9677ec23291084e9c70 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 1 Mar 2024 09:12:31 +0100 Subject: [PATCH 2/2] minor fix Signed-off-by: odubajDT --- .../controllers/options/keptnconfig_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lifecycle-operator/controllers/options/keptnconfig_controller.go b/lifecycle-operator/controllers/options/keptnconfig_controller.go index d6a04ce5a7..c75dc3077b 100644 --- a/lifecycle-operator/controllers/options/keptnconfig_controller.go +++ b/lifecycle-operator/controllers/options/keptnconfig_controller.go @@ -77,7 +77,7 @@ func (r *KeptnConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.config.SetCreationRequestTimeout(time.Duration(cfg.Spec.KeptnAppCreationRequestTimeoutSeconds) * time.Second) r.config.SetCloudEventsEndpoint(cfg.Spec.CloudEventsEndpoint) r.config.SetBlockDeployment(cfg.Spec.BlockDeployment) - r.config.SetBlockDeployment(cfg.Spec.ObservabilityTimeout) + r.config.SetObservabilityTimeout(cfg.Spec.ObservabilityTimeout) result, err := r.reconcileOtelCollectorUrl(cfg) if err != nil { return result, err