istio
109 строк · 2.9 Кб
1/*
2Copyright 2019 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 k8sresourcelock18
19import (20"bytes"21"context"22"encoding/json"23
24kerrors "k8s.io/apimachinery/pkg/api/errors"25)
26
27const (28UnknownLeader = "leaderelection.k8s.io/unknown"29)
30
31// MultiLock is used for lock's migration
32type MultiLock struct {33Primary Interface
34Secondary Interface
35}
36
37// Get returns the older election record of the lock
38func (ml *MultiLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {39primary, primaryRaw, err := ml.Primary.Get(ctx)40if err != nil {41return nil, nil, err42}43
44secondary, secondaryRaw, err := ml.Secondary.Get(ctx)45if err != nil {46// Lock is held by old client47if kerrors.IsNotFound(err) && primary.HolderIdentity != ml.Identity() {48return primary, primaryRaw, nil49}50return nil, nil, err51}52
53if primary.HolderIdentity != secondary.HolderIdentity {54primary.HolderIdentity = UnknownLeader55primaryRaw, err = json.Marshal(primary)56if err != nil {57return nil, nil, err58}59}60return primary, ConcatRawRecord(primaryRaw, secondaryRaw), nil61}
62
63// Create attempts to create both primary lock and secondary lock
64func (ml *MultiLock) Create(ctx context.Context, ler LeaderElectionRecord) error {65err := ml.Primary.Create(ctx, ler)66if err != nil && !kerrors.IsAlreadyExists(err) {67return err68}69return ml.Secondary.Create(ctx, ler)70}
71
72// Update will update and existing annotation on both two resources.
73func (ml *MultiLock) Update(ctx context.Context, ler LeaderElectionRecord) error {74err := ml.Primary.Update(ctx, ler)75if err != nil {76return err77}78_, _, err = ml.Secondary.Get(ctx)79if err != nil && kerrors.IsNotFound(err) {80return ml.Secondary.Create(ctx, ler)81}82return ml.Secondary.Update(ctx, ler)83}
84
85// RecordEvent in leader election while adding meta-data
86func (ml *MultiLock) RecordEvent(s string) {87ml.Primary.RecordEvent(s)88ml.Secondary.RecordEvent(s)89}
90
91// Describe is used to convert details on current resource lock
92// into a string
93func (ml *MultiLock) Describe() string {94return ml.Primary.Describe()95}
96
97// Identity returns the Identity of the lock
98func (ml *MultiLock) Identity() string {99return ml.Primary.Identity()100}
101
102// Key returns the Key of the lock
103func (ml *MultiLock) Key() string {104return ml.Primary.Key()105}
106
107func ConcatRawRecord(primaryRaw, secondaryRaw []byte) []byte {108return bytes.Join([][]byte{primaryRaw, secondaryRaw}, []byte(","))109}
110