Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
77a5ba6c
Commit
77a5ba6c
authored
Dec 08, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #16830 from mikedanese/leader-election
Auto commit by PR queue bot
parents
d568a92d
68cf1469
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
224 additions
and
0 deletions
+224
-0
leaderelection.go
pkg/client/leaderelection/leaderelection.go
+0
-0
leaderelection_test.go
pkg/client/leaderelection/leaderelection_test.go
+224
-0
No files found.
pkg/client/leaderelection/leaderelection.go
0 → 100644
View file @
77a5ba6c
This diff is collapsed.
Click to expand it.
pkg/client/leaderelection/leaderelection_test.go
0 → 100644
View file @
77a5ba6c
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package leaderelection implements leader election of a set of endpoints.
// It uses an annotation in the endpoints object to store the record of the
// election state.
package
leaderelection
import
(
"fmt"
"testing"
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/record"
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
"k8s.io/kubernetes/pkg/runtime"
)
func
TestTryAcquireOrRenew
(
t
*
testing
.
T
)
{
future
:=
time
.
Now
()
.
Add
(
1000
*
time
.
Hour
)
tests
:=
[]
struct
{
observedRecord
LeaderElectionRecord
observedTime
time
.
Time
reactors
[]
struct
{
verb
string
reaction
testclient
.
ReactionFunc
}
expectSuccess
bool
outHolder
string
}{
// acquire from no endpoints
{
reactors
:
[]
struct
{
verb
string
reaction
testclient
.
ReactionFunc
}{
{
verb
:
"get"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
nil
,
errors
.
NewNotFound
(
action
.
GetVerb
(),
action
.
(
testclient
.
GetAction
)
.
GetName
())
},
},
{
verb
:
"create"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
action
.
(
testclient
.
CreateAction
)
.
GetObject
()
.
(
*
api
.
Endpoints
),
nil
},
},
},
expectSuccess
:
true
,
outHolder
:
"baz"
,
},
// acquire from unled endpoints
{
reactors
:
[]
struct
{
verb
string
reaction
testclient
.
ReactionFunc
}{
{
verb
:
"get"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
api
.
Endpoints
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
action
.
GetNamespace
(),
Name
:
action
.
(
testclient
.
GetAction
)
.
GetName
(),
},
},
nil
},
},
{
verb
:
"update"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
action
.
(
testclient
.
CreateAction
)
.
GetObject
()
.
(
*
api
.
Endpoints
),
nil
},
},
},
expectSuccess
:
true
,
outHolder
:
"baz"
,
},
// acquire from led, unacked endpoints
{
reactors
:
[]
struct
{
verb
string
reaction
testclient
.
ReactionFunc
}{
{
verb
:
"get"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
api
.
Endpoints
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
action
.
GetNamespace
(),
Name
:
action
.
(
testclient
.
GetAction
)
.
GetName
(),
},
},
nil
},
},
{
verb
:
"update"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
action
.
(
testclient
.
CreateAction
)
.
GetObject
()
.
(
*
api
.
Endpoints
),
nil
},
},
},
expectSuccess
:
true
,
outHolder
:
"baz"
,
},
// don't acquire from led, acked endpoints
{
reactors
:
[]
struct
{
verb
string
reaction
testclient
.
ReactionFunc
}{
{
verb
:
"get"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
api
.
Endpoints
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
action
.
GetNamespace
(),
Name
:
action
.
(
testclient
.
GetAction
)
.
GetName
(),
Annotations
:
map
[
string
]
string
{
LeaderElectionRecordAnnotationKey
:
`{"holderIdentity":"bing"}`
,
},
},
},
nil
},
},
},
observedTime
:
future
,
expectSuccess
:
false
,
outHolder
:
"bing"
,
},
// renew already acquired endpoints
{
reactors
:
[]
struct
{
verb
string
reaction
testclient
.
ReactionFunc
}{
{
verb
:
"get"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
&
api
.
Endpoints
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
action
.
GetNamespace
(),
Name
:
action
.
(
testclient
.
GetAction
)
.
GetName
(),
Annotations
:
map
[
string
]
string
{
LeaderElectionRecordAnnotationKey
:
`{"holderIdentity":"baz"}`
,
},
},
},
nil
},
},
{
verb
:
"update"
,
reaction
:
func
(
action
testclient
.
Action
)
(
handled
bool
,
ret
runtime
.
Object
,
err
error
)
{
return
true
,
action
.
(
testclient
.
CreateAction
)
.
GetObject
()
.
(
*
api
.
Endpoints
),
nil
},
},
},
observedTime
:
future
,
observedRecord
:
LeaderElectionRecord
{
HolderIdentity
:
"baz"
},
expectSuccess
:
true
,
outHolder
:
"baz"
,
},
}
lec
:=
LeaderElectionConfig
{
EndpointsMeta
:
api
.
ObjectMeta
{
Namespace
:
"foo"
,
Name
:
"bar"
},
Identity
:
"baz"
,
EventRecorder
:
&
record
.
FakeRecorder
{},
LeaseDuration
:
10
*
time
.
Second
,
}
for
i
,
test
:=
range
tests
{
c
:=
&
testclient
.
Fake
{}
for
_
,
reactor
:=
range
test
.
reactors
{
c
.
AddReactor
(
reactor
.
verb
,
"endpoints"
,
reactor
.
reaction
)
}
c
.
AddReactor
(
"*"
,
"*"
,
func
(
action
testclient
.
Action
)
(
bool
,
runtime
.
Object
,
error
)
{
t
.
Errorf
(
"[%v] unreachable action. testclient called too many times: %+v"
,
i
,
action
)
return
true
,
nil
,
fmt
.
Errorf
(
"uncreachable action"
)
})
le
:=
&
LeaderElector
{
config
:
lec
,
observedRecord
:
test
.
observedRecord
,
observedTime
:
test
.
observedTime
,
}
le
.
config
.
Client
=
c
if
test
.
expectSuccess
!=
le
.
tryAcquireOrRenew
()
{
t
.
Errorf
(
"[%v]unexpected result of tryAcquireOrRenew: [succeded=%v]"
,
i
,
!
test
.
expectSuccess
)
}
le
.
observedRecord
.
AcquireTime
=
unversioned
.
Time
{}
le
.
observedRecord
.
RenewTime
=
unversioned
.
Time
{}
if
le
.
observedRecord
.
HolderIdentity
!=
test
.
outHolder
{
t
.
Errorf
(
"[%v]expected holder:
\n\t
%+v
\n
got:
\n\t
%+v"
,
i
,
test
.
outHolder
,
le
.
observedRecord
.
HolderIdentity
)
}
if
len
(
test
.
reactors
)
!=
len
(
c
.
Actions
())
{
t
.
Errorf
(
"[%v]wrong number of api interactions"
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment