Skip to content

Commit

Permalink
fix: allow metrics operator python sdk to take custom kubeconfig
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <[email protected]>
  • Loading branch information
vsoch committed Oct 18, 2023
1 parent 5fa5b07 commit 3c0ce13
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions sdk/python/v1alpha2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
The versions coincide with releases on pip. Only major versions will be released as tags on Github.

## [0.0.x](https://github.com/converged-computing/metrics-operator/tree/main) (0.0.x)
- Support to provide custom kubeconfig (0.1.11)
- LAMMPS parsing should include row names for component names (0.1.1)
- More specific parsing / control for OSU benchmarks (0.0.21)
- Support for OSU benchmark parsing with timed wrappers (0.0.2)
Expand Down
23 changes: 19 additions & 4 deletions sdk/python/v1alpha2/metricsoperator/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@


class MetricsOperator:
def __init__(self, yaml_file):
def __init__(self, yaml_file, kubeconfig=None):
"""
Given a YAML file with one or more metrics, apply
to create it and stream logs for each metric of interest.
"""
self._core_v1 = None
self.yaml_file = os.path.abspath(yaml_file)
self.spec = utils.read_yaml(self.yaml_file)
config.load_kube_config()
self.kubeconfig = kubeconfig
self.load_kube_config()

def load_kube_config(self, kubeconfig=None):
"""
Allow providing a custom kubeconfig to control a cluster and metric
"""
kubeconfig = kubeconfig or self.kubeconfig
if not kubeconfig:
config.load_kube_config()
return

self._core_v1 = utils.make_k8s_client(kubeconfig)
config.load_kube_config(config_file=kubeconfig)

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

for metric in self.spec["spec"]["metrics"]:
if raw_logs:
parser = mutils.get_metric()(self.spec, container_name=container_name)
parser = mutils.get_metric()(
self.spec, container_name=container_name, kubeconfig=self.kubeconfig
)
else:
parser = mutils.get_metric(metric["name"])(
self.spec, container_name=container_name
self.spec, container_name=container_name, kubeconfig=self.kubeconfig
)
print("Watching %s" % metric["name"])
for pod, container in parser.logging_containers(pod_prefix=pod_prefix):
Expand Down
15 changes: 15 additions & 0 deletions sdk/python/v1alpha2/metricsoperator/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from kubernetes.client.exceptions import ApiException
from kubernetes.client.models.v1_pod_list import V1PodList

import metricsoperator.utils as utils


class MetricBase:
separator = "METRICS OPERATOR TIMEPOINT"
Expand All @@ -23,14 +25,27 @@ def __init__(self, spec=None, **kwargs):
"""
self.spec = spec
self._core_v1 = kwargs.get("core_v1_api")
self.kubeconfig = kwargs.get("kubeconfig")

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

# Load kubeconfig on Metricbase init only
if self.spec is not None:
self.load_kube_config()

def load_kube_config(self):
"""
Allow providing a custom kubeconfig to control a cluster and metric
"""
if not self.kubeconfig:
config.load_kube_config()
return

# Assume the core v1 handler is replaced with this kubeconfig
self._core_v1 = utils.make_k8s_client(self.kubeconfig)
config.load_kube_config(config_file=self.kubeconfig)

@property
def namespace(self):
Expand Down
11 changes: 11 additions & 0 deletions sdk/python/v1alpha2/metricsoperator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
import re

import yaml
from kubernetes import client, config


def make_k8s_client(kubeconfig_yaml):
"""
Load the yaml config for use in Python
"""
with open(kubeconfig_yaml) as f:
kubeconfig = yaml.safe_load(f)
api_client = config.new_client_from_config_dict(kubeconfig)
return client.CoreV1Api(api_client)


def read_file(filename):
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/v1alpha2/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if __name__ == "__main__":
setup(
name="metricsoperator",
version="0.1.1",
version="0.1.11",
author="Vanessasaurus",
author_email="[email protected]",
maintainer="Vanessasaurus",
Expand Down

0 comments on commit 3c0ce13

Please sign in to comment.