-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm_client.py
92 lines (79 loc) · 2.89 KB
/
helm_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
from pathlib import Path
from pyhelm3 import Client
from kubernetes import client, config
import asyncio
import logging
K8S_CONFIG_PATH = "k8sconfigs"
base_dir = os.getcwd()
kubeConfigPath = os.path.abspath(os.path.join(base_dir, K8S_CONFIG_PATH))
if not os.path.exists(kubeConfigPath):
logging.error("K8s config dir does not exist")
# TODO: Create a factory for the Helm client
class HelmClient:
# Specify the kubeconfig file to use
# client = Client(executable = "/path/to/helm")
def __init__(self, hostIp):
self.hostIp = hostIp
kubeconfig = kubeConfigPath + "/" + hostIp
if not os.path.exists(kubeconfig):
logging.error("K8s config for this MEC host does not exist")
self.client = Client(kubeconfig=os.path.abspath(kubeconfig))
# List the deployed releases
async def list_releases(self):
releases = await self.client.list_releases(all=True, all_namespaces=True)
for release in releases:
revision = await release.current_revision()
logging.debug(
"[%s] [%s] [%s] [%s]",
release.name,
release.namespace,
revision.revision,
str(revision.status),
)
# Install or upgrade a release
async def install_chart(self, releasename, chart_ref):
chart = await self.client.get_chart(chart_ref=chart_ref)
revision = await self.client.install_or_upgrade_release(
release_name=releasename,
chart=chart,
atomic=False,
cleanup_on_fail=True,
wait=False,
)
logging.debug(
"[%s] [%s] [%s] [%s]",
revision.release.name,
revision.release.namespace,
revision.revision,
str(revision.status),
)
# Uninstall a release
# Via the revision
async def uninstall_chart(self, releasename, namespace):
revision = await self.client.get_current_revision(
releasename, namespace=namespace
)
await revision.release.uninstall()
async def deploy_chart(self, appPkgRecord, appInsId):
pass
async def unDeploy_chart(self):
pass
# Or directly by name
# await client.uninstall_release("cert-manager", namespace = "cert-manager", wait = True)
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s.%(msecs)03d - %(levelname)s:\t%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG,
)
hostIp = "172.25.131.129"
# kubeconfig = kubeConfigPath + "/" + hostIp + ".yaml"
helm = HelmClient(hostIp)
# asyncio.run(
# helm.install_chart(
# "my-release", "nginx", "https://charts.bitnami.com/bitnami", "13.2.23"
# )
# )
asyncio.run(helm.list_releases())
# asyncio.run(helm.uninstall_chart(releasename="my-release", namespace="default"))