Skip to content

Commit

Permalink
feat(lifecycle-operator): adapt KeptnConfig reconciler to set up bloc…
Browse files Browse the repository at this point in the history
…kedDeployment parameter (keptn#3112)

Signed-off-by: odubajDT <[email protected]>
Signed-off-by: vickysomtee <[email protected]>
  • Loading branch information
odubajDT authored and Vickysomtee committed Apr 22, 2024
1 parent bafc74e commit e07174c
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 11 deletions.
16 changes: 15 additions & 1 deletion lifecycle-operator/controllers/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ 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
var once = sync.Once{}

func Instance() *ControllerConfig {
once.Do(func() {
instance = &ControllerConfig{keptnAppCreationRequestTimeout: defaultKeptnAppCreationRequestTimeout}
instance = &ControllerConfig{
keptnAppCreationRequestTimeout: defaultKeptnAppCreationRequestTimeout,
blockDeployment: true,
}
})
return instance
}
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions lifecycle-operator/controllers/common/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
83 changes: 82 additions & 1 deletion lifecycle-operator/controllers/common/config/fake/config_mock.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestKeptnConfigReconciler_Reconcile(t *testing.T) {
wantErr bool
wantCreationRequestTimeoutConfig time.Duration
wantCloudEventsEndpointConfig string
wantBlockDeployment bool
blockDeploymentCalls int
}{
{
name: "test 1",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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)
}
})
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit e07174c

Please sign in to comment.