2 Copyright 2017 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.
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
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.
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
43 // Get returns the election record from a ConfigMap Annotation
44 func (cml *ConfigMapLock) Get() (*LeaderElectionRecord, error) {
45 var record LeaderElectionRecord
47 cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Get(cml.ConfigMapMeta.Name, metav1.GetOptions{})
51 if cml.cm.Annotations == nil {
52 cml.cm.Annotations = make(map[string]string)
54 if recordBytes, found := cml.cm.Annotations[LeaderElectionRecordAnnotationKey]; found {
55 if err := json.Unmarshal([]byte(recordBytes), &record); err != nil {
62 // Create attempts to create a LeaderElectionRecord annotation
63 func (cml *ConfigMapLock) Create(ler LeaderElectionRecord) error {
64 recordBytes, err := json.Marshal(ler)
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),
80 // Update will update an existing annotation on a given resource.
81 func (cml *ConfigMapLock) Update(ler LeaderElectionRecord) error {
83 return errors.New("configmap not initialized, call get or create first")
85 recordBytes, err := json.Marshal(ler)
89 cml.cm.Annotations[LeaderElectionRecordAnnotationKey] = string(recordBytes)
90 cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Update(cml.cm)
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)
100 // Describe is used to convert details on current resource lock
102 func (cml *ConfigMapLock) Describe() string {
103 return fmt.Sprintf("%v/%v", cml.ConfigMapMeta.Namespace, cml.ConfigMapMeta.Name)
106 // returns the Identity of the lock
107 func (cml *ConfigMapLock) Identity() string {
108 return cml.LockConfig.Identity