diff --git a/controllers/codebase/service/chain/put_deploy_configs_test.go b/controllers/codebase/service/chain/put_deploy_configs_test.go index d0121c59..31176c72 100644 --- a/controllers/codebase/service/chain/put_deploy_configs_test.go +++ b/controllers/codebase/service/chain/put_deploy_configs_test.go @@ -14,6 +14,7 @@ import ( codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" gitServerMocks "github.com/epam/edp-codebase-operator/v2/pkg/git/mocks" + "github.com/epam/edp-codebase-operator/v2/pkg/platform" "github.com/epam/edp-codebase-operator/v2/pkg/util" ) @@ -67,7 +68,7 @@ func TestPutDeployConfigs_ShouldPass(t *testing.T) { } cm := &coreV1.ConfigMap{ ObjectMeta: metaV1.ObjectMeta{ - Name: "edp-config", + Name: platform.KrciConfigMap, Namespace: fakeNamespace, }, Data: map[string]string{ diff --git a/controllers/codebase/service/template/template.go b/controllers/codebase/service/template/template.go index 31c78311..fb6048be 100644 --- a/controllers/codebase/service/template/template.go +++ b/controllers/codebase/service/template/template.go @@ -55,16 +55,16 @@ func buildTemplateConfig(ctx context.Context, c client.Client, cb *codebaseApi.C log.Info("Start creating template config") - us, err := util.GetUserSettings(c, cb.Namespace) + conf, err := platform.GetKrciConfig(ctx, c, cb.Namespace) if err != nil { - return nil, fmt.Errorf("failed to get user settings settings: %w", err) + return nil, fmt.Errorf("failed to get user setting: %w", err) } cf := model.ConfigGoTemplating{ Name: cb.Name, PlatformType: platform.GetPlatformType(), Lang: cb.Spec.Lang, - DnsWildcard: us.DnsWildcard, + DnsWildcard: conf.DnsWildcard, Framework: cb.Spec.Framework, } diff --git a/controllers/codebase/service/template/template_test.go b/controllers/codebase/service/template/template_test.go index e02d0ba5..4c4556c3 100644 --- a/controllers/codebase/service/template/template_test.go +++ b/controllers/codebase/service/template/template_test.go @@ -14,6 +14,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/fake" codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" + "github.com/epam/edp-codebase-operator/v2/pkg/platform" "github.com/epam/edp-codebase-operator/v2/pkg/util" ) @@ -40,7 +41,7 @@ func TestPrepareTemplates_ShouldPass(t *testing.T) { } config := &coreV1.ConfigMap{ ObjectMeta: metaV1.ObjectMeta{ - Name: "edp-config", + Name: platform.KrciConfigMap, Namespace: fakeNamespace, }, } @@ -75,7 +76,7 @@ func TestPrepareTemplates_ShouldSkipSonarConfig(t *testing.T) { } config := &coreV1.ConfigMap{ ObjectMeta: metaV1.ObjectMeta{ - Name: "edp-config", + Name: platform.KrciConfigMap, Namespace: fakeNamespace, }, } @@ -111,7 +112,7 @@ func TestPrepareTemplates_ShouldFailOnGetProjectUrl(t *testing.T) { } config := &coreV1.ConfigMap{ ObjectMeta: metaV1.ObjectMeta{ - Name: "edp-config", + Name: platform.KrciConfigMap, Namespace: fakeNamespace, }, } diff --git a/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream.go b/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream.go index d39d6015..c6f352cf 100644 --- a/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream.go +++ b/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream.go @@ -5,7 +5,6 @@ import ( "fmt" "strings" - corev1 "k8s.io/api/core/v1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -25,11 +24,6 @@ type PutCodebaseImageStream struct { Client client.Client } -const ( - EdpConfigContainerRegistryHost = "container_registry_host" - EdpConfigContainerRegistrySpace = "container_registry_space" -) - func (h PutCodebaseImageStream) ServeRequest(ctx context.Context, cb *codebaseApi.CodebaseBranch) error { log := ctrl.LoggerFrom(ctx).WithName("put-codebase-image-stream") @@ -89,23 +83,20 @@ func ProcessNameToK8sConvention(name string) string { } func (h PutCodebaseImageStream) getDockerRegistryUrl(ctx context.Context, namespace string) (string, error) { - config := &corev1.ConfigMap{} - if err := h.Client.Get(ctx, types.NamespacedName{ - Name: platform.EdpConfigMap, - Namespace: namespace, - }, config); err != nil { - return "", fmt.Errorf("failed to get %s config map: %w", platform.EdpConfigMap, err) + config, err := platform.GetKrciConfig(ctx, h.Client, namespace) + if err != nil { + return "", fmt.Errorf("failed to get config: %w", err) } - if _, ok := config.Data[EdpConfigContainerRegistryHost]; !ok { - return "", fmt.Errorf("%s is not set in %s config map", EdpConfigContainerRegistryHost, platform.EdpConfigMap) + if config.KrciConfigContainerRegistryHost == "" { + return "", fmt.Errorf("%s is not set in %s config map", platform.KrciConfigContainerRegistryHost, platform.KrciConfigMap) } - if _, ok := config.Data[EdpConfigContainerRegistrySpace]; !ok { - return "", fmt.Errorf("%s is not set in %s config map", EdpConfigContainerRegistrySpace, platform.EdpConfigMap) + if config.KrciConfigContainerRegistrySpace == "" { + return "", fmt.Errorf("%s is not set in %s config map", platform.KrciConfigContainerRegistrySpace, platform.KrciConfigMap) } - return fmt.Sprintf("%s/%s", config.Data[EdpConfigContainerRegistryHost], config.Data[EdpConfigContainerRegistrySpace]), nil + return fmt.Sprintf("%s/%s", config.KrciConfigContainerRegistryHost, config.KrciConfigContainerRegistrySpace), nil } func (h PutCodebaseImageStream) createCodebaseImageStreamIfNotExists( diff --git a/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream_test.go b/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream_test.go index c69d80c4..fc3d9615 100644 --- a/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream_test.go +++ b/controllers/codebasebranch/chain/put_codebase_image_stream/put_codebase_image_stream_test.go @@ -48,12 +48,12 @@ func TestPutCodebaseImageStream_ServeRequest(t *testing.T) { }, &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, Namespace: "default", }, Data: map[string]string{ - EdpConfigContainerRegistryHost: "test-registry", - EdpConfigContainerRegistrySpace: "test-space", + platform.KrciConfigContainerRegistryHost: "test-registry", + platform.KrciConfigContainerRegistrySpace: "test-space", }, }, }, @@ -95,12 +95,12 @@ func TestPutCodebaseImageStream_ServeRequest(t *testing.T) { }, &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, Namespace: "default", }, Data: map[string]string{ - EdpConfigContainerRegistryHost: "test-registry", - EdpConfigContainerRegistrySpace: "test-space", + platform.KrciConfigContainerRegistryHost: "test-registry", + platform.KrciConfigContainerRegistrySpace: "test-space", }, }, }, diff --git a/controllers/codebasebranch/codebasebranch_controller_test.go b/controllers/codebasebranch/codebasebranch_controller_test.go index 2da16bcd..c84093a3 100644 --- a/controllers/codebasebranch/codebasebranch_controller_test.go +++ b/controllers/codebasebranch/codebasebranch_controller_test.go @@ -17,7 +17,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" - "github.com/epam/edp-codebase-operator/v2/controllers/codebasebranch/chain/put_codebase_image_stream" "github.com/epam/edp-codebase-operator/v2/pkg/codebasebranch" "github.com/epam/edp-codebase-operator/v2/pkg/platform" "github.com/epam/edp-codebase-operator/v2/pkg/util" @@ -206,14 +205,14 @@ func TestReconcileCodebaseBranch_Reconcile_ShouldPassWithCreatingCIS(t *testing. Available: true, }, } - edpConfig := &coreV1.ConfigMap{ + config := &coreV1.ConfigMap{ ObjectMeta: metaV1.ObjectMeta{ - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, Namespace: "namespace", }, Data: map[string]string{ - put_codebase_image_stream.EdpConfigContainerRegistryHost: "stub-url", - put_codebase_image_stream.EdpConfigContainerRegistrySpace: "stub-space", + platform.KrciConfigContainerRegistryHost: "stub-url", + platform.KrciConfigContainerRegistrySpace: "stub-space", }, } s := &coreV1.Secret{ @@ -234,7 +233,7 @@ func TestReconcileCodebaseBranch_Reconcile_ShouldPassWithCreatingCIS(t *testing. fakeCl := fake.NewClientBuilder(). WithScheme(scheme). - WithRuntimeObjects(c, cb, s, cis, edpConfig). + WithRuntimeObjects(c, cb, s, cis, config). WithStatusSubresource(cb). Build() diff --git a/controllers/gitserver/create_event_listener.go b/controllers/gitserver/create_event_listener.go index 12834d43..a87a0c58 100644 --- a/controllers/gitserver/create_event_listener.go +++ b/controllers/gitserver/create_event_listener.go @@ -152,7 +152,7 @@ func (h *CreateEventListener) createIngress(ctx context.Context, gitServer *code return fmt.Errorf("failed to get Ingress: %w", err) } - edpConf, err := platform.GetEdpConfig(ctx, h.k8sClient, gitServer.Namespace) + config, err := platform.GetKrciConfig(ctx, h.k8sClient, gitServer.Namespace) if err != nil { return fmt.Errorf("failed to get dnsWildcard: %w", err) } @@ -176,7 +176,7 @@ func (h *CreateEventListener) createIngress(ctx context.Context, gitServer *code "el-%s-%s.%s", gitServer.Name, gitServer.Namespace, - edpConf.DnsWildcard, + config.DnsWildcard, ), IngressRuleValue: networkingv1.IngressRuleValue{ HTTP: &networkingv1.HTTPIngressRuleValue{ @@ -236,7 +236,7 @@ func (h *CreateEventListener) createRoute(ctx context.Context, gitServer *codeba return fmt.Errorf("failed to get Route: %w", err) } - edpConf, err := platform.GetEdpConfig(ctx, h.k8sClient, gitServer.Namespace) + config, err := platform.GetKrciConfig(ctx, h.k8sClient, gitServer.Namespace) if err != nil { return fmt.Errorf("failed to get dnsWildcard: %w", err) } @@ -253,7 +253,7 @@ func (h *CreateEventListener) createRoute(ctx context.Context, gitServer *codeba "el-%s-%s.%s", gitServer.Name, gitServer.Namespace, - edpConf.DnsWildcard, + config.DnsWildcard, ), TLS: &routeApi.TLSConfig{ InsecureEdgeTerminationPolicy: routeApi.InsecureEdgeTerminationPolicyRedirect, diff --git a/controllers/gitserver/create_event_listener_test.go b/controllers/gitserver/create_event_listener_test.go index fbf60795..02b8636a 100644 --- a/controllers/gitserver/create_event_listener_test.go +++ b/controllers/gitserver/create_event_listener_test.go @@ -79,7 +79,7 @@ func TestCreateEventListener_ServeRequest(t *testing.T) { WithObjects(&corev1.ConfigMap{ ObjectMeta: controllerruntime.ObjectMeta{ Namespace: "default", - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, }, }). Build() @@ -117,7 +117,7 @@ func TestCreateEventListener_ServeRequest(t *testing.T) { &corev1.ConfigMap{ ObjectMeta: controllerruntime.ObjectMeta{ Namespace: "default", - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, }, }, &networkingv1.Ingress{ @@ -166,7 +166,7 @@ func TestCreateEventListener_ServeRequest(t *testing.T) { &corev1.ConfigMap{ ObjectMeta: controllerruntime.ObjectMeta{ Namespace: "default", - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, }, }, el, @@ -205,7 +205,7 @@ func TestCreateEventListener_ServeRequest(t *testing.T) { WithObjects(&corev1.ConfigMap{ ObjectMeta: controllerruntime.ObjectMeta{ Namespace: "default", - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, }, }). Build() @@ -243,7 +243,7 @@ func TestCreateEventListener_ServeRequest(t *testing.T) { &corev1.ConfigMap{ ObjectMeta: controllerruntime.ObjectMeta{ Namespace: "default", - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, }, }, &routeApi.Route{ @@ -274,7 +274,7 @@ func TestCreateEventListener_ServeRequest(t *testing.T) { }, }, { - name: "edpConfig not found", + name: "config not found", gitServer: &codebaseApi.GitServer{ ObjectMeta: controllerruntime.ObjectMeta{ Name: "test-git-server", diff --git a/pkg/platform/config.go b/pkg/platform/config.go new file mode 100644 index 00000000..8bab8647 --- /dev/null +++ b/pkg/platform/config.go @@ -0,0 +1,66 @@ +package platform + +import ( + "context" + "fmt" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +const ( + KrciConfigMap = "krci-config" + KrciConfigContainerRegistryHost = "container_registry_host" + KrciConfigContainerRegistrySpace = "container_registry_space" +) + +type KrciConfig struct { + DnsWildcard string + ContainerRegistryType string + EdpVersion string + KrciConfigContainerRegistryHost string + KrciConfigContainerRegistrySpace string +} + +func GetKrciConfig(ctx context.Context, k8sClient client.Client, namespace string) (*KrciConfig, error) { + config := &corev1.ConfigMap{} + if err := k8sClient.Get(ctx, client.ObjectKey{ + Namespace: namespace, + Name: KrciConfigMap, + }, config); err != nil { + // backward compatibility for edp-config config map. + if errors.IsNotFound(err) { + return getEdpConfig(ctx, k8sClient, namespace) + } + + return nil, fmt.Errorf("failed to get %s: %w", KrciConfigMap, err) + } + + return mapConfigToKrciConfig(config), nil +} + +// getEdpConfig is backward compatibility for edp-config config map. +// Deprecated: use GetKrciConfig instead. +// TODO: remove this function after all instances will be migrated to krci-config. +func getEdpConfig(ctx context.Context, k8sClient client.Client, namespace string) (*KrciConfig, error) { + config := &corev1.ConfigMap{} + if err := k8sClient.Get(ctx, client.ObjectKey{ + Namespace: namespace, + Name: "edp-config", + }, config); err != nil { + return nil, fmt.Errorf("failed to get edp-config: %w", err) + } + + return mapConfigToKrciConfig(config), nil +} + +func mapConfigToKrciConfig(config *corev1.ConfigMap) *KrciConfig { + return &KrciConfig{ + DnsWildcard: config.Data["dns_wildcard"], + ContainerRegistryType: config.Data["container_registry_type"], + EdpVersion: config.Data["edp_version"], + KrciConfigContainerRegistryHost: config.Data[KrciConfigContainerRegistryHost], + KrciConfigContainerRegistrySpace: config.Data[KrciConfigContainerRegistrySpace], + } +} diff --git a/pkg/platform/config_test.go b/pkg/platform/config_test.go new file mode 100644 index 00000000..55407f1d --- /dev/null +++ b/pkg/platform/config_test.go @@ -0,0 +1,97 @@ +package platform + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestGetKrciConfig(t *testing.T) { + scheme := runtime.NewScheme() + require.NoError(t, corev1.AddToScheme(scheme)) + + tests := []struct { + name string + k8sClient client.Client + wantErr require.ErrorAssertionFunc + want *KrciConfig + }{ + { + name: "should return krci config", + k8sClient: fake.NewClientBuilder(). + WithScheme(scheme). + WithObjects(&corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: KrciConfigMap, + Namespace: "default", + }, + Data: map[string]string{ + "dns_wildcard": "test-wildcard", + "container_registry_type": "test-type", + "edp_version": "test-version", + KrciConfigContainerRegistryHost: "test-registry", + KrciConfigContainerRegistrySpace: "test-space", + }, + }). + Build(), + wantErr: require.NoError, + want: &KrciConfig{ + DnsWildcard: "test-wildcard", + ContainerRegistryType: "test-type", + EdpVersion: "test-version", + KrciConfigContainerRegistryHost: "test-registry", + KrciConfigContainerRegistrySpace: "test-space", + }, + }, + { + name: "should return error if config map not found", + k8sClient: fake.NewClientBuilder(). + WithScheme(scheme). + Build(), + wantErr: require.Error, + want: nil, + }, + { + name: "should return krci config from edp-config for backward compatibility", + k8sClient: fake.NewClientBuilder(). + WithScheme(scheme). + WithObjects(&corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "edp-config", + Namespace: "default", + }, + Data: map[string]string{ + "dns_wildcard": "test-wildcard", + "container_registry_type": "test-type", + "edp_version": "test-version", + KrciConfigContainerRegistryHost: "test-registry", + KrciConfigContainerRegistrySpace: "test-space", + }, + }). + Build(), + wantErr: require.NoError, + want: &KrciConfig{ + DnsWildcard: "test-wildcard", + ContainerRegistryType: "test-type", + EdpVersion: "test-version", + KrciConfigContainerRegistryHost: "test-registry", + KrciConfigContainerRegistrySpace: "test-space", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetKrciConfig(context.Background(), tt.k8sClient, "default") + tt.wantErr(t, err) + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/pkg/platform/edp_config.go b/pkg/platform/edp_config.go deleted file mode 100644 index 327f0734..00000000 --- a/pkg/platform/edp_config.go +++ /dev/null @@ -1,33 +0,0 @@ -package platform - -import ( - "context" - "fmt" - - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -const EdpConfigMap = "edp-config" - -type EdpConfig struct { - DnsWildcard string -} - -func GetEdpConfig(ctx context.Context, k8sClient client.Client, namespace string) (*EdpConfig, error) { - edpConfig := &corev1.ConfigMap{} - if err := k8sClient.Get(ctx, client.ObjectKey{ - Namespace: namespace, - Name: EdpConfigMap, - }, edpConfig); err != nil { - return nil, fmt.Errorf("failed to get edp config: %w", err) - } - - return mapConfigToEdpConfig(edpConfig), nil -} - -func mapConfigToEdpConfig(config *corev1.ConfigMap) *EdpConfig { - return &EdpConfig{ - DnsWildcard: config.Data["dns_wildcard"], - } -} diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index b3775f75..6b986cb5 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -6,7 +6,6 @@ import ( "time" "github.com/go-resty/resty/v2" - corev1 "k8s.io/api/core/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -75,20 +74,17 @@ func (c *Collector) Start(ctx context.Context, delay, sendEvery time.Duration) { } func (c *Collector) sendTelemetry(ctx context.Context) error { - edpConfig := &corev1.ConfigMap{} - if err := c.k8sClient.Get(ctx, client.ObjectKey{ - Namespace: c.namespace, - Name: platform.EdpConfigMap, - }, edpConfig); err != nil { - return fmt.Errorf("failed to get edp config: %w", err) + config, err := platform.GetKrciConfig(ctx, c.k8sClient, c.namespace) + if err != nil { + return fmt.Errorf("failed to get config: %w", err) } telemetry := PlatformMetrics{} - telemetry.RegistryType = edpConfig.Data["container_registry_type"] - telemetry.Version = edpConfig.Data["edp_version"] + telemetry.RegistryType = config.ContainerRegistryType + telemetry.Version = config.EdpVersion codebases := &codebaseApi.CodebaseList{} - if err := c.k8sClient.List(ctx, codebases, client.InNamespace(c.namespace)); err != nil { + if err = c.k8sClient.List(ctx, codebases, client.InNamespace(c.namespace)); err != nil { return fmt.Errorf("failed to get codebases: %w", err) } @@ -104,7 +100,7 @@ func (c *Collector) sendTelemetry(ctx context.Context) error { } gitProviders := &codebaseApi.GitServerList{} - if err := c.k8sClient.List(ctx, gitProviders, client.InNamespace(c.namespace)); err != nil { + if err = c.k8sClient.List(ctx, gitProviders, client.InNamespace(c.namespace)); err != nil { return fmt.Errorf("failed to get git providers: %w", err) } @@ -113,7 +109,7 @@ func (c *Collector) sendTelemetry(ctx context.Context) error { } stages := &pipelineAPi.StageList{} - if err := c.k8sClient.List(ctx, stages, client.InNamespace(c.namespace)); err != nil { + if err = c.k8sClient.List(ctx, stages, client.InNamespace(c.namespace)); err != nil { return fmt.Errorf("failed to get stages: %w", err) } @@ -129,7 +125,7 @@ func (c *Collector) sendTelemetry(ctx context.Context) error { } cdPipelines := &pipelineAPi.CDPipelineList{} - if err := c.k8sClient.List(ctx, cdPipelines, client.InNamespace(c.namespace)); err != nil { + if err = c.k8sClient.List(ctx, cdPipelines, client.InNamespace(c.namespace)); err != nil { return fmt.Errorf("failed to get cd pipelines: %w", err) } @@ -146,7 +142,7 @@ func (c *Collector) sendTelemetry(ctx context.Context) error { } jiraServers := &codebaseApi.JiraServerList{} - if err := c.k8sClient.List(ctx, jiraServers, client.InNamespace(c.namespace)); err != nil { + if err = c.k8sClient.List(ctx, jiraServers, client.InNamespace(c.namespace)); err != nil { return fmt.Errorf("failed to get jira servers: %w", err) } diff --git a/pkg/telemetry/telemetry_test.go b/pkg/telemetry/telemetry_test.go index 51d42106..bee0ebf8 100644 --- a/pkg/telemetry/telemetry_test.go +++ b/pkg/telemetry/telemetry_test.go @@ -77,7 +77,7 @@ func TestCollector_Start(t *testing.T) { WithObjects( &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: platform.EdpConfigMap, + Name: platform.KrciConfigMap, Namespace: ns, }, Data: map[string]string{ diff --git a/pkg/util/platform.go b/pkg/util/platform.go index 72e5b4a9..d42504b6 100644 --- a/pkg/util/platform.go +++ b/pkg/util/platform.go @@ -2,7 +2,6 @@ package util import ( "context" - "errors" "fmt" "os" "strconv" @@ -21,40 +20,6 @@ const ( debugModeEnvVar = "DEBUG_MODE" ) -func GetUserSettings(c client.Client, namespace string) (*model.UserSettings, error) { - ctx := context.Background() - us := &coreV1.ConfigMap{} - - err := c.Get(ctx, types.NamespacedName{ - Namespace: namespace, - Name: "edp-config", - }, us) - if err != nil { - return nil, fmt.Errorf("failed to fetch 'edp-config' resource: %w", err) - } - - return &model.UserSettings{ - DnsWildcard: us.Data["dns_wildcard"], - }, nil -} - -func GetGerritPort(c client.Client, namespace string) (*int32, error) { - gs, err := getGitServerCR(c, "gerrit", namespace) - if err != nil { - return nil, fmt.Errorf("failed to get %v Git Server CR: %w", "gerrit", err) - } - - if gs.Spec.SshPort == 0 { - return nil, errors.New("ssh port is zero or not defined in gerrit GitServer CR") - } - - return getInt32P(gs.Spec.SshPort), nil -} - -func getInt32P(val int32) *int32 { - return &val -} - func GetVcsBasicAuthConfig(c client.Client, namespace, secretName string) (userName, password string, err error) { log.Info("Start getting secret", "name", secretName) diff --git a/pkg/util/platform_test.go b/pkg/util/platform_test.go index 02067914..578a61de 100644 --- a/pkg/util/platform_test.go +++ b/pkg/util/platform_test.go @@ -14,69 +14,6 @@ import ( codebaseApi "github.com/epam/edp-codebase-operator/v2/api/v1" ) -func TestGetGerritPort_ShouldFound(t *testing.T) { - gs := &codebaseApi.GitServer{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "gerrit", - Namespace: "stub-namespace", - }, - Spec: codebaseApi.GitServerSpec{ - SshPort: 22, - }, - } - - scheme := runtime.NewScheme() - scheme.AddKnownTypes(v1.SchemeGroupVersion, gs) - fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(gs).Build() - - port, err := GetGerritPort(fakeCl, "stub-namespace") - assert.Equal(t, *port, int32(22)) - assert.NoError(t, err) -} - -func TestGetGerritPort_ShouldFailPortNotDefined(t *testing.T) { - gs := &codebaseApi.GitServer{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "gerrit", - Namespace: "stub-namespace", - }, - } - - scheme := runtime.NewScheme() - scheme.AddKnownTypes(v1.SchemeGroupVersion, gs) - fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(gs).Build() - - port, err := GetGerritPort(fakeCl, "stub-namespace") - assert.Nil(t, port) - assert.Error(t, err) - - if !strings.Contains(err.Error(), "ssh port is zero or not defined in gerrit GitServer CR") { - t.Fatalf("wrong error returned: %s", err.Error()) - } -} - -func TestGetGerritPort_ShouldNotFound(t *testing.T) { - gs := &codebaseApi.GitServer{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "stub-gerrit", - Namespace: "stub-namespace", - }, - Spec: codebaseApi.GitServerSpec{ - SshPort: 22, - }, - } - - scheme := runtime.NewScheme() - scheme.AddKnownTypes(v1.SchemeGroupVersion, gs) - fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(gs).Build() - - port, err := GetGerritPort(fakeCl, "stub-namespace") - assert.Nil(t, port) - assert.Error(t, err) - - assert.Contains(t, err.Error(), "failed to get gerrit Git Server CR") -} - func TestGetSecret_ShouldPass(t *testing.T) { secret := &coreV1.Secret{ ObjectMeta: metaV1.ObjectMeta{ @@ -229,45 +166,6 @@ func TestGetGitServer_ShouldFailIfNotFound(t *testing.T) { assert.Contains(t, err.Error(), "failed to find GitServer non-existing in k8s") } -func TestGetUserSettings_ShouldPass(t *testing.T) { - cm := &coreV1.ConfigMap{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "edp-config", - Namespace: "stub-namespace", - }, - Data: map[string]string{ - "dns_wildcard": "dns", - }, - } - scheme := runtime.NewScheme() - scheme.AddKnownTypes(coreV1.SchemeGroupVersion, cm) - fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(cm).Build() - - model, err := GetUserSettings(fakeCl, "stub-namespace") - assert.Equal(t, model.DnsWildcard, "dns") - assert.NoError(t, err) -} - -func TestGetUserSettings_ShouldFailOnFindConfigmap(t *testing.T) { - cm := &coreV1.ConfigMap{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "edp-config", - Namespace: "stub-namespace", - }, - } - scheme := runtime.NewScheme() - scheme.AddKnownTypes(coreV1.SchemeGroupVersion, cm) - fakeCl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(cm).Build() - - model, err := GetUserSettings(fakeCl, "another-namespace") - assert.Error(t, err) - assert.Nil(t, model) - - if !strings.Contains(err.Error(), "configmaps \"edp-config\" not found") { - t.Fatalf("wrong error returned: %s", err.Error()) - } -} - func TestGetWatchNamespace_IsDefined(t *testing.T) { t.Setenv("WATCH_NAMESPACE", "namespace")