forked from kosmos-io/kosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: init cluster manager framework
Signed-off-by: wangyizhi1 <[email protected]>
- Loading branch information
1 parent
6d824a7
commit 75794b1
Showing
22 changed files
with
442 additions
and
1,176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.