Skip to content

Commit

Permalink
Updated with code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyrickard committed Mar 19, 2019
1 parent 200a461 commit 22a7e40
Show file tree
Hide file tree
Showing 16 changed files with 335 additions and 169 deletions.
2 changes: 1 addition & 1 deletion cmd/kubernetes/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubernetes/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
}
}
2 changes: 1 addition & 1 deletion cmd/kubernetes/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down
7 changes: 3 additions & 4 deletions pkg/kubernetes/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type InstallAction struct {
}

type InstallStep struct {
*InstallArguments `yaml:"kubernetes"`
InstallArguments `yaml:"kubernetes"`
}

type InstallArguments struct {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
58 changes: 38 additions & 20 deletions pkg/kubernetes/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -39,59 +39,77 @@ 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,
},
},
},
{
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,
},
},
},
{
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,
},
},
},
Expand Down
16 changes: 5 additions & 11 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/xeipuuv/gojsonschema"
)

const defaultManifestPath = "/cnab/app/manifests/kubernetes"

type Mixin struct {
*context.Context

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
49 changes: 27 additions & 22 deletions pkg/kubernetes/schema/kubernetes.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@
"minLength": 1
},
"namespace": {
"type": "string",
"minLength": 0
"type": "string"
},
"manifests": {
"type": "array",
"items": {
"type": "string",
"minItems": 0
"minItems": 1
}
},
"record": {
"type": "boolean",
"default":"false"
},
"selector": {
"type": "string",
"minLength": 0
"type": "string"
},
"validate": {
"type": "boolean",
Expand All @@ -44,7 +42,7 @@
},
"additionalProperties": false,
"required": [
"description"
"description", "manifests"
]
}
},
Expand All @@ -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": {
Expand All @@ -110,7 +110,7 @@
},
"additionalProperties": false,
"required": [
"description"
"description", "manifests"
]
}
},
Expand All @@ -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"
},
Expand All @@ -157,7 +159,7 @@
},
"additionalProperties": false,
"required": [
"description"
"description", "manifests"
]
}
},
Expand All @@ -174,6 +176,9 @@
"name": {
"type": "string"
},
"namespace": {
"type": "string"
},
"resourceType": {
"type": "string"
},
Expand Down
Loading

0 comments on commit 22a7e40

Please sign in to comment.