From 4e8f44fdd6455decd01499eaf50d027ad990a9b7 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Mon, 18 May 2020 10:36:11 +0800 Subject: [PATCH] Split hub/spoke operator to two cmds --- cmd/nucleus/main.go | 7 +++--- pkg/cmd/operator/{operator.go => hub.go} | 10 ++++----- pkg/cmd/operator/spoke.go | 21 ++++++++++++++++++ pkg/operators/manager.go | 27 +++++++++++++++++++++--- 4 files changed, 54 insertions(+), 11 deletions(-) rename pkg/cmd/operator/{operator.go => hub.go} (52%) create mode 100644 pkg/cmd/operator/spoke.go diff --git a/cmd/nucleus/main.go b/cmd/nucleus/main.go index e7e05ba79..7c81f9851 100644 --- a/cmd/nucleus/main.go +++ b/cmd/nucleus/main.go @@ -26,14 +26,14 @@ func main() { logs.InitLogs() defer logs.FlushLogs() - command := newWorkCommand() + command := newNucleusCommand() if err := command.Execute(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } } -func newWorkCommand() *cobra.Command { +func newNucleusCommand() *cobra.Command { cmd := &cobra.Command{ Use: "nucleus", Short: "Nucleus Operator", @@ -49,7 +49,8 @@ func newWorkCommand() *cobra.Command { cmd.Version = v } - cmd.AddCommand(operator.NewOperatorCmd()) + cmd.AddCommand(operator.NewHubOperatorCmd()) + cmd.AddCommand(operator.NewSpokeOperatorCmd()) return cmd } diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/hub.go similarity index 52% rename from pkg/cmd/operator/operator.go rename to pkg/cmd/operator/hub.go index 97f9fc3cd..fb01f165f 100644 --- a/pkg/cmd/operator/operator.go +++ b/pkg/cmd/operator/hub.go @@ -9,13 +9,13 @@ import ( "github.com/open-cluster-management/nucleus/pkg/version" ) -// NewOperatorCmd generatee a command to start workload agent -func NewOperatorCmd() *cobra.Command { +// NewHubOperatorCmd generatee a command to start hub operator +func NewHubOperatorCmd() *cobra.Command { cmd := controllercmd. - NewControllerCommandConfig("nucleus-operator", version.Get(), operators.RunNucleusOperator). + NewControllerCommandConfig("nucleus-hub", version.Get(), operators.RunNucleusHubOperator). NewCommand() - cmd.Use = "operator" - cmd.Short = "Start the nucleus operator" + cmd.Use = "hub" + cmd.Short = "Start the nucleus hub operator" return cmd } diff --git a/pkg/cmd/operator/spoke.go b/pkg/cmd/operator/spoke.go new file mode 100644 index 000000000..7096ce2b9 --- /dev/null +++ b/pkg/cmd/operator/spoke.go @@ -0,0 +1,21 @@ +package operator + +import ( + "github.com/spf13/cobra" + + "github.com/openshift/library-go/pkg/controller/controllercmd" + + "github.com/open-cluster-management/nucleus/pkg/operators" + "github.com/open-cluster-management/nucleus/pkg/version" +) + +// NewSpokeOperatorCmd generatee a command to start spoke operator +func NewSpokeOperatorCmd() *cobra.Command { + cmd := controllercmd. + NewControllerCommandConfig("nucleus-spoke", version.Get(), operators.RunNucleusSpokeOperator). + NewCommand() + cmd.Use = "spoke" + cmd.Short = "Start the nucleus hub operator" + + return cmd +} diff --git a/pkg/operators/manager.go b/pkg/operators/manager.go index c80cacf63..2983a266f 100644 --- a/pkg/operators/manager.go +++ b/pkg/operators/manager.go @@ -15,8 +15,8 @@ import ( "github.com/open-cluster-management/nucleus/pkg/operators/spoke" ) -// RunNucleusOperator starts a new nucleus operator -func RunNucleusOperator(ctx context.Context, controllerContext *controllercmd.ControllerContext) error { +// RunNucleusHubOperator starts a new nucleus hub operator +func RunNucleusHubOperator(ctx context.Context, controllerContext *controllercmd.ControllerContext) error { // Build kubclient client and informer for spoke cluster kubeClient, err := kubernetes.NewForConfig(controllerContext.KubeConfig) if err != nil { @@ -40,6 +40,28 @@ func RunNucleusOperator(ctx context.Context, controllerContext *controllercmd.Co nucleusClient.NucleusV1().HubCores(), nucleusInformer.Nucleus().V1().HubCores(), controllerContext.EventRecorder) + + go nucleusInformer.Start(ctx.Done()) + go hubcontroller.Run(ctx, 1) + <-ctx.Done() + return nil +} + +// RunNucleusSpokeOperator starts a new nucleus spoke operator +func RunNucleusSpokeOperator(ctx context.Context, controllerContext *controllercmd.ControllerContext) error { + // Build kubclient client and informer for spoke cluster + kubeClient, err := kubernetes.NewForConfig(controllerContext.KubeConfig) + if err != nil { + return err + } + + // Build nucleus client and informer + nucleusClient, err := nucleusclient.NewForConfig(controllerContext.KubeConfig) + if err != nil { + return err + } + nucleusInformer := nucleusinformer.NewSharedInformerFactory(nucleusClient, 5*time.Minute) + spokeController := spoke.NewNucleusSpokeController( kubeClient, nucleusClient.NucleusV1().SpokeCores(), @@ -47,7 +69,6 @@ func RunNucleusOperator(ctx context.Context, controllerContext *controllercmd.Co controllerContext.EventRecorder) go nucleusInformer.Start(ctx.Done()) - go hubcontroller.Run(ctx, 1) go spokeController.Run(ctx, 1) <-ctx.Done() return nil