Skip to content

Commit 1b108c6

Browse files
authored
Add create, exists and delete methods for Kubernetes entities (#58)
* Check if kubernetes entities already exist. * Add create and delete for kubernetes entities.
1 parent 44caea9 commit 1b108c6

File tree

5 files changed

+400
-164
lines changed

5 files changed

+400
-164
lines changed

rayvens/cli/delete.py

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -77,77 +77,40 @@ def delete_deployment(args, with_job_launcher_privileges=True):
7777
# Delete kubernetes deployment for integration.
7878
from kubernetes import client, config
7979
config.load_kube_config()
80-
k8s_client = client.AppsV1Api(client.ApiClient())
80+
k8s_client = client.ApiClient()
8181

8282
# Delete the integration deployment.
8383
deployment_name = utils.get_kubernetes_integration_name(args.name)
84-
85-
exception_occured = False
86-
try:
87-
k8s_client.delete_namespaced_deployment(deployment_name, namespace)
88-
except client.exceptions.ApiException:
89-
exception_occured = True
84+
deployment = kube.Deployment(deployment_name, namespace=namespace)
85+
deployment.delete(k8s_client, starting_with=deployment_name)
9086

9187
# Delete the entrypoint service.
92-
k8s_client = client.CoreV1Api(client.ApiClient())
93-
entrypoint_service = utils.get_kubernetes_entrypoint_name(args.name)
94-
95-
try:
96-
k8s_client.delete_namespaced_service(entrypoint_service, namespace)
97-
except client.exceptions.ApiException:
98-
exception_occured = True
88+
entrypoint_service_name = utils.get_kubernetes_entrypoint_name(args.name)
89+
service = kube.Service(entrypoint_service_name, namespace=namespace)
90+
service.delete(k8s_client, starting_with=entrypoint_service_name)
9991

10092
# Delete configMap for updating the integration file.
101-
count = 0
102-
while True:
103-
config_map_name = kube.volume_base_name + "-" + str(count)
104-
try:
105-
k8s_client.delete_namespaced_config_map(config_map_name, namespace)
106-
except client.exceptions.ApiException:
107-
break
108-
else:
109-
count += 1
93+
config_map = kube.ConfigMap(namespace=namespace)
94+
config_map.delete(k8s_client, kube.volume_base_name)
11095

11196
if with_job_launcher_privileges:
11297
# Delete service account:
11398
# job-launcher-service-account
11499
name = utils.job_launcher_service_account
115-
try:
116-
k8s_client.delete_namespaced_service_account(name, namespace)
117-
except client.exceptions.ApiException:
118-
exception_occured = True
119-
120-
api_instance = client.RbacAuthorizationV1Api(client.ApiClient())
100+
service_account = kube.ServiceAccount(name, namespace=namespace)
101+
service_account.delete(k8s_client)
121102

122103
# Delete cluster role binding:
123104
# job-launcher-service-account
124105
name = utils.job_launcher_cluster_role_binding
125-
try:
126-
api_instance.delete_cluster_role_binding(
127-
name, body=client.V1DeleteOptions())
128-
except client.exceptions.ApiException:
129-
exception_occured = True
106+
cluster_role_binding = kube.ClusterRoleBinding(name, [], None)
107+
cluster_role_binding.delete(k8s_client)
130108

131109
# Delete cluster role:
132110
# job-manager-role
133111
name = utils.job_manager_role
134-
try:
135-
api_instance.delete_cluster_role(name,
136-
body=client.V1DeleteOptions())
137-
except client.exceptions.ApiException:
138-
exception_occured = True
139-
140-
try:
141-
k8s_client.delete_namespaced_service(entrypoint_service, namespace)
142-
except client.exceptions.ApiException:
143-
exception_occured = True
144-
145-
if not exception_occured:
146-
print(f"Successfully deleted {deployment_name} from {namespace} "
147-
"namespace")
148-
else:
149-
print("Successfully deleted the remaining components of "
150-
f"{deployment_name} from {namespace} namespace")
112+
cluster_role_binding = kube.ClusterRole(name, namespace=namespace)
113+
cluster_role_binding.delete(k8s_client)
151114

152115

153116
def delete_local_deployment(args):

0 commit comments

Comments
 (0)