Mercurial > code > home > repos > victoriametrics
annotate k8s_ops.py @ 32:eb1de82c93aa
refactor the merging of all the groups
author | drewp@bigasterisk.com |
---|---|
date | Wed, 19 Jul 2023 21:28:10 -0700 |
parents | cd115f1ca2a8 |
children | 80e275ab2f88 |
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() | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
23 pod_list = api_instance.list_namespaced_pod( |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
24 namespace="default", label_selector=selector |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
25 ) |
20 | 26 return pod_list.items[0].metadata.name |
27 | |
28 | |
29 def hup(ctx, deployment, process_name): | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
30 ctx.run(f"kubectl exec {deployment} -- pkill -HUP {process_name}") |
20 | 31 |
32 | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
33 def replaceCmap(name, dataObj): |
20 | 34 api_instance = client.CoreV1Api() |
35 api_response = api_instance.replace_namespaced_config_map( # | |
36 name=name, | |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
37 namespace="default", |
20 | 38 body={ |
22
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
39 "apiVersion": "v1", |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
40 "kind": "ConfigMap", |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
41 "metadata": {"name": name}, |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
42 "data": dict((fn, json.dumps(obj)) for fn, obj in dataObj.items()), |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
43 }, |
cd115f1ca2a8
use configmaps and a special pod refresh trick
drewp@bigasterisk.com
parents:
20
diff
changeset
|
44 ) |
20 | 45 print(f"{name} resource_version is now {api_response.metadata.resource_version}") |