Skip to content

Commit baf3e54

Browse files
committed
add k8sClient back
1 parent 5f9ae65 commit baf3e54

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ RUN conda install --name hsds --yes \
2323
imagecodecs \
2424
hdf5plugin \
2525
numcodecs \
26-
simplejson
26+
simplejson \
27+
&& \
28+
conda run -n hsds --no-capture-output pip install --no-cache-dir kubernetes
2729
RUN conda-pack -n hsds -o /tmp/hsds-env.tar \
2830
&& mkdir -p /opt/env/hsds \
2931
&& cd /opt/env/hsds \

hsds/basenode.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -633,20 +633,10 @@ def baseInit(node_type):
633633
# will set node_ip at registration time
634634
else:
635635
# check to see if we are running in a k8s cluster
636-
try:
637-
k8s_app_label = config.get("k8s_app_label")
638-
if "KUBERNETES_SERVICE_HOST" in os.environ:
639-
log.info("running in kubernetes")
640-
if k8s_app_label:
641-
log.info("setting is_k8s to True")
642-
app["is_k8s"] = True
643-
else:
644-
msg = "k8s_app_label not set, running in k8s single pod"
645-
log.info(msg)
646-
except KeyError:
647-
# guard against KeyError since k8s_app_label is a recent key
648-
log.warn("expected to find key k8s_app_label in config")
649-
if "is_k8s" not in app:
636+
if "KUBERNETES_SERVICE_HOST" in os.environ:
637+
log.info("running in kubernetes")
638+
app["is_k8s"] = True
639+
else:
650640
# check to see if we are running in a docker container
651641
proc_file = "/proc/self/cgroup"
652642
if os.path.isfile(proc_file):
@@ -657,6 +647,7 @@ def baseInit(node_type):
657647
if len(fields) >= 3:
658648
field = fields[2]
659649
if field.startswith("/docker/"):
650+
log.info("running in docker")
660651
app["is_docker"] = True
661652

662653
if "is_dcos" in app:

hsds/util/k8sClient.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)