mirror of https://github.com/kubernetes/kops.git
Implement completion for "kops distrust keypair"
This commit is contained in:
parent
9864281630
commit
ac1cf0b0ee
|
|
@ -22,20 +22,9 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/kops/cmd/kops/util"
|
"k8s.io/kops/cmd/kops/util"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
distrustLong = templates.LongDesc(i18n.T(`
|
|
||||||
Distrust keypairs.
|
|
||||||
`))
|
|
||||||
|
|
||||||
distrustExample = templates.Examples(i18n.T(`
|
|
||||||
# Distrust a keypair
|
|
||||||
kops distrust keypair ca 6977545226837259959403993899
|
|
||||||
|
|
||||||
`))
|
|
||||||
|
|
||||||
distrustShort = i18n.T("Distrust keypairs.")
|
distrustShort = i18n.T("Distrust keypairs.")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -43,8 +32,6 @@ func NewCmdDistrust(f *util.Factory, out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "distrust",
|
Use: "distrust",
|
||||||
Short: distrustShort,
|
Short: distrustShort,
|
||||||
Long: distrustLong,
|
|
||||||
Example: distrustExample,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create subcommands
|
// create subcommands
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,27 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
"k8s.io/kops/cmd/kops/util"
|
"k8s.io/kops/cmd/kops/util"
|
||||||
|
"k8s.io/kops/pkg/commands/commandutils"
|
||||||
|
"k8s.io/kops/upup/pkg/fi"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
distrustKeypairLong = templates.LongDesc(i18n.T(`
|
distrustKeypairLong = templates.LongDesc(i18n.T(`
|
||||||
Distrust one or more keypairs.`))
|
Distrust one or more keypairs in a keyset.
|
||||||
|
|
||||||
|
Distrusting removes the certificates of the specified keypairs from trust
|
||||||
|
stores.
|
||||||
|
|
||||||
|
Only secondary keypairs may be distrusted.
|
||||||
|
|
||||||
|
If no keypair IDs are specified, distrusts all keypairs in the keyset that
|
||||||
|
are older than the primary keypair.
|
||||||
|
`))
|
||||||
|
|
||||||
distrustKeypairExample = templates.Examples(i18n.T(`
|
distrustKeypairExample = templates.Examples(i18n.T(`
|
||||||
# Distrust all cluster CA keypairs older than the primary.
|
# Distrust all cluster CA keypairs older than the primary.
|
||||||
|
|
@ -55,31 +67,34 @@ func NewCmdDistrustKeypair(f *util.Factory, out io.Writer) *cobra.Command {
|
||||||
options := &DistrustKeypairOptions{}
|
options := &DistrustKeypairOptions{}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "keypair KEYSET [ID]...",
|
Use: "keypair keyset [id]...",
|
||||||
Short: distrustKeypairShort,
|
Short: distrustKeypairShort,
|
||||||
Long: distrustKeypairLong,
|
Long: distrustKeypairLong,
|
||||||
Example: distrustKeypairExample,
|
Example: distrustKeypairExample,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
options.ClusterName = rootCommand.ClusterName(true)
|
options.ClusterName = rootCommand.ClusterName(true)
|
||||||
if options.ClusterName == "" {
|
if options.ClusterName == "" {
|
||||||
exitWithError(fmt.Errorf("--name is required"))
|
return fmt.Errorf("--name is required")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
exitWithError(fmt.Errorf("must specify name of keyset to distrust keypair in"))
|
return fmt.Errorf("must specify name of keyset to distrust keypair in")
|
||||||
}
|
}
|
||||||
options.Keyset = args[0]
|
options.Keyset = args[0]
|
||||||
|
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
options.KeypairIDs = args[1:]
|
options.KeypairIDs = args[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
err := RunDistrustKeypair(ctx, f, out, options)
|
return nil
|
||||||
if err != nil {
|
},
|
||||||
exitWithError(err)
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
}
|
return completeDistrustKeyset(options, args, toComplete)
|
||||||
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
return RunDistrustKeypair(ctx, f, out, options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,13 +102,6 @@ func NewCmdDistrustKeypair(f *util.Factory, out io.Writer) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunDistrustKeypair(ctx context.Context, f *util.Factory, out io.Writer, options *DistrustKeypairOptions) error {
|
func RunDistrustKeypair(ctx context.Context, f *util.Factory, out io.Writer, options *DistrustKeypairOptions) error {
|
||||||
if options.ClusterName == "" {
|
|
||||||
return fmt.Errorf("ClusterName is required")
|
|
||||||
}
|
|
||||||
if options.Keyset == "" {
|
|
||||||
return fmt.Errorf("Keyset is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
clientset, err := f.Clientset()
|
clientset, err := f.Clientset()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -148,3 +156,23 @@ func RunDistrustKeypair(ctx context.Context, f *util.Factory, out io.Writer, opt
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func completeDistrustKeyset(options *DistrustKeypairOptions, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
commandutils.ConfigureKlogForCompletion()
|
||||||
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
cluster, clientSet, completions, directive := GetClusterForCompletion(ctx, &rootCommand)
|
||||||
|
if cluster == nil {
|
||||||
|
return completions, directive
|
||||||
|
}
|
||||||
|
|
||||||
|
keyset, _, completions, directive := completeKeyset(cluster, clientSet, args, rotatableKeysetFilter)
|
||||||
|
if keyset == nil {
|
||||||
|
return completions, directive
|
||||||
|
}
|
||||||
|
|
||||||
|
alreadySelected := sets.NewString(args[1:]...)
|
||||||
|
return completeKeypairID(keyset, func(keyset *fi.Keyset, item *fi.KeysetItem) bool {
|
||||||
|
return item.DistrustTimestamp == nil && item.Id != keyset.Primary.Id && !alreadySelected.Has(item.Id)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,6 @@
|
||||||
|
|
||||||
Distrust keypairs.
|
Distrust keypairs.
|
||||||
|
|
||||||
### Synopsis
|
|
||||||
|
|
||||||
Distrust keypairs.
|
|
||||||
|
|
||||||
### Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
# Distrust a keypair
|
|
||||||
kops distrust keypair ca 6977545226837259959403993899
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,16 @@ Distrust a keypair.
|
||||||
|
|
||||||
### Synopsis
|
### Synopsis
|
||||||
|
|
||||||
Distrust one or more keypairs.
|
Distrust one or more keypairs in a keyset.
|
||||||
|
|
||||||
|
Distrusting removes the certificates of the specified keypairs from trust stores.
|
||||||
|
|
||||||
|
Only secondary keypairs may be distrusted.
|
||||||
|
|
||||||
|
If no keypair IDs are specified, distrusts all keypairs in the keyset that are older than the primary keypair.
|
||||||
|
|
||||||
```
|
```
|
||||||
kops distrust keypair KEYSET [ID]... [flags]
|
kops distrust keypair keyset [id]... [flags]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue