add leader election to agent

Signed-off-by: Garrybest <garrybest@foxmail.com>
This commit is contained in:
Garrybest 2021-08-02 21:31:13 +08:00
parent 87937a0830
commit 4ac44669f3
4 changed files with 21 additions and 7 deletions

View File

@ -67,10 +67,12 @@ func run(ctx context.Context, karmadaConfig karmadactl.KarmadaConfig, opts *opti
}
controllerManager, err := controllerruntime.NewManager(controlPlaneRestConfig, controllerruntime.Options{
Scheme: gclient.NewSchema(),
Namespace: executionSpace,
LeaderElection: false,
LeaderElectionID: "agent.karmada.io",
Scheme: gclient.NewSchema(),
Namespace: executionSpace,
LeaderElection: opts.LeaderElection.LeaderElect,
LeaderElectionID: fmt.Sprintf("karmada-agent-%s", opts.ClusterName),
LeaderElectionNamespace: opts.LeaderElection.ResourceNamespace,
LeaderElectionResourceLock: opts.LeaderElection.ResourceLock,
})
if err != nil {
klog.Errorf("failed to build controller manager: %v", err)

View File

@ -5,10 +5,15 @@ import (
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/leaderelection/resourcelock"
componentbaseconfig "k8s.io/component-base/config"
"github.com/karmada-io/karmada/pkg/util"
)
// Options contains everything necessary to create and run controller-manager.
type Options struct {
LeaderElection componentbaseconfig.LeaderElectionConfiguration
KarmadaKubeConfig string
// ClusterContext is the name of the cluster context in control plane KUBECONFIG file.
// Default value is the current-context.
@ -28,7 +33,13 @@ type Options struct {
// NewOptions builds an default scheduler options.
func NewOptions() *Options {
return &Options{}
return &Options{
LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
LeaderElect: true,
ResourceLock: resourcelock.LeasesResourceLock,
ResourceNamespace: util.NamespaceKarmadaSystem,
},
}
}
// AddFlags adds flags of scheduler to the specified FlagSet
@ -37,6 +48,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
return
}
fs.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.")
fs.StringVar(&o.KarmadaKubeConfig, "karmada-kubeconfig", o.KarmadaKubeConfig, "Path to karmada control plane kubeconfig file.")
fs.StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in karmada control plane kubeconfig file.")
fs.StringVar(&o.ClusterName, "cluster-name", o.ClusterName, "Name of member cluster that the agent serves for.")

View File

@ -53,7 +53,7 @@ type Options struct {
func NewOptions() *Options {
return &Options{
LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
LeaderElect: false,
LeaderElect: true,
ResourceLock: resourcelock.LeasesResourceLock,
ResourceNamespace: util.NamespaceKarmadaSystem,
ResourceName: "karmada-controller-manager",

View File

@ -40,7 +40,7 @@ type Options struct {
func NewOptions() *Options {
return &Options{
LeaderElection: componentbaseconfig.LeaderElectionConfiguration{
LeaderElect: false,
LeaderElect: true,
ResourceLock: resourcelock.LeasesResourceLock,
ResourceNamespace: util.NamespaceKarmadaSystem,
ResourceName: "karmada-scheduler",