diff --git a/config/rbac/cluster_role.yaml b/config/rbac/cluster_role.yaml index 690c31e65..f45b442a8 100644 --- a/config/rbac/cluster_role.yaml +++ b/config/rbac/cluster_role.yaml @@ -355,6 +355,14 @@ rules: - kubernetesimagepullers verbs: - '*' + - apiGroups: + - config.openshift.io + resources: + - consoles + resourcesNames: + - cluster + verbs: + - get ### CHE-OPERATOR ROLES ONLY: END # devworkspace-controller-view-workspaces.ClusterRole.yaml - apiGroups: diff --git a/pkg/deploy/dashboard/dashboard.go b/pkg/deploy/dashboard/dashboard.go index c4d5b89df..9bcecf510 100644 --- a/pkg/deploy/dashboard/dashboard.go +++ b/pkg/deploy/dashboard/dashboard.go @@ -9,11 +9,14 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package dashboard import ( "fmt" + ctrl "sigs.k8s.io/controller-runtime" + "github.com/eclipse-che/che-operator/pkg/deploy" "github.com/eclipse-che/che-operator/pkg/deploy/expose" "github.com/eclipse-che/che-operator/pkg/deploy/gateway" @@ -26,6 +29,10 @@ const ( exposePath = "/dashboard/" ) +var ( + log = ctrl.Log.WithName("dashboard") +) + type Dashboard struct { deployContext *deploy.DeployContext component string diff --git a/pkg/deploy/dashboard/dashboard_deployment_test.go b/pkg/deploy/dashboard/dashboard_deployment_test.go index 23f4b2885..a55b7edfd 100644 --- a/pkg/deploy/dashboard/dashboard_deployment_test.go +++ b/pkg/deploy/dashboard/dashboard_deployment_test.go @@ -9,11 +9,14 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package dashboard import ( "os" + configv1 "github.com/openshift/api/config/v1" + "github.com/google/go-cmp/cmp" corev1 "k8s.io/api/core/v1" @@ -49,11 +52,13 @@ func TestDashboardDeploymentSecurityContext(t *testing.T) { }, }, ClusterAPI: deploy.ClusterAPI{ - Client: cli, - Scheme: scheme.Scheme, + Client: cli, + NonCachedClient: cli, + Scheme: scheme.Scheme, }, Proxy: &deploy.Proxy{}, } + deployContext.ClusterAPI.Scheme.AddKnownTypes(configv1.SchemeGroupVersion, &configv1.Console{}) dashboard := NewDashboard(deployContext) deployment, err := dashboard.getDashboardDeploymentSpec() @@ -122,11 +127,13 @@ func TestDashboardDeploymentResources(t *testing.T) { deployContext := &deploy.DeployContext{ CheCluster: testCase.cheCluster, ClusterAPI: deploy.ClusterAPI{ - Client: cli, - Scheme: scheme.Scheme, + Client: cli, + NonCachedClient: cli, + Scheme: scheme.Scheme, }, Proxy: &deploy.Proxy{}, } + deployContext.ClusterAPI.Scheme.AddKnownTypes(configv1.SchemeGroupVersion, &configv1.Console{}) dashboard := NewDashboard(deployContext) deployment, err := dashboard.getDashboardDeploymentSpec() @@ -171,6 +178,9 @@ func TestDashboardDeploymentEnvVars(t *testing.T) { Name: "CHE_INTERNAL_URL", Value: "http://che-host.eclipse-che.svc:8080/api", }, + { + Name: "OPENSHIFT_CONSOLE_URL", + }, }, cheCluster: &orgv1.CheCluster{ ObjectMeta: metav1.ObjectMeta{ @@ -195,6 +205,9 @@ func TestDashboardDeploymentEnvVars(t *testing.T) { Name: "CHE_URL", Value: "http://che.com", }, + { + Name: "OPENSHIFT_CONSOLE_URL", + }, // the following are not provisioned: CHE_INTERNAL_URL }, cheCluster: &orgv1.CheCluster{ @@ -209,6 +222,48 @@ func TestDashboardDeploymentEnvVars(t *testing.T) { }, }, }, + { + name: "Test provisioning OpenShift Console URL", + initObjects: []runtime.Object{ + &configv1.Console{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cluster", + Namespace: "openshift-console", + }, + Status: configv1.ConsoleStatus{ + ConsoleURL: "https://console-openshift-console.apps.my-host/", + }, + }, + }, + envVars: []corev1.EnvVar{ + { + Name: "CHE_HOST", + Value: "http://che.com", + }, + { + Name: "CHE_URL", + Value: "http://che.com", + }, + { + Name: "CHE_INTERNAL_URL", + Value: "http://che-host.eclipse-che.svc:8080/api", + }, + { + Name: "OPENSHIFT_CONSOLE_URL", + Value: "https://console-openshift-console.apps.my-host/", + }, + }, + cheCluster: &orgv1.CheCluster{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "eclipse-che", + }, + Spec: orgv1.CheClusterSpec{ + Server: orgv1.CheClusterSpecServer{ + CheHost: "che.com", + }, + }, + }, + }, } for _, testCase := range testCases { @@ -221,11 +276,13 @@ func TestDashboardDeploymentEnvVars(t *testing.T) { deployContext := &deploy.DeployContext{ CheCluster: testCase.cheCluster, ClusterAPI: deploy.ClusterAPI{ - Client: cli, - Scheme: scheme.Scheme, + Client: cli, + NonCachedClient: cli, + Scheme: scheme.Scheme, }, Proxy: &deploy.Proxy{}, } + deployContext.ClusterAPI.Scheme.AddKnownTypes(configv1.SchemeGroupVersion, &configv1.Console{}) dashboard := NewDashboard(deployContext) deployment, err := dashboard.getDashboardDeploymentSpec() @@ -337,8 +394,9 @@ func TestDashboardDeploymentVolumes(t *testing.T) { deployContext := &deploy.DeployContext{ CheCluster: testCase.cheCluster, ClusterAPI: deploy.ClusterAPI{ - Client: cli, - Scheme: scheme.Scheme, + Client: cli, + NonCachedClient: cli, + Scheme: scheme.Scheme, }, Proxy: &deploy.Proxy{}, } diff --git a/pkg/deploy/dashboard/dashboard_test.go b/pkg/deploy/dashboard/dashboard_test.go index de27919d1..23a005ea0 100644 --- a/pkg/deploy/dashboard/dashboard_test.go +++ b/pkg/deploy/dashboard/dashboard_test.go @@ -9,6 +9,7 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package dashboard import ( diff --git a/pkg/deploy/dashboard/deployment_dashboard.go b/pkg/deploy/dashboard/deployment_dashboard.go index e63e13bbe..442efe8bd 100644 --- a/pkg/deploy/dashboard/deployment_dashboard.go +++ b/pkg/deploy/dashboard/deployment_dashboard.go @@ -9,12 +9,17 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package dashboard import ( + "context" "fmt" "strconv" + configv1 "github.com/openshift/api/config/v1" + "k8s.io/apimachinery/pkg/types" + "github.com/eclipse-che/che-operator/pkg/deploy" "github.com/eclipse-che/che-operator/pkg/util" appsv1 "k8s.io/api/apps/v1" @@ -60,6 +65,13 @@ func (d *Dashboard) getDashboardDeploymentSpec() (*appsv1.Deployment, error) { ) } + if util.IsOpenShift { + envVars = append(envVars, + corev1.EnvVar{ + Name: "OPENSHIFT_CONSOLE_URL", + Value: d.evaluateOpenShiftConsoleURL()}) + } + terminationGracePeriodSeconds := int64(30) labels, labelsSelector := deploy.GetLabelsAndSelector(d.deployContext.CheCluster, d.component) @@ -186,6 +198,22 @@ func (d *Dashboard) getDashboardDeploymentSpec() (*appsv1.Deployment, error) { return deployment, nil } +func (d *Dashboard) evaluateOpenShiftConsoleURL() string { + console := &configv1.Console{} + + err := d.deployContext.ClusterAPI.NonCachedClient.Get(context.TODO(), types.NamespacedName{ + Name: "cluster", + Namespace: "openshift-console", + }, console) + + if err != nil { + // if error happen don't fail deployment but try again on the next reconcile loop + log.Error(err, "failed to get OpenShift Console Custom Resource to evaluate URL") + return "" + } + return console.Status.ConsoleURL +} + func (d *Dashboard) provisionCheSelfSignedCA(volumes []corev1.Volume, volumeMounts []corev1.VolumeMount) ([]corev1.Volume, []corev1.VolumeMount) { cheSelfSigned := corev1.Volume{ Name: "che-self-signed-ca", diff --git a/pkg/deploy/dashboard/init_test.go b/pkg/deploy/dashboard/init_test.go index 80ee31e8b..659a0b369 100644 --- a/pkg/deploy/dashboard/init_test.go +++ b/pkg/deploy/dashboard/init_test.go @@ -9,6 +9,7 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package dashboard import "github.com/eclipse-che/che-operator/pkg/deploy" diff --git a/pkg/deploy/dashboard/rbac.go b/pkg/deploy/dashboard/rbac.go index c14123c11..8c4437d20 100644 --- a/pkg/deploy/dashboard/rbac.go +++ b/pkg/deploy/dashboard/rbac.go @@ -9,6 +9,7 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package dashboard import (