cmd/k8s-operator: use c/r leader election

Signed-off-by: Irbe Krumina <irbe@tailscale.com>
This commit is contained in:
Irbe Krumina 2023-11-14 07:23:50 +01:00
parent 86c8ab7502
commit b015021747
2 changed files with 39 additions and 1 deletions

View File

@ -58,3 +58,31 @@ roleRef:
kind: Role
name: operator
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: tailscale-operator-leaderelection
namespace: kube-system
rules:
- apiGroups: ["coordination.k8s.io"]
resources: ["leases"]
resourceNames: ["tailscale-operator"]
verbs: ["get", "update", "patch"]
- apiGroups: ["coordination.k8s.io"]
resources: ["leases"]
verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: tailscale-operator-leaderelection
namespace: kube-system
subjects:
- kind: ServiceAccount
name: operator
namespace: {{ .Release.Namespace }}
roleRef:
kind: Role
name: tailscale-operator-leaderelection
apiGroup: rbac.authorization.k8s.io

View File

@ -41,6 +41,12 @@ import (
"tailscale.com/version"
)
const (
leaderElectionLock = "tailscale-operator"
)
type tsSetupFunc func() tsSetup
func main() {
// Required to use our client API. We're fine with the instability since the
// client lives in the same repo as this code.
@ -203,7 +209,11 @@ func runReconcilers(zlog *zap.SugaredLogger, s *tsnet.Server, tsNamespace string
nsFilter := cache.ByObject{
Field: client.InNamespace(tsNamespace).AsSelector(),
}
mgr, err := manager.New(restConfig, manager.Options{
mgr, err := manager.New(c.restConfig, manager.Options{
LeaderElectionNamespace: "kube-system",
LeaderElection: true,
LeaderElectionReleaseOnCancel: true,
LeaderElectionID: leaderElectionLock,
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Secret{}: nsFilter,