-
Notifications
You must be signed in to change notification settings - Fork 39k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to disable lock creation by default #109875
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,9 +82,10 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) { | |
observedTime time.Time | ||
reactors []Reactor | ||
|
||
expectSuccess bool | ||
transitionLeader bool | ||
outHolder string | ||
expectSuccess bool | ||
transitionLeader bool | ||
outHolder string | ||
disableLockCreation bool | ||
}{ | ||
{ | ||
name: "acquire from no object", | ||
|
@@ -206,6 +207,19 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) { | |
expectSuccess: false, | ||
outHolder: "bing", | ||
}, | ||
{ | ||
name: "don't create lock object if DisableLockCreation is true", | ||
reactors: []Reactor{ | ||
{ | ||
verb: "get", | ||
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) { | ||
return true, nil, errors.NewNotFound(action.(fakeclient.GetAction).GetResource().GroupResource(), action.(fakeclient.GetAction).GetName()) | ||
}, | ||
}, | ||
}, | ||
expectSuccess: false, | ||
disableLockCreation: true, | ||
}, | ||
{ | ||
name: "renew already acquired object", | ||
reactors: []Reactor{ | ||
|
@@ -273,6 +287,7 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) { | |
reportedLeader = l | ||
}, | ||
}, | ||
DisableLockCreation: test.disableLockCreation, | ||
} | ||
observedRawRecord := GetRawRecordOrDie(t, objectType, test.observedRecord) | ||
le := &LeaderElector{ | ||
|
@@ -302,6 +317,12 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) { | |
} | ||
|
||
le.maybeReportTransition() | ||
if test.disableLockCreation { | ||
// This sleep is to make sure that there is no async leader transition, | ||
// when lock creation is disabled. | ||
time.Sleep(time.Second * 10) | ||
wg.Done() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also add a 30s sleep? We want to make sure no one becomes the leader asynchronously. We can sleep and verify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that anyone can become the leader asynchronously if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's to prevent someone from accidentally change the behavior and break the assumption in the future. But I agree a 30s sleep is probably too much for a unit test. I don't feel strongly |
||
} | ||
wg.Wait() | ||
if reportedLeader != test.outHolder { | ||
t.Errorf("reported leader was not the new leader. expected %q, got %q", test.outHolder, reportedLeader) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: some clarifications:
"the lock should not be created when it doesn't exist"
By default (when DisableLockCreation is false), leader elector creates a new lock object when trying to acquire a non-existing lock object.
If DisableLockCreation is true and the lock object doesn't exist, the leader elector won't be able to become the leader.