-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathtidb_cluster_operator.py
98 lines (78 loc) · 3.27 KB
/
tidb_cluster_operator.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
93
94
95
96
97
98
import time
import json
from aiopslab.service.helm import Helm
from aiopslab.service.kubectl import KubeCtl
from aiopslab.service.apps.base import Application
from aiopslab.paths import TIDB_METADATA
class TiDBCluster(Application):
def __init__(self):
super().__init__(TIDB_METADATA)
self.load_app_json()
self.kubectl = KubeCtl()
self.helm_operator_config = self.metadata.get("Helm Operator Config", {})
self.k8s_config = self.metadata.get("K8S Config", {})
# self.create_namespace()
# self.deploy_tidb_operator()
def load_app_json(self):
with open(self.config_file, "r") as file:
self.metadata = json.load(file)
self.name = self.metadata["Name"]
self.namespace = self.metadata["Namespace"] # tidb cluster namespace
def deploy(self):
"""Deploy the TiDB operator and cluster."""
# self.install_crd()
# self.install_tidb_operator()
# self.deploy_tidb_cluster()
# NOTE: use Ansible to deploy
pass
def install_crd(self):
"""Install the Custom Resource Definitions (CRDs) for TiDB Operator."""
crd_url = self.helm_operator_config.get("CRD")
if not crd_url:
raise ValueError("CRD URL is not specified in the configuration.")
print("Installing CRDs...")
command = f"kubectl create -f {crd_url}"
self.kubectl.exec_command(command)
def install_tidb_operator(self):
"""Install TiDB Operator using Helm."""
repo_name = "pingcap"
repo_url = "https://charts.pingcap.org/"
Helm.add_repo(repo_name, repo_url)
operator_namespace = self.helm_operator_config.get("namespace", "tidb-admin")
self.kubectl.create_namespace_if_not_exist(operator_namespace)
print("Installing TiDB Operator...")
Helm.install(**self.helm_operator_config)
Helm.assert_if_deployed(operator_namespace)
time.sleep(5)
def deploy_tidb_cluster(self):
"""Deploy the TiDB Cluster using Helm."""
# cluster_namespace = self.k8s_config.get("namespace", "tidb-cluster")
# self.kubectl.create_namespace_if_not_exist(cluster_namespace)
# cluster_config_url = self.k8s_config.get("config_url")
# print("Deploying TiDB cluster...")
# command = f"kubectl -n {cluster_namespace} apply -f {cluster_config_url}"
# self.kubectl.exec_command(command)
# NOTE: use Ansible to deploy
pass
def delete_tidb_cluster(self):
"""Delete the TiDB Cluster."""
self.kubectl.exec_command(f"kubectl delete tc basic -n {self.namespace}")
def delete_tidb_operator(self):
"""Delete the TiDB Operator."""
Helm.uninstall(**self.helm_operator_config)
def delete(self):
"""Delete the TiDB Operator and Cluster."""
# self.kubectl.delete_namespace(self.namespace)
# self.delete_tidb_operator()
# time.sleep(5)
pass
def cleanup(self):
"""Clean up the namespace and all resources."""
# self.delete_tidb_cluster()
# self.delete_tidb_operator()
# self.kubectl.delete_namespace(self.namespace)
# time.sleep(15)
pass
if __name__ == "__main__":
tidb_app = TiDBCluster()
tidb_app.deploy_tidb_cluster()