Skip to content

Commit

Permalink
feat: configure DWOC from che-operator CR (#1494)
Browse files Browse the repository at this point in the history
* feat: configure DWOC from che-operator CR

Currently, only the following fields from the checluster custom resource
are propagated to the DevWorkspace-Operator Configuration:

- `checluster.spec.devEnvironments.Storage.pvcStrategy`
- `checluster.spec.devEnvironments.Storage.perUserStrategyPvcConfig.ClaimSize`
- `checluster.spec.devEnvironments.Storage.perUserStrategyPvcConfig.StorageClass`
- `checluster.spec.devEnvironments.Storage.perWorkspaceStrategyPvcConfig.ClaimSize`
- `checluster.spec.devEnvironments.Storage.perWorkspaceStrategyPvcConfig.StorageClass`

Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>

* Update CSV and autogenerated apiv2 deepcopy

Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>

* Refactoring

Signed-off-by: Anatolii Bazko <abazko@redhat.com>

* make fmt

Signed-off-by: Anatolii Bazko <abazko@redhat.com>

Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>
Signed-off-by: Anatolii Bazko <abazko@redhat.com>
Co-authored-by: Anatolii Bazko <abazko@redhat.com>
  • Loading branch information
AObuchow and tolusha committed Sep 2, 2022
1 parent bd8ca43 commit 0a3d57c
Show file tree
Hide file tree
Showing 6 changed files with 482 additions and 16 deletions.
2 changes: 2 additions & 0 deletions controllers/che/checluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/eclipse-che/che-operator/pkg/deploy/consolelink"
"github.com/eclipse-che/che-operator/pkg/deploy/dashboard"
devworkspace "github.com/eclipse-che/che-operator/pkg/deploy/dev-workspace"
devworkspaceconfig "github.com/eclipse-che/che-operator/pkg/deploy/dev-workspace-config"
"github.com/eclipse-che/che-operator/pkg/deploy/devfileregistry"
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
identityprovider "github.com/eclipse-che/che-operator/pkg/deploy/identity-provider"
Expand Down Expand Up @@ -97,6 +98,7 @@ func NewReconciler(
reconcileManager.RegisterReconciler(tls.NewCertificatesReconciler())
reconcileManager.RegisterReconciler(tls.NewTlsSecretReconciler())
reconcileManager.RegisterReconciler(devworkspace.NewDevWorkspaceReconciler())
reconcileManager.RegisterReconciler(devworkspaceconfig.NewDevWorkspaceConfigReconciler())
reconcileManager.RegisterReconciler(rbac.NewCheServerPermissionsReconciler())
reconcileManager.RegisterReconciler(rbac.NewGatewayPermissionsReconciler())
reconcileManager.RegisterReconciler(rbac.NewWorkspacePermissionsReconciler())
Expand Down
10 changes: 6 additions & 4 deletions pkg/common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ const (
CheEclipseOrgHash256 = "che.eclipse.org/hash256"
CheEclipseOrgManagedAnnotationsDigest = "che.eclipse.org/managed-annotations-digest"

// Workspaces
DefaultPvcStrategy = "common"
DefaultPvcClaimSize = "10Gi"
DefaultWorkspaceJavaOpts = "-XX:MaxRAM=150m -XX:MaxRAMFraction=2 -XX:+UseParallelGC " +
// DevEnvironments
PerUserPVCStorageStrategy = "per-user"
DefaultPvcStorageStrategy = "per-user"
PerWorkspacePVCStorageStrategy = "per-workspace"
CommonPVCStorageStrategy = "common"
DefaultWorkspaceJavaOpts = "-XX:MaxRAM=150m -XX:MaxRAMFraction=2 -XX:+UseParallelGC " +
"-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 " +
"-Dsun.zip.disableMemoryMapping=true " +
"-Xms20m -Djava.security.egd=file:/dev/./urandom"
Expand Down
5 changes: 4 additions & 1 deletion pkg/common/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import (
"strings"
"testing"

controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
routev1 "github.com/openshift/api/route/v1"

"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"

chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
console "github.com/openshift/api/console/v1"
oauthv1 "github.com/openshift/api/oauth/v1"
routev1 "github.com/openshift/api/route/v1"
userv1 "github.com/openshift/api/user/v1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -169,6 +171,7 @@ func GetDeployContext(cheCluster *chev2.CheCluster, initObjs []runtime.Object) *
scheme := scheme.Scheme
chev2.SchemeBuilder.AddToScheme(scheme)
scheme.AddKnownTypes(operatorsv1alpha1.SchemeGroupVersion, &operatorsv1alpha1.Subscription{})
scheme.AddKnownTypes(controllerv1alpha1.SchemeBuilder.GroupVersion, &controllerv1alpha1.DevWorkspaceOperatorConfig{})
scheme.AddKnownTypes(crdv1.SchemeGroupVersion, &crdv1.CustomResourceDefinition{})
scheme.AddKnownTypes(operatorsv1alpha1.SchemeGroupVersion, &operatorsv1alpha1.Subscription{})
scheme.AddKnownTypes(oauthv1.GroupVersion, &oauthv1.OAuthClient{})
Expand Down
122 changes: 122 additions & 0 deletions pkg/deploy/dev-workspace-config/dev_workspace_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// Copyright (c) 2019-2022 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 devworkspaceconfig

import (
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/eclipse-che/che-operator/pkg/common/constants"
"github.com/eclipse-che/che-operator/pkg/common/utils"
"github.com/eclipse-che/che-operator/pkg/deploy"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
devWorkspaceConfigName = "devworkspace-config"
)

type DevWorkspaceConfigReconciler struct {
deploy.Reconcilable
}

func NewDevWorkspaceConfigReconciler() *DevWorkspaceConfigReconciler {
return &DevWorkspaceConfigReconciler{}
}

func (d *DevWorkspaceConfigReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.Result, bool, error) {
dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{
Name: devWorkspaceConfigName,
Namespace: ctx.CheCluster.Namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "DevWorkspaceOperatorConfig",
APIVersion: controllerv1alpha1.GroupVersion.String(),
},
}

if _, err := deploy.GetNamespacedObject(ctx, devWorkspaceConfigName, dwoc); err != nil {
return reconcile.Result{}, false, err
}

if dwoc.Config == nil {
dwoc.Config = &controllerv1alpha1.OperatorConfiguration{}
}
err := updateOperatorConfig(ctx.CheCluster.Spec.DevEnvironments.Storage, dwoc.Config)
if err != nil {
return reconcile.Result{}, false, err
}

done, err := deploy.Sync(ctx, dwoc)
if !done {
return reconcile.Result{}, false, err
}

return reconcile.Result{}, true, nil
}

func (d *DevWorkspaceConfigReconciler) Finalize(ctx *chetypes.DeployContext) bool {
return true
}

func updateOperatorConfig(storage chev2.WorkspaceStorage, operatorConfig *controllerv1alpha1.OperatorConfiguration) error {
var pvc *chev2.PVC

pvcStrategy := utils.GetValue(storage.PvcStrategy, constants.DefaultPvcStorageStrategy)
switch pvcStrategy {
case constants.CommonPVCStorageStrategy:
fallthrough
case constants.PerUserPVCStorageStrategy:
if storage.PerUserStrategyPvcConfig != nil {
pvc = storage.PerUserStrategyPvcConfig
}
case constants.PerWorkspacePVCStorageStrategy:
if storage.PerWorkspaceStrategyPvcConfig != nil {
pvc = storage.PerWorkspaceStrategyPvcConfig
}
}

if pvc != nil {
if operatorConfig.Workspace == nil {
operatorConfig.Workspace = &controllerv1alpha1.WorkspaceConfig{}
}
return updateWorkspaceConfig(pvc, pvcStrategy == constants.PerWorkspacePVCStorageStrategy, operatorConfig.Workspace)
}
return nil
}

func updateWorkspaceConfig(pvc *chev2.PVC, isPerWorkspacePVCStorageStrategy bool, workspaceConfig *controllerv1alpha1.WorkspaceConfig) error {
if pvc.StorageClass != "" {
workspaceConfig.StorageClassName = &pvc.StorageClass
}

if pvc.ClaimSize != "" {
if workspaceConfig.DefaultStorageSize == nil {
workspaceConfig.DefaultStorageSize = &controllerv1alpha1.StorageSizes{}
}

pvcSize, err := resource.ParseQuantity(pvc.ClaimSize)
if err != nil {
return err
}

if isPerWorkspacePVCStorageStrategy {
workspaceConfig.DefaultStorageSize.PerWorkspace = &pvcSize
} else {
workspaceConfig.DefaultStorageSize.Common = &pvcSize
}
}
return nil
}
Loading

0 comments on commit 0a3d57c

Please sign in to comment.