...
1
16
17 package resourcelock
18
19 import (
20 "bytes"
21 "context"
22 "encoding/json"
23
24 apierrors "k8s.io/apimachinery/pkg/api/errors"
25 )
26
27 const (
28 UnknownLeader = "leaderelection.k8s.io/unknown"
29 )
30
31
32 type MultiLock struct {
33 Primary Interface
34 Secondary Interface
35 }
36
37
38 func (ml *MultiLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
39 primary, primaryRaw, err := ml.Primary.Get(ctx)
40 if err != nil {
41 return nil, nil, err
42 }
43
44 secondary, secondaryRaw, err := ml.Secondary.Get(ctx)
45 if err != nil {
46
47 if apierrors.IsNotFound(err) && primary.HolderIdentity != ml.Identity() {
48 return primary, primaryRaw, nil
49 }
50 return nil, nil, err
51 }
52
53 if primary.HolderIdentity != secondary.HolderIdentity {
54 primary.HolderIdentity = UnknownLeader
55 primaryRaw, err = json.Marshal(primary)
56 if err != nil {
57 return nil, nil, err
58 }
59 }
60 return primary, ConcatRawRecord(primaryRaw, secondaryRaw), nil
61 }
62
63
64 func (ml *MultiLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
65 err := ml.Primary.Create(ctx, ler)
66 if err != nil && !apierrors.IsAlreadyExists(err) {
67 return err
68 }
69 return ml.Secondary.Create(ctx, ler)
70 }
71
72
73 func (ml *MultiLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
74 err := ml.Primary.Update(ctx, ler)
75 if err != nil {
76 return err
77 }
78 _, _, err = ml.Secondary.Get(ctx)
79 if err != nil && apierrors.IsNotFound(err) {
80 return ml.Secondary.Create(ctx, ler)
81 }
82 return ml.Secondary.Update(ctx, ler)
83 }
84
85
86 func (ml *MultiLock) RecordEvent(s string) {
87 ml.Primary.RecordEvent(s)
88 ml.Secondary.RecordEvent(s)
89 }
90
91
92
93 func (ml *MultiLock) Describe() string {
94 return ml.Primary.Describe()
95 }
96
97
98 func (ml *MultiLock) Identity() string {
99 return ml.Primary.Identity()
100 }
101
102 func ConcatRawRecord(primaryRaw, secondaryRaw []byte) []byte {
103 return bytes.Join([][]byte{primaryRaw, secondaryRaw}, []byte(","))
104 }
105
View as plain text