Removed unused leaderelection package

Signed-off-by: Akshay Gaikwad <akgaikwad001@gmail.com>
This commit is contained in:
Akshay Gaikwad
2023-02-15 20:41:37 +05:30
parent 4d0af34e8a
commit b2d20b00c8
7 changed files with 3 additions and 342 deletions

View File

@@ -1,31 +0,0 @@
package leaderelection
import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
// aliasing controller runtime config package
var (
// GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver.
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
// in cluster and use the cluster provided kubeconfig.
//
// Will log an error and exit if there is an error creating the rest.Config.
GetConfigOrDie = config.GetConfigOrDie
// GetConfig creates a *rest.Config for talking to a Kubernetes apiserver.
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
// in cluster and use the cluster provided kubeconfig.
//
// Config precedence
//
// * --kubeconfig flag pointing at a file
//
// * KUBECONFIG environment variable pointing at a file
//
// * In-cluster config if running in cluster
//
// * $HOME/.kube/config if exists
GetConfig = config.GetConfig
)

View File

@@ -1,30 +0,0 @@
package leaderelection
import (
"time"
)
const (
// LeaseDuration is the duration that non-leader candidates will
// wait to force acquire leadership. This is measured against time of
// last observed ack.
//
// A client needs to wait a full LeaseDuration without observing a change to
// the record before it can attempt to take over. When all clients are
// shutdown and a new set of clients are started with different names against
// the same leader record, they must wait the full LeaseDuration before
// attempting to acquire the lease. Thus LeaseDuration should be as short as
// possible (within your tolerance for clock skew rate) to avoid a possible
// long waits in the scenario.
//
LeaseDuration = 15 * time.Second
// RenewDeadline is the duration that the acting master will retry
// refreshing leadership before giving up.
//
RenewDeadline = 10 * time.Second
// RetryPeriod is the duration the LeaderElector clients should wait
// between tries of actions.
//
RetryPeriod = 2 * time.Second
)

View File

@@ -1,51 +0,0 @@
package leaderelection
import (
"context"
log "github.com/paralus/paralus/pkg/log"
le "k8s.io/client-go/tools/leaderelection"
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
)
var (
_log = log.GetLogger()
)
// Run runs leader election and calls onStarted when runner becomes leader
func Run(lock rl.Interface, onStarted func(stop <-chan struct{}), stop <-chan struct{}) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_log.Infow("starting leader election", "for", lock.Describe(), "id", lock.Identity())
elector, err := le.NewLeaderElector(le.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: LeaseDuration,
RenewDeadline: RenewDeadline,
RetryPeriod: RetryPeriod,
Callbacks: le.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) {
_log.Infow("started leading", "for", lock.Describe(), "id", lock.Identity())
onStarted(stop)
},
OnStoppedLeading: func() {
_log.Infow("stopped leading", "for", lock.Describe(), "id", lock.Identity())
},
OnNewLeader: func(identity string) {
_log.Infow("new leader", "for", lock.Describe(), "id", identity)
},
},
})
if err != nil {
return err
}
go elector.Run(ctx)
_log.Infow("started leader election", "for", lock.Describe(), "id", lock.Identity())
<-stop
return nil
}

View File

@@ -1,114 +0,0 @@
package leaderelection
import (
"fmt"
"testing"
"time"
)
func TestLeaderElectionRun(t *testing.T) {
lock1, err := NewLock("test-lock", "default", "client-1")
if err != nil {
t.Error(err)
return
}
lock2, err := NewLock("test-lock", "default", "client-2")
if err != nil {
t.Error(err)
return
}
stop1 := make(chan struct{})
stop2 := make(chan struct{})
go func() {
Run(lock1, func(stop <-chan struct{}) {
fmt.Println(lock1.Identity(), " became leader")
loop:
for {
select {
case <-stop:
fmt.Println("stopping ", lock1.Identity())
break loop
}
}
}, stop1)
}()
go func() {
Run(lock2, func(stop <-chan struct{}) {
fmt.Println(lock2.Identity(), " became leader")
loop:
for {
select {
case <-stop:
fmt.Println("stopping ", lock2.Identity())
break loop
}
}
}, stop2)
}()
go func() {
time.Sleep(time.Second * 20)
close(stop1)
}()
go func() {
time.Sleep(time.Second * 20)
close(stop2)
}()
<-stop1
<-stop2
time.Sleep(time.Second * 1)
}
func TestLeaderElectionConfigMapRun(t *testing.T) {
lock1, err := NewConfigMapLock("test-lock", "default", "client-1")
if err != nil {
t.Error(err)
return
}
lock2, err := NewConfigMapLock("test-lock", "default", "client-2")
if err != nil {
t.Error(err)
return
}
stop1 := make(chan struct{})
stop2 := make(chan struct{})
go func() {
Run(lock1, func(stop <-chan struct{}) {
fmt.Println(lock1.Identity(), " became leader")
loop:
for {
select {
case <-stop:
fmt.Println("stopping ", lock1.Identity())
break loop
}
}
}, stop1)
}()
go func() {
Run(lock2, func(stop <-chan struct{}) {
fmt.Println(lock2.Identity(), " became leader")
loop:
for {
select {
case <-stop:
fmt.Println("stopping ", lock2.Identity())
break loop
}
}
}, stop2)
}()
time.Sleep(time.Second * 20)
close(stop2)
time.Sleep(time.Second * 20)
close(stop1)
}

View File

@@ -1,50 +0,0 @@
package leaderelection
import (
clientset "k8s.io/client-go/kubernetes"
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
)
// NewLock returns new resource lock
func NewLock(lockName, lockNamespace, id string) (rl.Interface, error) {
config, err := GetConfig()
if err != nil {
return nil, err
}
client, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
return rl.New(rl.LeasesResourceLock,
lockNamespace,
lockName,
client.CoreV1(),
client.CoordinationV1(),
rl.ResourceLockConfig{Identity: id},
)
}
// NewConfigMapLock returns new lock backed by ConfigMap
func NewConfigMapLock(lockName, lockNamespace, id string) (rl.Interface, error) {
config, err := GetConfig()
if err != nil {
return nil, err
}
client, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
return rl.New(rl.ConfigMapsResourceLock,
lockNamespace,
lockName,
client.CoreV1(),
nil,
rl.ResourceLockConfig{Identity: id},
)
}