Skip to content

Commit

Permalink
Wire up context from cobra in cmd/kops
Browse files Browse the repository at this point in the history
Removes a lot of context.TODO() calls.
  • Loading branch information
justinsb committed Dec 21, 2022
1 parent e226b2b commit fadb9d6
Show file tree
Hide file tree
Showing 40 changed files with 71 additions and 70 deletions.
2 changes: 1 addition & 1 deletion cmd/kops/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/kops/create_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions cmd/kops/create_keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -267,9 +267,7 @@ func createKeypair(ctx context.Context, out io.Writer, options *CreateKeypairOpt
return nil
}

func completeKeyset(cluster *kopsapi.Cluster, clientSet simple.Clientset, args []string, filter func(name string, keyset *fi.Keyset) bool) (keyset *fi.Keyset, keyStore fi.CAStore, completions []string, directive cobra.ShellCompDirective) {
ctx := context.TODO()

func completeKeyset(ctx context.Context, cluster *kopsapi.Cluster, clientSet simple.Clientset, args []string, filter func(name string, keyset *fi.Keyset) bool) (keyset *fi.Keyset, keyStore fi.CAStore, completions []string, directive cobra.ShellCompDirective) {
keyStore, err := clientSet.KeyStore(ctx, cluster)
if err != nil {
completions, directive := commandutils.CompletionError("getting keystore", err)
Expand Down Expand Up @@ -306,16 +304,15 @@ func completeKeyset(cluster *kopsapi.Cluster, clientSet simple.Clientset, args [
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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_secret_ciliumpassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_secret_dockerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_secret_encryptionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_secret_weavepassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_sshpublickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/kops/delete_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/delete_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions cmd/kops/delete_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/delete_sshpublickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
9 changes: 4 additions & 5 deletions cmd/kops/distrust_keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/export_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
9 changes: 4 additions & 5 deletions cmd/kops/get_keypairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -259,17 +259,16 @@ 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 {
return completions, directive
}

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)
})

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get_sshpublickeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/kops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit fadb9d6

Please sign in to comment.