Skip to content

Commit 98e454b

Browse files
authored
Merge pull request #107 from andrewsykim/external-snapshotter-leader-election
Add Leader Election Support
2 parents 54a21f1 + 2e2058d commit 98e454b

File tree

36 files changed

+2723
-380
lines changed

36 files changed

+2723
-380
lines changed

Gopkg.lock

Lines changed: 23 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ required = [
1111

1212
[[constraint]]
1313
name = "github.com/container-storage-interface/spec"
14-
version = "=1.0.0" # Set this to exactly 1.0.0 until we can update test code to handle 1.1.0
15-
14+
version = "=1.1.0"
1615

1716
# The dependency on external-provisioner should be removed with #60.
1817
[[constraint]]
@@ -37,7 +36,7 @@ required = [
3736

3837
[[constraint]]
3938
name = "github.com/kubernetes-csi/csi-lib-utils"
40-
version = ">=0.4.0"
39+
version = ">=v0.6.1"
4140

4241
[prune]
4342
non-go = true

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ It is necessary to create a new service account and give it enough privileges to
4848
for i in $(find deploy/kubernetes -name '*.yaml'); do kubectl create -f $i; done
4949
```
5050

51+
### Running with Leader Election
52+
53+
If you want to run external-snapshotter with higher availability, you can enable resource based leader election. To enable this, set the following flags:
54+
```bash
55+
--leader-election=true
56+
```
57+
5158
## Testing
5259

5360
Running Unit Tests:

cmd/csi-snapshotter/main.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
"github.com/container-storage-interface/spec/lib/go/csi"
3636
"github.com/kubernetes-csi/csi-lib-utils/connection"
37+
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
3738
csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc"
3839
"github.com/kubernetes-csi/external-snapshotter/pkg/controller"
3940
"github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
@@ -65,10 +66,14 @@ var (
6566
snapshotNamePrefix = flag.String("snapshot-name-prefix", "snapshot", "Prefix to apply to the name of a created snapshot")
6667
snapshotNameUUIDLength = flag.Int("snapshot-name-uuid-length", -1, "Length in characters for the generated uuid of a created snapshot. Defaults behavior is to NOT truncate.")
6768
showVersion = flag.Bool("version", false, "Show version.")
69+
70+
leaderElection = flag.Bool("leader-election", false, "Enables leader election.")
71+
leaderElectionNamespace = flag.String("leader-election-namespace", "", "The namespace where the leader election resource exists. Defaults to the pod namespace if not set.")
6872
)
6973

7074
var (
71-
version = "unknown"
75+
version = "unknown"
76+
leaderElectionLockName = "external-snapshotter-leader-election"
7277
)
7378

7479
func main() {
@@ -192,17 +197,31 @@ func main() {
192197
*snapshotNameUUIDLength,
193198
)
194199

195-
// run...
196-
stopCh := make(chan struct{})
197-
factory.Start(stopCh)
198-
coreFactory.Start(stopCh)
199-
go ctrl.Run(threads, stopCh)
200-
201-
// ...until SIGINT
202-
c := make(chan os.Signal, 1)
203-
signal.Notify(c, os.Interrupt)
204-
<-c
205-
close(stopCh)
200+
run := func(context.Context) {
201+
// run...
202+
stopCh := make(chan struct{})
203+
factory.Start(stopCh)
204+
coreFactory.Start(stopCh)
205+
go ctrl.Run(threads, stopCh)
206+
207+
// ...until SIGINT
208+
c := make(chan os.Signal, 1)
209+
signal.Notify(c, os.Interrupt)
210+
<-c
211+
close(stopCh)
212+
}
213+
214+
if !*leaderElection {
215+
run(context.TODO())
216+
} else {
217+
le := leaderelection.NewLeaderElection(kubeClient, leaderElectionLockName, run)
218+
if *leaderElectionNamespace != "" {
219+
le.WithNamespace(*leaderElectionNamespace)
220+
}
221+
if err := le.Run(); err != nil {
222+
klog.Fatalf("failed to initialize leader election: %v", err)
223+
}
224+
}
206225
}
207226

208227
func buildConfig(kubeconfig string) (*rest.Config, error) {

deploy/kubernetes/rbac-external-provisioner.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ roleRef:
5959
apiGroup: rbac.authorization.k8s.io
6060

6161
---
62-
# Provisioner must be able to work with endpoints in current namespace
62+
# Provisioner must be able to work with endpoints and leases in current namespace
6363
# if (and only if) leadership election is enabled
6464
kind: Role
6565
apiVersion: rbac.authorization.k8s.io/v1
@@ -71,6 +71,9 @@ rules:
7171
- apiGroups: [""]
7272
resources: ["endpoints"]
7373
verbs: ["get", "watch", "list", "delete", "update", "create"]
74+
- apiGroups: ["coordination.k8s.io"]
75+
resources: ["leases"]
76+
verbs: ["get", "watch", "list", "delete", "update", "create"]
7477

7578
---
7679
kind: RoleBinding

deploy/kubernetes/rbac.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,30 @@ roleRef:
6363
# change the name also here if the ClusterRole gets renamed
6464
name: external-snapshotter-runner
6565
apiGroup: rbac.authorization.k8s.io
66+
67+
---
68+
kind: Role
69+
apiVersion: rbac.authorization.k8s.io/v1
70+
metadata:
71+
namespace: default # TODO: replace with the namespace you want for your sidecar
72+
name: external-snapshotter-leaderelection
73+
rules:
74+
- apiGroups: ["coordination.k8s.io"]
75+
resources: ["leases"]
76+
verbs: ["get", "watch", "list", "delete", "update", "create"]
77+
78+
---
79+
kind: RoleBinding
80+
apiVersion: rbac.authorization.k8s.io/v1
81+
metadata:
82+
name: external-snapshotter-leaderelection
83+
namespace: default # TODO: replace with the namespace you want for your sidecar
84+
subjects:
85+
- kind: ServiceAccount
86+
name: csi-snapshotter
87+
namespace: default # TODO: replace with the namespace you want for your sidecar
88+
roleRef:
89+
kind: Role
90+
name: external-snapshotter-leaderelection
91+
apiGroup: rbac.authorization.k8s.io
92+

deploy/kubernetes/setup-csi-snapshotter.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ spec:
8989
args:
9090
- "--csi-address=$(ADDRESS)"
9191
- "--connection-timeout=15s"
92+
- "--leader-election=false"
9293
env:
9394
- name: ADDRESS
9495
value: /csi/csi.sock

0 commit comments

Comments
 (0)