|
| 1 | +############################################################################## |
| 2 | +# Copyright by The HDF Group. # |
| 3 | +# All rights reserved. # |
| 4 | +# # |
| 5 | +# This file is part of HSDS (HDF5 Scalable Data Service), Libraries and # |
| 6 | +# Utilities. The full HSDS copyright notice, including # |
| 7 | +# terms governing use, modification, and redistribution, is contained in # |
| 8 | +# the file COPYING, which can be found at the root of the source code # |
| 9 | +# distribution tree. If you do not have access to this file, you may # |
| 10 | +# request a copy from help@hdfgroup.org. # |
| 11 | +############################################################################## |
| 12 | +# |
| 13 | +# Kubernetes utility functions |
| 14 | +# |
| 15 | + |
| 16 | +from kubernetes import client as k8s_client |
| 17 | +from kubernetes import config as k8s_config |
| 18 | +import urllib3 |
| 19 | +from .. import hsds_logger as log |
| 20 | + |
| 21 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 22 | + |
| 23 | + |
| 24 | +def getPodIps(k8s_app_label, k8s_namespace=None): |
| 25 | + """ Return list of IPs of all pods in the cluster with given app label |
| 26 | + (and namespace if set) |
| 27 | + """ |
| 28 | + |
| 29 | + # get the config from within the cluster and set it as the default config |
| 30 | + # for all new clients |
| 31 | + k8s_config.load_incluster_config() |
| 32 | + c = k8s_client.Configuration() # go and get a copy of the default config |
| 33 | + c.verify_ssl = False # set verify_ssl to false in that config |
| 34 | + # make that config the default for all new clients |
| 35 | + k8s_client.Configuration.set_default(c) |
| 36 | + v1 = k8s_client.CoreV1Api() |
| 37 | + if k8s_namespace: |
| 38 | + # get pods for given namespace |
| 39 | + log.debug(f"getting pods for namespace: {k8s_namespace}") |
| 40 | + ret = v1.list_namespaced_pod(namespace=k8s_namespace) |
| 41 | + else: |
| 42 | + log.info("getting pods for all namespaces") |
| 43 | + ret = v1.list_pod_for_all_namespaces(watch=False) |
| 44 | + pod_ips = [] |
| 45 | + for i in ret.items: |
| 46 | + pod_ip = i.status.pod_ip |
| 47 | + if not pod_ip: |
| 48 | + continue |
| 49 | + labels = i.metadata.labels |
| 50 | + if labels and "app" in labels and labels["app"] == k8s_app_label: |
| 51 | + msg = f"found hsds pod with app label: {k8s_app_label} " |
| 52 | + msg += f"- ip: {pod_ip}" |
| 53 | + log.debug(msg) |
| 54 | + pod_ips.append(pod_ip) |
| 55 | + |
| 56 | + pod_ips.sort() # for assigning node numbers |
| 57 | + return pod_ips |
0 commit comments