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
41017585
Commit
41017585
authored
Nov 20, 2018
by
Matthew Wong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Lease implementation to leaderelection package
parent
35bd2a5e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
154 additions
and
17 deletions
+154
-17
controllermanager.go
cmd/cloud-controller-manager/app/controllermanager.go
+1
-0
controllermanager.go
cmd/kube-controller-manager/app/controllermanager.go
+1
-0
options.go
cmd/kube-scheduler/app/options/options.go
+1
-0
BUILD
staging/src/k8s.io/client-go/tools/leaderelection/BUILD
+7
-8
leaderelection_test.go
....io/client-go/tools/leaderelection/leaderelection_test.go
+0
-0
BUILD
.../k8s.io/client-go/tools/leaderelection/resourcelock/BUILD
+6
-6
interface.go
.../client-go/tools/leaderelection/resourcelock/interface.go
+14
-3
leaselock.go
.../client-go/tools/leaderelection/resourcelock/leaselock.go
+124
-0
No files found.
cmd/cloud-controller-manager/app/controllermanager.go
View file @
41017585
...
@@ -186,6 +186,7 @@ func Run(c *cloudcontrollerconfig.CompletedConfig, stopCh <-chan struct{}) error
...
@@ -186,6 +186,7 @@ func Run(c *cloudcontrollerconfig.CompletedConfig, stopCh <-chan struct{}) error
"kube-system"
,
"kube-system"
,
"cloud-controller-manager"
,
"cloud-controller-manager"
,
c
.
LeaderElectionClient
.
CoreV1
(),
c
.
LeaderElectionClient
.
CoreV1
(),
c
.
LeaderElectionClient
.
CoordinationV1
(),
resourcelock
.
ResourceLockConfig
{
resourcelock
.
ResourceLockConfig
{
Identity
:
id
,
Identity
:
id
,
EventRecorder
:
c
.
EventRecorder
,
EventRecorder
:
c
.
EventRecorder
,
...
...
cmd/kube-controller-manager/app/controllermanager.go
View file @
41017585
...
@@ -235,6 +235,7 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
...
@@ -235,6 +235,7 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
"kube-system"
,
"kube-system"
,
"kube-controller-manager"
,
"kube-controller-manager"
,
c
.
LeaderElectionClient
.
CoreV1
(),
c
.
LeaderElectionClient
.
CoreV1
(),
c
.
LeaderElectionClient
.
CoordinationV1
(),
resourcelock
.
ResourceLockConfig
{
resourcelock
.
ResourceLockConfig
{
Identity
:
id
,
Identity
:
id
,
EventRecorder
:
c
.
EventRecorder
,
EventRecorder
:
c
.
EventRecorder
,
...
...
cmd/kube-scheduler/app/options/options.go
View file @
41017585
...
@@ -274,6 +274,7 @@ func makeLeaderElectionConfig(config kubeschedulerconfig.KubeSchedulerLeaderElec
...
@@ -274,6 +274,7 @@ func makeLeaderElectionConfig(config kubeschedulerconfig.KubeSchedulerLeaderElec
config
.
LockObjectNamespace
,
config
.
LockObjectNamespace
,
config
.
LockObjectName
,
config
.
LockObjectName
,
client
.
CoreV1
(),
client
.
CoreV1
(),
client
.
CoordinationV1
(),
resourcelock
.
ResourceLockConfig
{
resourcelock
.
ResourceLockConfig
{
Identity
:
id
,
Identity
:
id
,
EventRecorder
:
recorder
,
EventRecorder
:
recorder
,
...
...
staging/src/k8s.io/client-go/tools/leaderelection/BUILD
View file @
41017585
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
go_library(
name = "go_default_library",
name = "go_default_library",
...
@@ -15,6 +9,7 @@ go_library(
...
@@ -15,6 +9,7 @@ go_library(
],
],
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/tools/leaderelection",
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/tools/leaderelection",
importpath = "k8s.io/client-go/tools/leaderelection",
importpath = "k8s.io/client-go/tools/leaderelection",
visibility = ["//visibility:public"],
deps = [
deps = [
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
...
@@ -34,12 +29,15 @@ go_test(
...
@@ -34,12 +29,15 @@ go_test(
],
],
embed = [":go_default_library"],
embed = [":go_default_library"],
deps = [
deps = [
"//staging/src/k8s.io/api/coordination/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1/fake:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/client-go/testing:go_default_library",
"//staging/src/k8s.io/client-go/testing:go_default_library",
"//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:go_default_library",
"//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
...
@@ -61,4 +59,5 @@ filegroup(
...
@@ -61,4 +59,5 @@ filegroup(
"//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:all-srcs",
"//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:all-srcs",
],
],
tags = ["automanaged"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
)
staging/src/k8s.io/client-go/tools/leaderelection/leaderelection_test.go
View file @
41017585
This diff is collapsed.
Click to expand it.
staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/BUILD
View file @
41017585
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
go_library(
name = "go_default_library",
name = "go_default_library",
...
@@ -11,13 +6,17 @@ go_library(
...
@@ -11,13 +6,17 @@ go_library(
"configmaplock.go",
"configmaplock.go",
"endpointslock.go",
"endpointslock.go",
"interface.go",
"interface.go",
"leaselock.go",
],
],
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/tools/leaderelection/resourcelock",
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/tools/leaderelection/resourcelock",
importpath = "k8s.io/client-go/tools/leaderelection/resourcelock",
importpath = "k8s.io/client-go/tools/leaderelection/resourcelock",
visibility = ["//visibility:public"],
deps = [
deps = [
"//staging/src/k8s.io/api/coordination/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/coordination/v1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
],
],
)
)
...
@@ -33,4 +32,5 @@ filegroup(
...
@@ -33,4 +32,5 @@ filegroup(
name = "all-srcs",
name = "all-srcs",
srcs = [":package-srcs"],
srcs = [":package-srcs"],
tags = ["automanaged"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
)
staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/interface.go
View file @
41017585
...
@@ -21,6 +21,7 @@ import (
...
@@ -21,6 +21,7 @@ import (
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
coordinationv1
"k8s.io/client-go/kubernetes/typed/coordination/v1"
corev1
"k8s.io/client-go/kubernetes/typed/core/v1"
corev1
"k8s.io/client-go/kubernetes/typed/core/v1"
)
)
...
@@ -28,6 +29,7 @@ const (
...
@@ -28,6 +29,7 @@ const (
LeaderElectionRecordAnnotationKey
=
"control-plane.alpha.kubernetes.io/leader"
LeaderElectionRecordAnnotationKey
=
"control-plane.alpha.kubernetes.io/leader"
EndpointsResourceLock
=
"endpoints"
EndpointsResourceLock
=
"endpoints"
ConfigMapsResourceLock
=
"configmaps"
ConfigMapsResourceLock
=
"configmaps"
LeasesResourceLock
=
"leases"
)
)
// LeaderElectionRecord is the record that is stored in the leader election annotation.
// LeaderElectionRecord is the record that is stored in the leader election annotation.
...
@@ -89,7 +91,7 @@ type Interface interface {
...
@@ -89,7 +91,7 @@ type Interface interface {
}
}
// Manufacture will create a lock of a given type according to the input parameters
// Manufacture will create a lock of a given type according to the input parameters
func
New
(
lockType
string
,
ns
string
,
name
string
,
c
lient
corev1
.
Core
V1Interface
,
rlc
ResourceLockConfig
)
(
Interface
,
error
)
{
func
New
(
lockType
string
,
ns
string
,
name
string
,
c
oreClient
corev1
.
CoreV1Interface
,
coordinationClient
coordinationv1
.
Coordination
V1Interface
,
rlc
ResourceLockConfig
)
(
Interface
,
error
)
{
switch
lockType
{
switch
lockType
{
case
EndpointsResourceLock
:
case
EndpointsResourceLock
:
return
&
EndpointsLock
{
return
&
EndpointsLock
{
...
@@ -97,7 +99,7 @@ func New(lockType string, ns string, name string, client corev1.CoreV1Interface,
...
@@ -97,7 +99,7 @@ func New(lockType string, ns string, name string, client corev1.CoreV1Interface,
Namespace
:
ns
,
Namespace
:
ns
,
Name
:
name
,
Name
:
name
,
},
},
Client
:
client
,
Client
:
c
oreC
lient
,
LockConfig
:
rlc
,
LockConfig
:
rlc
,
},
nil
},
nil
case
ConfigMapsResourceLock
:
case
ConfigMapsResourceLock
:
...
@@ -106,7 +108,16 @@ func New(lockType string, ns string, name string, client corev1.CoreV1Interface,
...
@@ -106,7 +108,16 @@ func New(lockType string, ns string, name string, client corev1.CoreV1Interface,
Namespace
:
ns
,
Namespace
:
ns
,
Name
:
name
,
Name
:
name
,
},
},
Client
:
client
,
Client
:
coreClient
,
LockConfig
:
rlc
,
},
nil
case
LeasesResourceLock
:
return
&
LeaseLock
{
LeaseMeta
:
metav1
.
ObjectMeta
{
Namespace
:
ns
,
Name
:
name
,
},
Client
:
coordinationClient
,
LockConfig
:
rlc
,
LockConfig
:
rlc
,
},
nil
},
nil
default
:
default
:
...
...
staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/leaselock.go
0 → 100644
View file @
41017585
/*
Copyright 2018 The Kubernetes Authors.
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
resourcelock
import
(
"errors"
"fmt"
coordinationv1
"k8s.io/api/coordination/v1"
corev1
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
coordinationv1client
"k8s.io/client-go/kubernetes/typed/coordination/v1"
)
type
LeaseLock
struct
{
// LeaseMeta should contain a Name and a Namespace of a
// LeaseMeta object that the LeaderElector will attempt to lead.
LeaseMeta
metav1
.
ObjectMeta
Client
coordinationv1client
.
LeasesGetter
LockConfig
ResourceLockConfig
lease
*
coordinationv1
.
Lease
}
// Get returns the election record from a Lease spec
func
(
ll
*
LeaseLock
)
Get
()
(
*
LeaderElectionRecord
,
error
)
{
var
err
error
ll
.
lease
,
err
=
ll
.
Client
.
Leases
(
ll
.
LeaseMeta
.
Namespace
)
.
Get
(
ll
.
LeaseMeta
.
Name
,
metav1
.
GetOptions
{})
if
err
!=
nil
{
return
nil
,
err
}
return
LeaseSpecToLeaderElectionRecord
(
&
ll
.
lease
.
Spec
),
nil
}
// Create attempts to create a Lease
func
(
ll
*
LeaseLock
)
Create
(
ler
LeaderElectionRecord
)
error
{
var
err
error
ll
.
lease
,
err
=
ll
.
Client
.
Leases
(
ll
.
LeaseMeta
.
Namespace
)
.
Create
(
&
coordinationv1
.
Lease
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
ll
.
LeaseMeta
.
Name
,
Namespace
:
ll
.
LeaseMeta
.
Namespace
,
},
Spec
:
LeaderElectionRecordToLeaseSpec
(
&
ler
),
})
return
err
}
// Update will update an existing Lease spec.
func
(
ll
*
LeaseLock
)
Update
(
ler
LeaderElectionRecord
)
error
{
if
ll
.
lease
==
nil
{
return
errors
.
New
(
"lease not initialized, call get or create first"
)
}
ll
.
lease
.
Spec
=
LeaderElectionRecordToLeaseSpec
(
&
ler
)
var
err
error
ll
.
lease
,
err
=
ll
.
Client
.
Leases
(
ll
.
LeaseMeta
.
Namespace
)
.
Update
(
ll
.
lease
)
return
err
}
// RecordEvent in leader election while adding meta-data
func
(
ll
*
LeaseLock
)
RecordEvent
(
s
string
)
{
if
ll
.
LockConfig
.
EventRecorder
==
nil
{
return
}
events
:=
fmt
.
Sprintf
(
"%v %v"
,
ll
.
LockConfig
.
Identity
,
s
)
ll
.
LockConfig
.
EventRecorder
.
Eventf
(
&
coordinationv1
.
Lease
{
ObjectMeta
:
ll
.
lease
.
ObjectMeta
},
corev1
.
EventTypeNormal
,
"LeaderElection"
,
events
)
}
// Describe is used to convert details on current resource lock
// into a string
func
(
ll
*
LeaseLock
)
Describe
()
string
{
return
fmt
.
Sprintf
(
"%v/%v"
,
ll
.
LeaseMeta
.
Namespace
,
ll
.
LeaseMeta
.
Name
)
}
// returns the Identity of the lock
func
(
ll
*
LeaseLock
)
Identity
()
string
{
return
ll
.
LockConfig
.
Identity
}
func
LeaseSpecToLeaderElectionRecord
(
spec
*
coordinationv1
.
LeaseSpec
)
*
LeaderElectionRecord
{
holderIdentity
:=
""
if
spec
.
HolderIdentity
!=
nil
{
holderIdentity
=
*
spec
.
HolderIdentity
}
leaseDurationSeconds
:=
0
if
spec
.
LeaseDurationSeconds
!=
nil
{
leaseDurationSeconds
=
int
(
*
spec
.
LeaseDurationSeconds
)
}
leaseTransitions
:=
0
if
spec
.
LeaseTransitions
!=
nil
{
leaseTransitions
=
int
(
*
spec
.
LeaseTransitions
)
}
return
&
LeaderElectionRecord
{
HolderIdentity
:
holderIdentity
,
LeaseDurationSeconds
:
leaseDurationSeconds
,
AcquireTime
:
metav1
.
Time
{
spec
.
AcquireTime
.
Time
},
RenewTime
:
metav1
.
Time
{
spec
.
RenewTime
.
Time
},
LeaderTransitions
:
leaseTransitions
,
}
}
func
LeaderElectionRecordToLeaseSpec
(
ler
*
LeaderElectionRecord
)
coordinationv1
.
LeaseSpec
{
leaseDurationSeconds
:=
int32
(
ler
.
LeaseDurationSeconds
)
leaseTransitions
:=
int32
(
ler
.
LeaderTransitions
)
return
coordinationv1
.
LeaseSpec
{
HolderIdentity
:
&
ler
.
HolderIdentity
,
LeaseDurationSeconds
:
&
leaseDurationSeconds
,
AcquireTime
:
&
metav1
.
MicroTime
{
ler
.
AcquireTime
.
Time
},
RenewTime
:
&
metav1
.
MicroTime
{
ler
.
RenewTime
.
Time
},
LeaseTransitions
:
&
leaseTransitions
,
}
}
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