Merge pull request #5487 from zhzhuang-zju/namespace
fix: command create cannot specify the namespace
This commit is contained in:
commit
aaae246280
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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).")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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.")
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -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.")
|
||||
|
@ -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.
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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")
|
||||
|
|
|
@ -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'.")
|
||||
|
|
Loading…
Reference in New Issue