annotate next/k8s_ops.py @ 62:8134cd480817

make next/ a complete standalone setup dir- no deps on ./
author drewp@bigasterisk.com
date Thu, 02 May 2024 20:33:29 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
1 import json
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
2 import time
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
3
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
4 from kubernetes import client
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
5
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
6
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
7 def refreshPodCmaps(pod_name, namespace="default"):
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
8 """
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
9 Per https://ahmet.im/blog/kubernetes-secret-volumes-delay/ there could be a while
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
10 until k8s updates the CM volume that a pod sees. Workaround is to edit the pod annotations.
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
11 """
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
12 api_instance = client.CoreV1Api()
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
13
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
14 pod = api_instance.read_namespaced_pod(name=pod_name, namespace=namespace)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
15 if pod.metadata.annotations is None:
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
16 pod.metadata.annotations = {}
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
17 pod.metadata.annotations["force-configmap-update"] = str(time.time())
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
18 api_instance.replace_namespaced_pod(name=pod_name, namespace=namespace, body=pod)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
19
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
20
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
21 def firstPodName(selector):
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
22 api_instance = client.CoreV1Api()
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
23 pod_list = api_instance.list_namespaced_pod(namespace="default", label_selector=selector)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
24 return pod_list.items[0].metadata.name
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
25
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
26
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
27 def hup(ctx, deployment, process_name):
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
28 ctx.run(f"kubectl exec {deployment} -- pkill -HUP {process_name}")
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
29
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
30
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
31 def replaceCmap(name, dataObj):
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
32 api_instance = client.CoreV1Api()
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
33
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
34 data = dict((fn, json.dumps(obj)) for fn, obj in dataObj.items())
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
35
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
36 try:
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
37
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
38 existing_config_map = api_instance.read_namespaced_config_map(name, 'default')
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
39 existing_config_map.data.update(data)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
40 api_response = api_instance.replace_namespaced_config_map(name, "default", existing_config_map)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
41 except client.rest.ApiException as e:
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
42 if e.status == 404:
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
43 config_map = client.V1ConfigMap()
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
44 config_map.metadata = client.V1ObjectMeta(name=name)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
45 config_map.data = data
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
46 api_response = api_instance.create_namespaced_config_map('default', config_map)
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
47 else:
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
48 raise
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
49
8134cd480817 make next/ a complete standalone setup dir- no deps on ./
drewp@bigasterisk.com
parents:
diff changeset
50 print(f"{name} resource_version is now {api_response.metadata.resource_version}")