Skip to content

Commit

Permalink
Merge pull request #494 from carolynvs/fix-custom-action-schema
Browse files Browse the repository at this point in the history
Fix custom action schema
  • Loading branch information
carolynvs-msft authored Aug 3, 2019
2 parents 5fc8d3b + 551989d commit 533ed71
Show file tree
Hide file tree
Showing 15 changed files with 687 additions and 466 deletions.
16 changes: 16 additions & 0 deletions cmd/kubernetes/invoke.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"github.com/deislabs/porter/pkg/kubernetes"
"github.com/spf13/cobra"
)

func buildInvokeCommand(mixin *kubernetes.Mixin) *cobra.Command {
return &cobra.Command{
Use: "invoke",
Short: "Use kubectl to apply manifests to a cluster",
RunE: func(cmd *cobra.Command, args []string) error {
return mixin.Execute()
},
}
}
3 changes: 2 additions & 1 deletion cmd/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func buildRootCommand(in io.Reader) *cobra.Command {
cmd.AddCommand(buildVersionCommand(mixin))
cmd.AddCommand(buildBuildCommand(mixin))
cmd.AddCommand(buildInstallCommand(mixin))
cmd.AddCommand(buildInvokeCommand(mixin))
cmd.AddCommand(buildUpgradeCommand(mixin))
cmd.AddCommand(buildUnInstallCommand(mixin))
cmd.AddCommand(buildUninstallCommand(mixin))
cmd.AddCommand(buildSchemaCommand(mixin))
return cmd
}
2 changes: 1 addition & 1 deletion cmd/kubernetes/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
)

func buildUnInstallCommand(mixin *kubernetes.Mixin) *cobra.Command {
func buildUninstallCommand(mixin *kubernetes.Mixin) *cobra.Command {
return &cobra.Command{
Use: "uninstall",
Short: "Use kubectl to delete resources contained in a manifest from a cluster",
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubernetes/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

func buildUpgradeCommand(mixin *kubernetes.Mixin) *cobra.Command {
return &cobra.Command{
Use: "Upgrade",
Use: "upgrade",
Short: "Use kubectl to apply manifests to a cluster",
RunE: func(cmd *cobra.Command, args []string) error {
return mixin.Upgrade()
return mixin.Execute()
},
}
}
12 changes: 7 additions & 5 deletions pkg/exec/schema/exec.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,18 @@
"$ref": "#/definitions/upgradeStep"
}
},
".*": {
"uninstall": {
"type": "array",
"items": {
"$ref": "#/definitions/invokeStep"
"$ref": "#/definitions/uninstallStep"
}
},
"uninstall": {
}
},
"patternProperties": {
".*": {
"type": "array",
"items": {
"$ref": "#/definitions/uninstallStep"
"$ref": "#/definitions/invokeStep"
}
}
},
Expand Down
12 changes: 7 additions & 5 deletions pkg/exec/testdata/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,18 @@
"$ref": "#/definitions/upgradeStep"
}
},
".*": {
"uninstall": {
"type": "array",
"items": {
"$ref": "#/definitions/invokeStep"
"$ref": "#/definitions/uninstallStep"
}
},
"uninstall": {
}
},
"patternProperties": {
".*": {
"type": "array",
"items": {
"$ref": "#/definitions/uninstallStep"
"$ref": "#/definitions/invokeStep"
}
}
},
Expand Down
49 changes: 39 additions & 10 deletions pkg/kubernetes/upgrade.go → pkg/kubernetes/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,44 @@ import (
yaml "gopkg.in/yaml.v2"
)

type UpgradeAction struct {
Steps []UpgradeStep `yaml:"upgrade"`
type ExecuteAction struct {
Steps []ExecuteStep // using UnmarshalYAML so that we don't need a custom type per action
}

type UpgradeStep struct {
UpgradeArguments `yaml:"kubernetes"`
// UnmarshalYAML takes any yaml in this form
// ACTION:
// - kubernetes: ...
// and puts the steps into the Action.Steps field
func (a *ExecuteAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
actionMap := map[interface{}][]interface{}{}
err := unmarshal(&actionMap)
if err != nil {
return errors.Wrap(err, "could not unmarshal yaml into an action map of kubernetes steps")
}

for _, stepMaps := range actionMap {
b, err := yaml.Marshal(stepMaps)
if err != nil {
return err
}

var steps []ExecuteStep
err = yaml.Unmarshal(b, &steps)
if err != nil {
return err
}

a.Steps = append(a.Steps, steps...)
}

return nil
}

type ExecuteStep struct {
ExecuteInstruction `yaml:"kubernetes"`
}

type UpgradeArguments struct {
type ExecuteInstruction struct {
InstallArguments `yaml:",inline"`

// Upgrade specific arguments
Expand All @@ -29,15 +58,15 @@ type UpgradeArguments struct {
Timeout *int `yaml:"timeout,omitempty"`
}

// Upgrade will reapply manifests using kubectl
func (m *Mixin) Upgrade() error {
// Execute will reapply manifests using kubectl
func (m *Mixin) Execute() error {

payload, err := m.getPayloadData()
if err != nil {
return err
}

var action UpgradeAction
var action ExecuteAction
err = yaml.Unmarshal(payload, &action)
if err != nil {
return err
Expand All @@ -51,7 +80,7 @@ func (m *Mixin) Upgrade() error {
var commands []*exec.Cmd

for _, manifestPath := range step.Manifests {
commandPayload, err := m.buildUpgradeCommand(step.UpgradeArguments, manifestPath)
commandPayload, err := m.buildExecuteCommand(step.ExecuteInstruction, manifestPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -79,7 +108,7 @@ func (m *Mixin) Upgrade() error {
return err
}

func (m *Mixin) buildUpgradeCommand(args UpgradeArguments, manifestPath string) ([]string, error) {
func (m *Mixin) buildExecuteCommand(args ExecuteInstruction, manifestPath string) ([]string, error) {
command, err := m.buildInstallCommand(args.InstallArguments, manifestPath)
if err != nil {
return nil, errors.Wrap(err, "unable to create upgrade command")
Expand Down
60 changes: 30 additions & 30 deletions pkg/kubernetes/upgrade_test.go → pkg/kubernetes/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
yaml "gopkg.in/yaml.v2"
)

type UpgradeTest struct {
type ExecuteTest struct {
expectedCommand string
upgradeStep UpgradeStep
executeStep ExecuteStep
}

func TestMixin_UpgradeStep(t *testing.T) {
func TestMixin_ExecuteStep(t *testing.T) {

manifestDirectory := "/cnab/app/manifests"

Expand All @@ -40,13 +40,13 @@ func TestMixin_UpgradeStep(t *testing.T) {

timeout := 1

upgradeTests := []UpgradeTest{
upgradeTests := []ExecuteTest{
// These tests are largely the same as the install, just testing that the embedded
// install gets handled correctly
{
expectedCommand: fmt.Sprintf("%s %s --wait", upgradeCmd, manifestDirectory),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -58,8 +58,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s --wait", upgradeCmd, manifestDirectory),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -71,8 +71,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s", upgradeCmd, manifestDirectory),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -85,8 +85,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s -n %s", upgradeCmd, manifestDirectory, namespace),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -100,8 +100,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s -n %s --validate=false", upgradeCmd, manifestDirectory, namespace),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -116,8 +116,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s -n %s --record=true", upgradeCmd, manifestDirectory, namespace),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -132,8 +132,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", upgradeCmd, manifestDirectory, selector),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
InstallArguments: InstallArguments{
Step: Step{
Description: "Hello",
Expand All @@ -148,8 +148,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
// These tests exercise the upgrade options
{
expectedCommand: fmt.Sprintf("%s %s --wait --force --grace-period=0", upgradeCmd, manifestDirectory),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
Force: &forceIt,
InstallArguments: InstallArguments{
Step: Step{
Expand All @@ -162,8 +162,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s --wait --grace-period=%d", upgradeCmd, manifestDirectory, withGrace),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
GracePeriod: &withGrace,
InstallArguments: InstallArguments{
Step: Step{
Expand All @@ -176,8 +176,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s --wait --overwrite=false", upgradeCmd, manifestDirectory),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
Overwrite: &overwriteIt,
InstallArguments: InstallArguments{
Step: Step{
Expand All @@ -190,8 +190,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s --wait --prune=true", upgradeCmd, manifestDirectory),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
Prune: &pruneIt,
InstallArguments: InstallArguments{
Step: Step{
Expand All @@ -204,8 +204,8 @@ func TestMixin_UpgradeStep(t *testing.T) {
},
{
expectedCommand: fmt.Sprintf("%s %s --wait --timeout=%ds", upgradeCmd, manifestDirectory, timeout),
upgradeStep: UpgradeStep{
UpgradeArguments: UpgradeArguments{
executeStep: ExecuteStep{
ExecuteInstruction: ExecuteInstruction{
Timeout: &timeout,
InstallArguments: InstallArguments{
Step: Step{
Expand All @@ -223,13 +223,13 @@ func TestMixin_UpgradeStep(t *testing.T) {
t.Run(upgradeTest.expectedCommand, func(t *testing.T) {
os.Setenv(test.ExpectedCommandEnv, upgradeTest.expectedCommand)

action := UpgradeAction{Steps: []UpgradeStep{upgradeTest.upgradeStep}}
action := ExecuteAction{Steps: []ExecuteStep{upgradeTest.executeStep}}
b, _ := yaml.Marshal(action)

h := NewTestMixin(t)
h.In = bytes.NewReader(b)

err := h.Upgrade()
err := h.Execute()

require.NoError(t, err)
})
Expand Down
Loading

0 comments on commit 533ed71

Please sign in to comment.