comparison k8s_ops.py @ 22:cd115f1ca2a8

use configmaps and a special pod refresh trick
author drewp@bigasterisk.com
date Sat, 24 Jun 2023 23:02:04 -0700
parents f5777b65f035
children 80e275ab2f88
comparison
equal deleted inserted replaced
21:10127391f6f3 22:cd115f1ca2a8
2 import time 2 import time
3 3
4 from kubernetes import client 4 from kubernetes import client
5 5
6 6
7 def refreshPodCmaps(pod_name, namespace='default'): 7 def refreshPodCmaps(pod_name, namespace="default"):
8 """ 8 """
9 Per https://ahmet.im/blog/kubernetes-secret-volumes-delay/ there could be a while 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. 10 until k8s updates the CM volume that a pod sees. Workaround is to edit the pod annotations.
11 """ 11 """
12 api_instance = client.CoreV1Api() 12 api_instance = client.CoreV1Api()
13 13
14 pod = api_instance.read_namespaced_pod(name=pod_name, namespace=namespace) 14 pod = api_instance.read_namespaced_pod(name=pod_name, namespace=namespace)
15 if pod.metadata.annotations is None: 15 if pod.metadata.annotations is None:
16 pod.metadata.annotations = {} 16 pod.metadata.annotations = {}
17 pod.metadata.annotations['force-configmap-update'] = str(time.time()) 17 pod.metadata.annotations["force-configmap-update"] = str(time.time())
18 api_instance.replace_namespaced_pod(name=pod_name, namespace=namespace, body=pod) 18 api_instance.replace_namespaced_pod(name=pod_name, namespace=namespace, body=pod)
19 19
20 20
21 def firstPodName(selector): 21 def firstPodName(selector):
22 api_instance = client.CoreV1Api() 22 api_instance = client.CoreV1Api()
23 pod_list = api_instance.list_namespaced_pod(namespace='default', label_selector=selector) 23 pod_list = api_instance.list_namespaced_pod(
24 namespace="default", label_selector=selector
25 )
24 return pod_list.items[0].metadata.name 26 return pod_list.items[0].metadata.name
25 27
26 28
27 def hup(ctx, deployment, process_name): 29 def hup(ctx, deployment, process_name):
28 ctx.run(f'kubectl exec {deployment} -- pkill -HUP {process_name}') 30 ctx.run(f"kubectl exec {deployment} -- pkill -HUP {process_name}")
29 31
30 32
31 def replaceCmap(name, configObj): 33 def replaceCmap(name, dataObj):
32 api_instance = client.CoreV1Api() 34 api_instance = client.CoreV1Api()
33 api_response = api_instance.replace_namespaced_config_map( # 35 api_response = api_instance.replace_namespaced_config_map( #
34 name=name, 36 name=name,
35 namespace='default', 37 namespace="default",
36 body={ 38 body={
37 'apiVersion': 'v1', 39 "apiVersion": "v1",
38 'kind': 'ConfigMap', 40 "kind": "ConfigMap",
39 'metadata': { 41 "metadata": {"name": name},
40 'name': name 42 "data": dict((fn, json.dumps(obj)) for fn, obj in dataObj.items()),
41 }, 43 },
42 'data': { 44 )
43 'scrape_main': json.dumps(configObj)
44 },
45 })
46 print(f"{name} resource_version is now {api_response.metadata.resource_version}") 45 print(f"{name} resource_version is now {api_response.metadata.resource_version}")