Skip to content

Commit

Permalink
Add support for .spec.contributions field in DevWorkspace
Browse files Browse the repository at this point in the history
Add support for specifying additional components in the
`.spec.contributions` field of the DevWorkspace. These
components are defined and handled similarly to plugin components.

Signed-off-by: Angel Misevski <[email protected]>
  • Loading branch information
amisevsk committed Oct 18, 2022
1 parent 04b29ee commit 9f760b3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 33 deletions.
2 changes: 1 addition & 1 deletion controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
wsDefaults.ApplyDefaultTemplate(workspace)
}

flattenedWorkspace, warnings, err := flatten.ResolveDevWorkspace(&workspace.Spec.Template, flattenHelpers)
flattenedWorkspace, warnings, err := flatten.ResolveDevWorkspace(&workspace.Spec.Template, workspace.Spec.Contributions, flattenHelpers)
if err != nil {
return r.failWorkspace(workspace, fmt.Sprintf("Error processing devfile: %s", err), metrics.ReasonBadRequest, reqLogger, &reconcileStatus)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/library/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
//
// Note: Requires DevWorkspace to be flattened (i.e. the DevWorkspace contains no Parent or Components of type Plugin)
func GetKubeContainersFromDevfile(workspace *dw.DevWorkspaceTemplateSpec, pullPolicy string) (*v1alpha1.PodAdditions, error) {
if !flatten.DevWorkspaceIsFlattened(workspace) {
if !flatten.DevWorkspaceIsFlattened(workspace, nil) {
return nil, fmt.Errorf("devfile is not flattened")
}
podAdditions := &v1alpha1.PodAdditions{}
Expand Down
12 changes: 11 additions & 1 deletion pkg/library/flatten/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ package flatten

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

func DevWorkspaceIsFlattened(devworkspace *dw.DevWorkspaceTemplateSpec) bool {
func DevWorkspaceIsFlattened(devworkspace *dw.DevWorkspaceTemplateSpec, contributions []dw.ComponentContribution) bool {
if devworkspace == nil {
return len(contributions) == 0
}

if devworkspace.Parent != nil {
return false
}

if len(contributions) > 0 {
return false
}

for _, component := range devworkspace.Components {
if component.Plugin != nil {
return false
}
}

return true
}
84 changes: 54 additions & 30 deletions pkg/library/flatten/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ type ResolverTools struct {

// ResolveDevWorkspace takes a devworkspace and returns a "resolved" version of it -- i.e. one where all plugins and parents
// are inlined as components.
func ResolveDevWorkspace(workspace *dw.DevWorkspaceTemplateSpec, tooling ResolverTools) (*dw.DevWorkspaceTemplateSpec, *variables.VariableWarning, error) {
func ResolveDevWorkspace(workspace *dw.DevWorkspaceTemplateSpec, contributions []dw.ComponentContribution, tooling ResolverTools) (*dw.DevWorkspaceTemplateSpec, *variables.VariableWarning, error) {
resolutionCtx := &resolutionContextTree{}
resolvedDW, err := recursiveResolve(workspace, tooling, resolutionCtx)
resolvedDW, err := recursiveResolve(workspace, contributions, tooling, resolutionCtx)
if err != nil {
return nil, nil, err
}
Expand All @@ -75,49 +75,73 @@ func ResolveDevWorkspace(workspace *dw.DevWorkspaceTemplateSpec, tooling Resolve
return resolvedDW, nil, nil
}

func recursiveResolve(workspace *dw.DevWorkspaceTemplateSpec, tooling ResolverTools, resolveCtx *resolutionContextTree) (*dw.DevWorkspaceTemplateSpec, error) {
if DevWorkspaceIsFlattened(workspace) {
func recursiveResolve(workspace *dw.DevWorkspaceTemplateSpec, contributions []dw.ComponentContribution, tooling ResolverTools, resolveCtx *resolutionContextTree) (*dw.DevWorkspaceTemplateSpec, error) {
if DevWorkspaceIsFlattened(workspace, contributions) {
return workspace.DeepCopy(), nil
}

resolvedParent := &dw.DevWorkspaceTemplateSpecContent{}
if workspace.Parent != nil {
resolvedParentSpec, err := resolveParentComponent(workspace.Parent, tooling)
var pluginSpecContents []*dw.DevWorkspaceTemplateSpecContent
for _, contribution := range contributions {
pluginComponent, err := resolvePluginComponent(contribution.Name, &contribution.PluginComponent, tooling)
if err != nil {
return nil, err
}
if !DevWorkspaceIsFlattened(resolvedParentSpec) {
// TODO: implemenent this
return nil, fmt.Errorf("parents containing plugins or parents are not supported")
newCtx := resolveCtx.addPlugin(contribution.Name, &contribution.PluginComponent)
if err := newCtx.hasCycle(); err != nil {
return nil, err
}

resolvedPlugin, err := recursiveResolve(pluginComponent, nil, tooling, newCtx)
if err != nil {
return nil, err
}
annotate.AddSourceAttributesForTemplate("parent", resolvedParentSpec)
resolvedParent = &resolvedParentSpec.DevWorkspaceTemplateSpecContent

annotate.AddSourceAttributesForTemplate(contribution.Name, resolvedPlugin)
pluginSpecContents = append(pluginSpecContents, &resolvedPlugin.DevWorkspaceTemplateSpecContent)
}
resolvedContent := workspace.DevWorkspaceTemplateSpecContent.DeepCopy()
resolvedContent.Components = nil

var pluginSpecContents []*dw.DevWorkspaceTemplateSpecContent
for _, component := range workspace.Components {
if component.Plugin == nil {
// No action necessary
resolvedContent.Components = append(resolvedContent.Components, component)
} else {
pluginComponent, err := resolvePluginComponent(component.Name, component.Plugin, tooling)
resolvedParent := &dw.DevWorkspaceTemplateSpecContent{}
resolvedContent := &dw.DevWorkspaceTemplateSpecContent{}
if workspace != nil {
resolvedContent = workspace.DevWorkspaceTemplateSpecContent.DeepCopy()
resolvedContent.Components = nil
}
if workspace != nil {
if workspace.Parent != nil {
resolvedParentSpec, err := resolveParentComponent(workspace.Parent, tooling)
if err != nil {
return nil, err
}
newCtx := resolveCtx.addPlugin(component.Name, component.Plugin)
if err := newCtx.hasCycle(); err != nil {
return nil, err
if !DevWorkspaceIsFlattened(resolvedParentSpec, nil) {
// TODO: implemenent this
return nil, fmt.Errorf("parents containing plugins or parents are not supported")
}
annotate.AddSourceAttributesForTemplate("parent", resolvedParentSpec)
resolvedParent = &resolvedParentSpec.DevWorkspaceTemplateSpecContent
}

resolvedPlugin, err := recursiveResolve(pluginComponent, tooling, newCtx)
if err != nil {
return nil, err
for _, component := range workspace.Components {
if component.Plugin == nil {
// No action necessary
resolvedContent.Components = append(resolvedContent.Components, component)
} else {
pluginComponent, err := resolvePluginComponent(component.Name, component.Plugin, tooling)
if err != nil {
return nil, err
}
newCtx := resolveCtx.addPlugin(component.Name, component.Plugin)
if err := newCtx.hasCycle(); err != nil {
return nil, err
}

resolvedPlugin, err := recursiveResolve(pluginComponent, nil, tooling, newCtx)
if err != nil {
return nil, err
}

annotate.AddSourceAttributesForTemplate(component.Name, resolvedPlugin)
pluginSpecContents = append(pluginSpecContents, &resolvedPlugin.DevWorkspaceTemplateSpecContent)
}

annotate.AddSourceAttributesForTemplate(component.Name, resolvedPlugin)
pluginSpecContents = append(pluginSpecContents, &resolvedPlugin.DevWorkspaceTemplateSpecContent)
}
}

Expand Down

0 comments on commit 9f760b3

Please sign in to comment.