Skip to content

Commit

Permalink
feat: configurable default Workspace Spec Template
Browse files Browse the repository at this point in the history
Fix devfile#853

Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>
  • Loading branch information
AObuchow committed Jun 10, 2022
1 parent 7475724 commit 2647032
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apis/controller/v1alpha1/devworkspaceoperatorconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package v1alpha1

import (
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -127,6 +128,10 @@ type WorkspaceConfig struct {
// configuration option is ignored. If set, the entire pod security context is overridden;
// values are not merged.
PodSecurityContext *corev1.PodSecurityContext `json:"podSecurityContext,omitempty"`
// DefaultTemplate defines an optional DevWorkspace Spec Template which gets applied to the workspace
// if the workspace's Template Spec Components are not defined. The DefaultTemplate will overwrite the existing
// Template Spec, with the exception of Projects (if any are defined).
DefaultTemplate *dw.DevWorkspaceTemplateSpecContent `json:"defaultTemplate,omitempty"`
}

// DevWorkspaceOperatorConfig is the Schema for the devworkspaceoperatorconfigs API
Expand Down
17 changes: 17 additions & 0 deletions controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)

Expand Down Expand Up @@ -240,6 +241,22 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
HttpClient: httpClient,
}

// TODO: Use merge code rather than deepCopy which overwrites?
if len(workspace.Spec.Template.Components) == 0 && config.Workspace.DefaultTemplate != nil {
var projectsCopy []v1alpha2.Project
if len(workspace.Spec.Template.Projects) > 0 {
projectsCopy = make([]v1alpha2.Project, len(workspace.Spec.Template.Projects))
}

// TODO: Use deepCopyInto instead?
defaultTemplateCopy := config.Workspace.DefaultTemplate.DeepCopy()
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = *defaultTemplateCopy

if len(projectsCopy) > 0 {
workspace.Spec.Template.Projects = projectsCopy
}
}

flattenedWorkspace, warnings, err := flatten.ResolveDevWorkspace(&workspace.Spec.Template, flattenHelpers)
if err != nil {
return r.failWorkspace(workspace, fmt.Sprintf("Error processing devfile: %s", err), metrics.ReasonBadRequest, reqLogger, &reconcileStatus)
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package config

import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

Expand All @@ -43,6 +45,16 @@ var defaultConfig = &v1alpha1.OperatorConfiguration{
RunAsNonRoot: &boolTrue,
FSGroup: &int64UID,
},

DefaultTemplate: &v1alpha2.DevWorkspaceTemplateSpecContent{
Variables: map[string]string{},
Attributes: map[string]v1.JSON{},
Components: []v1alpha2.Component{},
Projects: []v1alpha2.Project{},
StarterProjects: []v1alpha2.StarterProject{},
Commands: []v1alpha2.Command{},
Events: &v1alpha2.Events{},
},
},
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/config/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ func mergeConfig(from, to *controller.OperatorConfiguration) {
to.Workspace.DefaultStorageSize.PerWorkspace = &perWorkspaceSizeCopy
}
}
if from.Workspace.DefaultTemplate != nil {
templateSpecContentCopy := from.Workspace.DefaultTemplate.DeepCopy()
to.Workspace.DefaultTemplate = templateSpecContentCopy
}
}
}

Expand Down Expand Up @@ -297,6 +301,9 @@ func logCurrentConfig() {
config = append(config, fmt.Sprintf("workspace.defaultStorageSize.perWorkspace=%s", Workspace.DefaultStorageSize.PerWorkspace.String()))
}
}
if Workspace.DefaultTemplate != nil {
config = append(config, fmt.Sprintf("workspace.defaultTemplate is set"))
}
}
if internalConfig.EnableExperimentalFeatures != nil && *internalConfig.EnableExperimentalFeatures {
config = append(config, "enableExperimentalFeatures=true")
Expand Down

0 comments on commit 2647032

Please sign in to comment.