Skip to content

Commit 960fcc4

Browse files
authored
Merge pull request #73 from converged-computing/allow-custom-kubeconfig
fix: allow metrics operator python sdk to take custom kubeconfig
2 parents 5fa5b07 + 3c0ce13 commit 960fcc4

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

sdk/python/v1alpha2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/converged-computing/metrics-operator/tree/main) (0.0.x)
17+
- Support to provide custom kubeconfig (0.1.11)
1718
- LAMMPS parsing should include row names for component names (0.1.1)
1819
- More specific parsing / control for OSU benchmarks (0.0.21)
1920
- Support for OSU benchmark parsing with timed wrappers (0.0.2)

sdk/python/v1alpha2/metricsoperator/client.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@
1212

1313

1414
class MetricsOperator:
15-
def __init__(self, yaml_file):
15+
def __init__(self, yaml_file, kubeconfig=None):
1616
"""
1717
Given a YAML file with one or more metrics, apply
1818
to create it and stream logs for each metric of interest.
1919
"""
2020
self._core_v1 = None
2121
self.yaml_file = os.path.abspath(yaml_file)
2222
self.spec = utils.read_yaml(self.yaml_file)
23-
config.load_kube_config()
23+
self.kubeconfig = kubeconfig
24+
self.load_kube_config()
25+
26+
def load_kube_config(self, kubeconfig=None):
27+
"""
28+
Allow providing a custom kubeconfig to control a cluster and metric
29+
"""
30+
kubeconfig = kubeconfig or self.kubeconfig
31+
if not kubeconfig:
32+
config.load_kube_config()
33+
return
34+
35+
self._core_v1 = utils.make_k8s_client(kubeconfig)
36+
config.load_kube_config(config_file=kubeconfig)
2437

2538
def watch(self, raw_logs=False, pod_prefix=None, container_name=None):
2639
"""
@@ -31,10 +44,12 @@ def watch(self, raw_logs=False, pod_prefix=None, container_name=None):
3144

3245
for metric in self.spec["spec"]["metrics"]:
3346
if raw_logs:
34-
parser = mutils.get_metric()(self.spec, container_name=container_name)
47+
parser = mutils.get_metric()(
48+
self.spec, container_name=container_name, kubeconfig=self.kubeconfig
49+
)
3550
else:
3651
parser = mutils.get_metric(metric["name"])(
37-
self.spec, container_name=container_name
52+
self.spec, container_name=container_name, kubeconfig=self.kubeconfig
3853
)
3954
print("Watching %s" % metric["name"])
4055
for pod, container in parser.logging_containers(pod_prefix=pod_prefix):

sdk/python/v1alpha2/metricsoperator/metrics/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from kubernetes.client.exceptions import ApiException
77
from kubernetes.client.models.v1_pod_list import V1PodList
88

9+
import metricsoperator.utils as utils
10+
911

1012
class MetricBase:
1113
separator = "METRICS OPERATOR TIMEPOINT"
@@ -23,14 +25,27 @@ def __init__(self, spec=None, **kwargs):
2325
"""
2426
self.spec = spec
2527
self._core_v1 = kwargs.get("core_v1_api")
28+
self.kubeconfig = kwargs.get("kubeconfig")
2629

2730
# If we don't have a default container name...
2831
if not self.container_name:
2932
self.container_name = kwargs.get("container_name") or "launcher"
3033

3134
# Load kubeconfig on Metricbase init only
3235
if self.spec is not None:
36+
self.load_kube_config()
37+
38+
def load_kube_config(self):
39+
"""
40+
Allow providing a custom kubeconfig to control a cluster and metric
41+
"""
42+
if not self.kubeconfig:
3343
config.load_kube_config()
44+
return
45+
46+
# Assume the core v1 handler is replaced with this kubeconfig
47+
self._core_v1 = utils.make_k8s_client(self.kubeconfig)
48+
config.load_kube_config(config_file=self.kubeconfig)
3449

3550
@property
3651
def namespace(self):

sdk/python/v1alpha2/metricsoperator/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
import re
77

88
import yaml
9+
from kubernetes import client, config
10+
11+
12+
def make_k8s_client(kubeconfig_yaml):
13+
"""
14+
Load the yaml config for use in Python
15+
"""
16+
with open(kubeconfig_yaml) as f:
17+
kubeconfig = yaml.safe_load(f)
18+
api_client = config.new_client_from_config_dict(kubeconfig)
19+
return client.CoreV1Api(api_client)
920

1021

1122
def read_file(filename):

sdk/python/v1alpha2/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
if __name__ == "__main__":
3131
setup(
3232
name="metricsoperator",
33-
version="0.1.1",
33+
version="0.1.11",
3434
author="Vanessasaurus",
3535
author_email="vsoch@users.noreply.github.com",
3636
maintainer="Vanessasaurus",

0 commit comments

Comments
 (0)