Skip to content

Commit

Permalink
Plan Update and Trigger with Wait (#1470)
Browse files Browse the repository at this point in the history
Signed-off-by: Ken Sipe <kensipe@gmail.com>
  • Loading branch information
kensipe committed May 27, 2020
1 parent f3e6f9d commit 73cd8c2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/kudoctl/cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion pkg/kudoctl/cmd/plan/plan_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plan
import (
"errors"
"fmt"
"time"

"github.com/kudobuilder/kudo/pkg/kudoctl/clog"
"github.com/kudobuilder/kudo/pkg/kudoctl/env"
Expand All @@ -11,6 +12,8 @@ import (
type TriggerOptions struct {
Plan string
Instance string
Wait bool
WaitTime int64
}

// RunTrigger triggers a plan execution
Expand All @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/kudoctl/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"time"

"github.com/spf13/cobra"

Expand All @@ -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
Expand Down Expand Up @@ -54,6 +57,8 @@ 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().StringArrayVarP(&parameterFiles, "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
}
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/kudoctl/util/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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".
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/util/kudo/kudo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kudoctl/util/kudo/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 73cd8c2

Please sign in to comment.