Skip to content

Commit

Permalink
[CE-631] Support node delete operation for k8s agent
Browse files Browse the repository at this point in the history
For kubernetes agent support delete node operation.

Change-Id: I08c3a4952de5e24087df9a697a5b175d4e27e564
Signed-off-by: Haitao Yue <[email protected]>
  • Loading branch information
hightall committed Jul 9, 2019
1 parent a68a6f3 commit 27b96d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/agent/kubernetes-agent/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
9 changes: 8 additions & 1 deletion src/api-engine/api/routes/node/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
21 changes: 18 additions & 3 deletions src/api-engine/api/tasks/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 27b96d4

Please sign in to comment.