Validate lock identity
Ensure that the lock identity is not empty. This can cause unexpected issues during leader election.
This commit is contained in:
		@@ -99,6 +99,11 @@ func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) {
 | 
				
			|||||||
	if lec.Lock == nil {
 | 
						if lec.Lock == nil {
 | 
				
			||||||
		return nil, fmt.Errorf("Lock must not be nil.")
 | 
							return nil, fmt.Errorf("Lock must not be nil.")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						id := lec.Lock.Identity()
 | 
				
			||||||
 | 
						if id == "" {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("Lock identity is empty")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	le := LeaderElector{
 | 
						le := LeaderElector{
 | 
				
			||||||
		config:  lec,
 | 
							config:  lec,
 | 
				
			||||||
		clock:   clock.RealClock{},
 | 
							clock:   clock.RealClock{},
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,8 @@ import (
 | 
				
			|||||||
	rl "k8s.io/client-go/tools/leaderelection/resourcelock"
 | 
						rl "k8s.io/client-go/tools/leaderelection/resourcelock"
 | 
				
			||||||
	"k8s.io/client-go/tools/record"
 | 
						"k8s.io/client-go/tools/record"
 | 
				
			||||||
	"k8s.io/utils/clock"
 | 
						"k8s.io/utils/clock"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func createLockObject(t *testing.T, objectType, namespace, name string, record *rl.LeaderElectionRecord) (obj runtime.Object) {
 | 
					func createLockObject(t *testing.T, objectType, namespace, name string, record *rl.LeaderElectionRecord) (obj runtime.Object) {
 | 
				
			||||||
@@ -753,6 +755,41 @@ func testReleaseOnCancellation(t *testing.T, objectType string) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestLeaderElectionConfigValidation(t *testing.T) {
 | 
				
			||||||
 | 
						resourceLockConfig := rl.ResourceLockConfig{
 | 
				
			||||||
 | 
							Identity: "baz",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lock := &rl.LeaseLock{
 | 
				
			||||||
 | 
							LockConfig: resourceLockConfig,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lec := LeaderElectionConfig{
 | 
				
			||||||
 | 
							Lock:          lock,
 | 
				
			||||||
 | 
							LeaseDuration: 15 * time.Second,
 | 
				
			||||||
 | 
							RenewDeadline: 2 * time.Second,
 | 
				
			||||||
 | 
							RetryPeriod:   1 * time.Second,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ReleaseOnCancel: true,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							Callbacks: LeaderCallbacks{
 | 
				
			||||||
 | 
								OnNewLeader:      func(identity string) {},
 | 
				
			||||||
 | 
								OnStoppedLeading: func() {},
 | 
				
			||||||
 | 
								OnStartedLeading: func(context.Context) {},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := NewLeaderElector(lec)
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Invalid lock identity
 | 
				
			||||||
 | 
						resourceLockConfig.Identity = ""
 | 
				
			||||||
 | 
						lock.LockConfig = resourceLockConfig
 | 
				
			||||||
 | 
						lec.Lock = lock
 | 
				
			||||||
 | 
						_, err = NewLeaderElector(lec)
 | 
				
			||||||
 | 
						assert.Error(t, err, fmt.Errorf("Lock identity is empty"))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func assertEqualEvents(t *testing.T, expected []string, actual <-chan string) {
 | 
					func assertEqualEvents(t *testing.T, expected []string, actual <-chan string) {
 | 
				
			||||||
	c := time.After(wait.ForeverTestTimeout)
 | 
						c := time.After(wait.ForeverTestTimeout)
 | 
				
			||||||
	for _, e := range expected {
 | 
						for _, e := range expected {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user