New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce factory interface for karmadactl #2202
Conversation
b7f099e
to
25f749b
Compare
a7dfd63
to
eae85e6
Compare
Looks good, I will further look it. |
/assign |
Hi @lonelyCZ, any suggestions here? |
eae85e6
to
b1839aa
Compare
/cc @lonelyCZ |
Sorry for delay, I will back ASAP after previous prs. |
Hi @lonelyCZ, any suggestions here? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great thanks @carlory , I think we can push it forward now.
7a8118c
to
65836e5
Compare
/retitle introduce factory interface for karmadactl |
@lonelyCZ rename |
I like it. Could you please give a usage example for one subcommand that will be refactored by this interface? |
65836e5
to
2c3c7ea
Compare
@ lonelyCZ karmadactl logs uses factory to access member cluster is an example. (⎈ |karmada:default)➜ karmada git:(karmadactl-factory) go run cmd/karmadactl/karmadactl.go logs -C ocp master-etcd-nodedsp01.ocp.ats.io -n kube-system --tail 10
2022-08-09 15:21:51.800610 W | etcdserver: apply entries took too long [103.846094ms for 1 entries]
2022-08-09 15:21:51.800698 W | etcdserver: avoid queries with large range/delete range!
2022-08-09 15:24:04.745515 I | mvcc: store.index: compact 182209811
2022-08-09 15:24:04.877300 I | mvcc: finished scheduled compaction at 182209811 (took 125.525534ms)
2022-08-09 15:24:29.974311 W | etcdserver: apply entries took too long [120.414531ms for 1 entries]
2022-08-09 15:24:29.974353 W | etcdserver: avoid queries with large range/delete range!
2022-08-09 15:25:41.787238 W | wal: sync duration of 1.107194589s, expected less than 1s
2022-08-09 15:26:29.805921 W | wal: sync duration of 1.55465076s, expected less than 1s
2022-08-09 15:27:23.477009 W | etcdserver: apply entries took too long [118.29844ms for 1 entries]
2022-08-09 15:27:23.477058 W | etcdserver: avoid queries with large range/delete range!
(⎈ |karmada:default)➜ karmada git:(karmadactl-factory) go run cmd/karmadactl/karmadactl.go logs -C ocp router-1-hw9vw --tail 5
- Health check ok : 0 retry attempt(s).
I0809 07:26:05.182271 1 router.go:481] Router reloaded:
- Checking http://localhost:80 ...
- Health check ok : 0 retry attempt(s).
W0809 07:27:12.762383 1 reflector.go:272] github.com/openshift/origin/pkg/router/controller/factory/factory.go:112: watch of *v1.Route ended with: The resourceVersion for the provided watch is too old. |
/cc @lonelyCZ @prodanlabs |
Looks good to me. /cc @RainbowMango |
Hi @RainbowMango , any suggestions here? |
Please take a look @RainbowMango |
OK. I'll try to finish it by the end of this week. |
What's the |
@RainbowMango karmadactl logs uses factory to access member cluster is an example. |
karmadaRestConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig) | ||
if err != nil { | ||
return fmt.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v", | ||
o.KarmadaContext, o.KubeConfig, err) | ||
} | ||
clusterInfo, err := getClusterInfo(karmadaRestConfig, o.Cluster, o.KubeConfig, o.KarmadaContext) | ||
memberFactory, err := f.FactoryForMemberCluster(o.Cluster) | ||
if err != nil { | ||
return err | ||
} | ||
f := getFactory(o.Cluster, clusterInfo, o.Namespace) | ||
return o.KubectlLogsOptions.Complete(f, cmd, args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At present, there are many places in karmadactl, which are building clients and factories repeatedly, such as init, get, log, exec, etc.
Is it you mean building clients and factories repeatedly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, each subcommand has the logic to create client and cmdutil.Factory, for example:
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/apply.go#L114
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/cordon.go#L223
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/deinit.go#L92
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/describe.go#L105
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/exec.go#L114
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/get.go#L294
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/join.go#L161
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/join.go#L242
- https://github.com/karmada-io/karmada/blob/master/pkg/karmadactl/promote.go#L207
I expect each sub command take factory as input parameter. factory provides functions to create kube client, karmada client and cmdutil.Factory for member cluster.
groups := templates.CommandGroups{
{
Message: "Basic Commands:",
Commands: []*cobra.Command{
NewCmdGet(factory, parentCommand, ioStreams),
},
},
{
Message: "Cluster Registeration Commands:",
Commands: []*cobra.Command{
cmdinit.NewCmdInit(factory, parentCommand),
NewCmdDeInit(factory, parentCommand),
addons.NewCommandAddons(factory, parentCommand),
NewCmdJoin(factory, parentCommand),
NewCmdUnjoin(factory, parentCommand),
},
},
{
Message: "Cluster Management Commands:",
Commands: []*cobra.Command{
NewCmdCordon(factory, parentCommand),
NewCmdUncordon(factory, parentCommand),
NewCmdTaint(factory, parentCommand),
},
},
{
Message: "Troubleshooting and Debugging Commands:",
Commands: []*cobra.Command{
NewCmdLogs(factory, parentCommand, ioStreams),
NewCmdExec(factory, parentCommand, ioStreams),
NewCmdDescribe(factory, parentCommand, ioStreams),
},
},
{
Message: "Advanced Commands:",
Commands: []*cobra.Command{
NewCmdApply(factory, parentCommand, ioStreams),
NewCmdPromote(factory, parentCommand),
},
},
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems not all subcommands needing karmadaclientset
, should we need to provide a common kubeclientset
in my Factory
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factory extends cmdutil.Factory, so kubeclient can be created by this factory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factory extends cmdutil.Factory, so kubeclient can be created by this factory.
How to do it for deinit
subcommand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In future, the adm and control commands possibly will be seprated, like kubectl
and kubeadm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In future, the adm and control commands possibly will be seprated, like
kubectl
andkubeadm
I like it.
How to directly use
KubernetesClientSet(kubeconfig, context string)
?But I am worried about that we init the unuseful
KarmadaClientSet
forinit
,deinit
commends. Perhaps, we can don't care these cmds firstly, not need to passf
to them.
It makes sense for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lonelyCZ Does this PR need to be changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lonelyCZ ok
230ada6
to
e2fb179
Compare
@RainbowMango this issue #2349 list things we need to do |
I'm good with it. But I'll leave approval to the owner @lonelyCZ . |
Signed-off-by: carlory <baofa.fan@daocloud.io>
e2fb179
to
8798f33
Compare
Hi @carlory, sorry for delaying too long, let's back. |
pkg/karmadactl/logs.go
Outdated
@@ -50,18 +50,20 @@ var ( | |||
) | |||
|
|||
// NewCmdLogs new logs command. | |||
func NewCmdLogs(karmadaConfig KarmadaConfig, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command { | |||
// TODO(@carlory): take a factory as an argument when we resolve conflicts on flags. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we directly resolve this TODO
in this PR? Move f := util.NewFactory(defaultConfigFlags)
to pkg/karmadactl
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lonelyCZ This requires changing all subcommands, I filled #2349 to track this task.
(⎈ |karmada:default)➜ karmada git:(karmadactl-factory) ✗ go run cmd/karmadactl/karmadactl.go
panic: karmadactl flag redefined: kubeconfig
goroutine 1 [running]:
github.com/spf13/pflag.(*FlagSet).AddFlag(0x140000de600, 0x14000924b40)
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/vendor/github.com/spf13/pflag/flag.go:848 +0x548
github.com/spf13/pflag.(*FlagSet).VarPF(0xa?, {0x106849978, 0x140003df460}, {0x106049248, 0xa}, {0x0, 0x0}, {0x10609b245, 0x34})
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/vendor/github.com/spf13/pflag/flag.go:831 +0x140
github.com/spf13/pflag.(*FlagSet).VarP(...)
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/vendor/github.com/spf13/pflag/flag.go:837
github.com/spf13/pflag.(*FlagSet).StringVar(0x3?, 0x1020100?, {0x106049248?, 0xffffffffffffffff?}, {0x0?, 0x10602b450?}, {0x10609b245?, 0x10602b448?})
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/vendor/github.com/spf13/pflag/string.go:37 +0x88
k8s.io/cli-runtime/pkg/genericclioptions.(*ConfigFlags).AddFlags(0x140002c9200, 0x0?)
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/vendor/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go:327 +0x58
github.com/karmada-io/karmada/pkg/karmadactl.NewKarmadaCtlCommand({0x106049234, 0xa}, {0x106049234, 0xa})
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/pkg/karmadactl/karmadactl.go:55 +0x23c
main.main()
/Users/kiki/workspace/golang/src/github.com/karmada-io/karmada/cmd/karmadactl/karmadactl.go:11 +0x34
exit status 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tested it that seems no problems. How do you move it?
My test is shown below
func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
....
f := util.NewFactory(defaultConfigFlags)
....
Commands: []*cobra.Command{
NewCmdLogs(f, parentCommand, ioStreams),
NewCmdExec(karmadaConfig, parentCommand, ioStreams),
NewCmdDescribe(karmadaConfig, parentCommand, ioStreams),
},
func NewCmdLogs(f util.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tested it that seems no problems. How do you move it?
I make the defaultConfigFlags as global command-line options in the karmadactl.go
.
Signed-off-by: carlory <baofa.fan@daocloud.io> Co-authored-by: Hongcai Ren <renhongcai@huawei.com>
8798f33
to
eb31ff4
Compare
@lonelyCZ updated. |
Thanks @carlory , I just tested it that worked fine. /lgtm PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: RainbowMango The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind feature
What this PR does / why we need it:
At present, there are many places in karmadactl, which are building clients and factories repeatedly, such as init, get, log, exec, etc.
The purpose of this PR is to provide a unified place to get clients accessing the cluster and factories needed to access sub-clusters.
With this change, after a little refactoring, the following files can be removed in the future:
An example of the alpha command is provided to make it easier for reviewers to understand the usage. It will be removed after feedback is collected.
Does this PR introduce a user-facing change?: