kubernetes: k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow Index | Examples | Files

package workflow

import "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"

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 control-plane 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 phase runner can be bound to a cobra command; this operation sets the command description giving evidence of the list of phases, and automatically creates sub commands for invoking phases atomically.

Autogenerated sub commands get flags according to the following rule:

- global flags will be always inherited by autogenerated commands (this is managed by cobra)

- local flags defined in the parent command might be inherited by autogenerated commands, but this requires explicit opt-in so each phase can select the subset of relevant flags

- it is possible to define additional flags that might be inherited by autogenerated commands via explicit opt-in, but are not applied to the parent command

In order to keep flags definition under control, please refer to the "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" package.

Index

Examples

Package Files

doc.go phase.go runner.go

type Phase Uses

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

    // Aliases returns the aliases for the phase.
    Aliases []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

    // RunAllSiblings allows to assign to a phase the responsibility to
    // run all the sibling phases
    // Nb. phase marked as RunAllSiblings can not have Run functions
    RunAllSiblings bool

    // 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)

    // InheritFlags defines the list of flags that the cobra command generated for this phase should Inherit
    // from local flags defined in the parent command / or additional flags defined in the phase runner.
    // If the values is not set or empty, no flags will be assigned to the command
    // Nb. global flags are automatically inherited by nested cobra command
    InheritFlags []string

    // LocalFlags defines the list of flags that should be assigned to the cobra command generated
    // for this phase.
    // Nb. if two or phases have the same local flags, please consider using local flags in the parent command
    // or additional flags defined in the phase runner.
    LocalFlags *pflag.FlagSet

    // ArgsValidator defines the positional arg function to be used for validating args for this phase
    // If not set a phase will adopt the args of the top level command.
    ArgsValidator cobra.PositionalArgs
}

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

Code:

// 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\n", 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\n", 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)

func (*Phase) AppendPhase Uses

func (t *Phase) AppendPhase(phase Phase)

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

type RunData Uses

type RunData = interface{}

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

type Runner Uses

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 Uses

func NewRunner() *Runner

NewRunner return a new runner for composable kubeadm workflows.

func (*Runner) AppendPhase Uses

func (e *Runner) AppendPhase(t Phase)

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

func (*Runner) BindToCommand Uses

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 Uses

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

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

func (*Runner) InitData Uses

func (e *Runner) InitData(args []string) (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 Uses

func (e *Runner) Run(args []string) error

Run the kubeadm composable kubeadm workflows.

Code:

// 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\n", 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(cmd *cobra.Command, args []string) (RunData, error) {
    return myWorkflowData{data: "some data"}, nil
})

// Runs the workflow by passing a list of arguments
myWorkflowRunner.Run([]string{})

func (*Runner) SetAdditionalFlags Uses

func (e *Runner) SetAdditionalFlags(fn func(*pflag.FlagSet))

SetAdditionalFlags allows to define flags to be added to the subcommands generated for each phase (but not existing in the parent command). Please note that this command needs to be done before BindToCommand. Nb. if a flag is used only by one phase, please consider using phase LocalFlags.

func (*Runner) SetDataInitializer Uses

func (e *Runner) SetDataInitializer(builder func(cmd *cobra.Command, args []string) (RunData, error))

SetDataInitializer allows to setup a function that initialize the runtime data shared among all the phases included in the workflow. The method will receive in input the cmd that triggers the Runner (only if the runner is BindToCommand)

type RunnerOptions Uses

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

Package workflow imports 5 packages (graph) and is imported by 23 packages. Updated 2019-10-02. Refresh now. Tools for package owners.