Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions pkg/leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@ import (
"k8s.io/client-go/tools/leaderelection/resourcelock"
)

var defaultLeaderTTL = time.Minute

const (
devLeaderTTL = time.Hour
defaultLeaderTTL = time.Minute
devLeaderTTL = time.Hour

FileLockType = "file"
)

func init() {
if os.Getenv("NAH_DEV_MODE") != "" {
defaultLeaderTTL = devLeaderTTL
}
}

type OnLeader func(context.Context) error
type OnNewLeader func(string)

Expand All @@ -38,7 +31,7 @@ type ElectionConfig struct {

func NewDefaultElectionConfig(namespace, name string, cfg *rest.Config) *ElectionConfig {
return &ElectionConfig{
TTL: defaultLeaderTTL,
TTL: defaultElectionTTL(),
Namespace: namespace,
Name: name,
ResourceLockType: resourcelock.LeasesResourceLock,
Expand All @@ -48,7 +41,7 @@ func NewDefaultElectionConfig(namespace, name string, cfg *rest.Config) *Electio

func NewFileElectionConfig(fileName string) *ElectionConfig {
return &ElectionConfig{
TTL: defaultLeaderTTL,
TTL: defaultElectionTTL(),
Name: fileName,
ResourceLockType: FileLockType,
}
Expand All @@ -72,6 +65,13 @@ func NewElectionConfig(ttl time.Duration, namespace, name, lockType string, cfg
}
}

func defaultElectionTTL() time.Duration {
if os.Getenv("NAH_DEV_MODE") != "" {
return devLeaderTTL
}
return defaultLeaderTTL
}

func (ec *ElectionConfig) Run(ctx context.Context, id string, onLeader OnLeader, onSwitchLeader OnNewLeader, signalDone func()) error {
if ec == nil {
// Don't start leader election if there is no config.
Expand Down