-
Notifications
You must be signed in to change notification settings - Fork 3
/
leaderelection.go
130 lines (122 loc) · 3.53 KB
/
leaderelection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2021 Digital Ocean, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package leaderelection
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
// LeaderElection runs the "run" command when it is the leader, and terminates it when it is not.
func LeaderElection(
ctx context.Context,
log logrus.FieldLogger,
namespace string,
resourceName string,
client kubernetes.Interface,
runs ...func(ctx context.Context),
) error {
var childCtx context.Context
var childCancel context.CancelFunc
var wg sync.WaitGroup
var childLock sync.Mutex
// leaderCtx doesn't descend from ctx explicitly. It won't be canceled until the child
// is finished (or until we're certain it will never run). By waiting for children to
// finish, we can safely use ReleaseOnCancel=true.
leaderCtx, leaderCancel := context.WithCancel(context.Background())
defer leaderCancel()
go func() {
<-ctx.Done()
log.Debug("leaderelection canceled context; waiting for children")
childLock.Lock()
if childCtx != nil {
childCancel()
wg.Wait()
}
childLock.Unlock()
leaderCancel()
}()
hostname, err := os.Hostname()
if err != nil {
return err
}
id := fmt.Sprintf("%s_%s", hostname, string(uuid.NewUUID()))
rl, err := resourcelock.New(
resourcelock.LeasesResourceLock,
namespace,
resourceName,
client.CoreV1(),
client.CoordinationV1(),
resourcelock.ResourceLockConfig{
Identity: id,
EventRecorder: nil, // eventrecorder is optional, though we may want it sometime
})
if err != nil {
return err
}
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: rl,
// durations are recommendations from:
// https://godoc.org/k8s.io/client-go/tools/leaderelection#LeaderElectionConfig
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) {
// OnStartedLeading is spawned in a goroutine. We should only start if our parent
// will be around to wait for completion.
childLock.Lock()
defer childLock.Unlock()
if ctx.Err() != nil {
return
}
// If the parent context hasn't been canceled, we can be sure they haven't executed
// wg.Wait() yet, and it's safe to start.
childCtx, childCancel = context.WithCancel(ctx)
wg.Add(len(runs))
for _, run := range runs {
run := run
go func() {
defer wg.Done()
run(childCtx)
}()
}
},
OnStoppedLeading: func() {
childLock.Lock()
defer childLock.Unlock()
if childCtx == nil {
return // ctx must have been canceled before OnStartedLeading launched a child.
}
childCancel()
childCtx = nil
wg.Wait()
},
},
ReleaseOnCancel: true,
})
if err != nil {
return err
}
l.Run(leaderCtx)
return nil
}