From 73cd8c24b3381596438ef8b5e69e106f699bf5cc Mon Sep 17 00:00:00 2001 From: Ken Sipe Date: Wed, 27 May 2020 07:29:58 -0500 Subject: [PATCH] Plan Update and Trigger with Wait (#1470) Signed-off-by: Ken Sipe --- pkg/kudoctl/cmd/plan.go | 2 ++ pkg/kudoctl/cmd/plan/plan_trigger.go | 5 ++++- pkg/kudoctl/cmd/update.go | 7 ++++++- pkg/kudoctl/util/kudo/kudo.go | 19 +++++++++++++++++-- pkg/kudoctl/util/kudo/kudo_test.go | 2 +- pkg/kudoctl/util/kudo/upgrade.go | 4 ++-- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pkg/kudoctl/cmd/plan.go b/pkg/kudoctl/cmd/plan.go index 178f140bd..3fa2d5539 100644 --- a/pkg/kudoctl/cmd/plan.go +++ b/pkg/kudoctl/cmd/plan.go @@ -91,6 +91,8 @@ func NewPlanTriggerCmd() *cobra.Command { cmd.Flags().StringVar(&options.Instance, "instance", "", "The instance name available from 'kubectl get instances'") cmd.Flags().StringVar(&options.Plan, "name", "", "The plan name") + cmd.Flags().BoolVar(&options.Wait, "wait", false, "Specify if the CLI should wait for the plan to complete before returning (default \"false\")") + cmd.Flags().Int64Var(&options.WaitTime, "wait-time", 300, "Specify the max wait time in seconds for CLI for the plan to complete before returning (default \"300\")") return cmd } diff --git a/pkg/kudoctl/cmd/plan/plan_trigger.go b/pkg/kudoctl/cmd/plan/plan_trigger.go index ebbca67b2..eadfa1cf3 100644 --- a/pkg/kudoctl/cmd/plan/plan_trigger.go +++ b/pkg/kudoctl/cmd/plan/plan_trigger.go @@ -3,6 +3,7 @@ package plan import ( "errors" "fmt" + "time" "github.com/kudobuilder/kudo/pkg/kudoctl/clog" "github.com/kudobuilder/kudo/pkg/kudoctl/env" @@ -11,6 +12,8 @@ import ( type TriggerOptions struct { Plan string Instance string + Wait bool + WaitTime int64 } // RunTrigger triggers a plan execution @@ -27,7 +30,7 @@ func RunTrigger(options *TriggerOptions, settings *env.Settings) error { return fmt.Errorf("creating kudo client: %w", err) } - err = kc.UpdateInstance(options.Instance, settings.Namespace, nil, nil, &options.Plan) + err = kc.UpdateInstance(options.Instance, settings.Namespace, nil, nil, &options.Plan, options.Wait, time.Duration(options.WaitTime)*time.Second) if err == nil { clog.Printf("Triggered %s plan for %s/%s instance", options.Plan, settings.Namespace, options.Instance) } diff --git a/pkg/kudoctl/cmd/update.go b/pkg/kudoctl/cmd/update.go index 50a8d745e..bcd206443 100644 --- a/pkg/kudoctl/cmd/update.go +++ b/pkg/kudoctl/cmd/update.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "time" "github.com/spf13/cobra" @@ -25,6 +26,8 @@ var ( type updateOptions struct { InstanceName string Parameters map[string]string + Wait bool + WaitTime int64 } // defaultOptions initializes the install command options to its defaults @@ -54,6 +57,8 @@ func newUpdateCmd() *cobra.Command { updateCmd.Flags().StringVar(&options.InstanceName, "instance", "", "The instance name.") updateCmd.Flags().StringArrayVarP(¶meters, "parameter", "p", nil, "The parameter name and value separated by '='") updateCmd.Flags().StringArrayVarP(¶meterFiles, "parameter-file", "P", nil, "YAML file with parameters") + updateCmd.Flags().BoolVar(&options.Wait, "wait", false, "Specify if the CLI should wait for the update to complete before returning (default \"false\")") + updateCmd.Flags().Int64Var(&options.WaitTime, "wait-time", 300, "Specify the max wait time in seconds for CLI for the update to complete before returning (default \"300\")") return updateCmd } @@ -98,7 +103,7 @@ func update(instanceToUpdate string, kc *kudo.Client, options *updateOptions, se } // Update arguments - err = kc.UpdateInstance(instanceToUpdate, settings.Namespace, nil, options.Parameters, nil) + err = kc.UpdateInstance(instanceToUpdate, settings.Namespace, nil, options.Parameters, nil, options.Wait, time.Duration(options.WaitTime)*time.Second) if err != nil { return fmt.Errorf("updating instance %s %w", instanceToUpdate, err) } diff --git a/pkg/kudoctl/util/kudo/kudo.go b/pkg/kudoctl/util/kudo/kudo.go index 8cacf2375..3a4b4f89b 100644 --- a/pkg/kudoctl/util/kudo/kudo.go +++ b/pkg/kudoctl/util/kudo/kudo.go @@ -188,7 +188,16 @@ func (c *Client) GetOperatorVersion(name, namespace string) (*v1beta1.OperatorVe } // UpdateInstance updates operatorversion on instance -func (c *Client) UpdateInstance(instanceName, namespace string, operatorVersion *string, parameters map[string]string, triggeredPlan *string) error { +func (c *Client) UpdateInstance(instanceName, namespace string, operatorVersion *string, parameters map[string]string, triggeredPlan *string, wait bool, waitTime time.Duration) error { + var oldInstance *v1beta1.Instance + if wait { + var err error + oldInstance, err = c.GetInstance(instanceName, namespace) + if err != nil { + return err + } + } + instanceSpec := v1beta1.InstanceSpec{} // 1. new OperatorVersion if operatorVersion != nil { @@ -216,7 +225,13 @@ func (c *Client) UpdateInstance(instanceName, namespace string, operatorVersion return err } _, err = c.kudoClientset.KudoV1beta1().Instances(namespace).Patch(instanceName, types.MergePatchType, serializedPatch) - return err + if err != nil { + return err + } + if !wait { + return nil + } + return c.WaitForInstance(instanceName, namespace, oldInstance, waitTime) } // WaitForInstance waits for instance to be "complete". diff --git a/pkg/kudoctl/util/kudo/kudo_test.go b/pkg/kudoctl/util/kudo/kudo_test.go index 2c0498242..9ddb68b8e 100644 --- a/pkg/kudoctl/util/kudo/kudo_test.go +++ b/pkg/kudoctl/util/kudo/kudo_test.go @@ -516,7 +516,7 @@ func TestKudoClient_UpdateOperatorVersion(t *testing.T) { t.Errorf("Error creating operator version in tests setup for %s", tt.name) } - err = k2o.UpdateInstance(testInstance.Name, installNamespace, tt.patchToVersion, tt.parametersToPatch, nil) + err = k2o.UpdateInstance(testInstance.Name, installNamespace, tt.patchToVersion, tt.parametersToPatch, nil, false, 0) instance, _ := k2o.GetInstance(testInstance.Name, installNamespace) if tt.patchToVersion != nil { if err != nil || instance.Spec.OperatorVersion.Name != convert.StringValue(tt.patchToVersion) { diff --git a/pkg/kudoctl/util/kudo/upgrade.go b/pkg/kudoctl/util/kudo/upgrade.go index b6031fb89..a2deb7c7e 100644 --- a/pkg/kudoctl/util/kudo/upgrade.go +++ b/pkg/kudoctl/util/kudo/upgrade.go @@ -53,8 +53,8 @@ func UpgradeOperatorVersion(kc *Client, newOv *v1beta1.OperatorVersion, instance clog.Printf("operatorversion.%s/%s created", newOv.APIVersion, newOv.Name) } - if err = kc.UpdateInstance(instanceName, namespace, convert.StringPtr(newOv.Name), parameters, nil); err != nil { - return fmt.Errorf("failed to update instance for new OperatorVersion %s: %v", newOv.Name, err) + if err = kc.UpdateInstance(instanceName, namespace, convert.StringPtr(newOv.Name), parameters, nil, false, 0); err != nil { + return fmt.Errorf("failed to update instance for new OperatorVersion %s", newOv.Name) } clog.Printf("instance.%s/%s updated", instance.APIVersion, instanceName)