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 6, 2024
1 parent 32c2ef7 commit 8da2748
Show file tree
Hide file tree
Showing 19 changed files with 21 additions and 15 deletions.
2 changes: 2 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,6 @@ func NewCmdAnnotate(f util.Factory, parentCommand string, ioStreams genericioopt
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupSettingsCommands,
}
options.AddKubeConfigFlags(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
1 change: 0 additions & 1 deletion pkg/karmadactl/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ 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")
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
1 change: 0 additions & 1 deletion pkg/karmadactl/attach/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func NewCmdAttach(f util.Factory, parentCommand string, streams genericiooptions
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
2 changes: 2 additions & 0 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 Down Expand Up @@ -52,5 +53,6 @@ func NewCmdCreate(f util.Factory, parentCommnd string, ioStreams genericiooption
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupBasic,
}
options.AddKubeConfigFlags(cmd.PersistentFlags())
return cmd
}
2 changes: 2 additions & 0 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 @@ -95,5 +96,6 @@ func NewCmdDelete(f util.Factory, parentCommnd string, ioStreams genericiooption
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupBasic,
}
options.AddKubeConfigFlags(cmd.Flags())
return cmd
}
1 change: 0 additions & 1 deletion pkg/karmadactl/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func NewCmdDescribe(f util.Factory, parentCommand string, streams genericiooptio
options.AddKubeConfigFlags(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
2 changes: 2 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,6 @@ func NewCmdEdit(f util.Factory, parentCommand string, ioStreams genericiooptions
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupBasic,
}
options.AddKubeConfigFlags(cmd.Flags())
return cmd
}
1 change: 0 additions & 1 deletion pkg/karmadactl/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ 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")
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
3 changes: 1 addition & 2 deletions pkg/karmadactl/explain/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ func NewCmdExplain(f util.Factory, parentCommand string, streams genericiooption

flags := cmd.Flags()
o.OperationScope = options.KarmadaControlPlane
options.AddKubeConfigFlags(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
1 change: 0 additions & 1 deletion pkg/karmadactl/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func NewCmdGet(f util.Factory, parentCommand string, streams genericiooptions.IO
options.AddKubeConfigFlags(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
2 changes: 2 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,6 @@ func NewCmdLabel(f util.Factory, parentCommand string, ioStreams genericiooption
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupSettingsCommands,
}
options.AddKubeConfigFlags(cmd.Flags())
return cmd
}
1 change: 0 additions & 1 deletion pkg/karmadactl/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ 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")
flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")
o.KubectlLogsOptions.AddFlags(cmd)

Expand Down
1 change: 1 addition & 0 deletions pkg/karmadactl/options/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var DefaultConfigFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPa
func AddKubeConfigFlags(flags *pflag.FlagSet) {
flags.StringVar(DefaultConfigFlags.KubeConfig, "kubeconfig", *DefaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
flags.StringVar(DefaultConfigFlags.Context, "karmada-context", *DefaultConfigFlags.Context, "The name of the kubeconfig context to use")
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.
Expand Down
2 changes: 2 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,6 @@ func NewCmdPatch(f util.Factory, parentCommand string, ioStreams genericiooption
cmd.Annotations = map[string]string{
util.TagCommandGroup: util.GroupAdvancedCommands,
}
options.AddKubeConfigFlags(cmd.Flags())
return cmd
}
9 changes: 3 additions & 6 deletions pkg/karmadactl/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,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 +212,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
1 change: 0 additions & 1 deletion pkg/karmadactl/top/top_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ 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")
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 8da2748

Please sign in to comment.