workflow

package
v1.13.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package workflow implements a workflow manager to be used for implementing composable kubeadm workflows.

Composable kubeadm workflows are built by an ordered sequence of phases; each phase can have it's own, nested, ordered sequence of sub phases. For instance

preflight     	Run master pre-flight checks
certs         	Generates all PKI assets necessary to establish the control plane
	/ca             Generates a self-signed kubernetes CA to provision identities for Kubernetes components
	/apiserver      Generates an API server serving certificate and key
	...
kubeconfig		Generates all kubeconfig files necessary to establish the control plane
	/admin          Generates a kubeconfig file for the admin to use and for kubeadm itself
	/kubelet        Generates a kubeconfig file for the kubelet to use.
	...
...

Phases are designed to be reusable across different kubeadm workflows thus allowing e.g. reuse of phase certs in both kubeadm init and kubeadm join --control-plane workflows.

Each workflow can be defined and managed using a Runner, that will run all the phases according to the given order; nested phases will be executed immediately after their parent phase.

The Runner behavior can be changed by setting the RunnerOptions, typically exposed as kubeadm command line flags, thus allowing to filter the list of phases to be executed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Phase

type Phase struct {
	// name of the phase.
	// Phase name should be unique among peer phases (phases belonging to
	// the same workflow or phases belonging to the same parent phase).
	Name string

	// Short description of the phase.
	Short string

	// Long returns the long description of the phase.
	Long string

	// Example returns the example for the phase.
	Example string

	// Hidden define if the phase should be hidden in the workflow help.
	// e.g. PrintFilesIfDryRunning phase in the kubeadm init workflow is candidate for being hidden to the users
	Hidden bool

	// Phases defines a nested, ordered sequence of phases.
	Phases []Phase

	// Run defines a function implementing the phase action.
	// It is recommended to implent type assertion, e.g. using golang type switch,
	// for validating the RunData type.
	Run func(data RunData) error

	// RunIf define a function that implements a condition that should be checked
	// before executing the phase action.
	// If this function return nil, the phase action is always executed.
	RunIf func(data RunData) (bool, error)
}

Phase provides an implementation of a workflow phase that allows creation of new phases by simply instantiating a variable of this type.

Example
// Create a phase
var myPhase1 = Phase{
	Name:  "myPhase1",
	Short: "A phase of a kubeadm composable workflow...",
	Run: func(data RunData) error {
		// transform data into a typed data struct
		d, ok := data.(myPhaseData)
		if !ok {
			return errors.New("invalid RunData type")
		}

		// implement your phase logic...
		fmt.Printf("%v", d.Data())
		return nil
	},
}

// Create another phase
var myPhase2 = Phase{
	Name:  "myPhase2",
	Short: "Another phase of a kubeadm composable workflow...",
	Run: func(data RunData) error {
		// transform data into a typed data struct
		d, ok := data.(myPhaseData)
		if !ok {
			return errors.New("invalid RunData type")
		}

		// implement your phase logic...
		fmt.Printf("%v", d.Data())
		return nil
	},
}

// Adds the new phases to the workflow
// Phases will be executed the same order they are added to the workflow
myWorkflowRunner.AppendPhase(myPhase1)
myWorkflowRunner.AppendPhase(myPhase2)
Output:

func (*Phase) AppendPhase

func (t *Phase) AppendPhase(phase Phase)

AppendPhase adds the given phase to the nested, ordered sequence of phases.

type RunData

type RunData = interface{}

RunData defines the data shared among all the phases included in the workflow, that is any type.

type Runner

type Runner struct {
	// Options that regulate the runner behavior.
	Options RunnerOptions

	// Phases composing the workflow to be managed by the runner.
	Phases []Phase
	// contains filtered or unexported fields
}

Runner implements management of composable kubeadm workflows.

func NewRunner

func NewRunner() *Runner

NewRunner return a new runner for composable kubeadm workflows.

func (*Runner) AppendPhase

func (e *Runner) AppendPhase(t Phase)

AppendPhase adds the given phase to the ordered sequence of phases managed by the runner.

func (*Runner) BindToCommand

func (e *Runner) BindToCommand(cmd *cobra.Command)

BindToCommand bind the Runner to a cobra command by altering command help, adding phase related flags and by adding phases subcommands Please note that this command needs to be done once all the phases are added to the Runner.

func (*Runner) Help

func (e *Runner) Help(cmdUse string) string

Help returns text with the list of phases included in the workflow.

func (*Runner) InitData

func (e *Runner) InitData() (RunData, error)

InitData triggers the creation of runtime data shared among all the phases included in the workflow. This action can be executed explicitly out, when it is necessary to get the RunData before actually executing Run, or implicitly when invoking Run.

func (*Runner) Run

func (e *Runner) Run() error

Run the kubeadm composable kubeadm workflows.

Example
// Create a phase
var myPhase = Phase{
	Name:  "myPhase",
	Short: "A phase of a kubeadm composable workflow...",
	Run: func(data RunData) error {
		// transform data into a typed data struct
		d, ok := data.(myPhaseData)
		if !ok {
			return errors.New("invalid RunData type")
		}

		// implement your phase logic...
		fmt.Printf("%v", d.Data())
		return nil
	},
}

// Adds the new phase to the workflow
var myWorkflowRunner = NewRunner()
myWorkflowRunner.AppendPhase(myPhase)

// Defines the method that creates the runtime data shared
// among all the phases included in the workflow
myWorkflowRunner.SetDataInitializer(func() (RunData, error) {
	return myWorkflowData{data: "some data"}, nil
})

// Runs the workflow
myWorkflowRunner.Run()
Output:

func (*Runner) SetDataInitializer

func (e *Runner) SetDataInitializer(builder func() (RunData, error))

SetDataInitializer allows to setup a function that initialize the runtime data shared among all the phases included in the workflow.

type RunnerOptions

type RunnerOptions struct {
	// FilterPhases defines the list of phases to be executed (if empty, all).
	FilterPhases []string

	// SkipPhases defines the list of phases to be excluded by execution (if empty, none).
	SkipPhases []string
}

RunnerOptions defines the options supported during the execution of a kubeadm composable workflows

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL