Skip to content

Commit

Permalink
Merge pull request #7 from citizensadvice/pykube-ng
Browse files Browse the repository at this point in the history
Replace pykube with pykube-ng
  • Loading branch information
michelesr authored May 17, 2022
2 parents 14d3d78 + cd68ed1 commit ff543bc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
FROM python:3.8-alpine
FROM python:3.10-alpine

RUN apk add gcc libc-dev

RUN pip install --no-cache-dir pykube croniter
COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt && rm /tmp/requirements.txt

RUN adduser -u 1000 -D app && \
mkdir /app && \
Expand Down
8 changes: 5 additions & 3 deletions deploy/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ rules:
verbs: ["get", "list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "patch"]
verbs: ["get", "list"]
- apiGroups: ["apps"]
resources: ["deployments/scale"]
verbs: ["patch"]
- apiGroups: ["autoscaling", "extensions"]
resources:
- horizontalpodautoscalers
resources: ["horizontalpodautoscalers"]
verbs: ["get", "list", "patch"]
---
apiVersion: apps/v1
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pykube-ng==22.1.0
croniter==1.3.5
32 changes: 20 additions & 12 deletions schedule_scaling/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import pykube
from croniter import croniter
from resources import Deployment


logging.basicConfig(
Expand All @@ -21,13 +20,7 @@

def get_kube_api():
""" Initiating the API from Service Account or when running locally from ~/.kube/config """
try:
config = pykube.KubeConfig.from_service_account()
except FileNotFoundError:
# local testing
config = pykube.KubeConfig.from_file(
os.path.expanduser("~/.kube/config"))
return pykube.HTTPClient(config)
return pykube.HTTPClient(pykube.KubeConfig.from_env())


api = get_kube_api()
Expand All @@ -39,7 +32,7 @@ def deployments_to_scale():
scaling_dict = {}
for namespace in list(pykube.Namespace.objects(api)):
namespace = str(namespace)
for deployment in Deployment.objects(api).filter(namespace=namespace):
for deployment in pykube.Deployment.objects(api).filter(namespace=namespace):
annotations = deployment.metadata.get("annotations", {})
f_deployment = str(namespace + "/" + str(deployment))

Expand Down Expand Up @@ -124,18 +117,33 @@ def process_deployment(deployment, schedules):
def scale_deployment(name, namespace, replicas):
""" Scale the deployment to the given number of replicas """
try:
deployment = Deployment.objects(api).filter(
deployment = pykube.Deployment.objects(api).filter(
namespace=namespace).get(name=name)
except pykube.exceptions.ObjectDoesNotExist:
logging.warning("Deployment %s/%s does not exist", namespace, name)
return

if replicas is None or replicas == deployment.replicas:
return
deployment.replicas = replicas

try:
deployment.update()
try:
try:
deployment.patch({"spec": {"replicas": replicas}}, subresource="scale")
finally:
deployment.reload() # reload to fetch whole updated Deployment
except pykube.exceptions.HTTPError as e:
# XXX: In previous version deployment.update() was always used. It was unnecessary, but after moving to a
# subresource patch we've lost backward compatibility, as previous RBAC "patch" on "deployments" doesn't
# work. So if we get an 403 pring a warning and try again, this time with full resource update.
if e.code == 403:
logging.warning(
"Failed to apply patch on a 'scale' subresource, failing back to a full update. "
"Consider upgrading RBAC deployment.")
deployment.replicas = replicas
deployment.update()
else:
raise
logging.info("Deployment %s/%s scaled to %s replicas", namespace, name, replicas)
except pykube.exceptions.HTTPError as err:
logging.error("Exception raised while updating deployment %s/%s", namespace, name)
Expand Down
6 changes: 0 additions & 6 deletions schedule_scaling/resources.py

This file was deleted.

0 comments on commit ff543bc

Please sign in to comment.