From 27b96d4b8edf41f1d6ccc82b21dc59caaf241c31 Mon Sep 17 00:00:00 2001 From: Haitao Yue Date: Tue, 9 Jul 2019 23:54:11 +0800 Subject: [PATCH] [CE-631] Support node delete operation for k8s agent For kubernetes agent support delete node operation. Change-Id: I08c3a4952de5e24087df9a697a5b175d4e27e564 Signed-off-by: Haitao Yue --- src/agent/kubernetes-agent/src/main.py | 13 +++++++++++++ src/api-engine/api/routes/node/views.py | 9 ++++++++- src/api-engine/api/tasks/agent.py | 21 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/agent/kubernetes-agent/src/main.py b/src/agent/kubernetes-agent/src/main.py index d0382b07a..406d6823f 100644 --- a/src/agent/kubernetes-agent/src/main.py +++ b/src/agent/kubernetes-agent/src/main.py @@ -93,3 +93,16 @@ class NodeStatus(Enum): {"status": NodeStatus.Running.value, "ports": ports} ), ) + elif OPERATION == AgentOperation.Delete.value: + if ingress: + k8s_client.delete_ingress( + namespace=AGENT_ID, name=ingress.get("name") + ) + if service: + k8s_client.delete_service( + namespace=AGENT_ID, name=service.get("name") + ) + if deployment: + k8s_client.delete_deployment( + namespace=AGENT_ID, name=deployment.get("name") + ) diff --git a/src/api-engine/api/routes/node/views.py b/src/api-engine/api/routes/node/views.py index 5e07ca8fa..521eba081 100644 --- a/src/api-engine/api/routes/node/views.py +++ b/src/api-engine/api/routes/node/views.py @@ -218,7 +218,14 @@ def destroy(self, request, pk=None): node.status = NodeStatus.Deleting.name.lower() node.save() - delete_node.delay(str(node.id)) + agent_config_file = ( + request.build_absolute_uri(node.agent.config_file.url), + ) + if isinstance(agent_config_file, tuple): + agent_config_file = list(agent_config_file)[0] + delete_node.delay( + str(node.id), agent_config_file=agent_config_file + ) else: node.delete() diff --git a/src/api-engine/api/tasks/agent.py b/src/api-engine/api/tasks/agent.py index 3a1a1db94..f4c6eb544 100644 --- a/src/api-engine/api/tasks/agent.py +++ b/src/api-engine/api/tasks/agent.py @@ -57,14 +57,29 @@ def create_node(self, node_id=None, agent_image=None, **kwargs): @app.task(bind=True, default_retry_delay=5, max_retires=3, time_limit=360) -def delete_node(self, node_id=None): +def delete_node(self, node_id=None, **kwargs): if node_id is None: return False + agent_config_file = kwargs.get("agent_config_file") try: node = Node.objects.get(id=node_id) - # agent_handler = AgentHandler(node) - # agent_handler.delete_node() + environment = { + "AGENT_ID": str(node.agent.id), + "AGENT_CONFIG_FILE": agent_config_file, + "NETWORK_TYPE": node.network_type, + "NETWORK_VERSION": node.network_version, + "NODE_TYPE": node.type, + "NODE_ID": str(node.id), + "OPERATION": AgentOperation.Delete.value, + } + client = docker.from_env() + client.containers.run( + node.agent.image, + auto_remove=True, + environment=environment, + detach=True, + ) except ObjectDoesNotExist: return False except Exception as e: