comparison k8s_ops.py @ 20:f5777b65f035

fast update of scrape_main.yaml
author drewp@bigasterisk.com
date Sat, 24 Jun 2023 01:44:57 -0700
parents
children cd115f1ca2a8
comparison
equal deleted inserted replaced
19:10017def57ce 20:f5777b65f035
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, configObj):
32 api_instance = client.CoreV1Api()
33 api_response = api_instance.replace_namespaced_config_map( #
34 name=name,
35 namespace='default',
36 body={
37 'apiVersion': 'v1',
38 'kind': 'ConfigMap',
39 'metadata': {
40 'name': name
41 },
42 'data': {
43 'scrape_main': json.dumps(configObj)
44 },
45 })
46 print(f"{name} resource_version is now {api_response.metadata.resource_version}")