Skip to content

Commit

Permalink
wire install into the workflow provider
Browse files Browse the repository at this point in the history
* changed it so that install runs apply through the workflow engine
* switch install between deperator and the workflow engine as appropriate
* not implemented

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs committed Sep 4, 2022
1 parent e91fa45 commit 876c3e4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/porter/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (p *Porter) buildBundle(ctx context.Context, m *manifest.Manifest, digest d
return p.writeBundle(bun)
}

func (p Porter) writeBundle(b cnab.ExtendedBundle) error {
func (p *Porter) writeBundle(b cnab.ExtendedBundle) error {
f, err := p.Config.FileSystem.OpenFile(build.LOCAL_BUNDLE, os.O_RDWR|os.O_CREATE|os.O_TRUNC, pkg.FileModeWritable)
if err != nil {
return fmt.Errorf("error creating %s: %w", build.LOCAL_BUNDLE, err)
Expand Down
35 changes: 35 additions & 0 deletions pkg/porter/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"errors"
"fmt"

"get.porter.sh/porter/pkg/experimental"
"get.porter.sh/porter/pkg/workflow"

"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/storage"
"get.porter.sh/porter/pkg/tracing"
Expand Down Expand Up @@ -87,6 +90,24 @@ func (p *Porter) InstallBundle(ctx context.Context, opts InstallOptions) error {
}
i.TrackBundle(bundleRef.Reference)
i.Labels = opts.ParseLabels()

if p.useWorkflowEngine(bundleRef.Definition) {
workflowOpts := workflow.CreateWorkflowOptions{
Installation: i,
Bundle: bundleRef.Definition,
DebugMode: opts.DebugMode,
MaxParallel: 1,
}
w, err := p.Workflow.CreateWorkflow(ctx, workflowOpts)
if err != nil {
return err
}

// TODO (PEP003): if a dry-run is requested, print out the execution plan and then exit
return p.Workflow.StartWorkflow(ctx, w)
}

// Use the old implementation of bundle execution compatible with depsv1
err = p.Installations.UpsertInstallation(ctx, i)
if err != nil {
return fmt.Errorf("error saving installation record: %w", err)
Expand All @@ -96,6 +117,20 @@ func (p *Porter) InstallBundle(ctx context.Context, opts InstallOptions) error {
return p.ExecuteAction(ctx, i, opts)
}

// useWorkflowEngine determines if the new workflow engine or the old bundle execution code should be used.
// Once depsv2 is no longer experimental, we can switch 100% to the workflow engine
// Old bundles can still use depsv1, since depsv2 is a superset of depsv1.
// It will change how the bundle is run, for example calling install right now twice in a row
// results in an error, and this would remove that limitation, and instead a second call to install causes it to be reconciled and possibly skipped.
// In either case, the solution to the user is to call --force so the change isn't breaking.
func (p *Porter) useWorkflowEngine(bun cnab.ExtendedBundle) bool {
if bun.HasDependenciesV2() {
return true
}

return p.Config.IsFeatureEnabled(experimental.FlagDependenciesV2)
}

// Remember the parameters and credentials used with the bundle last.
// Appends any newly specified parameters, parameter/credential sets to the installation record.
// Users are expected to edit the installation record if they don't want that behavior.
Expand Down
2 changes: 2 additions & 0 deletions pkg/porter/porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package porter
import (
"context"
"errors"
"get.porter.sh/porter/pkg/workflow"
"strings"

"get.porter.sh/porter/pkg/build"
Expand Down Expand Up @@ -44,6 +45,7 @@ type Porter struct {
CNAB cnabprovider.CNABProvider
Secrets secrets.Store
Storage storage.Provider
Workflow workflow.Provider
}

// New porter client, initialized with useful defaults.
Expand Down
27 changes: 24 additions & 3 deletions pkg/workflow/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workflow
import (
"context"
"fmt"
"get.porter.sh/porter/pkg/storage"
"github.com/Masterminds/semver/v3"

"get.porter.sh/porter/pkg/cnab"
Expand All @@ -18,8 +19,29 @@ type Engine struct {
namespace string
}

func (t Engine) CreateWorkflow(ctx context.Context, bun cnab.ExtendedBundle, action string) (Workflow, error) {
g, err := t.GetDependencyGraph(ctx, bun)
// CreateWorkflowOptions are the set of options for creating a Workflow.
type CreateWorkflowOptions struct {
// DebugMode alters how the workflow is executed so that it can be stepped through.
DebugMode bool

// MaxParallel indicates how many parallel bundles may be executed at the same
// time. Defaults to 0, indicating that the maximum should be determined by the
// number of available CPUs or cluster nodes (depending on the runtime driver).
MaxParallel int

// Installation that triggered the workflow.
Installation storage.Installation

// Bundle definition of the Installation.
Bundle cnab.ExtendedBundle

// CustomAction is the name of a custom action defined on the bundle to execute.
// When not set, the installation is reconciled.
CustomAction string
}

func (t Engine) CreateWorkflow(ctx context.Context, opts CreateWorkflowOptions) (Workflow, error) {
g, err := t.GetDependencyGraph(ctx, opts.Bundle)
if err != nil {
return Workflow{}, err
}
Expand All @@ -29,7 +51,6 @@ func (t Engine) CreateWorkflow(ctx context.Context, bun cnab.ExtendedBundle, act
return Workflow{}, fmt.Errorf("could not generate a workflow for the bundle: the bundle graph has a cyle")
}

_ = WorkflowOptions{}
panic("not implemented")
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/workflow/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package workflow

import (
"context"
"get.porter.sh/porter/pkg/cnab"
)

// Provider defines the interface between Porter and executing a bundle workflow.
type Provider interface {
// CreateWorkflow running the specified bundle action.
CreateWorkflow(ctx context.Context, bun cnab.ExtendedBundle, action string) (Workflow, error)
// CreateWorkflow that runs the specified bundle action.
CreateWorkflow(ctx context.Context, opts CreateWorkflowOptions) (Workflow, error)

// StartWorkflow begins the specified workflow.
StartWorkflow(ctx context.Context, workflow Workflow) error
Expand Down
29 changes: 23 additions & 6 deletions pkg/workflow/workflow.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package workflow

type WorkflowOptions struct {
// DebugMode alters how the workflow is executed so that it can be stepped through.
DebugMode bool
import (
"get.porter.sh/porter/pkg/porter"
"get.porter.sh/porter/pkg/storage"
)

// MaxParallel indicates how many parallel bundles may be executed at the same time.
MaxParallel int
type Workflow struct {
Stages []Stage
}

type Workflow struct {
type Stage struct {
Jobs map[string]Job
}

type Job struct {
// Action name to execute on the bundle, when empty default to applying the installation
Action string

// Installation key
Installation storage.Installation

// Depends is a list of job keys that the Job depends upon.
Depends []string
}

func (j Job) Execute() error {
p := porter.New()
}

0 comments on commit 876c3e4

Please sign in to comment.