Skip to content

Commit

Permalink
feat: init cluster manager framework
Browse files Browse the repository at this point in the history
Signed-off-by: wangyizhi1 <[email protected]>
  • Loading branch information
wangyizhi1 committed Oct 20, 2023
1 parent 6d824a7 commit 75794b1
Show file tree
Hide file tree
Showing 22 changed files with 442 additions and 1,176 deletions.
81 changes: 81 additions & 0 deletions cmd/clustertree/cluster-manager/app/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package app

import (
"context"
"fmt"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
controllerruntime "sigs.k8s.io/controller-runtime"

"github.com/kosmos.io/kosmos/cmd/clustertree/cluster-manager/app/options"
clusterManager "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager"
"github.com/kosmos.io/kosmos/pkg/scheme"
"github.com/kosmos.io/kosmos/pkg/sharedcli/klogflag"
)

func NewAgentCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "clustertree-cluster-manager",
Long: `Cluster Resource Management and Synchronization.`,
RunE: func(cmd *cobra.Command, args []string) error {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
return err
}
return nil
},
}

fss := cliflag.NamedFlagSets{}

genericFlagSet := fss.FlagSet("generic")
opts.AddFlags(genericFlagSet)

logsFlagSet := fss.FlagSet("logs")
klogflag.Add(logsFlagSet)

cmd.Flags().AddFlagSet(genericFlagSet)
cmd.Flags().AddFlagSet(logsFlagSet)

return cmd
}

func run(ctx context.Context, opts *options.Options) error {
config, err := clientcmd.BuildConfigFromFlags(opts.KubernetesOptions.Master, opts.KubernetesOptions.KubeConfig)
if err != nil {
panic(err)
}
config.QPS, config.Burst = opts.KubernetesOptions.QPS, opts.KubernetesOptions.Burst

mgr, err := controllerruntime.NewManager(config, controllerruntime.Options{
Logger: klog.Background(),
Scheme: scheme.NewSchema(),
LeaderElection: opts.LeaderElection.LeaderElect,
LeaderElectionID: opts.LeaderElection.ResourceName,
LeaderElectionNamespace: opts.LeaderElection.ResourceNamespace,
})
if err != nil {
return fmt.Errorf("failed to build controller manager: %v", err)
}

ClusterController := clusterManager.ClusterController{
Master: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor(clusterManager.ControllerName),
}
if err = ClusterController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting %s: %v", clusterManager.ControllerName, err)
}

if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("failed to start controller manager: %v", err)
}

return nil
}
44 changes: 44 additions & 0 deletions cmd/clustertree/cluster-manager/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package options

import (
"github.com/spf13/pflag"
componentbaseconfig "k8s.io/component-base/config"
)

const (
LeaderElectionNamespace = "kosmos-system"
LeaderElectionResourceName = "cluster-manager"

DefaultKubeQPS = 40.0
DefaultKubeBurst = 60
)

type Options struct {
LeaderElection componentbaseconfig.LeaderElectionConfiguration
KubernetesOptions KubernetesOptions
}

type KubernetesOptions struct {
KubeConfig string `json:"kubeconfig" yaml:"kubeconfig"`
Master string `json:"master,omitempty" yaml:"master,omitempty"`
QPS float32 `json:"qps,omitempty" yaml:"qps,omitempty"`
Burst int `json:"burst,omitempty" yaml:"burst,omitempty"`
}

func NewOptions() *Options {
return &Options{}
}

func (o *Options) AddFlags(flags *pflag.FlagSet) {
if o == nil {
return
}

flags.BoolVar(&o.LeaderElection.LeaderElect, "leader-elect", true, "Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.")
flags.StringVar(&o.LeaderElection.ResourceName, "leader-elect-resource-name", LeaderElectionResourceName, "The name of resource object that is used for locking during leader election.")
flags.StringVar(&o.LeaderElection.ResourceNamespace, "leader-elect-resource-namespace", LeaderElectionNamespace, "The namespace of resource object that is used for locking during leader election.")
flags.Float32Var(&o.KubernetesOptions.QPS, "kube-qps", DefaultKubeQPS, "QPS to use while talking with kube-apiserver.")
flags.IntVar(&o.KubernetesOptions.Burst, "kube-burst", DefaultKubeBurst, "Burst to use while talking with kube-apiserver.")
flags.StringVar(&o.KubernetesOptions.KubeConfig, "kubeconfig", "", "Path for kubernetes kubeconfig file, if left blank, will use in cluster way.")
flags.StringVar(&o.KubernetesOptions.Master, "master", "", "Used to generate kubeconfig for downloading, if not specified, will use host in kubeconfig.")
}
10 changes: 10 additions & 0 deletions cmd/clustertree/cluster-manager/app/options/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package options

import "k8s.io/apimachinery/pkg/util/validation/field"

// Validate checks Options and return a slice of found errs.
func (o *Options) Validate() field.ErrorList {
errs := field.ErrorList{}

return errs
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
apiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/cli"

"github.com/kosmos.io/kosmos/cmd/clustertree/knode-manager/app"
"github.com/kosmos.io/kosmos/cmd/clustertree/cluster-manager/app"
)

func main() {
ctx := apiserver.SetupSignalContext()
command := app.NewKosmosNodeManagerCommand(ctx)
code := cli.Run(command)
cmd := app.NewAgentCommand(ctx)
code := cli.Run(cmd)
os.Exit(code)
}
78 changes: 0 additions & 78 deletions cmd/clustertree/knode-manager/app/config/config.go

This file was deleted.

109 changes: 0 additions & 109 deletions cmd/clustertree/knode-manager/app/extension_apps.go

This file was deleted.

Loading

0 comments on commit 75794b1

Please sign in to comment.