/
flags.go
118 lines (106 loc) · 3.13 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package client
import (
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubectl/pkg/cmd/get"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util"
)
const (
clientQPS = 50
clientBurst = 100
discoveryBurst = 300
discoveryQPS = 50.0
)
// Flags represents the common configuration flags to
// create a client that interact with a Kubernetes cluster.
type Flags struct {
*genericclioptions.ConfigFlags
}
// DefaultFlags returns a new set client configuration
// flags with default values.
func DefaultFlags() *Flags {
return &Flags{
ConfigFlags: genericclioptions.NewConfigFlags(true),
}
}
// AddFlags binds the client configuration flags
// to the given flag set.
func (f *Flags) AddFlags(flags *pflag.FlagSet) {
fs := pflag.NewFlagSet("", pflag.ContinueOnError)
f.ConfigFlags.AddFlags(fs)
// Normalize client flags by removing any dot
// character at the end of the usage string.
fs.VisitAll(func(f *pflag.Flag) {
f.Usage = strings.TrimSuffix(f.Usage, ".")
})
flags.AddFlagSet(fs)
}
// RegisterCompletionFunc registers the completion functions
// related to client configuration flags.
// Copied from the official `kubectl` command source:
// https://github.com/kubernetes/kubectl/blob/v0.23.1/pkg/cmd/cmd.go#L471-L492
func (*Flags) RegisterCompletionFunc(cmd *cobra.Command, f cmdutil.Factory) {
directive := cobra.ShellCompDirectiveNoFileComp
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
"namespace",
func(_ *cobra.Command, _ []string, tc string) ([]string, cobra.ShellCompDirective) {
return get.CompGetResource(f, cmd, "namespace", tc), directive
}))
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
"context",
func(_ *cobra.Command, _ []string, tc string) ([]string, cobra.ShellCompDirective) {
return util.ListContextsInConfig(tc), directive
}))
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
"cluster",
func(_ *cobra.Command, _ []string, tc string) ([]string, cobra.ShellCompDirective) {
return util.ListClustersInConfig(tc), directive
}))
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
"user",
func(_ *cobra.Command, _ []string, tc string) ([]string, cobra.ShellCompDirective) {
return util.ListUsersInConfig(tc), directive
}))
}
// NewClient returns a new clients based on the flags' configuration.
func (f *Flags) NewClient() (Interface, error) {
f.ConfigFlags = f.
WithDiscoveryQPS(discoveryQPS).
WithDiscoveryBurst(discoveryBurst).
WithDeprecatedPasswordFlag()
config, err := f.ToRESTConfig()
if err != nil {
return nil, err
}
config.QPS = clientQPS
config.Burst = clientBurst
dyn, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
dis, err := f.ToDiscoveryClient()
if err != nil {
return nil, err
}
m, err := f.ToRESTMapper()
if err != nil {
return nil, err
}
pc, err := corev1client.NewForConfig(config)
if err != nil {
return nil, err
}
c := &client{
flags: f,
dynamicClient: dyn,
discoveryClient: dis,
coreClient: pc,
mapper: m,
}
return c, nil
}