Skip to content

Commit 9e5901c

Browse files
authored
create and set timeouts globally via a flag (#43)
Signed-off-by: Joe Lanford <[email protected]>
1 parent 2885d30 commit 9e5901c

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

internal/cmd/catalog_add.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"io/ioutil"
65
"time"
76

@@ -16,7 +15,6 @@ import (
1615
)
1716

1817
func newCatalogAddCmd(cfg *action.Configuration) *cobra.Command {
19-
var timeout time.Duration
2018
a := internalaction.NewCatalogAdd(cfg)
2119
a.Logf = log.Printf
2220

@@ -32,29 +30,25 @@ func newCatalogAddCmd(cfg *action.Configuration) *cobra.Command {
3230
}
3331
},
3432
Run: func(cmd *cobra.Command, args []string) {
35-
ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
36-
defer cancel()
37-
3833
a.CatalogSourceName = args[0]
3934
a.IndexImage = args[1]
4035

41-
cs, err := a.Run(ctx)
36+
cs, err := a.Run(cmd.Context())
4237
if err != nil {
4338
log.Fatalf("failed to add catalog: %v", err)
4439
}
4540
log.Printf("created catalogsource %q\n", cs.Name)
4641
},
4742
}
4843
bindCatalogAddFlags(cmd.Flags(), a)
49-
cmd.Flags().DurationVarP(&timeout, "timeout", "t", time.Minute, "the amount of time to wait before cancelling the catalog addition")
5044

5145
return cmd
5246
}
5347

5448
func bindCatalogAddFlags(fs *pflag.FlagSet, a *internalaction.CatalogAdd) {
5549
fs.StringVarP(&a.DisplayName, "display-name", "d", "", "display name of the index")
5650
fs.StringVarP(&a.Publisher, "publisher", "p", "", "publisher of the index")
57-
fs.DurationVar(&a.CleanupTimeout, "cleanup-timeout", time.Minute, "the amount to time to wait before cancelling cleanup")
51+
fs.DurationVar(&a.CleanupTimeout, "cleanup-timeout", time.Minute, "the amount of time to wait before cancelling cleanup")
5852

5953
fs.StringArrayVarP(&a.InjectBundles, "inject-bundles", "b", nil, "inject extra bundles into the index at runtime")
6054
fs.StringVarP(&a.InjectBundleMode, "inject-bundle-mode", "m", "", "mode to use to inject bundles")

internal/cmd/operator_install.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"time"
76

@@ -15,7 +14,6 @@ import (
1514
)
1615

1716
func newOperatorInstallCmd(cfg *action.Configuration) *cobra.Command {
18-
var timeout time.Duration
1917
i := internalaction.NewOperatorInstall(cfg)
2018
i.Logf = log.Printf
2119

@@ -25,17 +23,14 @@ func newOperatorInstallCmd(cfg *action.Configuration) *cobra.Command {
2523
Args: cobra.ExactArgs(1),
2624
Run: func(cmd *cobra.Command, args []string) {
2725
i.Package = args[0]
28-
ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
29-
defer cancel()
30-
csv, err := i.Run(ctx)
26+
csv, err := i.Run(cmd.Context())
3127
if err != nil {
3228
log.Fatalf("failed to install operator: %v", err)
3329
}
3430
log.Printf("operator %q installed; installed csv is %q", i.Package, csv.Name)
3531
},
3632
}
3733
bindOperatorInstallFlags(cmd.Flags(), i)
38-
cmd.Flags().DurationVarP(&timeout, "timeout", "t", time.Minute, "the amount of time to wait before cancelling the install")
3934

4035
return cmd
4136
}
@@ -45,7 +40,7 @@ func bindOperatorInstallFlags(fs *pflag.FlagSet, i *internalaction.OperatorInsta
4540
fs.VarP(&i.Approval, "approval", "a", fmt.Sprintf("approval (%s or %s)", v1alpha1.ApprovalManual, v1alpha1.ApprovalAutomatic))
4641
fs.StringVarP(&i.Version, "version", "v", "", "install specific version for operator (default latest)")
4742
fs.StringSliceVarP(&i.WatchNamespaces, "watch", "w", []string{}, "namespaces to watch")
48-
fs.DurationVar(&i.CleanupTimeout, "cleanup-timeout", time.Minute, "the amount to time to wait before cancelling cleanup")
43+
fs.DurationVar(&i.CleanupTimeout, "cleanup-timeout", time.Minute, "the amount of time to wait before cancelling cleanup")
4944
fs.BoolVarP(&i.CreateOperatorGroup, "create-operator-group", "C", false, "create operator group if necessary")
5045

5146
fs.VarP(&i.InstallMode, "install-mode", "i", "install mode")

internal/cmd/operator_upgrade.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
5-
"time"
6-
74
"github.com/spf13/cobra"
85
"github.com/spf13/pflag"
96

@@ -13,25 +10,21 @@ import (
1310
)
1411

1512
func newOperatorUpgradeCmd(cfg *action.Configuration) *cobra.Command {
16-
var timeout time.Duration
1713
u := internalaction.NewOperatorUpgrade(cfg)
1814
cmd := &cobra.Command{
1915
Use: "upgrade <operator>",
2016
Short: "Upgrade an operator",
2117
Args: cobra.ExactArgs(1),
2218
Run: func(cmd *cobra.Command, args []string) {
2319
u.Package = args[0]
24-
ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
25-
defer cancel()
26-
csv, err := u.Run(ctx)
20+
csv, err := u.Run(cmd.Context())
2721
if err != nil {
2822
log.Fatalf("failed to upgrade operator: %v", err)
2923
}
3024
log.Printf("operator %q upgraded; installed csv is %q", u.Package, csv.Name)
3125
},
3226
}
3327
bindOperatorUpgradeFlags(cmd.Flags(), u)
34-
cmd.Flags().DurationVarP(&timeout, "timeout", "t", time.Minute, "the amount of time to wait before cancelling the upgrade")
3528
return cmd
3629
}
3730

internal/cmd/root.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package cmd
22

33
import (
4+
"context"
5+
"reflect"
6+
"time"
7+
"unsafe"
8+
49
"github.com/spf13/cobra"
510

611
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"
@@ -24,14 +29,30 @@ operators available for installation, and install and uninstall
2429
operators from the installed catalogs.`,
2530
}
2631

27-
flags := cmd.PersistentFlags()
32+
var (
33+
cfg action.Configuration
34+
timeout time.Duration
35+
cancel context.CancelFunc
36+
)
2837

29-
var cfg action.Configuration
38+
flags := cmd.PersistentFlags()
3039
cfg.BindFlags(flags)
40+
flags.DurationVar(&timeout, "timeout", 1*time.Minute, "The amount of time to wait before giving up on an operation.")
41+
42+
cmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
43+
var ctx context.Context
44+
ctx, cancel = context.WithTimeout(cmd.Context(), timeout)
45+
46+
// This sets the unexported cmd.ctx value using unsafe. If
47+
// https://github.com/spf13/cobra/pull/1118 gets merged, we
48+
// should use cmd.SetContext() instead.
49+
setContext(cmd, ctx)
3150

32-
cmd.PersistentPreRunE = func(*cobra.Command, []string) error {
3351
return cfg.Load()
3452
}
53+
cmd.PersistentPostRun = func(command *cobra.Command, _ []string) {
54+
cancel()
55+
}
3556

3657
cmd.AddCommand(
3758
newCatalogCmd(&cfg),
@@ -47,3 +68,10 @@ operators from the installed catalogs.`,
4768

4869
return cmd
4970
}
71+
72+
func setContext(cmd *cobra.Command, ctx context.Context) { //nolint:golint
73+
rs := reflect.ValueOf(cmd).Elem()
74+
rf := rs.FieldByName("ctx")
75+
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
76+
rf.Set(reflect.ValueOf(ctx))
77+
}

0 commit comments

Comments
 (0)