Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.

Commit b1881e5

Browse files
authored
Merge pull request #589 from karimra/k8s-locker-anno
add original key as a k8s lease annotation
2 parents 3caa03e + f9c8ece commit b1881e5

2 files changed

Lines changed: 19 additions & 32 deletions

File tree

lockers/k8s_locker/k8s_locker.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
defaultLeaseDuration = 10 * time.Second
2929
loggingPrefix = "[k8s_locker] "
3030
defaultNamespace = "default"
31+
origKeyName = "original-key"
3132
)
3233

3334
func init() {
@@ -38,9 +39,6 @@ func init() {
3839
acquiredlocks: make(map[string]*lock),
3940
attemtinglocks: make(map[string]*lock),
4041
logger: log.New(io.Discard, loggingPrefix, utils.DefaultLoggingFlags),
41-
services: make(map[string]context.CancelFunc),
42-
km: new(sync.RWMutex),
43-
keyMapping: make(map[string]string),
4442
}
4543
})
4644
}
@@ -52,11 +50,8 @@ type k8sLocker struct {
5250
m *sync.RWMutex
5351
acquiredlocks map[string]*lock
5452
attemtinglocks map[string]*lock
55-
services map[string]context.CancelFunc
56-
//
57-
identity string // hostname
58-
km *sync.RWMutex
59-
keyMapping map[string]string
53+
54+
identity string // hostname
6055
}
6156

6257
type config struct {
@@ -98,12 +93,12 @@ func (k *k8sLocker) Init(ctx context.Context, cfg map[string]interface{}, opts .
9893

9994
func (k *k8sLocker) Lock(ctx context.Context, key string, val []byte) (bool, error) {
10095
nkey := strings.ReplaceAll(key, "/", "-")
101-
k.km.Lock()
102-
k.keyMapping[nkey] = key
103-
k.km.Unlock()
10496
doneChan := make(chan struct{})
10597
l := &coordinationv1.Lease{
10698
ObjectMeta: metav1.ObjectMeta{
99+
Annotations: map[string]string{
100+
origKeyName: key,
101+
},
107102
Name: nkey,
108103
Namespace: k.Cfg.Namespace,
109104
Labels: map[string]string{
@@ -203,12 +198,8 @@ func (k *k8sLocker) Lock(ctx context.Context, key string, val []byte) (bool, err
203198
func (k *k8sLocker) KeepLock(ctx context.Context, key string) (chan struct{}, chan error) {
204199
doneChan := make(chan struct{})
205200
errChan := make(chan error)
206-
k.km.RLock()
207-
nkey, ok := k.keyMapping[key]
208-
k.km.RUnlock()
209-
if !ok {
210-
nkey = strings.ReplaceAll(key, "/", "-")
211-
}
201+
nkey := strings.ReplaceAll(key, "/", "-")
202+
212203
go func() {
213204
defer close(doneChan)
214205
ticker := time.NewTicker(k.Cfg.RenewPeriod)
@@ -260,12 +251,7 @@ func (k *k8sLocker) KeepLock(ctx context.Context, key string) (chan struct{}, ch
260251
}
261252

262253
func (k *k8sLocker) Unlock(ctx context.Context, key string) error {
263-
k.km.RLock()
264-
nkey, ok := k.keyMapping[key]
265-
k.km.RUnlock()
266-
if !ok {
267-
nkey = strings.ReplaceAll(key, "/", "-")
268-
}
254+
nkey := strings.ReplaceAll(key, "/", "-")
269255
k.m.Lock()
270256
defer k.m.Unlock()
271257
k.unlock(ctx, nkey)

lockers/k8s_locker/k8s_registration.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,26 @@ func (k *k8sLocker) IsLocked(ctx context.Context, key string) (bool, error) {
9696
}
9797

9898
func (k *k8sLocker) List(ctx context.Context, prefix string) (map[string]string, error) {
99-
ll, err := k.clientset.CoordinationV1().Leases(k.Cfg.Namespace).List(ctx, metav1.ListOptions{
100-
LabelSelector: "app=gnmic",
101-
})
99+
ll, err := k.clientset.CoordinationV1().Leases(k.Cfg.Namespace).List(ctx,
100+
metav1.ListOptions{
101+
LabelSelector: "app=gnmic",
102+
})
102103
if err != nil {
103104
return nil, err
104105
}
105106

106107
prefix = strings.ReplaceAll(prefix, "/", "-")
107108
rs := make(map[string]string, len(ll.Items))
108-
109-
k.km.Lock()
110-
defer k.km.Unlock()
111109
for _, l := range ll.Items {
112110
for key, v := range l.Labels {
111+
if key == "app" {
112+
continue
113+
}
113114
if strings.HasPrefix(key, prefix) {
114-
if okey, ok := k.keyMapping[key]; ok {
115+
okey, ok := l.Annotations[origKeyName]
116+
if ok {
115117
rs[okey] = v
116-
} else {
117-
rs[strings.ReplaceAll(key, "-", "/")] = v
118+
continue
118119
}
119120
}
120121
}

0 commit comments

Comments
 (0)