From 581159d4ca69c3be6fcbe9b95ca404943f528850 Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Sun, 26 Apr 2020 14:05:31 -0700 Subject: [PATCH] Implement the third wave of per-reconciler leaderelection. This lets downstream components enable leaderelection on `WebhookMain*`-based components. Detailed design: https://docs.google.com/document/d/1i_QHjQO2T3SNv49xjZLWlivcc0UvZN1Tbw2NKxThkyM/edit# Issue: https://github.com/knative/pkg/issues/1181 --- injection/sharedmain/main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 5c2ba94a36..fdc910ef03 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -232,6 +232,18 @@ func WebhookMainWithConfig(ctx context.Context, component string, cfg *rest.Conf CheckK8sClientMinimumVersionOrDie(ctx, logger) cmw := SetupConfigMapWatchOrDie(ctx, logger) + + // Set up leader election config + leaderElectionConfig, err := GetLeaderElectionConfig(ctx) + if err != nil { + logger.Fatalf("Error loading leader election configuration: %v", err) + } + leConfig := leaderElectionConfig.GetComponentConfig(component) + if leConfig.LeaderElect { + // Signal that we are executing in a context with leader election. + ctx = kle.WithStandardLeaderElectorBuilder(ctx, kubeclient.Get(ctx), leConfig) + } + controllers, webhooks := ControllersAndWebhooksFromCtors(ctx, cmw, ctors...) WatchLoggingConfigOrDie(ctx, cmw, logger, atomicLevel, component) WatchObservabilityConfigOrDie(ctx, cmw, profilingHandler, logger, component) @@ -242,7 +254,6 @@ func WebhookMainWithConfig(ctx context.Context, component string, cfg *rest.Conf // If we have one or more admission controllers, then start the webhook // and pass them in. var wh *webhook.Webhook - var err error if len(webhooks) > 0 { // Register webhook metrics webhook.RegisterMetrics()