Skip to content

Commit

Permalink
fix: command create cannot specify the namespace
Browse files Browse the repository at this point in the history
Signed-off-by: zhzhuang-zju <m17799853869@163.com>
  • Loading branch information
zhzhuang-zju committed Sep 10, 2024
1 parent 93a1a49 commit 4a2dc3d
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 21 deletions.
3 changes: 3 additions & 0 deletions pkg/karmadactl/annotate/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
kubectlannotate "k8s.io/kubectl/pkg/cmd/annotate"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand Down Expand Up @@ -54,5 +55,7 @@ func NewCmdAnnotate(f util.Factory, parentCommand string, ioStreams genericioopt
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupSettingsCommands,
}
options.AddKubeConfigFlags(cmd.Flags())
options.AddNamespaceFlag(cmd.Flags())
return cmd
}
1 change: 1 addition & 0 deletions pkg/karmadactl/apiresources/apiresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewCmdAPIResources(f util.Factory, parentCommand string, ioStreams generici
}

o.OperationScope = options.KarmadaControlPlane
options.AddKubeConfigFlags(cmd.Flags())
cmd.Flags().VarP(&o.OperationScope, "operation-scope", "s", "Used to control the operation scope of the command. The optional values are karmada and members. Defaults to karmada.")
cmd.Flags().StringVar(&o.Cluster, "cluster", "", "Used to specify a target member cluster and only takes effect when the command's operation scope is members, for example: --operation-scope=members --cluster=member1")
cmd.Flags().BoolVar(&o.NoHeaders, "no-headers", o.NoHeaders, "When using the default or custom-column output format, don't print headers (default print headers).")
Expand Down
1 change: 1 addition & 0 deletions pkg/karmadactl/apiresources/apiversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewCmdAPIVersions(f util.Factory, parentCommand string, ioStreams genericio
}

o.OperationScope = options.KarmadaControlPlane
options.AddKubeConfigFlags(cmd.Flags())
cmd.Flags().VarP(&o.OperationScope, "operation-scope", "s", "Used to control the operation scope of the command. The optional values are karmada and members. Defaults to karmada.")
cmd.Flags().StringVar(&o.Cluster, "cluster", "", "Used to specify a target member cluster and only takes effect when the command's operation scope is members, for example: --operation-scope=members --cluster=member1")
return cmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/karmadactl/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewCmdApply(f util.Factory, parentCommand string, streams genericiooptions.
o.KubectlApplyFlags.AddFlags(cmd)
flags := cmd.Flags()
options.AddKubeConfigFlags(flags)
flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
options.AddNamespaceFlag(flags)
flags.BoolVarP(&o.AllClusters, "all-clusters", "", o.AllClusters, "If present, propagates a group of resources to all member clusters.")
flags.StringSliceVarP(&o.Clusters, "cluster", "C", o.Clusters, "If present, propagates a group of resources to specified clusters.")
return cmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/karmadactl/attach/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func NewCmdAttach(f util.Factory, parentCommand string, streams genericiooptions
cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodAttachTimeout)
cmdutil.AddContainerVarFlags(cmd, &o.ContainerName, o.ContainerName)
options.AddKubeConfigFlags(cmd.Flags())
options.AddNamespaceFlag(cmd.Flags())
o.OperationScope = options.KarmadaControlPlane
cmd.Flags().BoolVarP(&o.Stdin, "stdin", "i", o.Stdin, "Pass stdin to the container")
cmd.Flags().BoolVarP(&o.TTY, "tty", "t", o.TTY, "Stdin is a TTY")
cmd.Flags().BoolVarP(&o.Quiet, "quiet", "q", o.Quiet, "Only print output from the remote session")
cmd.Flags().StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
cmd.Flags().VarP(&o.OperationScope, "operation-scope", "s", "Used to control the operation scope of the command. The optional values are karmada and members. Defaults to karmada.")
cmd.Flags().StringVar(&o.Cluster, "cluster", "", "Used to specify a target member cluster and only takes effect when the command's operation scope is members, for example: --operation-scope=members --cluster=member1")
return cmd
Expand Down
9 changes: 6 additions & 3 deletions pkg/karmadactl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
kubectlcreate "k8s.io/kubectl/pkg/cmd/create"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand All @@ -45,12 +46,14 @@ var (
)

// NewCmdCreate returns new initialized instance of create sub command
func NewCmdCreate(f util.Factory, parentCommnd string, ioStreams genericiooptions.IOStreams) *cobra.Command {
func NewCmdCreate(f util.Factory, parentCommand string, ioStreams genericiooptions.IOStreams) *cobra.Command {
cmd := kubectlcreate.NewCmdCreate(f, ioStreams)
cmd.Long = fmt.Sprintf(createLong, parentCommnd)
cmd.Example = fmt.Sprintf(createExample, parentCommnd)
cmd.Long = fmt.Sprintf(createLong, parentCommand)
cmd.Example = fmt.Sprintf(createExample, parentCommand)
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupBasic,
}
options.AddKubeConfigFlags(cmd.PersistentFlags())
options.AddNamespaceFlag(cmd.PersistentFlags())
return cmd
}
9 changes: 6 additions & 3 deletions pkg/karmadactl/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
kubectldelete "k8s.io/kubectl/pkg/cmd/delete"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand Down Expand Up @@ -88,12 +89,14 @@ var (
)

// NewCmdDelete returns new initialized instance of delete sub command
func NewCmdDelete(f util.Factory, parentCommnd string, ioStreams genericiooptions.IOStreams) *cobra.Command {
func NewCmdDelete(f util.Factory, parentCommand string, ioStreams genericiooptions.IOStreams) *cobra.Command {
cmd := kubectldelete.NewCmdDelete(f, ioStreams)
cmd.Long = fmt.Sprintf(deleteLong, parentCommnd)
cmd.Example = fmt.Sprintf(deleteExample, parentCommnd)
cmd.Long = fmt.Sprintf(deleteLong, parentCommand)
cmd.Example = fmt.Sprintf(deleteExample, parentCommand)
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupBasic,
}
options.AddKubeConfigFlags(cmd.Flags())
options.AddNamespaceFlag(cmd.Flags())
return cmd
}
2 changes: 1 addition & 1 deletion pkg/karmadactl/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func NewCmdDescribe(f util.Factory, parentCommand string, streams genericiooptio
kubedescribeFlags.AddFlags(cmd)

options.AddKubeConfigFlags(flags)
options.AddNamespaceFlag(flags)
o.OperationScope = options.KarmadaControlPlane
flags.VarP(&o.OperationScope, "operation-scope", "s", "Used to control the operation scope of the command. The optional values are karmada and members. Defaults to karmada.")
flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
flags.StringVarP(&o.Cluster, "cluster", "C", "", "Used to specify a target member cluster and only takes effect when the command's operation scope is members, for example: --operation-scope=members --cluster=member1")

return cmd
Expand Down
3 changes: 3 additions & 0 deletions pkg/karmadactl/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand Down Expand Up @@ -53,5 +54,7 @@ func NewCmdEdit(f util.Factory, parentCommand string, ioStreams genericiooptions
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupBasic,
}
options.AddKubeConfigFlags(cmd.Flags())
options.AddNamespaceFlag(cmd.Flags())
return cmd
}
2 changes: 1 addition & 1 deletion pkg/karmadactl/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NewCmdExec(f util.Factory, parentCommand string, streams genericiooptions.I
o.OperationScope = options.KarmadaControlPlane
flags := cmd.Flags()
options.AddKubeConfigFlags(flags)
flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
options.AddNamespaceFlag(flags)
cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodExecTimeout)
cmdutil.AddJsonFilenameFlag(flags, &o.KubectlExecOptions.FilenameOptions.Filenames, "to use to exec into the resource")
cmdutil.AddContainerVarFlags(cmd, &o.KubectlExecOptions.ContainerName, o.KubectlExecOptions.ContainerName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/karmadactl/explain/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ func NewCmdExplain(f util.Factory, parentCommand string, streams genericiooption

flags := cmd.Flags()
o.OperationScope = options.KarmadaControlPlane
options.AddKubeConfigFlags(flags)
options.AddNamespaceFlag(flags)
flags.VarP(&o.OperationScope, "operation-scope", "s", "Used to control the operation scope of the command. The optional values are karmada and members. Defaults to karmada.")
flags.BoolVar(&o.Recursive, "recursive", o.Recursive, "When true, print the name of all the fields recursively. Otherwise, print the available fields with their description.")
flags.StringVar(&o.APIVersion, "api-version", o.APIVersion, "Use given api-version (group/version) of the resource.")

// Only enable --output as a valid flag if the feature is enabled
flags.StringVar(&o.OutputFormat, "output", plaintextTemplateName, "Format in which to render the schema. Valid values are: (plaintext, plaintext-openapiv2).")

flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
flags.StringVar(&o.Cluster, "cluster", "", "Used to specify a target member cluster and only takes effect when the command's operation scope is member clusters, for example: --operation-scope=all --cluster=member1")
return cmd
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/karmadactl/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ func NewCmdGet(f util.Factory, parentCommand string, streams genericiooptions.IO
o.PrintFlags.AddFlags(cmd)
flags := cmd.Flags()
options.AddKubeConfigFlags(flags)
options.AddNamespaceFlag(flags)
o.OperationScope = options.KarmadaControlPlane
flags.VarP(&o.OperationScope, "operation-scope", "s", "Used to control the operation scope of the command. The optional values are karmada, members, and all. Defaults to karmada.")
flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
flags.StringVarP(&o.LabelSelector, "labels", "l", "", "-l=label or -l label")
flags.StringSliceVarP(&o.Clusters, "clusters", "C", []string{}, "Used to specify target member clusters and only takes effect when the command's operation scope is members or all, for example: --operation-scope=all --clusters=member1,member2")
flags.BoolVarP(&o.AllNamespaces, "all-namespaces", "A", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
Expand Down
3 changes: 3 additions & 0 deletions pkg/karmadactl/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
kubectllabel "k8s.io/kubectl/pkg/cmd/label"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand Down Expand Up @@ -53,5 +54,7 @@ func NewCmdLabel(f util.Factory, parentCommand string, ioStreams genericiooption
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupSettingsCommands,
}
options.AddKubeConfigFlags(cmd.Flags())
options.AddNamespaceFlag(cmd.Flags())
return cmd
}
2 changes: 1 addition & 1 deletion pkg/karmadactl/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewCmdLogs(f util.Factory, parentCommand string, streams genericiooptions.I

flags := cmd.Flags()
options.AddKubeConfigFlags(flags)
flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
options.AddNamespaceFlag(flags)
flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")
o.KubectlLogsOptions.AddFlags(cmd)

Expand Down
5 changes: 5 additions & 0 deletions pkg/karmadactl/options/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func AddKubeConfigFlags(flags *pflag.FlagSet) {
flags.StringVar(DefaultConfigFlags.Context, "karmada-context", *DefaultConfigFlags.Context, "The name of the kubeconfig context to use")
}

// AddNamespaceFlag add namespace flag to the specified FlagSet.
func AddNamespaceFlag(flags *pflag.FlagSet) {
flags.StringVarP(DefaultConfigFlags.Namespace, "namespace", "n", *DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request.")
}

// OperationScope defines the operation scope of a command.
type OperationScope string

Expand Down
3 changes: 3 additions & 0 deletions pkg/karmadactl/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
kubectlpatch "k8s.io/kubectl/pkg/cmd/patch"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand Down Expand Up @@ -49,5 +50,7 @@ func NewCmdPatch(f util.Factory, parentCommand string, ioStreams genericiooption
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupAdvancedCommands,
}
options.AddKubeConfigFlags(cmd.Flags())
options.AddNamespaceFlag(cmd.Flags())
return cmd
}
10 changes: 4 additions & 6 deletions pkg/karmadactl/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func NewCmdPromote(f util.Factory, parentCommand string) *cobra.Command {
flag := cmd.Flags()
opts.AddFlags(flag)
options.AddKubeConfigFlags(flag)
options.AddNamespaceFlag(flag)

return cmd
}
Expand Down Expand Up @@ -176,7 +177,6 @@ func (o *CommandPromoteOption) AddFlags(flags *pflag.FlagSet) {
"The name of the PropagationPolicy(or ClusterPropagationPolicy) that is automatically created after promotion. If not specified, the name will be the resource name with a hash suffix that is generated by resource metadata.")
flags.StringVarP(&o.OutputFormat, "output", "o", "", "Output format. One of: json|yaml")

flags.StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request")
flags.StringVarP(&o.Cluster, "cluster", "C", "", "the name of legacy cluster (eg -C=member1)")
flags.StringVar(&o.ClusterContext, "cluster-context", "",
"Context name of legacy cluster in kubeconfig. Only works when there are multiple contexts in the kubeconfig.")
Expand Down Expand Up @@ -213,11 +213,9 @@ func (o *CommandPromoteOption) Complete(f util.Factory, args []string) error {
}
}

if o.Namespace == "" {
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return fmt.Errorf("failed to get namespace from Factory. error: %w", err)
}
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return fmt.Errorf("failed to get namespace from Factory. error: %w", err)
}

// If '--cluster-context' not specified, take the cluster name as the context.
Expand Down
2 changes: 2 additions & 0 deletions pkg/karmadactl/top/top_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1"
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

Expand Down Expand Up @@ -112,6 +113,7 @@ func NewCmdTopNode(f util.Factory, parentCommand string, o *NodeOptions, streams
Aliases: []string{"nodes", "no"},
}
cmdutil.AddLabelSelectorFlagVar(cmd, &o.Selector)
options.AddKubeConfigFlags(cmd.Flags())
cmd.Flags().StringVar(&o.SortBy, "sort-by", o.SortBy, "If non-empty, sort nodes list using specified field. The field can be either 'cpu' or 'memory'.")
cmd.Flags().StringSliceVar(&o.Clusters, "clusters", []string{}, "Used to specify target member clusters, for example: --clusters=member1,member2")
cmd.Flags().BoolVar(&o.NoHeaders, "no-headers", o.NoHeaders, "If present, print output without headers")
Expand Down
2 changes: 1 addition & 1 deletion pkg/karmadactl/top/top_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func NewCmdTopPod(f util.Factory, parentCommand string, o *PodOptions, streams g
}
cmdutil.AddLabelSelectorFlagVar(cmd, &o.LabelSelector)
options.AddKubeConfigFlags(cmd.Flags())
cmd.Flags().StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
options.AddNamespaceFlag(cmd.Flags())
cmd.Flags().StringSliceVarP(&o.Clusters, "clusters", "C", []string{}, "-C=member1,member2")
cmd.Flags().StringVar(&o.FieldSelector, "field-selector", o.FieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
cmd.Flags().StringVar(&o.SortBy, "sort-by", o.SortBy, "If non-empty, sort pods list using specified field. The field can be either 'cpu' or 'memory'.")
Expand Down

0 comments on commit 4a2dc3d

Please sign in to comment.