mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 15:39:39 +00:00
Merge pull request #118054 from johanoskarsson/johan_leader_election_lock_id_validate
Validate lock identity Kubernetes-commit: 36d81a5818665b55c2b0f307c15572626d84b07a
This commit is contained in:
commit
7a7ea20936
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user