Mercurial > code > home > repos > victoriametrics
comparison k8s_ops.py @ 67:adde35eb4773
collapse ./next to ./
author | drewp@bigasterisk.com |
---|---|
date | Fri, 03 May 2024 11:21:08 -0700 |
parents | next/k8s_ops.py@8134cd480817 |
children |
comparison
equal
deleted
inserted
replaced
66:429bfd62e6ba | 67:adde35eb4773 |
---|---|
1 import json | |
2 import time | |
3 | |
4 from kubernetes import client | |
5 | |
6 | |
7 def refreshPodCmaps(pod_name, namespace="default"): | |
8 """ | |
9 Per https://ahmet.im/blog/kubernetes-secret-volumes-delay/ there could be a while | |
10 until k8s updates the CM volume that a pod sees. Workaround is to edit the pod annotations. | |
11 """ | |
12 api_instance = client.CoreV1Api() | |
13 | |
14 pod = api_instance.read_namespaced_pod(name=pod_name, namespace=namespace) | |
15 if pod.metadata.annotations is None: | |
16 pod.metadata.annotations = {} | |
17 pod.metadata.annotations["force-configmap-update"] = str(time.time()) | |
18 api_instance.replace_namespaced_pod(name=pod_name, namespace=namespace, body=pod) | |
19 | |
20 | |
21 def firstPodName(selector): | |
22 api_instance = client.CoreV1Api() | |
23 pod_list = api_instance.list_namespaced_pod(namespace="default", label_selector=selector) | |
24 return pod_list.items[0].metadata.name | |
25 | |
26 | |
27 def hup(ctx, deployment, process_name): | |
28 ctx.run(f"kubectl exec {deployment} -- pkill -HUP {process_name}") | |
29 | |
30 | |
31 def replaceCmap(name, dataObj): | |
32 api_instance = client.CoreV1Api() | |
33 | |
34 data = dict((fn, json.dumps(obj)) for fn, obj in dataObj.items()) | |
35 | |
36 try: | |
37 | |
38 existing_config_map = api_instance.read_namespaced_config_map(name, 'default') | |
39 existing_config_map.data.update(data) | |
40 api_response = api_instance.replace_namespaced_config_map(name, "default", existing_config_map) | |
41 except client.rest.ApiException as e: | |
42 if e.status == 404: | |
43 config_map = client.V1ConfigMap() | |
44 config_map.metadata = client.V1ObjectMeta(name=name) | |
45 config_map.data = data | |
46 api_response = api_instance.create_namespaced_config_map('default', config_map) | |
47 else: | |
48 raise | |
49 | |
50 print(f"{name} resource_version is now {api_response.metadata.resource_version}") |