Skip to content

Commit

Permalink
fix(lifecycle-operator): remove hardcoded keptn namespace (#2141)
Browse files Browse the repository at this point in the history
Signed-off-by: realanna <[email protected]>
  • Loading branch information
RealAnna committed Sep 27, 2023
1 parent 5487152 commit 1ff9fe8
Show file tree
Hide file tree
Showing 24 changed files with 208 additions and 71 deletions.
6 changes: 3 additions & 3 deletions klt-cert-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
}

type envConfig struct {
KLTNamespace string `envconfig:"NAMESPACE" default:"keptn-lifecycle-toolkit-system"`
KeptnNamespace string `envconfig:"NAMESPACE" default:"keptn-lifecycle-toolkit-system"`
KLTLabelSelectorKey string `envconfig:"LABEL_SELECTOR_KEY" default:"keptn.sh/inject-cert"`
KLTLabelSelectorValue string `envconfig:"LABEL_SELECTOR_VALUE" default:"true"`
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func main() {
Scheme: scheme,
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
env.KLTNamespace: {},
env.KeptnNamespace: {},
},
},
Metrics: metricsserver.Options{
Expand Down Expand Up @@ -99,7 +99,7 @@ func main() {
Scheme: mgr.GetScheme(),
CancelMgrFunc: nil,
Log: ctrl.Log.WithName("KeptnWebhookCert Controller"),
Namespace: env.KLTNamespace,
Namespace: env.KeptnNamespace,
MatchLabels: map[string]string{
env.KLTLabelSelectorKey: env.KLTLabelSelectorValue,
},
Expand Down
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
}
19 changes: 19 additions & 0 deletions lifecycle-operator/controllers/common/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,22 @@ func TestGetOptionsInstance(t *testing.T) {
timeout := o2.GetCreationRequestTimeout()
require.Equal(t, 5*time.Second, timeout)
}

func TestConfig_SetAndGetDefaultNamespace(t *testing.T) {
i := Instance()

ns := i.GetDefaultNamespace()

require.Empty(t, ns)
i.SetDefaultNamespace("test")
require.Equal(t, "test", i.GetDefaultNamespace())
}

func TestConfig_SetAndGetCloudEventEndpoint(t *testing.T) {
i := Instance()

ns := i.GetCloudEventsEndpoint()
require.Empty(t, ns)
i.SetCloudEventsEndpoint("mytestendpoint")
require.Equal(t, "mytestendpoint", i.GetCloudEventsEndpoint())
}
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.

5 changes: 2 additions & 3 deletions lifecycle-operator/controllers/common/helperfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ 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"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const KLTNamespace = "keptn-lifecycle-toolkit-system"

// GetItemStatus retrieves the state of the task/evaluation, if it does not exists, it creates a default one
func GetItemStatus(name string, instanceStatus []klcv1alpha3.ItemStatus) klcv1alpha3.ItemStatus {
for _, status := range instanceStatus {
Expand Down Expand Up @@ -96,7 +95,7 @@ func getObject(k8sclient client.Client, log logr.Logger, ctx context.Context, de
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: KLTNamespace}, 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
14 changes: 10 additions & 4 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 All @@ -14,6 +15,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

const KeptnNamespace = "keptn"

func Test_GetItemStatus(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -402,15 +405,15 @@ func Test_GetTaskDefinition(t *testing.T) {
taskDef: &klcv1alpha3.KeptnTaskDefinition{
ObjectMeta: v1.ObjectMeta{
Name: "taskDef",
Namespace: KLTNamespace,
Namespace: KeptnNamespace,
},
},
taskDefName: "taskDef",
taskDefNamespace: "some-namespace",
out: &klcv1alpha3.KeptnTaskDefinition{
ObjectMeta: v1.ObjectMeta{
Name: "taskDef",
Namespace: KLTNamespace,
Namespace: KeptnNamespace,
},
},
wantError: false,
Expand All @@ -420,6 +423,8 @@ 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()
Expand Down Expand Up @@ -484,15 +489,15 @@ func Test_GetEvaluationDefinition(t *testing.T) {
evalDef: &klcv1alpha3.KeptnEvaluationDefinition{
ObjectMeta: v1.ObjectMeta{
Name: "evalDef",
Namespace: KLTNamespace,
Namespace: KeptnNamespace,
},
},
evalDefName: "evalDef",
evalDefNamespace: "some-namespace",
out: &klcv1alpha3.KeptnEvaluationDefinition{
ObjectMeta: v1.ObjectMeta{
Name: "evalDef",
Namespace: KLTNamespace,
Namespace: KeptnNamespace,
},
},
wantError: false,
Expand All @@ -501,6 +506,7 @@ 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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"
"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"
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: common.KLTNamespace}, metric); err != nil {
p.Log.Error(err, "Failed to get KeptnMetric from "+common.KLTNamespace+" 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,7 +5,7 @@ import (
"testing"

klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common"
"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 All @@ -15,6 +15,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

const KeptnNamespace = "test"

func Test_keptnmetric(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -74,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 Down Expand Up @@ -187,14 +190,14 @@ func Test_Getkeptnmetric(t *testing.T) {
metric: &metricsapi.KeptnMetric{
ObjectMeta: metav1.ObjectMeta{
Name: "metric",
Namespace: common.KLTNamespace,
Namespace: KeptnNamespace,
},
},
namespace: "my-other-namespace",
out: &metricsapi.KeptnMetric{
ObjectMeta: metav1.ObjectMeta{
Name: "metric",
Namespace: common.KLTNamespace,
Namespace: KeptnNamespace,
},
},
wantError: false,
Expand All @@ -204,6 +207,7 @@ 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()
Expand Down
Loading

0 comments on commit 1ff9fe8

Please sign in to comment.