-
Notifications
You must be signed in to change notification settings - Fork 823
/
karmadactl.go
144 lines (125 loc) · 4.7 KB
/
karmadactl.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2020 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package karmadactl
import (
"flag"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericiooptions"
apiserverflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/util/templates"
"github.com/karmada-io/karmada/pkg/karmadactl/addons"
"github.com/karmada-io/karmada/pkg/karmadactl/apply"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit"
"github.com/karmada-io/karmada/pkg/karmadactl/cordon"
"github.com/karmada-io/karmada/pkg/karmadactl/deinit"
"github.com/karmada-io/karmada/pkg/karmadactl/describe"
"github.com/karmada-io/karmada/pkg/karmadactl/exec"
"github.com/karmada-io/karmada/pkg/karmadactl/get"
"github.com/karmada-io/karmada/pkg/karmadactl/interpret"
"github.com/karmada-io/karmada/pkg/karmadactl/join"
"github.com/karmada-io/karmada/pkg/karmadactl/logs"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/promote"
"github.com/karmada-io/karmada/pkg/karmadactl/register"
"github.com/karmada-io/karmada/pkg/karmadactl/taint"
"github.com/karmada-io/karmada/pkg/karmadactl/token"
"github.com/karmada-io/karmada/pkg/karmadactl/top"
"github.com/karmada-io/karmada/pkg/karmadactl/unjoin"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
"github.com/karmada-io/karmada/pkg/version/sharedcommand"
)
var (
rootCmdShort = "%s controls a Kubernetes Cluster Federation."
rootCmdLong = "%s controls a Kubernetes Cluster Federation."
)
// NewKarmadaCtlCommand creates the `karmadactl` command.
func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
// Parent command to which all sub-commands are added.
rootCmd := &cobra.Command{
Use: cmdUse,
Short: fmt.Sprintf(rootCmdShort, parentCommand),
Long: fmt.Sprintf(rootCmdLong, parentCommand),
RunE: runHelp,
}
// Init log flags
klog.InitFlags(flag.CommandLine)
// Add the command line flags from other dependencies (e.g., klog), but do not
// warn if they contain underscores.
pflag.CommandLine.SetNormalizeFunc(apiserverflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
rootCmd.PersistentFlags().AddFlagSet(pflag.CommandLine)
// From this point and forward we get warnings on flags that contain "_" separators
rootCmd.SetGlobalNormalizationFunc(apiserverflag.WarnWordSepNormalizeFunc)
// Prevent klog errors about logging before parsing.
_ = flag.CommandLine.Parse(nil)
f := util.NewFactory(options.DefaultConfigFlags)
ioStreams := genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
groups := templates.CommandGroups{
{
Message: "Basic Commands:",
Commands: []*cobra.Command{
get.NewCmdGet(f, parentCommand, ioStreams),
},
},
{
Message: "Cluster Registration Commands:",
Commands: []*cobra.Command{
cmdinit.NewCmdInit(parentCommand),
deinit.NewCmdDeInit(parentCommand),
addons.NewCmdAddons(parentCommand),
join.NewCmdJoin(f, parentCommand),
unjoin.NewCmdUnjoin(f, parentCommand),
token.NewCmdToken(f, parentCommand, ioStreams),
register.NewCmdRegister(parentCommand),
},
},
{
Message: "Cluster Management Commands:",
Commands: []*cobra.Command{
cordon.NewCmdCordon(f, parentCommand),
cordon.NewCmdUncordon(f, parentCommand),
taint.NewCmdTaint(f, parentCommand),
},
},
{
Message: "Troubleshooting and Debugging Commands:",
Commands: []*cobra.Command{
logs.NewCmdLogs(f, parentCommand, ioStreams),
exec.NewCmdExec(f, parentCommand, ioStreams),
describe.NewCmdDescribe(f, parentCommand, ioStreams),
interpret.NewCmdInterpret(f, parentCommand, ioStreams),
},
},
{
Message: "Advanced Commands:",
Commands: []*cobra.Command{
apply.NewCmdApply(f, parentCommand, ioStreams),
promote.NewCmdPromote(f, parentCommand),
top.NewCmdTop(f, parentCommand, ioStreams),
},
},
}
groups.Add(rootCmd)
filters := []string{"options"}
rootCmd.AddCommand(sharedcommand.NewCmdVersion(parentCommand))
rootCmd.AddCommand(options.NewCmdOptions(parentCommand, ioStreams.Out))
templates.ActsAsRootCommand(rootCmd, filters, groups...)
return rootCmd
}
func runHelp(cmd *cobra.Command, _ []string) error {
return cmd.Help()
}