NewLeaderElector creates a LeaderElector from a LeaderElectionConfig
(lec LeaderElectionConfig)
| 74 | |
| 75 | // NewLeaderElector creates a LeaderElector from a LeaderElectionConfig |
| 76 | func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) { |
| 77 | if lec.LeaseDuration <= lec.RenewDeadline { |
| 78 | return nil, fmt.Errorf("leaseDuration must be greater than renewDeadline") |
| 79 | } |
| 80 | if lec.RenewDeadline <= time.Duration(JitterFactor*float64(lec.RetryPeriod)) { |
| 81 | return nil, fmt.Errorf("renewDeadline must be greater than retryPeriod*JitterFactor") |
| 82 | } |
| 83 | if lec.LeaseDuration < 1 { |
| 84 | return nil, fmt.Errorf("leaseDuration must be greater than zero") |
| 85 | } |
| 86 | if lec.RenewDeadline < 1 { |
| 87 | return nil, fmt.Errorf("renewDeadline must be greater than zero") |
| 88 | } |
| 89 | if lec.RetryPeriod < 1 { |
| 90 | return nil, fmt.Errorf("retryPeriod must be greater than zero") |
| 91 | } |
| 92 | |
| 93 | if lec.Lock == nil { |
| 94 | return nil, fmt.Errorf("Lock must not be nil.") |
| 95 | } |
| 96 | le := LeaderElector{ |
| 97 | config: lec, |
| 98 | clock: clock.RealClock{}, |
| 99 | metrics: globalMetricsFactory.newLeaderMetrics(), |
| 100 | } |
| 101 | le.metrics.leaderOff(le.config.Name) |
| 102 | return &le, nil |
| 103 | } |
| 104 | |
| 105 | type LeaderElectionConfig struct { |
| 106 | // Lock is the resource that will be used for locking |
no test coverage detected