From 082dde93edd71b4d1f117b6d5d119f2fe84dd4c6 Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Wed, 7 Aug 2024 11:40:45 +0200 Subject: [PATCH] chore: Clean up devfile registry deployment Signed-off-by: Anatolii Bazko --- api/v2/checluster_types.go | 2 +- .../che-operator.clusterserviceversion.yaml | 8 +- .../org.eclipse.che_checlusters.yaml | 2 +- .../bases/org.eclipse.che_checlusters.yaml | 2 +- config/manager/manager.yaml | 2 + .../che-operator.clusterserviceversion.yaml | 2 +- deploy/deployment/kubernetes/combined.yaml | 4 +- .../objects/che-operator.Deployment.yaml | 2 + ....eclipse.che.CustomResourceDefinition.yaml | 2 +- deploy/deployment/openshift/combined.yaml | 4 +- .../objects/che-operator.Deployment.yaml | 2 + ....eclipse.che.CustomResourceDefinition.yaml | 2 +- ....eclipse.che.CustomResourceDefinition.yaml | 2 +- .../templates/che-operator.Deployment.yaml | 2 + pkg/common/operator-defaults/defaults.go | 10 + .../dashboard/dashboard_deployment_test.go | 8 + pkg/deploy/devfileregistry/devfileregistry.go | 41 +++++ .../devfileregistry/devfileregistry_test.go | 174 ++++++++++++++++++ pkg/deploy/devfileregistry/init_test.go | 26 +++ 19 files changed, 285 insertions(+), 12 deletions(-) create mode 100644 pkg/deploy/devfileregistry/devfileregistry_test.go create mode 100644 pkg/deploy/devfileregistry/init_test.go diff --git a/api/v2/checluster_types.go b/api/v2/checluster_types.go index 28e6d0f1b3..1e7aed5019 100644 --- a/api/v2/checluster_types.go +++ b/api/v2/checluster_types.go @@ -857,7 +857,7 @@ type CheClusterStatus struct { // +operator-sdk:csv:customresourcedefinitions:type=status,displayName="ChePhase" // +operator-sdk:csv:customresourcedefinitions:type=status,xDescriptors="urn:alm:descriptor:text" ChePhase CheClusterPhase `json:"chePhase,omitempty"` - // The public URL of the internal devfile registry. + // Deprecated the public URL of the internal devfile registry. // +optional // +operator-sdk:csv:customresourcedefinitions:type=status // +operator-sdk:csv:customresourcedefinitions:type=status,displayName="Devfile registry URL" diff --git a/bundle/next/eclipse-che/manifests/che-operator.clusterserviceversion.yaml b/bundle/next/eclipse-che/manifests/che-operator.clusterserviceversion.yaml index d90362a59a..51daf6d72a 100644 --- a/bundle/next/eclipse-che/manifests/che-operator.clusterserviceversion.yaml +++ b/bundle/next/eclipse-che/manifests/che-operator.clusterserviceversion.yaml @@ -104,7 +104,7 @@ metadata: operators.operatorframework.io/project_layout: go.kubebuilder.io/v3 repository: https://github.com/eclipse-che/che-operator support: Eclipse Foundation - name: eclipse-che.v7.90.0-877.next + name: eclipse-che.v7.90.0-878.next namespace: placeholder spec: apiservicedefinitions: {} @@ -225,7 +225,7 @@ spec: path: cheVersion x-descriptors: - urn:alm:descriptor:text - - description: The public URL of the internal devfile registry. + - description: Deprecated the public URL of the internal devfile registry. displayName: Devfile registry URL path: devfileRegistryURL x-descriptors: @@ -916,6 +916,8 @@ spec: {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: "false" - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT @@ -1033,7 +1035,7 @@ spec: minKubeVersion: 1.19.0 provider: name: Eclipse Foundation - version: 7.90.0-877.next + version: 7.90.0-878.next webhookdefinitions: - admissionReviewVersions: - v1 diff --git a/bundle/next/eclipse-che/manifests/org.eclipse.che_checlusters.yaml b/bundle/next/eclipse-che/manifests/org.eclipse.che_checlusters.yaml index 47a3eceb2b..bc2fe8dcea 100644 --- a/bundle/next/eclipse-che/manifests/org.eclipse.che_checlusters.yaml +++ b/bundle/next/eclipse-che/manifests/org.eclipse.che_checlusters.yaml @@ -8338,7 +8338,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. diff --git a/config/crd/bases/org.eclipse.che_checlusters.yaml b/config/crd/bases/org.eclipse.che_checlusters.yaml index 2b7fdd8f7f..e9583ee45b 100644 --- a/config/crd/bases/org.eclipse.che_checlusters.yaml +++ b/config/crd/bases/org.eclipse.che_checlusters.yaml @@ -8285,7 +8285,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 839a17a05c..2dff0b24eb 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -103,6 +103,8 @@ spec: value: '[{"name": "universal-developer-image", "container": {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: 'false' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT diff --git a/config/manifests/bases/che-operator.clusterserviceversion.yaml b/config/manifests/bases/che-operator.clusterserviceversion.yaml index 98446df118..91253b498c 100644 --- a/config/manifests/bases/che-operator.clusterserviceversion.yaml +++ b/config/manifests/bases/che-operator.clusterserviceversion.yaml @@ -154,7 +154,7 @@ spec: path: cheVersion x-descriptors: - urn:alm:descriptor:text - - description: The public URL of the internal devfile registry. + - description: Deprecated the public URL of the internal devfile registry. displayName: Devfile registry URL path: devfileRegistryURL x-descriptors: diff --git a/deploy/deployment/kubernetes/combined.yaml b/deploy/deployment/kubernetes/combined.yaml index de3198d755..bb7391f513 100644 --- a/deploy/deployment/kubernetes/combined.yaml +++ b/deploy/deployment/kubernetes/combined.yaml @@ -8306,7 +8306,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. @@ -8867,6 +8867,8 @@ spec: value: '[{"name": "universal-developer-image", "container": {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: "false" - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT diff --git a/deploy/deployment/kubernetes/objects/che-operator.Deployment.yaml b/deploy/deployment/kubernetes/objects/che-operator.Deployment.yaml index 3b2af5a0c4..679ae746c5 100644 --- a/deploy/deployment/kubernetes/objects/che-operator.Deployment.yaml +++ b/deploy/deployment/kubernetes/objects/che-operator.Deployment.yaml @@ -97,6 +97,8 @@ spec: value: '[{"name": "universal-developer-image", "container": {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: "false" - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT diff --git a/deploy/deployment/kubernetes/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml b/deploy/deployment/kubernetes/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml index 0c25e36e01..1869f7d78b 100644 --- a/deploy/deployment/kubernetes/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml +++ b/deploy/deployment/kubernetes/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml @@ -8301,7 +8301,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. diff --git a/deploy/deployment/openshift/combined.yaml b/deploy/deployment/openshift/combined.yaml index 57b476bb8c..38133fb1b3 100644 --- a/deploy/deployment/openshift/combined.yaml +++ b/deploy/deployment/openshift/combined.yaml @@ -8306,7 +8306,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. @@ -8869,6 +8869,8 @@ spec: value: '[{"name": "universal-developer-image", "container": {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: "false" - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT diff --git a/deploy/deployment/openshift/objects/che-operator.Deployment.yaml b/deploy/deployment/openshift/objects/che-operator.Deployment.yaml index 0327fef14a..58ded21bd4 100644 --- a/deploy/deployment/openshift/objects/che-operator.Deployment.yaml +++ b/deploy/deployment/openshift/objects/che-operator.Deployment.yaml @@ -97,6 +97,8 @@ spec: value: '[{"name": "universal-developer-image", "container": {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: "false" - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT diff --git a/deploy/deployment/openshift/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml b/deploy/deployment/openshift/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml index 8b1553ddda..ddbdd292c5 100644 --- a/deploy/deployment/openshift/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml +++ b/deploy/deployment/openshift/objects/checlusters.org.eclipse.che.CustomResourceDefinition.yaml @@ -8301,7 +8301,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. diff --git a/helmcharts/next/crds/checlusters.org.eclipse.che.CustomResourceDefinition.yaml b/helmcharts/next/crds/checlusters.org.eclipse.che.CustomResourceDefinition.yaml index 0c25e36e01..1869f7d78b 100644 --- a/helmcharts/next/crds/checlusters.org.eclipse.che.CustomResourceDefinition.yaml +++ b/helmcharts/next/crds/checlusters.org.eclipse.che.CustomResourceDefinition.yaml @@ -8301,7 +8301,7 @@ spec: description: Currently installed Che version. type: string devfileRegistryURL: - description: The public URL of the internal devfile registry. + description: Deprecated the public URL of the internal devfile registry. type: string gatewayPhase: description: Specifies the current phase of the gateway deployment. diff --git a/helmcharts/next/templates/che-operator.Deployment.yaml b/helmcharts/next/templates/che-operator.Deployment.yaml index 3b2af5a0c4..679ae746c5 100644 --- a/helmcharts/next/templates/che-operator.Deployment.yaml +++ b/helmcharts/next/templates/che-operator.Deployment.yaml @@ -97,6 +97,8 @@ spec: value: '[{"name": "universal-developer-image", "container": {"image": "quay.io/devfile/universal-developer-image:ubi8-latest"}}]' - name: CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL value: https://open-vsx.org + - name: CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES + value: '[{"url": "https://registry.devfile.io"}]' - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DISABLECONTAINERBUILDCAPABILITIES value: "false" - name: CHE_DEFAULT_SPEC_DEVENVIRONMENTS_CONTAINERSECURITYCONTEXT diff --git a/pkg/common/operator-defaults/defaults.go b/pkg/common/operator-defaults/defaults.go index e8c29265c2..b5726b6494 100644 --- a/pkg/common/operator-defaults/defaults.go +++ b/pkg/common/operator-defaults/defaults.go @@ -47,6 +47,7 @@ var ( defaultDevEnvironmentsContainerSecurityContext string defaultPluginRegistryOpenVSXURL string defaultDashboardHeaderMessageText string + defaultDevfileRegistryExternalDevfileRegistries string initialized = false ) @@ -82,6 +83,7 @@ func Initialize() { defaultDevEnvironmentsDefaultEditor = os.Getenv("CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DEFAULTEDITOR") defaultPluginRegistryOpenVSXURL = os.Getenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL") defaultDashboardHeaderMessageText = os.Getenv("CHE_DEFAULT_SPEC_COMPONENTS_DASHBOARD_HEADERMESSAGE_TEXT") + defaultDevfileRegistryExternalDevfileRegistries = os.Getenv("CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES") defaultCheServerImage = ensureEnv(util.GetArchitectureDependentEnvName("RELATED_IMAGE_che_server")) defaultDashboardImage = ensureEnv(util.GetArchitectureDependentEnvName("RELATED_IMAGE_dashboard")) @@ -222,6 +224,14 @@ func GetConsoleLinkImage() string { return defaultsConsoleLinkImage } +func GetDevfileRegistryExternalDevfileRegistries() string { + if !initialized { + logrus.Fatalf("Operator defaults are not initialized.") + } + + return defaultDevfileRegistryExternalDevfileRegistries +} + func GetPluginRegistryOpenVSXURL() string { if !initialized { logrus.Fatalf("Operator defaults are not initialized.") diff --git a/pkg/deploy/dashboard/dashboard_deployment_test.go b/pkg/deploy/dashboard/dashboard_deployment_test.go index 802750e437..27b32ff67a 100644 --- a/pkg/deploy/dashboard/dashboard_deployment_test.go +++ b/pkg/deploy/dashboard/dashboard_deployment_test.go @@ -197,6 +197,10 @@ func TestDashboardDeploymentEnvVars(t *testing.T) { Name: "CHE_DEFAULT_SPEC_DEVENVIRONMENTS_DEFAULTCOMPONENTS", Value: defaults.GetDevEnvironmentsDefaultComponents(), }, + { + Name: "CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES", + Value: defaults.GetDevfileRegistryExternalDevfileRegistries(), + }, { Name: "CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL", Value: defaults.GetPluginRegistryOpenVSXURL(), @@ -262,6 +266,10 @@ func TestDashboardDeploymentEnvVars(t *testing.T) { Name: "CHE_WORKSPACE_PLUGIN__REGISTRY__INTERNAL__URL", Value: "http://plugin-registry.eclipse-che.svc:8080/v3", }, + { + Name: "CHE_DEFAULT_SPEC_COMPONENTS_DEVFILEREGISTRY_EXTERNAL_DEVFILE_REGISTRIES", + Value: defaults.GetDevfileRegistryExternalDevfileRegistries(), + }, { Name: "OPENSHIFT_CONSOLE_URL", Value: "https://console-openshift-console.apps.my-host/", diff --git a/pkg/deploy/devfileregistry/devfileregistry.go b/pkg/deploy/devfileregistry/devfileregistry.go index 108afc2ff9..a5c6a99aca 100644 --- a/pkg/deploy/devfileregistry/devfileregistry.go +++ b/pkg/deploy/devfileregistry/devfileregistry.go @@ -13,8 +13,14 @@ package devfileregistry import ( + "context" + "encoding/json" + + v2 "github.com/eclipse-che/che-operator/api/v2" + defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + ctrl "sigs.k8s.io/controller-runtime" "github.com/eclipse-che/che-operator/pkg/common/chetypes" "github.com/eclipse-che/che-operator/pkg/common/constants" @@ -23,6 +29,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" ) +var ( + logger = ctrl.Log.WithName("devfile-registry") +) + type DevfileRegistryReconciler struct { deploy.Reconcilable } @@ -38,6 +48,37 @@ func (d *DevfileRegistryReconciler) Reconcile(ctx *chetypes.DeployContext) (reco _, _ = deploy.DeleteNamespacedObject(ctx, constants.DevfileRegistryName, &appsv1.Deployment{}) if ctx.CheCluster.Status.DevfileRegistryURL != "" { + var externalDevfileRegistries []v2.ExternalDevfileRegistry + if err := json.Unmarshal([]byte(defaults.GetDevfileRegistryExternalDevfileRegistries()), &externalDevfileRegistries); err == nil { + + // Add default external devfile registries to the CheCluster CR + for _, newRegistry := range externalDevfileRegistries { + newRegistryAlreadyExists := false + for _, existedRegistry := range ctx.CheCluster.Spec.Components.DevfileRegistry.ExternalDevfileRegistries { + if existedRegistry.Url == newRegistry.Url { + newRegistryAlreadyExists = true + break + } + } + + if !newRegistryAlreadyExists { + logger.Info("Adding external devfile registry to the CheCluster CR", "Url", newRegistry.Url) + ctx.CheCluster.Spec.Components.DevfileRegistry.ExternalDevfileRegistries = + append(ctx.CheCluster.Spec.Components.DevfileRegistry.ExternalDevfileRegistries, newRegistry) + } + } + + if err := ctx.ClusterAPI.Client.Update(context.TODO(), ctx.CheCluster); err != nil { + return reconcile.Result{}, false, err + } + } else { + logger.Error( + err, "Failed to unmarshal environment variable", + "key", "CHE_OPERATOR_DEFAULTS_DEVFILE_REGISTRY_EXTERNAL_DEVFILE_REGISTRIES", + "value", defaults.GetDevfileRegistryExternalDevfileRegistries(), + ) + } + ctx.CheCluster.Status.DevfileRegistryURL = "" _ = deploy.UpdateCheCRStatus(ctx, "DevfileRegistryURL", "") } diff --git a/pkg/deploy/devfileregistry/devfileregistry_test.go b/pkg/deploy/devfileregistry/devfileregistry_test.go new file mode 100644 index 0000000000..21c3a53e30 --- /dev/null +++ b/pkg/deploy/devfileregistry/devfileregistry_test.go @@ -0,0 +1,174 @@ +// +// Copyright (c) 2019-2024 Red Hat, Inc. +// This program and the accompanying materials are made +// available under the terms of the Eclipse Public License 2.0 +// which is available at https://www.eclipse.org/legal/epl-2.0/ +// +// SPDX-License-Identifier: EPL-2.0 +// +// Contributors: +// Red Hat, Inc. - initial API and implementation +// + +package devfileregistry + +import ( + "testing" + + "github.com/devfile/devworkspace-operator/pkg/infrastructure" + chev2 "github.com/eclipse-che/che-operator/api/v2" + "github.com/eclipse-che/che-operator/pkg/common/test" + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +func TestDevfileRegistryReconciler(t *testing.T) { + type testCase struct { + name string + cheCluster *chev2.CheCluster + expectedDisableInternalRegistry bool + expectedExternalDevfileRegistries []chev2.ExternalDevfileRegistry + expectedDevfileRegistryURL string + } + + testCases := []testCase{ + { + name: "DisableInternalRegistry=false #1", + cheCluster: &chev2.CheCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "eclipse-che", + Namespace: "eclipse-che", + }, + Spec: chev2.CheClusterSpec{ + Components: chev2.CheClusterComponents{ + DevfileRegistry: chev2.DevfileRegistry{ + DisableInternalRegistry: false, + }, + }, + }, + Status: chev2.CheClusterStatus{ + DevfileRegistryURL: "http://devfile-registry:8080", + }, + }, + expectedDisableInternalRegistry: false, + expectedExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{{Url: "https://registry.devfile.io"}}, + expectedDevfileRegistryURL: "", + }, + { + name: "DisableInternalRegistry=false #2", + cheCluster: &chev2.CheCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "eclipse-che", + Namespace: "eclipse-che", + }, + Spec: chev2.CheClusterSpec{ + Components: chev2.CheClusterComponents{ + DevfileRegistry: chev2.DevfileRegistry{ + DisableInternalRegistry: false, + ExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{ + {Url: "my-external-registry"}, + }, + }, + }, + }, + Status: chev2.CheClusterStatus{ + DevfileRegistryURL: "http://devfile-registry:8080", + }, + }, + expectedDisableInternalRegistry: false, + expectedExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{ + {Url: "my-external-registry"}, + {Url: "https://registry.devfile.io"}, + }, + expectedDevfileRegistryURL: "", + }, + { + name: "DisableInternalRegistry=false #3", + cheCluster: &chev2.CheCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "eclipse-che", + Namespace: "eclipse-che", + }, + Spec: chev2.CheClusterSpec{ + Components: chev2.CheClusterComponents{ + DevfileRegistry: chev2.DevfileRegistry{ + DisableInternalRegistry: false, + ExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{ + {Url: "https://registry.devfile.io"}, + }, + }, + }, + }, + Status: chev2.CheClusterStatus{ + DevfileRegistryURL: "http://devfile-registry:8080", + }, + }, + expectedDisableInternalRegistry: false, + expectedExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{ + {Url: "https://registry.devfile.io"}, + }, + expectedDevfileRegistryURL: "", + }, + { + name: "DisableInternalRegistry=true #1", + cheCluster: &chev2.CheCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "eclipse-che", + Namespace: "eclipse-che", + }, + Spec: chev2.CheClusterSpec{ + Components: chev2.CheClusterComponents{ + DevfileRegistry: chev2.DevfileRegistry{ + DisableInternalRegistry: true, + }, + }, + }, + Status: chev2.CheClusterStatus{}, + }, + expectedDisableInternalRegistry: true, + expectedExternalDevfileRegistries: nil, + expectedDevfileRegistryURL: "", + }, + { + name: "DisableInternalRegistry=true #2", + cheCluster: &chev2.CheCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "eclipse-che", + Namespace: "eclipse-che", + }, + Spec: chev2.CheClusterSpec{ + Components: chev2.CheClusterComponents{ + DevfileRegistry: chev2.DevfileRegistry{ + DisableInternalRegistry: true, + ExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{{Url: "my-external-registry"}}, + }, + }, + }, + Status: chev2.CheClusterStatus{}, + }, + expectedDisableInternalRegistry: true, + expectedExternalDevfileRegistries: []chev2.ExternalDevfileRegistry{{Url: "my-external-registry"}}, + expectedDevfileRegistryURL: "", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + infrastructure.InitializeForTesting(infrastructure.OpenShiftv4) + ctx := test.GetDeployContext(testCase.cheCluster, []runtime.Object{}) + + devfileRegistryReconciler := NewDevfileRegistryReconciler() + for i := 0; i < 2; i++ { + _, done, err := devfileRegistryReconciler.Reconcile(ctx) + assert.True(t, done) + assert.Nil(t, err) + } + + assert.Equal(t, testCase.expectedDevfileRegistryURL, ctx.CheCluster.Status.DevfileRegistryURL) + assert.Equal(t, testCase.expectedDisableInternalRegistry, ctx.CheCluster.Spec.Components.DevfileRegistry.DisableInternalRegistry) + assert.Equal(t, len(testCase.expectedExternalDevfileRegistries), len(ctx.CheCluster.Spec.Components.DevfileRegistry.ExternalDevfileRegistries)) + assert.Equal(t, testCase.expectedExternalDevfileRegistries, ctx.CheCluster.Spec.Components.DevfileRegistry.ExternalDevfileRegistries) + }) + } +} diff --git a/pkg/deploy/devfileregistry/init_test.go b/pkg/deploy/devfileregistry/init_test.go new file mode 100644 index 0000000000..198a4ad18b --- /dev/null +++ b/pkg/deploy/devfileregistry/init_test.go @@ -0,0 +1,26 @@ +// +// Copyright (c) 2019-2023 Red Hat, Inc. +// This program and the accompanying materials are made +// available under the terms of the Eclipse Public License 2.0 +// which is available at https://www.eclipse.org/legal/epl-2.0/ +// +// SPDX-License-Identifier: EPL-2.0 +// +// Contributors: +// Red Hat, Inc. - initial API and implementation +// + +package devfileregistry + +import ( + "github.com/devfile/devworkspace-operator/pkg/infrastructure" + defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults" + "github.com/eclipse-che/che-operator/pkg/common/test" +) + +func init() { + test.EnableTestMode() + + infrastructure.InitializeForTesting(infrastructure.OpenShiftv4) + defaults.InitializeForTesting("../../../config/manager/manager.yaml") +}