From daa9096c799e828eb5a07ee4490ec9e291962b66 Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Mon, 1 Nov 2021 03:58:10 +0200 Subject: [PATCH 1/4] Auto enable dev workspace engine after OLM channel migration. Signed-off-by: Oleksandr Andriienko --- controllers/che/checluster_controller.go | 8 ++++ .../checlusterbackup/backup_data_collector.go | 2 +- pkg/deploy/defaults.go | 2 + pkg/deploy/dev-workspace/dev_workspace.go | 5 ++- pkg/util/namespace_provider.go | 40 +++++++++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 pkg/util/namespace_provider.go diff --git a/controllers/che/checluster_controller.go b/controllers/che/checluster_controller.go index 48e75cbf5b..fe3c7bd92a 100644 --- a/controllers/che/checluster_controller.go +++ b/controllers/che/checluster_controller.go @@ -334,6 +334,14 @@ func (r *CheClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) } } + r.Log.Info("======Operator namespace", "is:", util.GetCheOperatorNamespace()) + if util.IsOpenShift && !checluster.Spec.DevWorkspace.Enable && util.GetCheOperatorNamespace() == deploy.DefaultNamespaceForAllNamespacesMode && devworkspace.IsDevWorkspaceOperatorCSVExists(deployContext) { + checluster.Spec.DevWorkspace.Enable = true + if err := deploy.UpdateCheCRSpec(deployContext, "enable", strconv.FormatBool(true)); err != nil { + return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 1}, err + } + } + if util.IsOpenShift && checluster.Spec.DevWorkspace.Enable && checluster.Spec.Auth.NativeUserMode == nil { newNativeUserModeValue := util.NewBoolPointer(true) checluster.Spec.Auth.NativeUserMode = newNativeUserModeValue diff --git a/controllers/checlusterbackup/backup_data_collector.go b/controllers/checlusterbackup/backup_data_collector.go index 8df9277d06..a3ff4e4e63 100644 --- a/controllers/checlusterbackup/backup_data_collector.go +++ b/controllers/checlusterbackup/backup_data_collector.go @@ -177,7 +177,7 @@ func backupDatabases(bctx *BackupContext, destDir string) (bool, error) { return false, err } - // Get and seve all dumps from the Postgres container + // Get and save all dumps from the Postgres container for _, dbName := range databasesToBackup { execReason := fmt.Sprintf("getting database %s dump", dbName) dbDump, err := k8sClient.DoExecIntoPod(bctx.namespace, postgresPodName, getMoveDatabaseDumpScript(dbName), execReason) diff --git a/pkg/deploy/defaults.go b/pkg/deploy/defaults.go index 9acbe44332..8cf0ec5acd 100644 --- a/pkg/deploy/defaults.go +++ b/pkg/deploy/defaults.go @@ -125,6 +125,8 @@ const ( // CheServiceAccountName - service account name for che-server. CheServiceAccountName = "che" + DefaultNamespaceForAllNamespacesMode = "openshift-operators" + // limits DefaultDashboardMemoryLimit = "256Mi" DefaultDashboardMemoryRequest = "32Mi" diff --git a/pkg/deploy/dev-workspace/dev_workspace.go b/pkg/deploy/dev-workspace/dev_workspace.go index 160aafa636..f1050736e4 100644 --- a/pkg/deploy/dev-workspace/dev_workspace.go +++ b/pkg/deploy/dev-workspace/dev_workspace.go @@ -66,7 +66,7 @@ func ReconcileDevWorkspace(deployContext *deploy.DeployContext) (done bool, err return true, nil } - if isDevWorkspaceOperatorCSVExists(deployContext) { + if IsDevWorkspaceOperatorCSVExists(deployContext) { // Do nothing if DevWorkspace has been already deployed via OLM return true, nil } @@ -165,13 +165,14 @@ func isDevWorkspaceDeploymentExists(deployContext *deploy.DeployContext) (bool, }, &appsv1.Deployment{}) } -func isDevWorkspaceOperatorCSVExists(deployContext *deploy.DeployContext) bool { +func IsDevWorkspaceOperatorCSVExists(deployContext *deploy.DeployContext) bool { // If clusterserviceversions resource doesn't exist in cluster DWO as well will not be present if !util.HasK8SResourceObject(deployContext.ClusterAPI.DiscoveryClient, ClusterServiceVersionResourceName) { return false } csvList := &operatorsv1alpha1.ClusterServiceVersionList{} + // Namespace: deploy.DefaultNamespaceForAllNamespacesMode err := deployContext.ClusterAPI.Client.List(context.TODO(), csvList, &client.ListOptions{}) if err != nil { return false diff --git a/pkg/util/namespace_provider.go b/pkg/util/namespace_provider.go new file mode 100644 index 0000000000..ebb405914c --- /dev/null +++ b/pkg/util/namespace_provider.go @@ -0,0 +1,40 @@ +// +// Copyright (c) 2012-2021 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 util + +import ( + "io/ioutil" + + "github.com/sirupsen/logrus" +) + +const ( + namespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" +) + +var operator_namespace string + +func readNamespace() string { + nsBytes, err := ioutil.ReadFile(namespaceFile) + if err != nil { + logrus.Fatal("Failed to get operator namespace", err) + } + return string(nsBytes) +} + +// GetCheOperatorNamespace returns namespace for current Eclipse Che operator. +func GetCheOperatorNamespace() string { + if operator_namespace == "" && !IsTestMode() { + operator_namespace = readNamespace() + } + return operator_namespace +} \ No newline at end of file From e0193def0bbc11b35d5a0a81653bc5284a16a283 Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Mon, 1 Nov 2021 06:06:54 +0200 Subject: [PATCH 2/4] Fix sigterm handler for all namespaces mode. Signed-off-by: Oleksandr Andriienko --- controllers/che/checluster_controller.go | 1 - main.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/controllers/che/checluster_controller.go b/controllers/che/checluster_controller.go index fe3c7bd92a..f7fd7111d6 100644 --- a/controllers/che/checluster_controller.go +++ b/controllers/che/checluster_controller.go @@ -334,7 +334,6 @@ func (r *CheClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) } } - r.Log.Info("======Operator namespace", "is:", util.GetCheOperatorNamespace()) if util.IsOpenShift && !checluster.Spec.DevWorkspace.Enable && util.GetCheOperatorNamespace() == deploy.DefaultNamespaceForAllNamespacesMode && devworkspace.IsDevWorkspaceOperatorCSVExists(deployContext) { checluster.Spec.DevWorkspace.Enable = true if err := deploy.UpdateCheCRSpec(deployContext, "enable", strconv.FormatBool(true)); err != nil { diff --git a/main.go b/main.go index faeff64e69..4fc5b9ac83 100644 --- a/main.go +++ b/main.go @@ -261,7 +261,7 @@ func main() { os.Exit(1) } - period := signal.GetTerminationGracePeriodSeconds(mgr.GetAPIReader(), watchNamespace) + period := signal.GetTerminationGracePeriodSeconds(mgr.GetAPIReader(), util.GetCheOperatorNamespace()) sigHandler := signal.SetupSignalHandler(period) // we install the devworkspace CheCluster reconciler even if dw is not supported so that it From 1c9986161b37fed3391a1eee9cc61b4977963b24 Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Mon, 1 Nov 2021 06:07:26 +0200 Subject: [PATCH 3/4] Make request to get csv files with less namespaces scope. Signed-off-by: Oleksandr Andriienko --- pkg/deploy/dev-workspace/dev_workspace.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/deploy/dev-workspace/dev_workspace.go b/pkg/deploy/dev-workspace/dev_workspace.go index f1050736e4..78aac63c31 100644 --- a/pkg/deploy/dev-workspace/dev_workspace.go +++ b/pkg/deploy/dev-workspace/dev_workspace.go @@ -172,8 +172,7 @@ func IsDevWorkspaceOperatorCSVExists(deployContext *deploy.DeployContext) bool { } csvList := &operatorsv1alpha1.ClusterServiceVersionList{} - // Namespace: deploy.DefaultNamespaceForAllNamespacesMode - err := deployContext.ClusterAPI.Client.List(context.TODO(), csvList, &client.ListOptions{}) + err := deployContext.ClusterAPI.Client.List(context.TODO(), csvList, &client.ListOptions{Namespace: deploy.DefaultNamespaceForAllNamespacesMode}) if err != nil { return false } From 4edf902f9a99f33503d908228807bac8d8f6e81e Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Mon, 1 Nov 2021 09:41:21 +0200 Subject: [PATCH 4/4] Add new line. Signed-off-by: Oleksandr Andriienko --- pkg/util/namespace_provider.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/namespace_provider.go b/pkg/util/namespace_provider.go index ebb405914c..40b03fdc7f 100644 --- a/pkg/util/namespace_provider.go +++ b/pkg/util/namespace_provider.go @@ -37,4 +37,4 @@ func GetCheOperatorNamespace() string { operator_namespace = readNamespace() } return operator_namespace -} \ No newline at end of file +}