Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / tools / leaderelection / resourcelock / interface.go
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package resourcelock
18
19 import (
20         "fmt"
21
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"
25 )
26
27 const (
28         LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader"
29         EndpointsResourceLock             = "endpoints"
30         ConfigMapsResourceLock            = "configmaps"
31 )
32
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"`
43 }
44
45 // ResourceLockConfig common data that exists across different
46 // resource locks
47 type ResourceLockConfig struct {
48         Identity      string
49         EventRecorder record.EventRecorder
50 }
51
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)
60
61         // Create attempts to create a LeaderElectionRecord
62         Create(ler LeaderElectionRecord) error
63
64         // Update will update and existing LeaderElectionRecord
65         Update(ler LeaderElectionRecord) error
66
67         // RecordEvent is used to record events
68         RecordEvent(string)
69
70         // Identity will return the locks Identity
71         Identity() string
72
73         // Describe is used to convert details on current resource lock
74         // into a string
75         Describe() string
76 }
77
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) {
80         switch lockType {
81         case EndpointsResourceLock:
82                 return &EndpointsLock{
83                         EndpointsMeta: metav1.ObjectMeta{
84                                 Namespace: ns,
85                                 Name:      name,
86                         },
87                         Client:     client,
88                         LockConfig: rlc,
89                 }, nil
90         case ConfigMapsResourceLock:
91                 return &ConfigMapLock{
92                         ConfigMapMeta: metav1.ObjectMeta{
93                                 Namespace: ns,
94                                 Name:      name,
95                         },
96                         Client:     client,
97                         LockConfig: rlc,
98                 }, nil
99         default:
100                 return nil, fmt.Errorf("Invalid lock-type %s", lockType)
101         }
102 }