Skip to content
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

refactoring for easy unit-test and cm-cli integration #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,10 @@ Anyone can comment on issues and submit reviews for pull requests. In order to b
- The project tries to follow the following grammar for the commands:

```bash
clusteradm <verb> [<noun>]
clusteradm <cmd> [subcmd] [flags]
```

- A number of verbs are already defined in [verbs](pkg/cmd/verbs/verbs.go), if you would like to add a new verb or noun, please contact the [OWNERS](OWNERS).

- The noun represents the object on which the verb applies.

- Each pair (verb/[noum]) has its own package.

- Inside the package, the code is split in 3 files: The [cmd.go](pkg/cmd/version/cmd.go) which creates the cobra command, the [options.go](pkg/cmd/version/options.go) which defines the different option parameters for the command and the the [exec.go](pkg/cmd/version/exec.go) which contains the code to execute the command.
- Each cmd/subcmd are in a package, the code is split in 3 files: The [cmd.go](pkg/cmd/version/cmd.go) which creates the cobra command, the [options.go](pkg/cmd/version/options.go) which defines the different option parameters for the command and the the [exec.go](pkg/cmd/version/exec.go) which contains the code to execute the command.


## Resources
Expand Down
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
0.0.2
42 changes: 27 additions & 15 deletions cmd/clusteradm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import (
"k8s.io/kubectl/pkg/cmd/options"
"k8s.io/kubectl/pkg/cmd/plugin"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"open-cluster-management.io/clusteradm/pkg/cmd/verbs"
ktemplates "k8s.io/kubectl/pkg/util/templates"
"open-cluster-management.io/clusteradm/pkg/cmd/version"

acceptclusters "open-cluster-management.io/clusteradm/pkg/cmd/accept"
inithub "open-cluster-management.io/clusteradm/pkg/cmd/init"
joinhub "open-cluster-management.io/clusteradm/pkg/cmd/join"
)

func main() {
Expand All @@ -22,7 +27,10 @@ func main() {
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(configFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

root := newCmdVerbs("clusteradm", f, streams)
root :=
&cobra.Command{
Use: "clusteradm",
}

flags := root.PersistentFlags()
matchVersionKubeConfigFlags.AddFlags(flags)
Expand All @@ -39,20 +47,24 @@ func main() {
plugin.ValidPluginFilenamePrefixes = []string{os.Args[0]}
root.AddCommand(plugin.NewCmdPlugin(f, streams))

groups := ktemplates.CommandGroups{
{
Message: "General commands:",
Commands: []*cobra.Command{
version.NewCmd(f, streams),
},
},
{
Message: "Registration commands:",
Commands: []*cobra.Command{
inithub.NewCmd(f, streams),
joinhub.NewCmd(f, streams),
acceptclusters.NewCmd(f, streams),
},
},
}
groups.Add(root)
if err := root.Execute(); err != nil {
os.Exit(1)
}
}

// NewCmdNamespace provides a cobra command wrapping NamespaceOptions
func newCmdVerbs(parent string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{Use: parent}
cmd.AddCommand(
verbs.NewVerbVersion("version", f, streams),
verbs.NewVerbInit("init", f, streams),
verbs.NewVerbJoin("join", f, streams),
verbs.NewVerbAccept("accept", f, streams),
)

return cmd
}
4 changes: 0 additions & 4 deletions pkg/cmd/accept/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ var example = `
%[1]s accept --clusters <cluster_1>,<cluster_2>,...
`

const (
scenarioDirectory = "accept"
)

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(f, streams)
Expand Down
4 changes: 0 additions & 4 deletions pkg/cmd/init/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ var example = `
%[1]s init
`

const (
scenarioDirectory = "init"
)

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(f, streams)
Expand Down
24 changes: 20 additions & 4 deletions pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package init

import (
"fmt"
"time"

"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"open-cluster-management.io/clusteradm/pkg/cmd/init/scenario"
"open-cluster-management.io/clusteradm/pkg/helpers"
"open-cluster-management.io/clusteradm/pkg/helpers/apply"

"github.com/spf13/cobra"

apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/discovery"
"k8s.io/client-go/util/retry"
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
Expand Down Expand Up @@ -45,8 +48,13 @@ func (o *Options) run() error {
return err
}

apiExtensionsClient, err := apiextensionsclient.NewForConfig(restConfig)
if err != nil {
return err
}

clientHolder := resourceapply.NewClientHolder().
WithAPIExtensionsClient(apiextensionsclient.NewForConfigOrDie(restConfig)).
WithAPIExtensionsClient(apiExtensionsClient).
WithKubernetes(kubeClient).
WithDynamicClient(dynamicClient)

Expand All @@ -61,17 +69,25 @@ func (o *Options) run() error {
"init/service_account.yaml",
}

err = helpers.ApplyDirectly(clientHolder, reader, scenarioDirectory, o.values, files...)
err = apply.ApplyDirectly(clientHolder, reader, o.values, "", files...)
if err != nil {
return err
}

err = helpers.ApplyDeployment(kubeClient, reader, scenarioDirectory, o.values, "init/operator.yaml")
err = apply.ApplyDeployment(kubeClient, reader, o.values, "", "init/operator.yaml")
if err != nil {
return err
}

b := retry.DefaultBackoff
b.Duration = 100 * time.Millisecond
err = helpers.WaitCRDToBeReady(*apiExtensionsClient, "clustermanagers.operator.open-cluster-management.io", b)
if err != nil {
return err
}

discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(restConfig)
err = helpers.ApplyCustomResouces(dynamicClient, discoveryClient, reader, scenarioDirectory, o.values, "init/clustermanagers.cr.yaml")
err = apply.ApplyCustomResouces(dynamicClient, discoveryClient, reader, o.values, "", "init/clustermanagers.cr.yaml")
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/init/scenario/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package scenario
import (
"embed"

"open-cluster-management.io/clusteradm/pkg/helpers"
"open-cluster-management.io/clusteradm/pkg/helpers/asset"
)

//go:embed init
var files embed.FS

func GetScenarioResourcesReader() *helpers.ScenarioResourcesReader {
return helpers.NewScenarioResourcesReader(&files)
func GetScenarioResourcesReader() *asset.ScenarioResourcesReader {
return asset.NewScenarioResourcesReader(&files)
}
4 changes: 0 additions & 4 deletions pkg/cmd/join/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ var example = `
%[1]s join --hub-token <tokenID.tokenSecret> --hub-apiserver <hub_apiserveR_url> --name <cluster_name>
`

const (
scenarioDirectory = "join"
)

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(f, streams)
Expand Down
26 changes: 22 additions & 4 deletions pkg/cmd/join/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ package join

import (
"fmt"
"time"

"github.com/ghodss/yaml"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"

// "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
clientcmdapiv1 "k8s.io/client-go/tools/clientcmd/api/v1"
"k8s.io/client-go/util/retry"
"open-cluster-management.io/clusteradm/pkg/cmd/join/scenario"
"open-cluster-management.io/clusteradm/pkg/helpers"
"open-cluster-management.io/clusteradm/pkg/helpers/apply"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -75,8 +80,13 @@ func (o *Options) run() error {
return err
}

apiExtensionsClient, err := apiextensionsclient.NewForConfig(restConfig)
if err != nil {
return err
}

clientHolder := resourceapply.NewClientHolder().
WithAPIExtensionsClient(apiextensionsclient.NewForConfigOrDie(restConfig)).
WithAPIExtensionsClient(apiExtensionsClient).
WithKubernetes(kubeClient).
WithDynamicClient(dynamicClient)

Expand All @@ -90,18 +100,26 @@ func (o *Options) run() error {
"join/service_account.yaml",
}

err = helpers.ApplyDirectly(clientHolder, reader, scenarioDirectory, o.values, files...)
err = apply.ApplyDirectly(clientHolder, reader, o.values, "", files...)
if err != nil {
return err
}

err = helpers.ApplyDeployment(kubeClient, reader, scenarioDirectory, o.values, "join/operator.yaml")
err = apply.ApplyDeployment(kubeClient, reader, o.values, "", "join/operator.yaml")
if err != nil {
return err
}

b := retry.DefaultBackoff
b.Duration = 100 * time.Millisecond

err = helpers.WaitCRDToBeReady(*apiExtensionsClient, "klusterlets.operator.open-cluster-management.io", b)
if err != nil {
return err
}

discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(restConfig)
err = helpers.ApplyCustomResouces(dynamicClient, discoveryClient, reader, scenarioDirectory, o.values, "join/klusterlets.cr.yaml")
err = apply.ApplyCustomResouces(dynamicClient, discoveryClient, reader, o.values, "", "join/klusterlets.cr.yaml")
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/join/scenario/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package scenario
import (
"embed"

"open-cluster-management.io/clusteradm/pkg/helpers"
"open-cluster-management.io/clusteradm/pkg/helpers/asset"
)

//go:embed join
var files embed.FS

func GetScenarioResourcesReader() *helpers.ScenarioResourcesReader {
return helpers.NewScenarioResourcesReader(&files)
func GetScenarioResourcesReader() *asset.ScenarioResourcesReader {
return asset.NewScenarioResourcesReader(&files)
}
32 changes: 0 additions & 32 deletions pkg/cmd/verbs/verbs.go

This file was deleted.

Loading