diff --git a/cmd/kops/create.go b/cmd/kops/create.go index 441f9cd18e..51c31a7480 100644 --- a/cmd/kops/create.go +++ b/cmd/kops/create.go @@ -76,7 +76,7 @@ func NewCmdCreate(f *util.Factory, out io.Writer) *cobra.Command { Example: createExample, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return RunCreate(context.TODO(), f, out, options) + return RunCreate(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/create_cluster.go b/cmd/kops/create_cluster.go index 7d382d445c..369f553b3d 100644 --- a/cmd/kops/create_cluster.go +++ b/cmd/kops/create_cluster.go @@ -200,7 +200,7 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command { } } - return RunCreateCluster(context.TODO(), f, out, options) + return RunCreateCluster(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/create_instancegroup.go b/cmd/kops/create_instancegroup.go index 775387726e..1a95365c00 100644 --- a/cmd/kops/create_instancegroup.go +++ b/cmd/kops/create_instancegroup.go @@ -119,7 +119,7 @@ func NewCmdCreateInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command { return nil, cobra.ShellCompDirectiveNoFileComp }, RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateInstanceGroup(context.TODO(), f, out, options) + return RunCreateInstanceGroup(cmd.Context(), f, out, options) }, } @@ -277,8 +277,9 @@ func RunCreateInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer, func completeClusterSubnet(f commandutils.Factory, excludeSubnets *[]string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ctx := cmd.Context() + commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, _, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { diff --git a/cmd/kops/create_keypair.go b/cmd/kops/create_keypair.go index 9c4cfaf00a..d8a0e90c2a 100644 --- a/cmd/kops/create_keypair.go +++ b/cmd/kops/create_keypair.go @@ -128,10 +128,10 @@ func NewCmdCreateKeypair(f *util.Factory, out io.Writer) *cobra.Command { return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completeCreateKeypair(f, options, args, toComplete) + return completeCreateKeypair(cmd.Context(), f, options, args, toComplete) }, RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateKeypair(context.TODO(), f, out, options) + return RunCreateKeypair(cmd.Context(), f, out, options) }, } @@ -304,16 +304,15 @@ func completeKeyset(ctx context.Context, cluster *kopsapi.Cluster, clientSet sim return keyset, keyStore, nil, cobra.ShellCompDirectiveNoFileComp } -func completeCreateKeypair(f commandutils.Factory, options *CreateKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeCreateKeypair(ctx context.Context, f commandutils.Factory, options *CreateKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { return completions, directive } - keyset, _, completions, directive := completeKeyset(cluster, clientSet, args, rotatableKeysetFilter) + keyset, _, completions, directive := completeKeyset(ctx, cluster, clientSet, args, rotatableKeysetFilter) if keyset == nil { return completions, directive } diff --git a/cmd/kops/create_secret_ciliumpassword.go b/cmd/kops/create_secret_ciliumpassword.go index 33f30d0508..edc4375678 100644 --- a/cmd/kops/create_secret_ciliumpassword.go +++ b/cmd/kops/create_secret_ciliumpassword.go @@ -71,7 +71,7 @@ func NewCmdCreateSecretCiliumPassword(f *util.Factory, out io.Writer) *cobra.Com Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateSecretCiliumEncryptionConfig(context.TODO(), f, out, options) + return RunCreateSecretCiliumEncryptionConfig(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/create_secret_dockerconfig.go b/cmd/kops/create_secret_dockerconfig.go index faab77610a..5f0a038ca6 100644 --- a/cmd/kops/create_secret_dockerconfig.go +++ b/cmd/kops/create_secret_dockerconfig.go @@ -76,7 +76,7 @@ func NewCmdCreateSecretDockerConfig(f *util.Factory, out io.Writer) *cobra.Comma Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateSecretDockerConfig(context.TODO(), f, out, options) + return RunCreateSecretDockerConfig(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/create_secret_encryptionconfig.go b/cmd/kops/create_secret_encryptionconfig.go index 6b3d86b0a2..c306d623af 100644 --- a/cmd/kops/create_secret_encryptionconfig.go +++ b/cmd/kops/create_secret_encryptionconfig.go @@ -70,7 +70,7 @@ func NewCmdCreateSecretEncryptionConfig(f *util.Factory, out io.Writer) *cobra.C Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateSecretEncryptionConfig(context.TODO(), f, out, options) + return RunCreateSecretEncryptionConfig(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/create_secret_weavepassword.go b/cmd/kops/create_secret_weavepassword.go index d2c18dc979..c2aeebc968 100644 --- a/cmd/kops/create_secret_weavepassword.go +++ b/cmd/kops/create_secret_weavepassword.go @@ -78,7 +78,7 @@ func NewCmdCreateSecretWeavePassword(f *util.Factory, out io.Writer) *cobra.Comm Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateSecretWeavePassword(context.TODO(), f, out, options) + return RunCreateSecretWeavePassword(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/create_sshpublickey.go b/cmd/kops/create_sshpublickey.go index 3cb69bbdc4..d650fd06ef 100644 --- a/cmd/kops/create_sshpublickey.go +++ b/cmd/kops/create_sshpublickey.go @@ -59,7 +59,7 @@ func NewCmdCreateSSHPublicKey(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunCreateSSHPublicKey(context.TODO(), f, out, options) + return RunCreateSSHPublicKey(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/delete.go b/cmd/kops/delete.go index c6a37b00a0..f34c146336 100644 --- a/cmd/kops/delete.go +++ b/cmd/kops/delete.go @@ -60,7 +60,7 @@ func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command { SuggestFor: []string{"rm"}, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return RunDelete(context.TODO(), f, out, options) + return RunDelete(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/delete_cluster.go b/cmd/kops/delete_cluster.go index f4352f17d9..fda3432a38 100644 --- a/cmd/kops/delete_cluster.go +++ b/cmd/kops/delete_cluster.go @@ -73,7 +73,7 @@ func NewCmdDeleteCluster(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgsNoKubeconfig(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteCluster(context.TODO(), f, out, options) + return RunDeleteCluster(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/delete_instance.go b/cmd/kops/delete_instance.go index 76a60ca991..0f8a0ab9e5 100644 --- a/cmd/kops/delete_instance.go +++ b/cmd/kops/delete_instance.go @@ -133,7 +133,7 @@ func NewCmdDeleteInstance(f *util.Factory, out io.Writer) *cobra.Command { }, ValidArgsFunction: completeInstanceOrNode(f, &options), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteInstance(context.TODO(), f, out, &options) + return RunDeleteInstance(cmd.Context(), f, out, &options) }, } @@ -307,12 +307,13 @@ func findDeletionNode(groups map[string]*cloudinstances.CloudInstanceGroup, opti func completeInstanceOrNode(f commandutils.Factory, options *DeleteInstanceOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ctx := cmd.Context() + if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { diff --git a/cmd/kops/delete_instancegroup.go b/cmd/kops/delete_instancegroup.go index 0d703c1dc6..1f0cdcb790 100644 --- a/cmd/kops/delete_instancegroup.go +++ b/cmd/kops/delete_instancegroup.go @@ -86,7 +86,7 @@ func NewCmdDeleteInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command { }, ValidArgsFunction: completeInstanceGroup(f, nil, &[]string{kops.InstanceGroupRoleControlPlane.ToLowerString()}), RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.TODO() + ctx := cmd.Context() if !options.Yes { message := fmt.Sprintf("Do you really want to delete instance group %q? This action cannot be undone.", options.GroupName) diff --git a/cmd/kops/delete_secret.go b/cmd/kops/delete_secret.go index d6ac3bf8b9..49cddf230b 100644 --- a/cmd/kops/delete_secret.go +++ b/cmd/kops/delete_secret.go @@ -79,7 +79,7 @@ func NewCmdDeleteSecret(f *util.Factory, out io.Writer) *cobra.Command { }, ValidArgsFunction: completeSecretNames(f), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteSecret(context.TODO(), f, out, options) + return RunDeleteSecret(cmd.Context(), f, out, options) }, } @@ -120,8 +120,9 @@ func RunDeleteSecret(ctx context.Context, f *util.Factory, out io.Writer, option func completeSecretNames(f commandutils.Factory) func(cmd *cobra.Command, args []string, complete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, complete string) ([]string, cobra.ShellCompDirective) { + ctx := cmd.Context() + commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { diff --git a/cmd/kops/delete_sshpublickey.go b/cmd/kops/delete_sshpublickey.go index 03e4a0766d..d979dfd8d7 100644 --- a/cmd/kops/delete_sshpublickey.go +++ b/cmd/kops/delete_sshpublickey.go @@ -52,7 +52,7 @@ func NewCmdDeleteSSHPublicKey(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.TODO() + ctx := cmd.Context() return RunDeleteSSHPublicKey(ctx, f, out, options) }, diff --git a/cmd/kops/distrust_keypair.go b/cmd/kops/distrust_keypair.go index 9664da753a..6a48936c16 100644 --- a/cmd/kops/distrust_keypair.go +++ b/cmd/kops/distrust_keypair.go @@ -98,10 +98,10 @@ func NewCmdDistrustKeypair(f *util.Factory, out io.Writer) *cobra.Command { return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completeDistrustKeyset(f, options, args, toComplete) + return completeDistrustKeyset(cmd.Context(), f, options, args, toComplete) }, RunE: func(cmd *cobra.Command, args []string) error { - return RunDistrustKeypair(context.TODO(), f, out, options) + return RunDistrustKeypair(cmd.Context(), f, out, options) }, } @@ -192,16 +192,15 @@ func distrustKeypair(ctx context.Context, out io.Writer, name string, keypairIDs return nil } -func completeDistrustKeyset(f commandutils.Factory, options *DistrustKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeDistrustKeyset(ctx context.Context, f commandutils.Factory, options *DistrustKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { return completions, directive } - keyset, _, completions, directive := completeKeyset(cluster, clientSet, args, rotatableKeysetFilter) + keyset, _, completions, directive := completeKeyset(ctx, cluster, clientSet, args, rotatableKeysetFilter) if keyset == nil { return completions, directive } diff --git a/cmd/kops/edit_cluster.go b/cmd/kops/edit_cluster.go index 31d37bc94f..342c8b6967 100644 --- a/cmd/kops/edit_cluster.go +++ b/cmd/kops/edit_cluster.go @@ -81,7 +81,7 @@ func NewCmdEditCluster(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunEditCluster(context.TODO(), f, out, options) + return RunEditCluster(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/edit_instancegroup.go b/cmd/kops/edit_instancegroup.go index 5e00e3daf6..5b0d89ea0a 100644 --- a/cmd/kops/edit_instancegroup.go +++ b/cmd/kops/edit_instancegroup.go @@ -104,7 +104,7 @@ func NewCmdEditInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command { }, ValidArgsFunction: completeInstanceGroup(f, nil, nil), RunE: func(cmd *cobra.Command, args []string) error { - return RunEditInstanceGroup(context.TODO(), f, out, options) + return RunEditInstanceGroup(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/export_kubeconfig.go b/cmd/kops/export_kubeconfig.go index bda0a7dc97..00095624f9 100644 --- a/cmd/kops/export_kubeconfig.go +++ b/cmd/kops/export_kubeconfig.go @@ -90,7 +90,7 @@ func NewCmdExportKubeconfig(f *util.Factory, out io.Writer) *cobra.Command { }, ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunExportKubeconfig(context.TODO(), f, out, options, args) + return RunExportKubeconfig(cmd.Context(), f, out, options, args) }, } diff --git a/cmd/kops/get.go b/cmd/kops/get.go index 6b3fae091e..8425c2d2ad 100644 --- a/cmd/kops/get.go +++ b/cmd/kops/get.go @@ -52,7 +52,7 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command { Short: i18n.T(`Get one or many resources.`), Args: rootCommand.clusterNameArgs(&options.ClusterName), RunE: func(cmd *cobra.Command, args []string) error { - return RunGet(context.TODO(), f, out, options) + return RunGet(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/get_all.go b/cmd/kops/get_all.go index ff5e3bfaec..d0d3329486 100644 --- a/cmd/kops/get_all.go +++ b/cmd/kops/get_all.go @@ -65,7 +65,7 @@ func NewCmdGetAll(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetAll(context.TODO(), f, out, options) + return RunGetAll(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/get_assets.go b/cmd/kops/get_assets.go index bb0757b947..061c2c9c65 100644 --- a/cmd/kops/get_assets.go +++ b/cmd/kops/get_assets.go @@ -91,7 +91,7 @@ func NewCmdGetAssets(f *util.Factory, out io.Writer, getOptions *GetOptions) *co Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetAssets(context.TODO(), f, out, &options) + return RunGetAssets(cmd.Context(), f, out, &options) }, } diff --git a/cmd/kops/get_cluster.go b/cmd/kops/get_cluster.go index 62712bb4a5..a7f6c88519 100644 --- a/cmd/kops/get_cluster.go +++ b/cmd/kops/get_cluster.go @@ -108,7 +108,7 @@ func NewCmdGetCluster(f *util.Factory, out io.Writer, getOptions *GetOptions) *c }, ValidArgsFunction: commandutils.CompleteClusterName(f, false, true), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetClusters(context.TODO(), f, out, &options) + return RunGetClusters(cmd.Context(), f, out, &options) }, } diff --git a/cmd/kops/get_instancegroups.go b/cmd/kops/get_instancegroups.go index 592d791c28..0e57ff8104 100644 --- a/cmd/kops/get_instancegroups.go +++ b/cmd/kops/get_instancegroups.go @@ -81,7 +81,7 @@ func NewCmdGetInstanceGroups(f *util.Factory, out io.Writer, getOptions *GetOpti return completeInstanceGroup(f, &args, nil)(cmd, nil, toComplete) }, RunE: func(cmd *cobra.Command, args []string) error { - return RunGetInstanceGroups(context.TODO(), f, out, &options) + return RunGetInstanceGroups(cmd.Context(), f, out, &options) }, } diff --git a/cmd/kops/get_instances.go b/cmd/kops/get_instances.go index df305a2c87..eec8d59be1 100644 --- a/cmd/kops/get_instances.go +++ b/cmd/kops/get_instances.go @@ -73,7 +73,7 @@ func NewCmdGetInstances(f *util.Factory, out io.Writer, options *GetOptions) *co Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetInstances(context.TODO(), f, out, options) + return RunGetInstances(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/get_keypairs.go b/cmd/kops/get_keypairs.go index df05ba997d..d243f898e2 100644 --- a/cmd/kops/get_keypairs.go +++ b/cmd/kops/get_keypairs.go @@ -72,10 +72,10 @@ func NewCmdGetKeypairs(f *util.Factory, out io.Writer, getOptions *GetOptions) * return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completeGetKeypairs(f, options, args, toComplete) + return completeGetKeypairs(cmd.Context(), f, options, args, toComplete) }, RunE: func(cmd *cobra.Command, args []string) error { - return RunGetKeypairs(context.TODO(), f, out, options) + return RunGetKeypairs(cmd.Context(), f, out, options) }, } @@ -259,9 +259,8 @@ func RunGetKeypairs(ctx context.Context, f commandutils.Factory, out io.Writer, return nil } -func completeGetKeypairs(f commandutils.Factory, options *GetKeypairsOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeGetKeypairs(ctx context.Context, f commandutils.Factory, options *GetKeypairsOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { @@ -269,7 +268,7 @@ func completeGetKeypairs(f commandutils.Factory, options *GetKeypairsOptions, ar } alreadySelected := sets.NewString(args...).Insert("all") - _, _, completions, directive = completeKeyset(cluster, clientSet, nil, func(name string, keyset *fi.Keyset) bool { + _, _, completions, directive = completeKeyset(ctx, cluster, clientSet, nil, func(name string, keyset *fi.Keyset) bool { return !alreadySelected.Has(name) }) diff --git a/cmd/kops/get_secrets.go b/cmd/kops/get_secrets.go index fc73bb938c..a9de68e1b8 100644 --- a/cmd/kops/get_secrets.go +++ b/cmd/kops/get_secrets.go @@ -69,7 +69,7 @@ func NewCmdGetSecrets(f *util.Factory, out io.Writer, getOptions *GetOptions) *c }, ValidArgsFunction: completeSecretNames(f), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetSecrets(context.TODO(), f, out, &options) + return RunGetSecrets(cmd.Context(), f, out, &options) }, } diff --git a/cmd/kops/get_sshpublickeys.go b/cmd/kops/get_sshpublickeys.go index bdd4fc0553..b8aa800ddb 100644 --- a/cmd/kops/get_sshpublickeys.go +++ b/cmd/kops/get_sshpublickeys.go @@ -57,7 +57,7 @@ func NewCmdGetSSHPublicKeys(f *util.Factory, out io.Writer, getOptions *GetOptio Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetSSHPublicKeys(context.TODO(), f, out, &options) + return RunGetSSHPublicKeys(cmd.Context(), f, out, &options) }, } diff --git a/cmd/kops/main.go b/cmd/kops/main.go index 56c8ac8835..06fce3cd41 100644 --- a/cmd/kops/main.go +++ b/cmd/kops/main.go @@ -16,6 +16,9 @@ limitations under the License. package main // import "k8s.io/kops/cmd/kops" +import "context" + func main() { - Execute() + ctx := context.Background() + Execute(ctx) } diff --git a/cmd/kops/promote_keypair.go b/cmd/kops/promote_keypair.go index 280ca52090..7b41a7669d 100644 --- a/cmd/kops/promote_keypair.go +++ b/cmd/kops/promote_keypair.go @@ -102,7 +102,7 @@ func NewCmdPromoteKeypair(f *util.Factory, out io.Writer) *cobra.Command { return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completePromoteKeyset(f, options, args, toComplete) + return completePromoteKeyset(cmd.Context(), f, options, args, toComplete) }, RunE: func(cmd *cobra.Command, args []string) error { return RunPromoteKeypair(cmd.Context(), f, out, options) @@ -201,16 +201,15 @@ func promoteKeypair(ctx context.Context, out io.Writer, name string, keypairID s return nil } -func completePromoteKeyset(f commandutils.Factory, options *PromoteKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completePromoteKeyset(ctx context.Context, f commandutils.Factory, options *PromoteKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { return completions, directive } - keyset, _, completions, directive := completeKeyset(cluster, clientSet, args, rotatableKeysetFilter) + keyset, _, completions, directive := completeKeyset(ctx, cluster, clientSet, args, rotatableKeysetFilter) if keyset == nil { return completions, directive } diff --git a/cmd/kops/replace.go b/cmd/kops/replace.go index 7461783dfe..6984a4e64c 100644 --- a/cmd/kops/replace.go +++ b/cmd/kops/replace.go @@ -73,7 +73,7 @@ func NewCmdReplace(f *util.Factory, out io.Writer) *cobra.Command { Args: cobra.NoArgs, ValidArgsFunction: cobra.NoFileCompletions, RunE: func(cmd *cobra.Command, args []string) error { - return RunReplace(context.TODO(), f, out, options) + return RunReplace(cmd.Context(), f, out, options) }, } cmd.Flags().StringSliceVarP(&options.Filenames, "filename", "f", options.Filenames, "A list of one or more files separated by a comma.") diff --git a/cmd/kops/rolling-update_cluster.go b/cmd/kops/rolling-update_cluster.go index 6e601e15d3..dae723cf76 100644 --- a/cmd/kops/rolling-update_cluster.go +++ b/cmd/kops/rolling-update_cluster.go @@ -178,7 +178,7 @@ func NewCmdRollingUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunRollingUpdateCluster(context.TODO(), f, out, &options) + return RunRollingUpdateCluster(cmd.Context(), f, out, &options) }, } @@ -458,8 +458,9 @@ func RunRollingUpdateCluster(ctx context.Context, f *util.Factory, out io.Writer func completeInstanceGroup(f commandutils.Factory, selectedInstanceGroups *[]string, selectedInstanceGroupRoles *[]string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ctx := cmd.Context() + commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, args) if cluster == nil { diff --git a/cmd/kops/root.go b/cmd/kops/root.go index 707ec517e5..1578be0e52 100644 --- a/cmd/kops/root.go +++ b/cmd/kops/root.go @@ -89,10 +89,10 @@ var rootCommand = RootCmd{ }, } -func Execute() { +func Execute(ctx context.Context) { goflag.Set("logtostderr", "true") goflag.CommandLine.Parse([]string{}) - if err := rootCommand.cobraCommand.Execute(); err != nil { + if err := rootCommand.cobraCommand.ExecuteContext(ctx); err != nil { os.Exit(1) } } diff --git a/cmd/kops/toolbox_dump.go b/cmd/kops/toolbox_dump.go index 70868b6a5d..acf284f625 100644 --- a/cmd/kops/toolbox_dump.go +++ b/cmd/kops/toolbox_dump.go @@ -83,7 +83,7 @@ func NewCmdToolboxDump(f commandutils.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - return RunToolboxDump(context.TODO(), f, out, options) + return RunToolboxDump(cmd.Context(), f, out, options) }, } diff --git a/cmd/kops/toolbox_instance-selector.go b/cmd/kops/toolbox_instance-selector.go index 85805c19b8..3ff65f6149 100644 --- a/cmd/kops/toolbox_instance-selector.go +++ b/cmd/kops/toolbox_instance-selector.go @@ -152,7 +152,7 @@ func NewCmdToolboxInstanceSelector(f commandutils.Factory, out io.Writer) *cobra return nil, cobra.ShellCompDirectiveNoFileComp } commandline.Command.RunE = func(cmd *cobra.Command, args []string) error { - return RunToolboxInstanceSelector(context.TODO(), f, out, &commandline, options) + return RunToolboxInstanceSelector(cmd.Context(), f, out, &commandline, options) } cpuArchs := []string{cpuArchitectureAMD64, cpuArchitectureARM64} diff --git a/cmd/kops/trust_keypair.go b/cmd/kops/trust_keypair.go index 4e1ab8c6a0..237f822e54 100644 --- a/cmd/kops/trust_keypair.go +++ b/cmd/kops/trust_keypair.go @@ -80,10 +80,10 @@ func NewCmdTrustKeypair(f *util.Factory, out io.Writer) *cobra.Command { return nil }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completeTrustKeyset(f, options, args, toComplete) + return completeTrustKeyset(cmd.Context(), f, options, args, toComplete) }, RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.TODO() + ctx := cmd.Context() return RunTrustKeypair(ctx, f, out, options) }, @@ -135,16 +135,15 @@ func RunTrustKeypair(ctx context.Context, f *util.Factory, out io.Writer, option return nil } -func completeTrustKeyset(f commandutils.Factory, options *TrustKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func completeTrustKeyset(ctx context.Context, f commandutils.Factory, options *TrustKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { return completions, directive } - keyset, _, completions, directive := completeKeyset(cluster, clientSet, args, func(name string, keyset *fi.Keyset) bool { + keyset, _, completions, directive := completeKeyset(ctx, cluster, clientSet, args, func(name string, keyset *fi.Keyset) bool { if name == "all" { return false } diff --git a/cmd/kops/update_cluster.go b/cmd/kops/update_cluster.go index d6f077ced5..eed80cbb27 100644 --- a/cmd/kops/update_cluster.go +++ b/cmd/kops/update_cluster.go @@ -107,7 +107,7 @@ func NewCmdUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - _, err := RunUpdateCluster(context.TODO(), f, out, options) + _, err := RunUpdateCluster(cmd.Context(), f, out, options) return err }, } @@ -456,8 +456,9 @@ func hasKubecfg(contextName string) (bool, error) { func completeUpdateClusterTarget(f commandutils.Factory, options *UpdateClusterOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ctx := cmd.Context() + commandutils.ConfigureKlogForCompletion() - ctx := context.TODO() cluster, _, _, directive := GetClusterForCompletion(ctx, f, nil) if cluster == nil { diff --git a/cmd/kops/upgrade_cluster.go b/cmd/kops/upgrade_cluster.go index 504cdc4ee5..cc08ba6709 100644 --- a/cmd/kops/upgrade_cluster.go +++ b/cmd/kops/upgrade_cluster.go @@ -73,7 +73,7 @@ func NewCmdUpgradeCluster(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - ctx := context.TODO() + ctx := cmd.Context() return RunUpgradeCluster(ctx, f, out, options) }, diff --git a/cmd/kops/validate_cluster.go b/cmd/kops/validate_cluster.go index 879901a377..a7c13d6880 100644 --- a/cmd/kops/validate_cluster.go +++ b/cmd/kops/validate_cluster.go @@ -85,7 +85,7 @@ func NewCmdValidateCluster(f *util.Factory, out io.Writer) *cobra.Command { Args: rootCommand.clusterNameArgs(&options.ClusterName), ValidArgsFunction: commandutils.CompleteClusterName(f, true, false), RunE: func(cmd *cobra.Command, args []string) error { - result, err := RunValidateCluster(context.TODO(), f, out, options) + result, err := RunValidateCluster(cmd.Context(), f, out, options) if err != nil { return fmt.Errorf("validation failed: %v", err) } diff --git a/pkg/commands/commandutils/cluster.go b/pkg/commands/commandutils/cluster.go index 5d08389eb0..333b61599a 100644 --- a/pkg/commands/commandutils/cluster.go +++ b/pkg/commands/commandutils/cluster.go @@ -17,8 +17,6 @@ limitations under the License. package commandutils import ( - "context" - "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" @@ -27,6 +25,8 @@ import ( // CompleteClusterName returns a Cobra completion function for cluster names. func CompleteClusterName(f Factory, suppressIfArgs bool, suppressArgs bool) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ctx := cmd.Context() + if suppressIfArgs && len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } @@ -38,7 +38,7 @@ func CompleteClusterName(f Factory, suppressIfArgs bool, suppressArgs bool) func return CompletionError("getting clientset", err) } - list, err := client.ListClusters(context.TODO(), metav1.ListOptions{}) + list, err := client.ListClusters(ctx, metav1.ListOptions{}) if err != nil { return CompletionError("listing clusters", err) }