Skip to content

Commit 956ac4d

Browse files
committed
Implement leader election
Signed-off-by: Markus Blaschke <[email protected]>
1 parent a139330 commit 956ac4d

File tree

7 files changed

+311
-13
lines changed

7 files changed

+311
-13
lines changed

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ RUN ./kube-pool-manager --help
1919
# FINAL IMAGE
2020
#############################################
2121
FROM gcr.io/distroless/base
22-
ENV LOG_JSON=1
22+
ENV LOG_JSON=1 \
23+
LEASE_ENABLE=1
2324
COPY --from=build /go/src/github.com/webdevops/kube-pool-manager/kube-pool-manager /
2425
USER 1000
2526
ENTRYPOINT ["/kube-pool-manager"]

config/opts.go

+13
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,24 @@ type (
1515
LogJson bool ` long:"log.json" env:"LOG_JSON" description:"Switch log output to json format"`
1616
}
1717

18+
// instance
19+
Instance struct {
20+
Nodename *string `long:"instance.nodename" env:"INSTANCE_NODENAME" description:"Name of node where autopilot is running"`
21+
Namespace *string `long:"instance.namespace" env:"INSTANCE_NAMESPACE" description:"Name of namespace where autopilot is running"`
22+
Pod *string `long:"instance.pod" env:"INSTANCE_POD" description:"Name of pod where autopilot is running"`
23+
}
24+
1825
K8s struct {
1926
NodeLabelSelector string `long:"kube.node.labelselector" env:"KUBE_NODE_LABELSELECTOR" description:"Node Label selector which nodes should be checked" default:""`
2027
WatchTimeout time.Duration `long:"kube.watch.timeout" env:"KUBE_WATCH_TIMEOUT" description:"Timeout & full resync for node watch (time.Duration)" default:"24h"`
2128
}
2229

30+
// lease
31+
Lease struct {
32+
Enabled bool `long:"lease.enable" env:"LEASE_ENABLE" description:"Enable lease (leader election; enabled by default in docker images)"`
33+
Name string `long:"lease.name" env:"LEASE_NAME" description:"Name of lease lock" default:"kube-pool-manager-leader"`
34+
}
35+
2336
// general options
2437
DryRun bool `long:"dry-run" env:"DRY_RUN" description:"Dry run (do not apply to nodes)"`
2538
Config string `long:"config" env:"CONFIG" description:"Config path" required:"true"`

deployment/deployment.yaml

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ metadata:
77
labels:
88
app: kube-pool-manager
99
spec:
10-
replicas: 1
10+
replicas: 2
1111
selector:
1212
matchLabels:
1313
app: kube-pool-manager
@@ -28,6 +28,19 @@ spec:
2828
env:
2929
- name: CONFIG
3030
value: "/config/pools.yaml"
31+
# Instance
32+
- name: INSTANCE_NODENAME
33+
valueFrom:
34+
fieldRef:
35+
fieldPath: spec.nodeName
36+
- name: INSTANCE_POD
37+
valueFrom:
38+
fieldRef:
39+
fieldPath: metadata.name
40+
- name: INSTANCE_NAMESPACE
41+
valueFrom:
42+
fieldRef:
43+
fieldPath: metadata.namespace
3144
securityContext:
3245
readOnlyRootFilesystem: true
3346
runAsNonRoot: true

deployment/rbac.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ rules:
99
verbs: ["get", "list", "patch", "watch"]
1010
---
1111
apiVersion: rbac.authorization.k8s.io/v1
12+
kind: Role
13+
metadata:
14+
name: kube-pool-manager
15+
namespace: kube-system
16+
rules:
17+
- apiGroups: [""]
18+
resources: ["configmaps"]
19+
verbs: ["create"]
20+
- apiGroups: [""]
21+
resources: ["configmaps"]
22+
resourceNames: ["kube-pool-manager-leader"]
23+
verbs: ["get", "watch", "update", "patch"]
24+
- apiGroups: [""]
25+
resources: ["pods"]
26+
verbs: ["get"]
27+
---
28+
apiVersion: rbac.authorization.k8s.io/v1
1229
kind: ClusterRoleBinding
1330
metadata:
1431
name: kube-pool-manager

go.mod

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ go 1.15
44

55
require (
66
github.com/jessevdk/go-flags v1.4.0
7+
github.com/operator-framework/operator-sdk v0.8.2
78
github.com/prometheus/client_golang v1.7.1
89
github.com/sirupsen/logrus v1.6.0
9-
gopkg.in/yaml.v2 v2.2.8
10-
k8s.io/api v0.18.0
11-
k8s.io/apimachinery v0.18.0
12-
k8s.io/client-go v0.18.0
10+
gopkg.in/yaml.v2 v2.3.0
11+
k8s.io/api v0.18.6
12+
k8s.io/apimachinery v0.18.6
13+
k8s.io/client-go v0.18.6
14+
sigs.k8s.io/controller-runtime v0.6.2 // indirect
1315
)

0 commit comments

Comments
 (0)