Skip to content

Commit

Permalink
Add timeout flag to apply, destroy, & status cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
karlkfi committed Oct 16, 2021
1 parent 16eff57 commit 066fd77
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
17 changes: 16 additions & 1 deletion cmd/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func GetApplyRunner(factory cmdutil.Factory, invFactory inventory.InventoryClien
cmd.Flags().StringVar(&r.inventoryPolicy, flagutils.InventoryPolicyFlag, flagutils.InventoryPolicyStrict,
"It determines the behavior when the resources don't belong to current inventory. Available options "+
fmt.Sprintf("%q, %q and %q.", flagutils.InventoryPolicyStrict, flagutils.InventoryPolicyAdopt, flagutils.InventoryPolicyForceAdopt))
cmd.Flags().DurationVar(&r.timeout, "timeout", 0,
"How long to wait before exiting")

r.Command = cmd
return r
Expand All @@ -85,9 +87,22 @@ type ApplyRunner struct {
prunePropagationPolicy string
pruneTimeout time.Duration
inventoryPolicy string
timeout time.Duration
}

func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
// If specified, use command context.
ctx := cmd.Context()
if ctx == nil {
ctx = context.Background()
}
// If specified, cancel with timeout.
if r.timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.timeout)
defer cancel()
}

prunePropPolicy, err := flagutils.ConvertPropagationPolicy(r.prunePropagationPolicy)
if err != nil {
return err
Expand Down Expand Up @@ -147,7 +162,7 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
ch := a.Run(context.Background(), inv, objs, apply.Options{
ch := a.Run(ctx, inv, objs, apply.Options{
ServerSideOptions: r.serverSideOptions,
PollInterval: r.period,
ReconcileTimeout: r.reconcileTimeout,
Expand Down
17 changes: 16 additions & 1 deletion cmd/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func GetDestroyRunner(factory cmdutil.Factory, invFactory inventory.InventoryCli
"Timeout threshold for waiting for all deleted resources to complete deletion")
cmd.Flags().StringVar(&r.deletePropagationPolicy, "delete-propagation-policy",
"Background", "Propagation policy for deletion")
cmd.Flags().DurationVar(&r.timeout, "timeout", 0,
"How long to wait before exiting")

r.Command = cmd
return r
Expand All @@ -71,9 +73,22 @@ type DestroyRunner struct {
deleteTimeout time.Duration
deletePropagationPolicy string
inventoryPolicy string
timeout time.Duration
}

func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
// If specified, use command context.
ctx := cmd.Context()
if ctx == nil {
ctx = context.Background()
}
// If specified, cancel with timeout.
if r.timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.timeout)
defer cancel()
}

deletePropPolicy, err := flagutils.ConvertPropagationPolicy(r.deletePropagationPolicy)
if err != nil {
return err
Expand Down Expand Up @@ -118,7 +133,7 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
// Run the destroyer. It will return a channel where we can receive updates
// to keep track of progress and any issues.
printStatusEvents := r.deleteTimeout != time.Duration(0)
ch := d.Run(context.Background(), inv, apply.DestroyerOptions{
ch := d.Run(ctx, inv, apply.DestroyerOptions{
DeleteTimeout: r.deleteTimeout,
DeletePropagationPolicy: deletePropPolicy,
InventoryPolicy: inventoryPolicy,
Expand Down
20 changes: 18 additions & 2 deletions cmd/preview/cmdpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down Expand Up @@ -57,6 +58,8 @@ func GetPreviewRunner(factory cmdutil.Factory, invFactory inventory.InventoryCli
cmd.Flags().StringVar(&r.inventoryPolicy, flagutils.InventoryPolicyFlag, flagutils.InventoryPolicyStrict,
"It determines the behavior when the resources don't belong to current inventory. Available options "+
fmt.Sprintf("%q, %q and %q.", flagutils.InventoryPolicyStrict, flagutils.InventoryPolicyAdopt, flagutils.InventoryPolicyForceAdopt))
cmd.Flags().DurationVar(&r.timeout, "timeout", 0,
"How long to wait before exiting")

r.Command = cmd
return r
Expand All @@ -80,10 +83,23 @@ type PreviewRunner struct {
serverSideOptions common.ServerSideOptions
output string
inventoryPolicy string
timeout time.Duration
}

// RunE is the function run from the cobra command.
func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
// If specified, use command context.
ctx := cmd.Context()
if ctx == nil {
ctx = context.Background()
}
// If specified, cancel with timeout.
if r.timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.timeout)
defer cancel()
}

var ch <-chan event.Event

drs := common.DryRunClient
Expand Down Expand Up @@ -141,7 +157,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {

// Run the applier. It will return a channel where we can receive updates
// to keep track of progress and any issues.
ch = a.Run(context.Background(), inv, objs, apply.Options{
ch = a.Run(ctx, inv, objs, apply.Options{
EmitStatusEvents: false,
NoPrune: noPrune,
DryRunStrategy: drs,
Expand All @@ -153,7 +169,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
ch = d.Run(context.Background(), inv, apply.DestroyerOptions{
ch = d.Run(ctx, inv, apply.DestroyerOptions{
InventoryPolicy: inventoryPolicy,
DryRunStrategy: drs,
})
Expand Down

0 comments on commit 066fd77

Please sign in to comment.