2 Copyright 2016 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
24 "k8s.io/client-go/tools/record"
28 LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader"
29 EndpointsResourceLock = "endpoints"
30 ConfigMapsResourceLock = "configmaps"
33 // LeaderElectionRecord is the record that is stored in the leader election annotation.
34 // This information should be used for observational purposes only and could be replaced
35 // with a random string (e.g. UUID) with only slight modification of this code.
36 // TODO(mikedanese): this should potentially be versioned
37 type LeaderElectionRecord struct {
38 HolderIdentity string `json:"holderIdentity"`
39 LeaseDurationSeconds int `json:"leaseDurationSeconds"`
40 AcquireTime metav1.Time `json:"acquireTime"`
41 RenewTime metav1.Time `json:"renewTime"`
42 LeaderTransitions int `json:"leaderTransitions"`
45 // ResourceLockConfig common data that exists across different
47 type ResourceLockConfig struct {
49 EventRecorder record.EventRecorder
52 // Interface offers a common interface for locking on arbitrary
53 // resources used in leader election. The Interface is used
54 // to hide the details on specific implementations in order to allow
55 // them to change over time. This interface is strictly for use
56 // by the leaderelection code.
57 type Interface interface {
58 // Get returns the LeaderElectionRecord
59 Get() (*LeaderElectionRecord, error)
61 // Create attempts to create a LeaderElectionRecord
62 Create(ler LeaderElectionRecord) error
64 // Update will update and existing LeaderElectionRecord
65 Update(ler LeaderElectionRecord) error
67 // RecordEvent is used to record events
70 // Identity will return the locks Identity
73 // Describe is used to convert details on current resource lock
78 // Manufacture will create a lock of a given type according to the input parameters
79 func New(lockType string, ns string, name string, client corev1.CoreV1Interface, rlc ResourceLockConfig) (Interface, error) {
81 case EndpointsResourceLock:
82 return &EndpointsLock{
83 EndpointsMeta: metav1.ObjectMeta{
90 case ConfigMapsResourceLock:
91 return &ConfigMapLock{
92 ConfigMapMeta: metav1.ObjectMeta{
100 return nil, fmt.Errorf("Invalid lock-type %s", lockType)