Skip to content

Commit

Permalink
move leader election to different file
Browse files Browse the repository at this point in the history
  • Loading branch information
basit9958 committed Aug 27, 2023
1 parent d9d68c1 commit c18ff15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 52 deletions.
61 changes: 61 additions & 0 deletions cmd/controller/leader_election.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"context"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/klog/v2"
"os"
"time"
)

var (
client *clientset.Clientset
)

func getNewLock(lockname, podname, namespace string) *resourcelock.LeaseLock {
return &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: lockname,
Namespace: namespace,
},
Client: client.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: podname,
},
}
}

func runLeaderElection(ctx context.Context, lock *resourcelock.LeaseLock, podname string, ctrlOpts Options, log logr.Logger) {
// Start the leader election for running kapp-controller
log.Info("Waiting for leader election")
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(c context.Context) {
err := Run(ctrlOpts, log.WithName("controller"))
if err != nil {
klog.Errorf("Error while running as leader: %v", err)
}
},
OnStoppedLeading: func() {
klog.Fatalf("no longer the leader, staying inactive.")
os.Exit(0)
},
OnNewLeader: func(identity string) {
//Notify when a new leader is elected
if identity == podname {
return
}
klog.InfoS("new leader elected", "id", identity)
},
},
})
}
52 changes: 0 additions & 52 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ package main
import (
"context"
"flag"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/klog/v2"
"os"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -22,54 +18,6 @@ import (
// Version of kapp-controller is set via ldflags at build-time from the most recent git tag; see hack/build.sh
var Version = "develop"

var (
client *clientset.Clientset
)

func getNewLock(lockname, podname, namespace string) *resourcelock.LeaseLock {
return &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: lockname,
Namespace: namespace,
},
Client: client.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: podname,
},
}
}

func runLeaderElection(ctx context.Context, lock *resourcelock.LeaseLock, podname string, ctrlOpts Options, log logr.Logger) {
// Start the leader election for running kapp-controller
log.Info("Waiting for leader election")
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(c context.Context) {
err := Run(ctrlOpts, log.WithName("controller"))
if err != nil {
klog.Errorf("Error while running as leader: %v", err)
}
},
OnStoppedLeading: func() {
klog.Fatalf("no longer the leader, staying inactive.")
os.Exit(0)
},
OnNewLeader: func(identity string) {
//Notify when a new leader is elected
if identity == podname {
return
}
klog.InfoS("new leader elected", "id", identity)
},
},
})
}

func main() {
ctrlOpts := Options{}
var sidecarexec bool
Expand Down

0 comments on commit c18ff15

Please sign in to comment.