Skip to content

Instantly share code, notes, and snippets.

@tcolgate
Created October 30, 2017 09:39
Show Gist options
  • Select an option

  • Save tcolgate/467dd394e91b8602af614c9194add424 to your computer and use it in GitHub Desktop.

Select an option

Save tcolgate/467dd394e91b8602af614c9194add424 to your computer and use it in GitHub Desktop.
package thing
import (
kube_leaderelection "k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
func elect(){
kubeClient := createKubeClient()
lock, err := resourcelock.New(
leaderElection.ResourceLock,
*namespace,
"cluster-autoscaler",
kubeClient.Core(),
resourcelock.ResourceLockConfig{
Identity: id,
EventRecorder: kube_util.CreateEventRecorder(kubeClient),
},
)
if err != nil {
glog.Fatalf("Unable to create leader election lock: %v", err)
}
kube_leaderelection.RunOrDie(kube_leaderelection.LeaderElectionConfig{
Lock: lock,
LeaseDuration: leaderElection.LeaseDuration.Duration,
RenewDeadline: leaderElection.RenewDeadline.Duration,
RetryPeriod: leaderElection.RetryPeriod.Duration,
Callbacks: kube_leaderelection.LeaderCallbacks{
OnStartedLeading: func(_ <-chan struct{}) {
// Since we are committing a suicide after losing
// mastership, we can safely ignore the argument.
run(healthCheck)
},
OnStoppedLeading: func() {
glog.Fatalf("lost master")
},
},
})
}
}
func defaultLeaderElectionConfiguration() componentconfig.LeaderElectionConfiguration {
return componentconfig.LeaderElectionConfiguration{
LeaderElect: false,
LeaseDuration: metav1.Duration{Duration: defaultLeaseDuration},
RenewDeadline: metav1.Duration{Duration: defaultRenewDeadline},
RetryPeriod: metav1.Duration{Duration: defaultRetryPeriod},
ResourceLock: resourcelock.EndpointsResourceLock,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment