Skip to content

Commit

Permalink
Unify feel of install, upgrade and update (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz committed Aug 2, 2019
1 parent 3c3346a commit fba47ba
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
25 changes: 15 additions & 10 deletions pkg/kudoctl/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import (

var (
updateExample = `
The update argument must be a name of the instance.
The update does not accept any arguments.
# Update dev-flink instance with setting parameter param with value value
kubectl kudo update dev-flink -p param=value
kubectl kudo update --instance dev-flink -p param=value
# Update dev-flink instance in namespace services with setting parameter param with value value
kubectl kudo update dev-flink -n services -p param=value`
kubectl kudo update --instance dev-flink -n services -p param=value`
)

type updateOptions struct {
Namespace string
Parameters map[string]string
InstanceName string
Namespace string
Parameters map[string]string
}

// defaultOptions initializes the install command options to its defaults
Expand All @@ -36,7 +37,7 @@ func newUpdateCmd() *cobra.Command {
options := defaultUpdateOptions
var parameters []string
updateCmd := &cobra.Command{
Use: "update <instance-name>",
Use: "update",
Short: "Update installed KUDO operator.",
Long: `Update installed KUDO operator with new parameters.`,
Example: updateExample,
Expand All @@ -51,17 +52,21 @@ func newUpdateCmd() *cobra.Command {
},
}

updateCmd.Flags().StringVar(&options.InstanceName, "instance", "", "The instance name.")
updateCmd.Flags().StringArrayVarP(&parameters, "parameter", "p", nil, "The parameter name and value separated by '='")
updateCmd.Flags().StringVar(&options.Namespace, "namespace", defaultOptions.Namespace, "The namespace where the instance you want to upgrade is installed in.")
return updateCmd
}

func validateUpdateCmd(args []string, options *updateOptions) error {
if len(args) != 1 {
return errors.New("expecting exactly one argument - name of the instance installed in your cluster")
if len(args) != 0 {
return errors.New("expecting no arguments provided for update. Only named flags are accepted")
}
if options.InstanceName == "" {
return errors.New("--instance flag has to be provided to indicate which instance you want to update")
}
if len(options.Parameters) == 0 {
return errors.New("Need to specify at least one parameter to override via -p otherwise there is nothing to update")
return errors.New("need to specify at least one parameter to override via -p otherwise there is nothing to update")
}

return nil
Expand All @@ -72,7 +77,7 @@ func runUpdate(args []string, options *updateOptions) error {
if err != nil {
return err
}
instanceToUpdate := args[0]
instanceToUpdate := options.InstanceName

kc, err := kudo.NewClient(options.Namespace, viper.GetString("kubeconfig"))
if err != nil {
Expand Down
20 changes: 12 additions & 8 deletions pkg/kudoctl/cmd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (

func TestUpdateCommand_Validation(t *testing.T) {
tests := []struct {
name string
args []string
parameters map[string]string
err string
name string
args []string
instanceName string
parameters map[string]string
err string
}{
{"no argument", []string{}, map[string]string{"param": "value"}, "expecting exactly one argument - name of the instance installed in your cluster"},
{"too many arguments", []string{"aaa", "bbb"}, map[string]string{"param": "value"}, "expecting exactly one argument - name of the instance installed in your cluster"},
{"no instance name", []string{"arg"}, map[string]string{}, "Need to specify at least one parameter to override via -p otherwise there is nothing to update"},
{"too many arguments", []string{"aaa"}, "instance", map[string]string{"param": "value"}, "expecting no arguments provided"},
{"no instance name", []string{}, "", map[string]string{}, "--instance flag has to be provided"},
{"no parameter", []string{}, "instance", map[string]string{}, "need to specify at least one parameter to override "},
}

for _, tt := range tests {
Expand All @@ -28,8 +29,11 @@ func TestUpdateCommand_Validation(t *testing.T) {
for _, v := range tt.parameters {
cmd.Flags().Set("p", v)
}
if tt.instanceName != "" {
cmd.Flags().Set("instance", tt.instanceName)
}
_, err := cmd.ExecuteC()
if err.Error() != tt.err {
if !strings.Contains(err.Error(), tt.err) {
t.Errorf("%s: expecting error %s got %v", tt.name, tt.err, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/update-command/01-update.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: kudo.dev/v1alpha1
kind: TestStep
kubectl:
- kudo update update-command-instance -p "param=value"
- kudo update --instance update-command-instance -p "param=value"
2 changes: 1 addition & 1 deletion test/integration/update-command/02-add-new-param.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: kudo.dev/v1alpha1
kind: TestStep
kubectl:
- kudo update update-command-instance -p "param2=value2"
- kudo update --instance update-command-instance -p "param2=value2"
2 changes: 1 addition & 1 deletion test/integration/update-command/03-update-param-value.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: kudo.dev/v1alpha1
kind: TestStep
kubectl:
- kudo update update-command-instance -p "param2=othervalue"
- kudo update --instance update-command-instance -p "param2=othervalue"

0 comments on commit fba47ba

Please sign in to comment.