fix: do not allow nil Callbacks functions

If user specifies a nil callback, then error out
rather than crashing at runtime due to a nil reference.

Kubernetes-commit: 9dbbc652efee21bd421db1b1f52235ed20ffb7eb
This commit is contained in:
Harry Bagdi 2019-07-29 11:27:28 -07:00 committed by Kubernetes Publisher
parent 693ed41095
commit bc72a37034

View File

@ -89,6 +89,12 @@ func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) {
if lec.RetryPeriod < 1 {
return nil, fmt.Errorf("retryPeriod must be greater than zero")
}
if lec.Callbacks.OnStartedLeading == nil {
return nil, fmt.Errorf("OnStartedLeading callback must not be nil")
}
if lec.Callbacks.OnStoppedLeading == nil {
return nil, fmt.Errorf("OnStoppedLeading callback must not be nil")
}
if lec.Lock == nil {
return nil, fmt.Errorf("Lock must not be nil.")