Skip to main content

k8s-leader-election-py

Client-go has a very easy to use leader-election package for kubernetes controllers to utilize leader-election. The python kubernetes-client doesn't have such a method, so this will have to do for now.

Requirements

Your controller must have a pod and namespace environment variable defined:

env:
- name: POD
  valueFrom:
    fieldRef:
      fieldPath: metadata.name
- name: NAMESPACE
  valueFrom:
    fieldRef:
      fieldPath: metadata.namespace

Your controller must be able to list, get, update and create configmaps.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: sample-controller
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: sample-controller
rules:
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: sample-controller
roleRef:
  apiGroup: ""
  kind: Role
  name: sample-controller
subjects:
- kind: ServiceAccount
  name: sample-controller
  apiGroup: ""

Example

Sample code:

from threading import Thread
from leaderelection import Elect

# Init leader election class. Configmap is the name of the configmap to create to store leader election information
leaderelection = Elect(configmap='sample-controller-leader-election')
# Run leader election in new thread
th = Thread(target=leaderelection.run)
th.setDaemon(True)
th.start()
#start main controller loop
while True:
    # Check if pod is the leader. If so continue on with controller logic.
    leader = leaderelection.check_leader()
    if leader:
        logger.info("I am the leader!!")
    else:
        logger.info("I am NOT the leader")

Release files for leaderelection 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for leaderelection 0.0.3
File Size Uploaded
leaderelection-0.0.3.tar.gz 3.3 kB Details

Release files / leaderelection-0.0.3.tar.gz

Download URL leaderelection-0.0.3.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6781bbd1a03bcb06bb322d9a075ab3a690b758d71555c6680c31d75fe79e59b7
BLAKE2b-256 checksum
How to use checksums
2af858d4a3b37dfb3ca01446098400499f7bb52713026b9630b8c5e7ce764380
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page