Skip to content

Commit

Permalink
chore: use config instead
Browse files Browse the repository at this point in the history
Signed-off-by: realanna <[email protected]>
  • Loading branch information
RealAnna committed Sep 21, 2023
1 parent a6741b7 commit 29f643c
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 65 deletions.
11 changes: 11 additions & 0 deletions lifecycle-operator/controllers/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ type IConfig interface {
GetCreationRequestTimeout() time.Duration
SetCloudEventsEndpoint(endpoint string)
GetCloudEventsEndpoint() string
SetDefaultNamespace(namespace string)
GetDefaultNamespace() string
}

type ControllerConfig struct {
keptnAppCreationRequestTimeout time.Duration
cloudEventsEndpoint string
defaultNamespace string
}

var instance *ControllerConfig
Expand Down Expand Up @@ -45,3 +48,11 @@ func (o *ControllerConfig) SetCloudEventsEndpoint(endpoint string) {
func (o *ControllerConfig) GetCloudEventsEndpoint() string {
return o.cloudEventsEndpoint
}

func (o *ControllerConfig) SetDefaultNamespace(ns string) {
o.defaultNamespace = ns
}

func (o *ControllerConfig) GetDefaultNamespace() string {
return o.defaultNamespace
}
81 changes: 81 additions & 0 deletions 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.

13 changes: 7 additions & 6 deletions lifecycle-operator/controllers/common/helperfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-logr/logr"
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/lifecycle/interfaces"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -73,28 +74,28 @@ func copyMap[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
}
}

func GetTaskDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string, defaultNamespace string) (*klcv1alpha3.KeptnTaskDefinition, error) {
func GetTaskDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string) (*klcv1alpha3.KeptnTaskDefinition, error) {
definition := &klcv1alpha3.KeptnTaskDefinition{}
if err := getObject(k8sclient, log, ctx, definitionName, namespace, definition, defaultNamespace); err != nil {
if err := getObject(k8sclient, log, ctx, definitionName, namespace, definition); err != nil {
return nil, err
}
return definition, nil
}

func GetEvaluationDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string, defaultNamespace string) (*klcv1alpha3.KeptnEvaluationDefinition, error) {
func GetEvaluationDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string) (*klcv1alpha3.KeptnEvaluationDefinition, error) {
definition := &klcv1alpha3.KeptnEvaluationDefinition{}
if err := getObject(k8sclient, log, ctx, definitionName, namespace, definition, defaultNamespace); err != nil {
if err := getObject(k8sclient, log, ctx, definitionName, namespace, definition); err != nil {
return nil, err
}
return definition, nil
}

func getObject(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string, definition client.Object, defaultNamespace string) error {
func getObject(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string, definition client.Object) error {
err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: namespace}, definition)
if err != nil {
log.Info("Failed to get resource from application namespace", "resource type", fmt.Sprintf("%T", definition), "Definition name", definitionName, "namespace", namespace)
if k8serrors.IsNotFound(err) {
if err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: defaultNamespace}, definition); err != nil {
if err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: config.Instance().GetDefaultNamespace()}, definition); err != nil {
log.Info("Failed to get resource from default KLT namespace", "resource type", fmt.Sprintf("%T", definition), "definition name", definitionName)
return err
}
Expand Down
8 changes: 6 additions & 2 deletions lifecycle-operator/controllers/common/helperfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config"
"github.com/stretchr/testify/require"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -422,10 +423,12 @@ func Test_GetTaskDefinition(t *testing.T) {
err := klcv1alpha3.AddToScheme(scheme.Scheme)
require.Nil(t, err)

config.Instance().SetDefaultNamespace(KeptnNamespace)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder().WithObjects(tt.taskDef).Build()
d, err := GetTaskDefinition(client, ctrl.Log.WithName("testytest"), context.TODO(), tt.taskDefName, tt.taskDefNamespace, KeptnNamespace)
d, err := GetTaskDefinition(client, ctrl.Log.WithName("testytest"), context.TODO(), tt.taskDefName, tt.taskDefNamespace)
if tt.out != nil && d != nil {
require.Equal(t, tt.out.Name, d.Name)
require.Equal(t, tt.out.Namespace, d.Namespace)
Expand Down Expand Up @@ -503,11 +506,12 @@ func Test_GetEvaluationDefinition(t *testing.T) {

err := klcv1alpha3.AddToScheme(scheme.Scheme)
require.Nil(t, err)
config.Instance().SetDefaultNamespace(KeptnNamespace)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder().WithObjects(tt.evalDef).Build()
d, err := GetEvaluationDefinition(client, ctrl.Log.WithName("testytest"), context.TODO(), tt.evalDefName, tt.evalDefNamespace, KeptnNamespace)
d, err := GetEvaluationDefinition(client, ctrl.Log.WithName("testytest"), context.TODO(), tt.evalDefName, tt.evalDefNamespace)
if tt.out != nil && d != nil {
require.Equal(t, tt.out.Name, d.Name)
require.Equal(t, tt.out.Namespace, d.Namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

"github.com/go-logr/logr"
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type KeptnMetricProvider struct {
Log logr.Logger
K8sClient client.Client
DefaultNamespace string
Log logr.Logger
K8sClient client.Client
}

// FetchData fetches the SLI values from KeptnMetric resource
Expand Down Expand Up @@ -57,8 +57,9 @@ func (p *KeptnMetricProvider) GetKeptnMetric(ctx context.Context, objective klcv
} else {
if err := p.K8sClient.Get(ctx, types.NamespacedName{Name: objective.KeptnMetricRef.Name, Namespace: namespace}, metric); err != nil {
p.Log.Error(err, "Failed to get KeptnMetric from KeptnEvaluation resource namespace")
if err := p.K8sClient.Get(ctx, types.NamespacedName{Name: objective.KeptnMetricRef.Name, Namespace: p.DefaultNamespace}, metric); err != nil {
p.Log.Error(err, "Failed to get KeptnMetric from "+p.DefaultNamespace+" namespace")
defaultNamespace := config.Instance().GetDefaultNamespace()
if err := p.K8sClient.Get(ctx, types.NamespacedName{Name: objective.KeptnMetricRef.Name, Namespace: defaultNamespace}, metric); err != nil {
p.Log.Error(err, "Failed to get KeptnMetric from "+defaultNamespace+" namespace")
return nil, err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config"
metricsapi "github.com/keptn/lifecycle-toolkit/lifecycle-operator/test/api/metrics/v1alpha3"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -75,6 +76,7 @@ func Test_keptnmetric(t *testing.T) {
wantError: false,
},
}
config.Instance().SetDefaultNamespace(KeptnNamespace)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -83,9 +85,8 @@ func Test_keptnmetric(t *testing.T) {
client := fake.NewClientBuilder().WithObjects(tt.metric).Build()

kmp := KeptnMetricProvider{
Log: ctrl.Log.WithName("testytest"),
K8sClient: client,
DefaultNamespace: KeptnNamespace,
Log: ctrl.Log.WithName("testytest"),
K8sClient: client,
}

obj := klcv1alpha3.Objective{
Expand Down Expand Up @@ -206,13 +207,13 @@ func Test_Getkeptnmetric(t *testing.T) {
err := metricsapi.AddToScheme(scheme.Scheme)
require.Nil(t, err)

config.Instance().SetDefaultNamespace(KeptnNamespace)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder().WithObjects(tt.metric).Build()
kmp := KeptnMetricProvider{
Log: ctrl.Log.WithName("testytest"),
K8sClient: client,
DefaultNamespace: KeptnNamespace,
Log: ctrl.Log.WithName("testytest"),
K8sClient: client,
}

m, err := kmp.GetKeptnMetric(context.TODO(), tt.objective, tt.namespace)
Expand Down
13 changes: 6 additions & 7 deletions lifecycle-operator/controllers/common/taskhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (

type TaskHandler struct {
client.Client
EventSender IEvent
Log logr.Logger
Tracer trace.Tracer
Scheme *runtime.Scheme
SpanHandler telemetry.ISpanHandler
DefaultNamespace string
EventSender IEvent
Log logr.Logger
Tracer trace.Tracer
Scheme *runtime.Scheme
SpanHandler telemetry.ISpanHandler
}

type CreateTaskAttributes struct {
Expand Down Expand Up @@ -159,7 +158,7 @@ func (r TaskHandler) setupTasks(taskCreateAttributes CreateTaskAttributes, piWra
}

func (r TaskHandler) handleTaskNotExists(ctx context.Context, phaseCtx context.Context, taskCreateAttributes CreateTaskAttributes, taskName string, piWrapper *interfaces.PhaseItemWrapper, reconcileObject client.Object, task *klcv1alpha3.KeptnTask, taskStatus *klcv1alpha3.ItemStatus) error {
definition, err := GetTaskDefinition(r.Client, r.Log, ctx, taskName, piWrapper.GetNamespace(), r.DefaultNamespace)
definition, err := GetTaskDefinition(r.Client, r.Log, ctx, taskName, piWrapper.GetNamespace())
if err != nil {
r.Log.Error(err, "could not find KeptnTaskDefinition")
return controllererrors.ErrCannotGetKeptnTaskDefinition
Expand Down
15 changes: 8 additions & 7 deletions lifecycle-operator/controllers/common/taskhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3/common"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/config"
kltfake "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/fake"
controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -353,6 +354,7 @@ func TestTaskHandler(t *testing.T) {
unbindSpanCalls: 1,
},
}
config.Instance().SetDefaultNamespace(KeptnNamespace)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -371,13 +373,12 @@ func TestTaskHandler(t *testing.T) {
initObjs = append(initObjs, tt.taskDef)
}
handler := TaskHandler{
SpanHandler: &spanHandlerMock,
Log: ctrl.Log.WithName("controller"),
EventSender: NewK8sSender(record.NewFakeRecorder(100)),
Client: fake.NewClientBuilder().WithObjects(initObjs...).Build(),
Tracer: trace.NewNoopTracerProvider().Tracer("tracer"),
Scheme: scheme.Scheme,
DefaultNamespace: KeptnNamespace,
SpanHandler: &spanHandlerMock,
Log: ctrl.Log.WithName("controller"),
EventSender: NewK8sSender(record.NewFakeRecorder(100)),
Client: fake.NewClientBuilder().WithObjects(initObjs...).Build(),
Tracer: trace.NewNoopTracerProvider().Tracer("tracer"),
Scheme: scheme.Scheme,
}
status, summary, err := handler.ReconcileTasks(context.TODO(), context.TODO(), tt.object, tt.createAttr)
if len(tt.wantStatus) == len(status) {
Expand Down
Loading

0 comments on commit 29f643c

Please sign in to comment.