Kubernetes + Python = ❤
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scale-down.py 813B

1234567891011121314151617181920212223
  1. """
  2. Simplified example script to set replicas to zero, no error handling!
  3. See https://github.com/hjacobs/kube-downscaler for a production-ready controller to downscale
  4. """
  5. import datetime, os, pykube, time
  6. while True:
  7. # loads in-cluster auth or local ~/.kube/config for testing
  8. config = pykube.KubeConfig.from_env()
  9. api = pykube.HTTPClient(config)
  10. # no timezone handling!
  11. weekday = datetime.datetime.today().isoweekday()
  12. is_weekend = weekday in (6, 7) or os.getenv("ALWAYS_WEEKEND")
  13. for deploy in pykube.Deployment.objects(api, namespace=pykube.all):
  14. if "scale-down-on-weekend" in deploy.annotations and is_weekend:
  15. print(f"Updating deployment {deploy.namespace}/{deploy.name}..")
  16. deploy.replicas = 0
  17. deploy.update()
  18. time.sleep(15)