Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / tools / leaderelection / resourcelock / configmaplock.go
1 /*
2 Copyright 2017 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         "encoding/json"
21         "errors"
22         "fmt"
23
24         "k8s.io/api/core/v1"
25         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26         corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
27 )
28
29 // TODO: This is almost a exact replica of Endpoints lock.
30 // going forwards as we self host more and more components
31 // and use ConfigMaps as the means to pass that configuration
32 // data we will likely move to deprecate the Endpoints lock.
33
34 type ConfigMapLock struct {
35         // ConfigMapMeta should contain a Name and a Namespace of a
36         // ConfigMapMeta object that the LeaderElector will attempt to lead.
37         ConfigMapMeta metav1.ObjectMeta
38         Client        corev1client.ConfigMapsGetter
39         LockConfig    ResourceLockConfig
40         cm            *v1.ConfigMap
41 }
42
43 // Get returns the election record from a ConfigMap Annotation
44 func (cml *ConfigMapLock) Get() (*LeaderElectionRecord, error) {
45         var record LeaderElectionRecord
46         var err error
47         cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Get(cml.ConfigMapMeta.Name, metav1.GetOptions{})
48         if err != nil {
49                 return nil, err
50         }
51         if cml.cm.Annotations == nil {
52                 cml.cm.Annotations = make(map[string]string)
53         }
54         if recordBytes, found := cml.cm.Annotations[LeaderElectionRecordAnnotationKey]; found {
55                 if err := json.Unmarshal([]byte(recordBytes), &record); err != nil {
56                         return nil, err
57                 }
58         }
59         return &record, nil
60 }
61
62 // Create attempts to create a LeaderElectionRecord annotation
63 func (cml *ConfigMapLock) Create(ler LeaderElectionRecord) error {
64         recordBytes, err := json.Marshal(ler)
65         if err != nil {
66                 return err
67         }
68         cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Create(&v1.ConfigMap{
69                 ObjectMeta: metav1.ObjectMeta{
70                         Name:      cml.ConfigMapMeta.Name,
71                         Namespace: cml.ConfigMapMeta.Namespace,
72                         Annotations: map[string]string{
73                                 LeaderElectionRecordAnnotationKey: string(recordBytes),
74                         },
75                 },
76         })
77         return err
78 }
79
80 // Update will update an existing annotation on a given resource.
81 func (cml *ConfigMapLock) Update(ler LeaderElectionRecord) error {
82         if cml.cm == nil {
83                 return errors.New("configmap not initialized, call get or create first")
84         }
85         recordBytes, err := json.Marshal(ler)
86         if err != nil {
87                 return err
88         }
89         cml.cm.Annotations[LeaderElectionRecordAnnotationKey] = string(recordBytes)
90         cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Update(cml.cm)
91         return err
92 }
93
94 // RecordEvent in leader election while adding meta-data
95 func (cml *ConfigMapLock) RecordEvent(s string) {
96         events := fmt.Sprintf("%v %v", cml.LockConfig.Identity, s)
97         cml.LockConfig.EventRecorder.Eventf(&v1.ConfigMap{ObjectMeta: cml.cm.ObjectMeta}, v1.EventTypeNormal, "LeaderElection", events)
98 }
99
100 // Describe is used to convert details on current resource lock
101 // into a string
102 func (cml *ConfigMapLock) Describe() string {
103         return fmt.Sprintf("%v/%v", cml.ConfigMapMeta.Namespace, cml.ConfigMapMeta.Name)
104 }
105
106 // returns the Identity of the lock
107 func (cml *ConfigMapLock) Identity() string {
108         return cml.LockConfig.Identity
109 }