1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

feat: add context support to leader election (#4811)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-10-05 12:19:50 +02:00 committed by GitHub
parent 433c5bfd77
commit 3941754a92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 7 deletions

View file

@ -122,7 +122,7 @@ func main() {
defer close(done)
failure := false
run := func() {
run := func(context.Context) {
name := tls.GenerateRootCASecretName()
_, err = kubeClient.CoreV1().Secrets(config.KyvernoNamespace()).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
@ -168,7 +168,15 @@ func main() {
os.Exit(0)
}
le, err := leaderelection.New("kyvernopre", config.KyvernoNamespace(), kubeClient, config.KyvernoPodName(), run, nil, logging.WithName("kyvernopre/LeaderElection"))
le, err := leaderelection.New(
logging.WithName("kyvernopre/LeaderElection"),
"kyvernopre",
config.KyvernoNamespace(),
kubeClient,
config.KyvernoPodName(),
run,
nil,
)
if err != nil {
setupLog.Error(err, "failed to elect a leader")
os.Exit(1)

View file

@ -466,7 +466,7 @@ func main() {
// wrap all controllers that need leaderelection
// start them once by the leader
registerWrapperRetry := common.RetryFunc(time.Second, webhookRegistrationTimeout, webhookCfg.Register, "failed to register webhook", logger)
run := func() {
run := func(context.Context) {
if err := certRenewer.InitTLSPemPair(); err != nil {
logger.Error(err, "tls initialization error")
os.Exit(1)
@ -526,7 +526,15 @@ func main() {
server.Stop(c)
}
le, err := leaderelection.New("kyverno", config.KyvernoNamespace(), kubeClientLeaderElection, config.KyvernoPodName(), run, stop, logging.WithName("kyverno/LeaderElection"))
le, err := leaderelection.New(
logger.WithName("leader-election"),
"kyverno",
config.KyvernoNamespace(),
kubeClientLeaderElection,
config.KyvernoPodName(),
run,
stop,
)
if err != nil {
logger.Error(err, "failed to elect a leader")
os.Exit(1)

View file

@ -36,7 +36,7 @@ type Interface interface {
type config struct {
name string
namespace string
startWork func()
startWork func(context.Context)
stopWork func()
kubeClient kubernetes.Interface
lock resourcelock.Interface
@ -46,7 +46,7 @@ type config struct {
log logr.Logger
}
func New(name, namespace string, kubeClient kubernetes.Interface, id string, startWork, stopWork func(), log logr.Logger) (Interface, error) {
func New(log logr.Logger, name, namespace string, kubeClient kubernetes.Interface, id string, startWork func(context.Context), stopWork func()) (Interface, error) {
lock, err := resourcelock.New(
resourcelock.LeasesResourceLock,
namespace,
@ -80,7 +80,7 @@ func New(name, namespace string, kubeClient kubernetes.Interface, id string, sta
atomic.StoreInt64(&e.isLeader, 1)
e.log.Info("started leading")
if e.startWork != nil {
e.startWork()
e.startWork(ctx)
}
},
OnStoppedLeading: func() {