diff --git a/cmd/kubernetes/install.go b/cmd/kubernetes/install.go index 1ca7c679c7..529d8ea479 100644 --- a/cmd/kubernetes/install.go +++ b/cmd/kubernetes/install.go @@ -8,7 +8,7 @@ import ( func buildInstallCommand(mixin *kubernetes.Mixin) *cobra.Command { return &cobra.Command{ Use: "install", - Short: "Use kubectl to apply manifests to cluster", + Short: "Use kubectl to apply manifests to a cluster", RunE: func(cmd *cobra.Command, args []string) error { return mixin.Install() }, diff --git a/cmd/kubernetes/main.go b/cmd/kubernetes/main.go index 1fa49a2c52..d6fa5f5651 100644 --- a/cmd/kubernetes/main.go +++ b/cmd/kubernetes/main.go @@ -22,11 +22,12 @@ func buildRootCommand(in io.Reader) *cobra.Command { mixin.In = in cmd := &cobra.Command{ Use: "kubernetes", - Long: "kuberetes is a porter 👩🏽‍✈️ mixin that you can you can use to leverage kubernetes manifests", + Long: "kuberetes is a porter 👩🏽‍✈️ mixin that you can you can use to apply kubernetes manifests in your bundle", PersistentPreRun: func(cmd *cobra.Command, args []string) { mixin.Out = cmd.OutOrStdout() mixin.Err = cmd.OutOrStderr() }, + SilenceUsage: true, } cmd.PersistentFlags().BoolVar(&mixin.Debug, "debug", false, "Enable debug logging") diff --git a/cmd/kubernetes/uninstall.go b/cmd/kubernetes/uninstall.go index f5769c7270..509bb46cdd 100644 --- a/cmd/kubernetes/uninstall.go +++ b/cmd/kubernetes/uninstall.go @@ -8,9 +8,9 @@ import ( func buildUnInstallCommand(mixin *kubernetes.Mixin) *cobra.Command { return &cobra.Command{ Use: "uninstall", - Short: "Use kubectl to delete manifests from cluster", + Short: "Use kubectl to delete resources contained in a manifest from a cluster", RunE: func(cmd *cobra.Command, args []string) error { - return mixin.UnInstall() + return mixin.Uninstall() }, } } diff --git a/cmd/kubernetes/upgrade.go b/cmd/kubernetes/upgrade.go index 13908b3047..98553ff50f 100644 --- a/cmd/kubernetes/upgrade.go +++ b/cmd/kubernetes/upgrade.go @@ -8,7 +8,7 @@ import ( func buildUpgradeCommand(mixin *kubernetes.Mixin) *cobra.Command { return &cobra.Command{ Use: "Upgrade", - Short: "Use kubectl to apply manifests to cluster", + Short: "Use kubectl to apply manifests to a cluster", RunE: func(cmd *cobra.Command, args []string) error { return mixin.Upgrade() }, diff --git a/pkg/kubernetes/install.go b/pkg/kubernetes/install.go index 2487ed1690..f2f30cd6cc 100644 --- a/pkg/kubernetes/install.go +++ b/pkg/kubernetes/install.go @@ -15,7 +15,7 @@ type InstallAction struct { } type InstallStep struct { - *InstallArguments `yaml:"kubernetes"` + InstallArguments `yaml:"kubernetes"` } type InstallArguments struct { @@ -46,9 +46,8 @@ func (m *Mixin) Install() error { step := action.Steps[0] var commands []*exec.Cmd - manifests := m.resolveManifests(step.Manifests) - for _, manifestPath := range manifests { + for _, manifestPath := range step.Manifests { commandPayload, err := m.buildInstallCommand(step.InstallArguments, manifestPath) if err != nil { return err @@ -87,7 +86,7 @@ func (m *Mixin) getInstallStep(payload []byte) (*InstallStep, error) { return &step, nil } -func (m *Mixin) buildInstallCommand(step *InstallArguments, manifestPath string) ([]string, error) { +func (m *Mixin) buildInstallCommand(step InstallArguments, manifestPath string) ([]string, error) { command := []string{"apply", "-f", manifestPath} if step.Namespace != "" { command = append(command, "-n", step.Namespace) diff --git a/pkg/kubernetes/install_test.go b/pkg/kubernetes/install_test.go index 1c15fec7f9..7358ade65a 100644 --- a/pkg/kubernetes/install_test.go +++ b/pkg/kubernetes/install_test.go @@ -23,7 +23,7 @@ func TestMain(m *testing.M) { func TestMixin_InstallStep(t *testing.T) { - manifestDirectory := "/cnab/app/manifesto" + manifestDirectory := "/cnab/app/manifests" installCmd := "kubectl apply -f" @@ -39,38 +39,48 @@ func TestMixin_InstallStep(t *testing.T) { { expectedCommand: fmt.Sprintf("%s %s --wait", installCmd, manifestDirectory), installStep: InstallStep{ - InstallArguments: &InstallArguments{ + + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, Manifests: []string{manifestDirectory}, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait", installCmd, defaultManifestPath), - installStep: InstallStep{ - InstallArguments: &InstallArguments{}, - }, - }, - { - expectedCommand: fmt.Sprintf("%s %s", installCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s", installCmd, manifestDirectory), installStep: InstallStep{ - InstallArguments: &InstallArguments{ - Wait: &dontWait, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Wait: &dontWait, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s", installCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s", installCmd, manifestDirectory, namespace), installStep: InstallStep{ - InstallArguments: &InstallArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Wait: &dontWait, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s --validate=false", installCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s --validate=false", installCmd, manifestDirectory, namespace), installStep: InstallStep{ - InstallArguments: &InstallArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Validate: &validateIt, Wait: &dontWait, @@ -78,9 +88,13 @@ func TestMixin_InstallStep(t *testing.T) { }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s --record=true", installCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s --record=true", installCmd, manifestDirectory, namespace), installStep: InstallStep{ - InstallArguments: &InstallArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Record: &recordIt, Wait: &dontWait, @@ -88,10 +102,14 @@ func TestMixin_InstallStep(t *testing.T) { }, }, { - expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", installCmd, defaultManifestPath, selector), + expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", installCmd, manifestDirectory, selector), installStep: InstallStep{ - InstallArguments: &InstallArguments{ - Selector: selector, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Selector: selector, }, }, }, diff --git a/pkg/kubernetes/kubernetes.go b/pkg/kubernetes/kubernetes.go index f616a4e0df..b1257b3089 100644 --- a/pkg/kubernetes/kubernetes.go +++ b/pkg/kubernetes/kubernetes.go @@ -16,8 +16,6 @@ import ( "github.com/xeipuuv/gojsonschema" ) -const defaultManifestPath = "/cnab/app/manifests/kubernetes" - type Mixin struct { *context.Context @@ -46,7 +44,11 @@ func (m *Mixin) getCommandFile(commandFile string, w io.Writer) ([]byte, error) func (m *Mixin) getPayloadData() ([]byte, error) { reader := bufio.NewReader(m.In) data, err := ioutil.ReadAll(reader) - return data, errors.Wrap(err, "could not read payload from STDIN") + if err != nil { + errors.Wrap(err, "could not read payload from STDIN") + } + err = m.ValidatePayload(data) + return data, errors.Wrap(err, "could not validate payload") } func (m *Mixin) ValidatePayload(b []byte) error { @@ -86,14 +88,6 @@ func (m *Mixin) ValidatePayload(b []byte) error { return nil } -// If no manifest is specified, update the empty slice to include the default path -func (m *Mixin) resolveManifests(manifests []string) []string { - if len(manifests) == 0 { - return append(manifests, defaultManifestPath) - } - return manifests -} - func (m *Mixin) getOutput(resourceType, resourceName, namespace, jsonPath string) (string, error) { args := []string{"get", resourceType, resourceName} args = append(args, fmt.Sprintf("-o=jsonpath='%s'", jsonPath)) diff --git a/pkg/kubernetes/schema/kubernetes.json b/pkg/kubernetes/schema/kubernetes.json index b7fa163563..3c6fb3e3a7 100644 --- a/pkg/kubernetes/schema/kubernetes.json +++ b/pkg/kubernetes/schema/kubernetes.json @@ -12,14 +12,13 @@ "minLength": 1 }, "namespace": { - "type": "string", - "minLength": 0 + "type": "string" }, "manifests": { "type": "array", "items": { "type": "string", - "minItems": 0 + "minItems": 1 } }, "record": { @@ -27,8 +26,7 @@ "default":"false" }, "selector": { - "type": "string", - "minLength": 0 + "type": "string" }, "validate": { "type": "boolean", @@ -44,7 +42,7 @@ }, "additionalProperties": false, "required": [ - "description" + "description", "manifests" ] } }, @@ -64,44 +62,46 @@ "minLength": 1 }, "namespace": { - "type": "string", - "minLength": 0 + "type": "string" }, "manifests": { "type": "array", "items": { "type": "string", - "minItems": 0 + "minItems": 1 } }, "force": { - "type": "boolean", + "type": ["boolean","null"], "default":"false" }, "gracePeriod" : { "type": "integer" }, "overwrite": { - "type": "boolean", + "type": ["boolean","null"], + "default":"true" + }, + "prune": { + "type": ["boolean","null"], "default":"true" }, "record": { - "type": "boolean", + "type": ["boolean","null"], "default":"false" }, "selector": { - "type": "string", - "minLength": 0 + "type": "string" }, "timeout" : { "type": "integer" }, "validate": { - "type": "boolean", + "type": ["boolean","null"], "default":"true" }, "wait": { - "type": "boolean", + "type": ["boolean","null"], "default":"true" }, "outputs": { @@ -110,7 +110,7 @@ }, "additionalProperties": false, "required": [ - "description" + "description", "manifests" ] } }, @@ -130,23 +130,25 @@ "minLength": 1 }, "namespace": { - "type": "string", - "minLength": 0 + "type": "string" }, "manifests": { "type": "array", "items": { "type": "string", - "minItems": 0 + "minItems": 1 } }, "force": { - "type": "boolean", + "type": ["boolean","null"], "default":"false" }, "gracePeriod" : { "type": "integer" }, + "selector": { + "type": "string" + }, "timeout" : { "type": "integer" }, @@ -157,7 +159,7 @@ }, "additionalProperties": false, "required": [ - "description" + "description", "manifests" ] } }, @@ -174,6 +176,9 @@ "name": { "type": "string" }, + "namespace": { + "type": "string" + }, "resourceType": { "type": "string" }, diff --git a/pkg/kubernetes/schema_test.go b/pkg/kubernetes/schema_test.go index 2e156b04b7..6a0556dbb7 100644 --- a/pkg/kubernetes/schema_test.go +++ b/pkg/kubernetes/schema_test.go @@ -1,11 +1,17 @@ package kubernetes import ( + //"bytes" + //"fmt" "io/ioutil" + //"os" "testing" + //"github.com/deislabs/porter/pkg/test" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + //yaml "gopkg.in/yaml.v2" ) func TestMixin_PrintSchema(t *testing.T) { @@ -32,8 +38,8 @@ func TestMixin_ValidatePayload(t *testing.T) { {"install", "testdata/install-input.yaml", true, ""}, {"upgrade", "testdata/upgrade-input.yaml", true, ""}, {"uninstall", "testdata/uninstall-input.yaml", true, ""}, - {"install-default-manifest-ok", "testdata/install-input-no-manifests.yaml", true, ""}, {"install-bad-wait-flag", "testdata/install-input-bad-wait-flag.yaml", false, "install.0.kubernetes.wait: Invalid type. Expected: boolean, given: string"}, + {"install-no-manifests", "testdata/install-input-no-manifests.yaml", false, "install.0.kubernetes: manifests is required"}, {"install-bad-outputs", "testdata/install-input-bad-outputs.yaml", false, "install.0.kubernetes.outputs.0: resourceType is required\n\t* install.0.kubernetes.outputs.0: resourceName is required\n\t* install.0.kubernetes.outputs.0: jsonPath is required"}, } @@ -52,3 +58,71 @@ func TestMixin_ValidatePayload(t *testing.T) { }) } } + +// TODO: Currently can't test this with pkg/test because it runs multiple commands +// func TestOuputSchema(t *testing.T) { +// manifestDirectory := "/cnab/app/manifests" + +// installCmd := "kubectl apply -f" + +// installTests := []InstallTest{ +// { +// expectedCommand: fmt.Sprintf("%s %s --wait", installCmd, manifestDirectory), +// installStep: InstallStep{ + +// InstallArguments: InstallArguments{ +// Step: Step{ +// Description: "Hello", +// Outputs: []KubernetesOutput{ +// KubernetesOutput{ +// Name: "test", +// Namespace: "Default", +// ResourceType: "service", +// ResourceName: "aservice", +// JSONPath: "a.path.", +// }, +// }, +// }, +// Manifests: []string{manifestDirectory}, +// }, +// }, +// }, +// { +// expectedCommand: fmt.Sprintf("%s %s --wait", installCmd, manifestDirectory), +// installStep: InstallStep{ + +// InstallArguments: InstallArguments{ +// Step: Step{ +// Description: "Hello", +// Outputs: []KubernetesOutput{ +// KubernetesOutput{ +// Name: "test", +// ResourceType: "service", +// ResourceName: "aservice", +// JSONPath: "a.path.", +// }, +// }, +// }, +// Manifests: []string{manifestDirectory}, +// }, +// }, +// }, +// } + +// defer os.Unsetenv(test.ExpectedCommandEnv) +// for _, installTest := range installTests { +// t.Run(installTest.expectedCommand, func(t *testing.T) { +// os.Setenv(test.ExpectedCommandEnv, installTest.expectedCommand) + +// action := InstallAction{Steps: []InstallStep{installTest.installStep}} +// b, _ := yaml.Marshal(action) + +// h := NewTestMixin(t) +// h.In = bytes.NewReader(b) + +// err := h.Install() + +// require.NoError(t, err) +// }) +// } +// } diff --git a/pkg/kubernetes/step.go b/pkg/kubernetes/step.go index a60e25e287..50c8b9495b 100644 --- a/pkg/kubernetes/step.go +++ b/pkg/kubernetes/step.go @@ -2,13 +2,13 @@ package kubernetes type Step struct { Description string `yaml:"description"` - Outputs []KubernetesOutput `yaml:"outputs"` + Outputs []KubernetesOutput `yaml:"outputs,omitempty"` } type KubernetesOutput struct { Name string `yaml:"name"` ResourceType string `yaml:"resourceType"` ResourceName string `yaml:"resourceName"` - Namespace string `yaml:"namespace",omitempty` + Namespace string `yaml:"namespace,omitempty"` JSONPath string `yaml:"jsonPath"` } diff --git a/pkg/kubernetes/testdata/install-input-no-manifests.yaml b/pkg/kubernetes/testdata/install-input-no-manifests.yaml index 5629432014..af963a0ef8 100644 --- a/pkg/kubernetes/testdata/install-input-no-manifests.yaml +++ b/pkg/kubernetes/testdata/install-input-no-manifests.yaml @@ -1,6 +1,4 @@ install: - kubernetes: description: "Install Hello World App" - manifests: - - /cnab/app/manifests/hello - wait: true + wait: true \ No newline at end of file diff --git a/pkg/kubernetes/testdata/schema.json b/pkg/kubernetes/testdata/schema.json index b7fa163563..3c6fb3e3a7 100644 --- a/pkg/kubernetes/testdata/schema.json +++ b/pkg/kubernetes/testdata/schema.json @@ -12,14 +12,13 @@ "minLength": 1 }, "namespace": { - "type": "string", - "minLength": 0 + "type": "string" }, "manifests": { "type": "array", "items": { "type": "string", - "minItems": 0 + "minItems": 1 } }, "record": { @@ -27,8 +26,7 @@ "default":"false" }, "selector": { - "type": "string", - "minLength": 0 + "type": "string" }, "validate": { "type": "boolean", @@ -44,7 +42,7 @@ }, "additionalProperties": false, "required": [ - "description" + "description", "manifests" ] } }, @@ -64,44 +62,46 @@ "minLength": 1 }, "namespace": { - "type": "string", - "minLength": 0 + "type": "string" }, "manifests": { "type": "array", "items": { "type": "string", - "minItems": 0 + "minItems": 1 } }, "force": { - "type": "boolean", + "type": ["boolean","null"], "default":"false" }, "gracePeriod" : { "type": "integer" }, "overwrite": { - "type": "boolean", + "type": ["boolean","null"], + "default":"true" + }, + "prune": { + "type": ["boolean","null"], "default":"true" }, "record": { - "type": "boolean", + "type": ["boolean","null"], "default":"false" }, "selector": { - "type": "string", - "minLength": 0 + "type": "string" }, "timeout" : { "type": "integer" }, "validate": { - "type": "boolean", + "type": ["boolean","null"], "default":"true" }, "wait": { - "type": "boolean", + "type": ["boolean","null"], "default":"true" }, "outputs": { @@ -110,7 +110,7 @@ }, "additionalProperties": false, "required": [ - "description" + "description", "manifests" ] } }, @@ -130,23 +130,25 @@ "minLength": 1 }, "namespace": { - "type": "string", - "minLength": 0 + "type": "string" }, "manifests": { "type": "array", "items": { "type": "string", - "minItems": 0 + "minItems": 1 } }, "force": { - "type": "boolean", + "type": ["boolean","null"], "default":"false" }, "gracePeriod" : { "type": "integer" }, + "selector": { + "type": "string" + }, "timeout" : { "type": "integer" }, @@ -157,7 +159,7 @@ }, "additionalProperties": false, "required": [ - "description" + "description", "manifests" ] } }, @@ -174,6 +176,9 @@ "name": { "type": "string" }, + "namespace": { + "type": "string" + }, "resourceType": { "type": "string" }, diff --git a/pkg/kubernetes/uninstall.go b/pkg/kubernetes/uninstall.go index 56d268386b..9e3498e1f0 100644 --- a/pkg/kubernetes/uninstall.go +++ b/pkg/kubernetes/uninstall.go @@ -15,7 +15,7 @@ type UninstallAction struct { } type UninstallStep struct { - *UninstallArguments `yaml:"kubernetes"` + UninstallArguments `yaml:"kubernetes"` } type UninstallArguments struct { @@ -31,8 +31,8 @@ type UninstallArguments struct { Wait *bool `yaml:"wait,omitempty"` } -// UnInstall will delete anything created during the install or upgrade step -func (m *Mixin) UnInstall() error { +// Uninstall will delete anything created during the install or upgrade step +func (m *Mixin) Uninstall() error { payload, err := m.getPayloadData() if err != nil { return err @@ -50,9 +50,8 @@ func (m *Mixin) UnInstall() error { step := action.Steps[0] var commands []*exec.Cmd - manifests := m.resolveManifests(step.Manifests) - for _, manifestPath := range manifests { + for _, manifestPath := range step.Manifests { commandPayload, err := m.buildUninstallCommand(step.UninstallArguments, manifestPath) if err != nil { return err @@ -78,7 +77,7 @@ func (m *Mixin) UnInstall() error { return nil } -func (m *Mixin) buildUninstallCommand(args *UninstallArguments, manifestPath string) ([]string, error) { +func (m *Mixin) buildUninstallCommand(args UninstallArguments, manifestPath string) ([]string, error) { command := []string{"delete", "-f", manifestPath} if args.Namespace != "" { command = append(command, "-n", args.Namespace) diff --git a/pkg/kubernetes/uninstall_test.go b/pkg/kubernetes/uninstall_test.go index cc07937463..6db7d3b0c6 100644 --- a/pkg/kubernetes/uninstall_test.go +++ b/pkg/kubernetes/uninstall_test.go @@ -19,7 +19,7 @@ type UnInstallTest struct { func TestMixin_UninstallStep(t *testing.T) { - manifestDirectory := "/cnab/app/manifesto" + manifestDirectory := "/cnab/app/manifests" deleteCmd := "kubectl delete -f" @@ -37,63 +37,84 @@ func TestMixin_UninstallStep(t *testing.T) { { expectedCommand: fmt.Sprintf("%s %s --wait", deleteCmd, manifestDirectory), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, Manifests: []string{manifestDirectory}, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait", deleteCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s", deleteCmd, manifestDirectory), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{}, - }, - }, - { - expectedCommand: fmt.Sprintf("%s %s", deleteCmd, defaultManifestPath), - uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ - Wait: &dontWait, + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Wait: &dontWait, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s", deleteCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s", deleteCmd, manifestDirectory, namespace), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Wait: &dontWait, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", deleteCmd, defaultManifestPath, selector), + expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", deleteCmd, manifestDirectory, selector), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ - Selector: selector, + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Selector: selector, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --force --grace-period=0 --wait", deleteCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s --force --grace-period=0 --wait", deleteCmd, manifestDirectory), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ - Force: &forceIt, + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Force: &forceIt, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --grace-period=%d --wait", deleteCmd, defaultManifestPath, withGrace), + expectedCommand: fmt.Sprintf("%s %s --grace-period=%d --wait", deleteCmd, manifestDirectory, withGrace), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, GracePeriod: &withGrace, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --timeout=%ds --wait", deleteCmd, defaultManifestPath, timeout), + expectedCommand: fmt.Sprintf("%s %s --timeout=%ds --wait", deleteCmd, manifestDirectory, timeout), uninstallStep: UninstallStep{ - UninstallArguments: &UninstallArguments{ - Timeout: &timeout, + UninstallArguments: UninstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Timeout: &timeout, }, }, }, @@ -110,7 +131,7 @@ func TestMixin_UninstallStep(t *testing.T) { h := NewTestMixin(t) h.In = bytes.NewReader(b) - err := h.UnInstall() + err := h.Uninstall() require.NoError(t, err) }) diff --git a/pkg/kubernetes/upgrade.go b/pkg/kubernetes/upgrade.go index 8256728f7b..3ad41abd09 100644 --- a/pkg/kubernetes/upgrade.go +++ b/pkg/kubernetes/upgrade.go @@ -15,11 +15,11 @@ type UpgradeAction struct { } type UpgradeStep struct { - *UpgradeArguments `yaml:"kubernetes"` + UpgradeArguments `yaml:"kubernetes"` } type UpgradeArguments struct { - *InstallArguments + InstallArguments `yaml:",inline"` // Upgrade specific arguments Force *bool `yaml:"force,omitempty"` @@ -49,9 +49,8 @@ func (m *Mixin) Upgrade() error { step := action.Steps[0] var commands []*exec.Cmd - manifests := m.resolveManifests(step.Manifests) - for _, manifestPath := range manifests { + for _, manifestPath := range step.Manifests { commandPayload, err := m.buildUpgradeCommand(step.UpgradeArguments, manifestPath) if err != nil { return err @@ -80,7 +79,7 @@ func (m *Mixin) Upgrade() error { return err } -func (m *Mixin) buildUpgradeCommand(args *UpgradeArguments, manifestPath string) ([]string, error) { +func (m *Mixin) buildUpgradeCommand(args UpgradeArguments, manifestPath string) ([]string, error) { command, err := m.buildInstallCommand(args.InstallArguments, manifestPath) if err != nil { return nil, errors.Wrap(err, "unable to create upgrade command") diff --git a/pkg/kubernetes/upgrade_test.go b/pkg/kubernetes/upgrade_test.go index 1de3c68adc..002e3b586b 100644 --- a/pkg/kubernetes/upgrade_test.go +++ b/pkg/kubernetes/upgrade_test.go @@ -19,7 +19,7 @@ type UpgradeTest struct { func TestMixin_UpgradeStep(t *testing.T) { - manifestDirectory := "/cnab/app/manifesto" + manifestDirectory := "/cnab/app/manifests" upgradeCmd := "kubectl apply -f" @@ -46,36 +46,52 @@ func TestMixin_UpgradeStep(t *testing.T) { { expectedCommand: fmt.Sprintf("%s %s --wait", upgradeCmd, manifestDirectory), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{ + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, Manifests: []string{manifestDirectory}, }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait", upgradeCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s --wait", upgradeCmd, manifestDirectory), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{}, + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s", upgradeCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s", upgradeCmd, manifestDirectory), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{ - Wait: &dontWait, + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Wait: &dontWait, }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s", upgradeCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s", upgradeCmd, manifestDirectory, namespace), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{ + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Wait: &dontWait, }, @@ -83,10 +99,14 @@ func TestMixin_UpgradeStep(t *testing.T) { }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s --validate=false", upgradeCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s --validate=false", upgradeCmd, manifestDirectory, namespace), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{ + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Validate: &validateIt, Wait: &dontWait, @@ -95,10 +115,14 @@ func TestMixin_UpgradeStep(t *testing.T) { }, }, { - expectedCommand: fmt.Sprintf("%s %s -n %s --record=true", upgradeCmd, defaultManifestPath, namespace), + expectedCommand: fmt.Sprintf("%s %s -n %s --record=true", upgradeCmd, manifestDirectory, namespace), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{ + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, Namespace: namespace, Record: &recordIt, Wait: &dontWait, @@ -107,11 +131,15 @@ func TestMixin_UpgradeStep(t *testing.T) { }, }, { - expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", upgradeCmd, defaultManifestPath, selector), + expectedCommand: fmt.Sprintf("%s %s --selector=%s --wait", upgradeCmd, manifestDirectory, selector), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - InstallArguments: &InstallArguments{ - Selector: selector, + UpgradeArguments: UpgradeArguments{ + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + Selector: selector, }, }, }, @@ -119,47 +147,72 @@ func TestMixin_UpgradeStep(t *testing.T) { // These tests exercise the upgrade options { - expectedCommand: fmt.Sprintf("%s %s --wait --force --grace-period=0", upgradeCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s --wait --force --grace-period=0", upgradeCmd, manifestDirectory), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - Force: &forceIt, - InstallArguments: &InstallArguments{}, + UpgradeArguments: UpgradeArguments{ + Force: &forceIt, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait --grace-period=%d", upgradeCmd, defaultManifestPath, withGrace), + expectedCommand: fmt.Sprintf("%s %s --wait --grace-period=%d", upgradeCmd, manifestDirectory, withGrace), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - GracePeriod: &withGrace, - InstallArguments: &InstallArguments{}, + UpgradeArguments: UpgradeArguments{ + GracePeriod: &withGrace, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "Hello", + }, + Manifests: []string{manifestDirectory}, + }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait --overwrite=false", upgradeCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s --wait --overwrite=false", upgradeCmd, manifestDirectory), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - Overwrite: &overwriteIt, - InstallArguments: &InstallArguments{}, + UpgradeArguments: UpgradeArguments{ + Overwrite: &overwriteIt, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "upgrade", + }, + Manifests: []string{manifestDirectory}, + }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait --prune=true", upgradeCmd, defaultManifestPath), + expectedCommand: fmt.Sprintf("%s %s --wait --prune=true", upgradeCmd, manifestDirectory), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - Prune: &pruneIt, - InstallArguments: &InstallArguments{}, + UpgradeArguments: UpgradeArguments{ + Prune: &pruneIt, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "upgrade", + }, + Manifests: []string{manifestDirectory}, + }, }, }, }, { - expectedCommand: fmt.Sprintf("%s %s --wait --timeout=%ds", upgradeCmd, defaultManifestPath, timeout), + expectedCommand: fmt.Sprintf("%s %s --wait --timeout=%ds", upgradeCmd, manifestDirectory, timeout), upgradeStep: UpgradeStep{ - UpgradeArguments: &UpgradeArguments{ - Timeout: &timeout, - InstallArguments: &InstallArguments{}, + UpgradeArguments: UpgradeArguments{ + Timeout: &timeout, + InstallArguments: InstallArguments{ + Step: Step{ + Description: "upgrade", + }, + Manifests: []string{manifestDirectory}, + }, }, }, },