Mercurial > code > home > repos > victoriametrics
annotate k8s_ops.py @ 65:fada8d64c4d3
notes
author | drewp@bigasterisk.com |
---|---|
date | Thu, 02 May 2024 23:15:37 -0700 |
parents | 80e275ab2f88 |
children |
rev | line source |
---|---|
20 | 1 import json |
2 import time | |
3 | |
4 from kubernetes import client | |
5 | |
6 | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
7 def refreshPodCmaps(pod_name, namespace="default"): |
20 | 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 = {} | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
17 pod.metadata.annotations["force-configmap-update"] = str(time.time()) |
20 | 18 api_instance.replace_namespaced_pod(name=pod_name, namespace=namespace, body=pod) |
19 | |
20 | |
21 def firstPodName(selector): | |
22 api_instance = client.CoreV1Api() | |
35
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
23 pod_list = api_instance.list_namespaced_pod(namespace="default", label_selector=selector) |
20 | 24 return pod_list.items[0].metadata.name |
25 | |
26 | |
27 def hup(ctx, deployment, process_name): | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
28 ctx.run(f"kubectl exec {deployment} -- pkill -HUP {process_name}") |
20 | 29 |
30 | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
31 def replaceCmap(name, dataObj): |
20 | 32 api_instance = client.CoreV1Api() |
35
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
33 |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
34 data = dict((fn, json.dumps(obj)) for fn, obj in dataObj.items()) |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
35 |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
36 try: |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
37 |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
38 existing_config_map = api_instance.read_namespaced_config_map(name, 'default') |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
39 existing_config_map.data.update(data) |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
40 api_response = api_instance.replace_namespaced_config_map(name, "default", existing_config_map) |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
41 except client.rest.ApiException as e: |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
42 if e.status == 404: |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
43 config_map = client.V1ConfigMap() |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
44 config_map.metadata = client.V1ObjectMeta(name=name) |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
45 config_map.data = data |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
46 api_response = api_instance.create_namespaced_config_map('default', config_map) |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
47 else: |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
48 raise |
80e275ab2f88
rewrite replaceCmap, though idk if it was broken or not
drewp@bigasterisk.com
parents:
22
diff
changeset
|
49 |
20 | 50 print(f"{name} resource_version is now {api_response.metadata.resource_version}") |