istio
122 строки · 3.7 Кб
1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package k8sresourcelock
18
19import (
20"context"
21"encoding/json"
22"errors"
23"fmt"
24
25v1 "k8s.io/api/core/v1"
26metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
28)
29
30type EndpointsLock struct {
31// EndpointsMeta should contain a Name and a Namespace of an
32// Endpoints object that the LeaderElector will attempt to lead.
33EndpointsMeta metav1.ObjectMeta
34Client corev1client.EndpointsGetter
35LockConfig ResourceLockConfig
36e *v1.Endpoints
37}
38
39// Get returns the election record from a Endpoints Annotation
40func (el *EndpointsLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
41var record LeaderElectionRecord
42var err error
43el.e, err = el.Client.Endpoints(el.EndpointsMeta.Namespace).Get(ctx, el.EndpointsMeta.Name, metav1.GetOptions{})
44if err != nil {
45return nil, nil, err
46}
47if el.e.Annotations == nil {
48el.e.Annotations = make(map[string]string)
49}
50recordStr, found := el.e.Annotations[LeaderElectionRecordAnnotationKey]
51recordBytes := []byte(recordStr)
52if found {
53if err := json.Unmarshal(recordBytes, &record); err != nil {
54return nil, nil, err
55}
56}
57return &record, recordBytes, nil
58}
59
60// Create attempts to create a LeaderElectionRecord annotation
61func (el *EndpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
62recordBytes, err := json.Marshal(ler)
63if err != nil {
64return err
65}
66el.e, err = el.Client.Endpoints(el.EndpointsMeta.Namespace).Create(ctx, &v1.Endpoints{
67ObjectMeta: metav1.ObjectMeta{
68Name: el.EndpointsMeta.Name,
69Namespace: el.EndpointsMeta.Namespace,
70Annotations: map[string]string{
71LeaderElectionRecordAnnotationKey: string(recordBytes),
72},
73},
74}, metav1.CreateOptions{})
75return err
76}
77
78// Update will update and existing annotation on a given resource.
79func (el *EndpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
80if el.e == nil {
81return errors.New("endpoint not initialized, call get or create first")
82}
83recordBytes, err := json.Marshal(ler)
84if err != nil {
85return err
86}
87if el.e.Annotations == nil {
88el.e.Annotations = make(map[string]string)
89}
90el.e.Annotations[LeaderElectionRecordAnnotationKey] = string(recordBytes)
91e, err := el.Client.Endpoints(el.EndpointsMeta.Namespace).Update(ctx, el.e, metav1.UpdateOptions{})
92if err != nil {
93return err
94}
95el.e = e
96return nil
97}
98
99// RecordEvent in leader election while adding meta-data
100func (el *EndpointsLock) RecordEvent(s string) {
101if el.LockConfig.EventRecorder == nil {
102return
103}
104events := fmt.Sprintf("%v %v", el.LockConfig.Identity, s)
105el.LockConfig.EventRecorder.Eventf(&v1.Endpoints{ObjectMeta: el.e.ObjectMeta}, v1.EventTypeNormal, "LeaderElection", events)
106}
107
108// Describe is used to convert details on current resource lock
109// into a string
110func (el *EndpointsLock) Describe() string {
111return fmt.Sprintf("%v/%v", el.EndpointsMeta.Namespace, el.EndpointsMeta.Name)
112}
113
114// Identity returns the Identity of the lock
115func (el *EndpointsLock) Identity() string {
116return el.LockConfig.Identity
117}
118
119// Key returns the Key of the lock
120func (el *EndpointsLock) Key() string {
121return el.LockConfig.Key
122}
123