Skip to content

Commit

Permalink
Made dry-run persistenflag
Browse files Browse the repository at this point in the history
Signed-off-by: Dominique Vernier <[email protected]>
  • Loading branch information
itdove committed Jun 9, 2021
1 parent 5770641 commit e7320e8
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 149 deletions.
4 changes: 2 additions & 2 deletions build/run-functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ else
echo "join command result: " $CMDJOINRESULT
fi

echo "Sleep 2 min to stabilize"
echo "Sleep 4 min to stabilize"
# we need to wait 2 min but once we will have watch status monitor
# we will not need to sleep anymore
sleep 120
sleep 240

CMDACCEPT=`echo $CMDJOINRESULT | cut -d ':' -f2`
CMDACCEPT="$CMDACCEPT c1"
Expand Down
16 changes: 11 additions & 5 deletions cmd/clusteradm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"flag"
"os"

"github.com/spf13/cobra"
Expand All @@ -19,13 +20,15 @@ import (
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"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)

func main() {
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
configFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(configFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
clusteradmFlags := genericclioptionsclusteradm.NewClusteradmFlags(f)

root :=
&cobra.Command{
Expand All @@ -40,7 +43,10 @@ func main() {
// From this point and forward we get warnings on flags that contain "_" separators
root.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)

configFlags.AddFlags(flags)
configFlags.AddFlags(root.PersistentFlags())
clusteradmFlags.AddFlags(root.PersistentFlags())
flags.AddGoFlagSet(flag.CommandLine)

root.AddCommand(cmdconfig.NewCmdConfig(f, clientcmd.NewDefaultPathOptions(), streams))
root.AddCommand(options.NewCmdOptions(streams.Out))
//enable plugin functionality: all `os.Args[0]-<binary>` in the $PATH will be available for plugin
Expand All @@ -51,15 +57,15 @@ func main() {
{
Message: "General commands:",
Commands: []*cobra.Command{
version.NewCmd(f, streams),
version.NewCmd(clusteradmFlags, streams),
},
},
{
Message: "Registration commands:",
Commands: []*cobra.Command{
inithub.NewCmd(f, streams),
joinhub.NewCmd(f, streams),
acceptclusters.NewCmd(f, streams),
inithub.NewCmd(clusteradmFlags, streams),
joinhub.NewCmd(clusteradmFlags, streams),
acceptclusters.NewCmd(clusteradmFlags, streams),
},
},
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/onsi/gomega v1.10.2 // indirect
github.com/openshift/library-go v0.0.0-20210521084623-7392ea9b02ca
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
google.golang.org/appengine v1.6.6 // indirect
k8s.io/api v0.21.1
k8s.io/apiextensions-apiserver v0.21.1
Expand Down
10 changes: 6 additions & 4 deletions pkg/cmd/accept/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package accept
import (
"fmt"

genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/helpers"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

var example = `
Expand All @@ -17,14 +17,17 @@ var example = `
`

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(f, streams)
func NewCmd(clusteradmFlages *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(clusteradmFlages, streams)

cmd := &cobra.Command{
Use: "accept",
Short: "accept a list of clusters",
Example: fmt.Sprintf(example, helpers.GetExampleHeader()),
SilenceUsage: true,
PreRun: func(c *cobra.Command, args []string) {
helpers.DryRunMessage(o.ClusteradmFlags.DryRun)
},
RunE: func(c *cobra.Command, args []string) error {
if err := o.complete(c, args); err != nil {
return err
Expand All @@ -41,7 +44,6 @@ func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Comma
}

cmd.Flags().StringVar(&o.clusters, "clusters", "", "Names of the cluster to accept (comma separated)")
cmd.Flags().BoolVar(&o.dryRun, "dry-run", false, "If set the generated resources will be displayed but not applied")

return cmd
}
10 changes: 5 additions & 5 deletions pkg/cmd/accept/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (o *Options) validate() error {
}

func (o *Options) run() error {
kubeClient, err := o.factory.KubernetesClientSet()
kubeClient, err := o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
return err
}
restConfig, err := o.factory.ToRESTConfig()
restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient
}

if csr != nil {
if !o.dryRun {
if !o.ClusteradmFlags.DryRun {
if csr.Status.Conditions == nil {
csr.Status.Conditions = make([]certificatesv1.CertificateSigningRequestCondition, 0)
}
Expand All @@ -118,7 +118,7 @@ func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient
LastUpdateTime: metav1.Now(),
})

kubeClient, err := o.factory.KubernetesClientSet()
kubeClient, err := o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
return err
}
Expand All @@ -139,7 +139,7 @@ func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient
return err
}
if !mc.Spec.HubAcceptsClient {
if !o.dryRun {
if !o.ClusteradmFlags.DryRun {
mc.Spec.HubAcceptsClient = true
_, err = clusterClient.ClusterV1().ManagedClusters().Update(context.TODO(), mc, metav1.UpdateOptions{})
if err != nil {
Expand Down
15 changes: 5 additions & 10 deletions pkg/cmd/accept/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@
package accept

import (
cmdutil "k8s.io/kubectl/pkg/cmd/util"

"k8s.io/cli-runtime/pkg/genericclioptions"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)

type Options struct {
//ConfigFlags: The generic options from the kubernetes cli-runtime.
ConfigFlags *genericclioptions.ConfigFlags
factory cmdutil.Factory
//ClusteradmFlags: The generic optiosn from the clusteradm cli-runtime.
ClusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags
//A list of comma separated cluster names
clusters string
values Values
//if set the resources will be sent to stdout instead of being applied
dryRun bool
}

//Values: The values used in the template
type Values struct {
clusters []string
}

func newOptions(f cmdutil.Factory, streams genericclioptions.IOStreams) *Options {
func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options {
return &Options{
ConfigFlags: genericclioptions.NewConfigFlags(true),
factory: f,
ClusteradmFlags: clusteradmFlags,
}
}
11 changes: 7 additions & 4 deletions pkg/cmd/init/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)

var example = `
Expand All @@ -17,14 +17,17 @@ var example = `
`

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(f, streams)
func NewCmd(clusteradmFlages *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(clusteradmFlages, streams)

cmd := &cobra.Command{
Use: "init",
Short: "init the hub",
Example: fmt.Sprintf(example, helpers.GetExampleHeader()),
SilenceUsage: true,
PreRun: func(c *cobra.Command, args []string) {
helpers.DryRunMessage(o.ClusteradmFlags.DryRun)
},
RunE: func(c *cobra.Command, args []string) error {
if err := o.complete(c, args); err != nil {
return err
Expand All @@ -39,7 +42,7 @@ func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Comma
return nil
},
}
cmd.Flags().BoolVar(&o.dryRun, "dry-run", false, "If set the generated resources will be displayed but not applied")

cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")

return cmd
Expand Down
14 changes: 7 additions & 7 deletions pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ func (o *Options) run() error {
output := make([]string, 0)
reader := scenario.GetScenarioResourcesReader()

kubeClient, err := o.factory.KubernetesClientSet()
kubeClient, err := o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
return err
}
dynamicClient, err := o.factory.DynamicClient()
dynamicClient, err := o.ClusteradmFlags.KubectlFactory.DynamicClient()
if err != nil {
return err
}

restConfig, err := o.factory.ToRESTConfig()
restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
Expand All @@ -70,19 +70,19 @@ func (o *Options) run() error {
"init/service_account.yaml",
}

out, err := apply.ApplyDirectly(clientHolder, reader, o.values, o.dryRun, "", files...)
out, err := apply.ApplyDirectly(clientHolder, reader, o.values, o.ClusteradmFlags.DryRun, "", files...)
if err != nil {
return err
}
output = append(output, out...)

out, err = apply.ApplyDeployment(kubeClient, reader, o.values, o.dryRun, "", "init/operator.yaml")
out, err = apply.ApplyDeployments(kubeClient, reader, o.values, o.ClusteradmFlags.DryRun, "", "init/operator.yaml")
if err != nil {
return err
}
output = append(output, out...)

if !o.dryRun {
if !o.ClusteradmFlags.DryRun {
b := retry.DefaultBackoff
b.Duration = 100 * time.Millisecond
err = helpers.WaitCRDToBeReady(*apiExtensionsClient, "clustermanagers.operator.open-cluster-management.io", b)
Expand All @@ -92,7 +92,7 @@ func (o *Options) run() error {
}

discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(restConfig)
out, err = apply.ApplyCustomResouces(dynamicClient, discoveryClient, reader, o.values, o.dryRun, "", "init/clustermanagers.cr.yaml")
out, err = apply.ApplyCustomResouces(dynamicClient, discoveryClient, reader, o.values, o.ClusteradmFlags.DryRun, "", "init/clustermanagers.cr.yaml")
if err != nil {
return err
}
Expand Down
17 changes: 7 additions & 10 deletions pkg/cmd/init/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package init

import (
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
)

//Options: The structure holding all the command-line options
type Options struct {
//ConfigFlags: The generic options from the kubernetes cli-runtime.
ConfigFlags *genericclioptions.ConfigFlags
factory cmdutil.Factory
values Values
//if set the resources will be sent to stdout instead of being applied
dryRun bool
//ClusteradmFlags: The generic optiosn from the clusteradm cli-runtime.
ClusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags
// factory cmdutil.Factory
values Values
//The file to output the resources will be sent to the file.
outputFile string
}
Expand All @@ -32,9 +30,8 @@ type Hub struct {
TokenSecret string `json:"tokenSecret"`
}

func newOptions(f cmdutil.Factory, streams genericclioptions.IOStreams) *Options {
func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options {
return &Options{
ConfigFlags: genericclioptions.NewConfigFlags(true),
factory: f,
ClusteradmFlags: clusteradmFlags,
}
}
10 changes: 6 additions & 4 deletions pkg/cmd/join/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package join
import (
"fmt"

genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/helpers"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

var example = `
Expand All @@ -17,14 +17,17 @@ var example = `
`

// NewCmd ...
func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(f, streams)
func NewCmd(clusteradmFlages *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(clusteradmFlages, streams)

cmd := &cobra.Command{
Use: "join",
Short: "join a hub",
Example: fmt.Sprintf(example, helpers.GetExampleHeader()),
SilenceUsage: true,
PreRun: func(c *cobra.Command, args []string) {
helpers.DryRunMessage(o.ClusteradmFlags.DryRun)
},
RunE: func(c *cobra.Command, args []string) error {
if err := o.complete(c, args); err != nil {
return err
Expand All @@ -43,7 +46,6 @@ func NewCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Comma
cmd.Flags().StringVar(&o.token, "hub-token", "", "The token to access the hub")
cmd.Flags().StringVar(&o.hubAPIServer, "hub-apiserver", "", "The api server url to the hub")
cmd.Flags().StringVar(&o.clusterName, "cluster-name", "", "The name of the joining cluster")
cmd.Flags().BoolVar(&o.dryRun, "dry-run", false, "If set the generated resources will be displayed but not applied")
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
return cmd
}
Loading

0 comments on commit e7320e8

Please sign in to comment.