1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-23 03:59:29 +00:00

Revert "Make leader election have leader callback"

This reverts commit 807103dc58.
This commit is contained in:
Darren Shepherd
2018-05-16 10:12:23 -07:00
parent d6865df52b
commit 3499440178

View File

@@ -3,7 +3,6 @@ package leader
import (
"context"
"os"
"sync"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
@@ -16,37 +15,17 @@ import (
"k8s.io/kubernetes/pkg/client/leaderelectionconfig"
)
type State struct {
sync.Mutex
identity string
leader bool
}
func (l *State) Get() (string, bool) {
l.Lock()
defer l.Unlock()
return l.identity, l.leader
}
func (l *State) Status(identity string, leader bool) {
l.Lock()
l.identity = identity
l.leader = leader
l.Unlock()
}
type Callback func(cb context.Context)
type StatusCallback func(identity string, leader bool)
func RunOrDie(ctx context.Context, name string, client kubernetes.Interface, cb Callback, status StatusCallback) {
err := run(ctx, name, client, cb, status)
func RunOrDie(ctx context.Context, name string, client kubernetes.Interface, cb Callback) {
err := run(ctx, name, client, cb)
if err != nil {
logrus.Fatalf("Failed to start leader election for %s", name)
}
panic("Failed to start leader election for " + name)
}
func run(ctx context.Context, name string, client kubernetes.Interface, cb Callback, status StatusCallback) error {
func run(ctx context.Context, name string, client kubernetes.Interface, cb Callback) error {
id, err := os.Hostname()
if err != nil {
return err
@@ -70,8 +49,6 @@ func run(ctx context.Context, name string, client kubernetes.Interface, cb Callb
logrus.Fatalf("error creating leader lock for %s: %v", name, err)
}
status(id, false)
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: le.LeaseDuration.Duration,
@@ -81,16 +58,12 @@ func run(ctx context.Context, name string, client kubernetes.Interface, cb Callb
OnStartedLeading: func(stop <-chan struct{}) {
subCtx, cancel := context.WithCancel(ctx)
go cb(subCtx)
status(id, true)
<-stop
cancel()
},
OnStoppedLeading: func() {
logrus.Fatalf("leaderelection lost for %s", name)
},
OnNewLeader: func(identity string) {
status(identity, identity == id)
},
},
})
panic("unreachable")