Skip to content

Commit 2607653

Browse files
committed
support karmadactl config current-context
Signed-off-by: zhzhuang-zju <[email protected]>
1 parent 4fe9dff commit 2607653

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

pkg/karmadactl/config/config.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2023 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"fmt"
21+
"path"
22+
23+
"github.com/spf13/cobra"
24+
"k8s.io/cli-runtime/pkg/genericclioptions"
25+
"k8s.io/client-go/tools/clientcmd"
26+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
27+
"k8s.io/kubectl/pkg/util/i18n"
28+
"k8s.io/kubectl/pkg/util/templates"
29+
)
30+
31+
// NewCmdConfig creates a command object for the "config" action, and adds all child commands to it.
32+
func NewCmdConfig(parentCommand string, pathOptions *clientcmd.PathOptions, streams genericclioptions.IOStreams) *cobra.Command {
33+
if len(pathOptions.ExplicitFileFlag) == 0 {
34+
pathOptions.ExplicitFileFlag = clientcmd.RecommendedConfigPathFlag
35+
}
36+
37+
cmd := &cobra.Command{
38+
Use: "config SUBCOMMAND",
39+
DisableFlagsInUseLine: true,
40+
Short: i18n.T("Modify kubeconfig files"),
41+
Long: fmt.Sprintf(templates.LongDesc(i18n.T(`
42+
Modify kubeconfig files using subcommands like "%[1]s config set current-context my-context".
43+
44+
The loading order follows these rules:
45+
46+
1. If the --`)+pathOptions.ExplicitFileFlag+i18n.T(` flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
47+
2. If $`)+pathOptions.EnvVar+i18n.T(` environment variable is set, then it is used as a list of paths (normal path delimiting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
48+
49+
3. Otherwise, `)+path.Join("${HOME}", pathOptions.GlobalFileSubpath)+i18n.T(` is used and no merging takes place.`)), parentCommand),
50+
Run: cmdutil.DefaultSubCommandRun(streams.ErrOut),
51+
}
52+
53+
// file paths are common to all sub commands
54+
cmd.PersistentFlags().StringVar(&pathOptions.LoadingRules.ExplicitPath, pathOptions.ExplicitFileFlag, pathOptions.LoadingRules.ExplicitPath, "use a particular kubeconfig file")
55+
56+
cmd.AddCommand(NewCmdConfigCurrentContext(parentCommand, streams, pathOptions))
57+
return cmd
58+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2023 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
"k8s.io/cli-runtime/pkg/genericclioptions"
24+
"k8s.io/client-go/tools/clientcmd"
25+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
26+
"k8s.io/kubectl/pkg/util/i18n"
27+
"k8s.io/kubectl/pkg/util/templates"
28+
)
29+
30+
// holds the command-line options for 'config current-context' sub command
31+
type currentContextOptions struct {
32+
configAccess clientcmd.ConfigAccess
33+
streams genericclioptions.IOStreams
34+
}
35+
36+
var (
37+
currentContextLong = templates.LongDesc(i18n.T(`
38+
Display the current-context.`))
39+
40+
currentContextExample = templates.Examples(`
41+
# Display the current-context
42+
%[1]s config current-context`)
43+
)
44+
45+
// NewCmdConfigCurrentContext returns a Command instance for 'config current-context' sub command
46+
func NewCmdConfigCurrentContext(parentCommand string, streams genericclioptions.IOStreams, configAccess clientcmd.ConfigAccess) *cobra.Command {
47+
options := &currentContextOptions{
48+
configAccess: configAccess,
49+
streams: streams,
50+
}
51+
52+
cmd := &cobra.Command{
53+
Use: "current-context",
54+
Short: i18n.T("Display the current-context"),
55+
Long: currentContextLong,
56+
Example: fmt.Sprintf(currentContextExample, parentCommand),
57+
Run: func(cmd *cobra.Command, args []string) {
58+
cmdutil.CheckErr(options.run())
59+
},
60+
}
61+
62+
return cmd
63+
}
64+
65+
func (o *currentContextOptions) run() error {
66+
config, err := o.configAccess.GetStartingConfig()
67+
if err != nil {
68+
return err
69+
}
70+
71+
if config.CurrentContext == "" {
72+
err = fmt.Errorf("current-context is not set")
73+
return err
74+
}
75+
76+
fmt.Fprintf(o.streams.Out, "%s\n", config.CurrentContext)
77+
return nil
78+
}

pkg/karmadactl/karmadactl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ import (
2424
"github.com/spf13/cobra"
2525
"github.com/spf13/pflag"
2626
"k8s.io/cli-runtime/pkg/genericclioptions"
27+
"k8s.io/client-go/tools/clientcmd"
2728
apiserverflag "k8s.io/component-base/cli/flag"
2829
"k8s.io/klog/v2"
2930
"k8s.io/kubectl/pkg/util/templates"
3031

3132
"github.com/karmada-io/karmada/pkg/karmadactl/addons"
3233
"github.com/karmada-io/karmada/pkg/karmadactl/apply"
3334
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit"
35+
"github.com/karmada-io/karmada/pkg/karmadactl/config"
3436
"github.com/karmada-io/karmada/pkg/karmadactl/cordon"
3537
"github.com/karmada-io/karmada/pkg/karmadactl/deinit"
3638
"github.com/karmada-io/karmada/pkg/karmadactl/describe"
@@ -133,6 +135,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
133135

134136
rootCmd.AddCommand(sharedcommand.NewCmdVersion(parentCommand))
135137
rootCmd.AddCommand(options.NewCmdOptions(parentCommand, ioStreams.Out))
138+
rootCmd.AddCommand(config.NewCmdConfig(parentCommand, clientcmd.NewDefaultPathOptions(), ioStreams))
136139

137140
templates.ActsAsRootCommand(rootCmd, filters, groups...)
138141

0 commit comments

Comments
 (0)