apiVersion: v1 kind: ConfigMap metadata: name: mariadb-replicas data: replicas.py: | #!/usr/bin/env python import json import os import ssl import sys import urllib2 URL = ('https://kubernetes.default.svc.{{ .Values.network.dns.kubernetes_domain }}/apis/extensions/v1beta1/deployments') TOKEN_FILE = '/var/run/secrets/kubernetes.io/serviceaccount/token' def create_ctx(): ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE return ctx def get_deployments(): url = URL.format() try: token = file(TOKEN_FILE, 'r').read() except KeyError: exit("Unable to open a file with token.") header = {'Authorization': " Bearer {}".format(token)} req = urllib2.Request(url=url, headers=header) ctx = create_ctx() response = urllib2.urlopen(req, context=ctx) output = json.load(response) return output def main(): reply = get_deployments() name = "mariadb" namespace = "default" if not os.environ["NAMESPACE"] else os.environ["NAMESPACE"] mariadb = filter(lambda d: d["metadata"]["namespace"] == namespace and d["metadata"]["name"].startswith(name), reply["items"]) print len(mariadb) if __name__ == "__main__": main()