mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 07:57:35 +00:00
Add lock type flags for controller-manager leadership election
This commit is contained in:
committed by
Timothy St. Clair
parent
5a1ec9bf6f
commit
a2248a2bcd
@@ -14,6 +14,7 @@ go_library(
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/apis/componentconfig:go_default_library",
|
||||
"//pkg/client/clientset_generated/clientset:go_default_library",
|
||||
"//pkg/client/leaderelection/resourcelock:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
|
||||
@@ -53,11 +53,12 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/kubernetes/pkg/apis/componentconfig"
|
||||
cs "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
||||
rl "k8s.io/kubernetes/pkg/client/leaderelection/resourcelock"
|
||||
|
||||
"github.com/golang/glog"
|
||||
@@ -69,6 +70,9 @@ const (
|
||||
DefaultLeaseDuration = 15 * time.Second
|
||||
DefaultRenewDeadline = 10 * time.Second
|
||||
DefaultRetryPeriod = 2 * time.Second
|
||||
|
||||
LockTypeEndpoints = "endpoints"
|
||||
LockTypeConfigMaps = "configmaps"
|
||||
)
|
||||
|
||||
// NewLeadereElector creates a LeaderElector from a LeaderElecitionConfig
|
||||
@@ -225,7 +229,7 @@ func (le *LeaderElector) tryAcquireOrRenew() bool {
|
||||
// 1. obtain or create the ElectionRecord
|
||||
oldLeaderElectionRecord, err := le.config.Lock.Get()
|
||||
if err != nil {
|
||||
if !errors.IsNotFound(err) {
|
||||
if !apierrors.IsNotFound(err) {
|
||||
glog.Errorf("error retrieving resource lock %v: %v", le.config.Lock.Describe(), err)
|
||||
return false
|
||||
}
|
||||
@@ -284,6 +288,7 @@ func DefaultLeaderElectionConfiguration() componentconfig.LeaderElectionConfigur
|
||||
LeaseDuration: metav1.Duration{Duration: DefaultLeaseDuration},
|
||||
RenewDeadline: metav1.Duration{Duration: DefaultRenewDeadline},
|
||||
RetryPeriod: metav1.Duration{Duration: DefaultRetryPeriod},
|
||||
LockType: LockTypeEndpoints,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,4 +311,33 @@ func BindFlags(l *componentconfig.LeaderElectionConfiguration, fs *pflag.FlagSet
|
||||
fs.DurationVar(&l.RetryPeriod.Duration, "leader-elect-retry-period", l.RetryPeriod.Duration, ""+
|
||||
"The duration the clients should wait between attempting acquisition and renewal "+
|
||||
"of a leadership. This is only applicable if leader election is enabled.")
|
||||
fs.StringVar(&l.LockType, "leader-elect-lock-type", l.LockType, fmt.Sprintf(""+
|
||||
"Indicates the type of locking to use for leadership election. Supported options "+
|
||||
"are `%s` (default) and `%s`.", LockTypeConfigMaps, LockTypeEndpoints))
|
||||
}
|
||||
|
||||
func GetLock(l componentconfig.LeaderElectionConfiguration, client cs.Clientset, rlc rl.ResourceLockConfig) (rl.Interface, error) {
|
||||
name := "kube-controller-manager"
|
||||
switch l.LockType {
|
||||
case LockTypeEndpoints:
|
||||
return &rl.EndpointsLock{
|
||||
EndpointsMeta: metav1.ObjectMeta{
|
||||
Namespace: metav1.NamespaceSystem,
|
||||
Name: name,
|
||||
},
|
||||
Client: client,
|
||||
LockConfig: rlc,
|
||||
}, nil
|
||||
case LockTypeConfigMaps:
|
||||
return &rl.ConfigMapLock{
|
||||
ConfigMapMeta: metav1.ObjectMeta{
|
||||
Namespace: metav1.NamespaceSystem,
|
||||
Name: name,
|
||||
},
|
||||
Client: client,
|
||||
LockConfig: rlc,
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Invalid lock-type provided: %s. Supported types are `%s` and `%s`", l.LockType, LockTypeConfigMaps, LockTypeEndpoints)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user