Implement benign delete

This commit is contained in:
David Boreham 2023-11-21 15:28:01 -07:00
parent 99a21761bd
commit 93b32e6fd3
2 changed files with 46 additions and 21 deletions

View File

@ -30,7 +30,6 @@ class ClusterInfo:
parsed_pod_yaml_map: Any parsed_pod_yaml_map: Any
image_set: Set[str] = set() image_set: Set[str] = set()
app_name: str = "test-app" app_name: str = "test-app"
deployment_name: str = "test-deployment"
environment_variables: DeployEnvVars environment_variables: DeployEnvVars
spec: Spec spec: Spec
@ -74,7 +73,7 @@ class ClusterInfo:
backend=client.V1IngressBackend( backend=client.V1IngressBackend(
service=client.V1IngressServiceBackend( service=client.V1IngressServiceBackend(
# TODO: this looks wrong # TODO: this looks wrong
name=self.deployment_name, name=f"{self.app_name}-service",
# TODO: pull port number from the service # TODO: pull port number from the service
port=client.V1ServiceBackendPort(number=80) port=client.V1ServiceBackendPort(number=80)
) )
@ -195,7 +194,7 @@ class ClusterInfo:
deployment = client.V1Deployment( deployment = client.V1Deployment(
api_version="apps/v1", api_version="apps/v1",
kind="Deployment", kind="Deployment",
metadata=client.V1ObjectMeta(name=self.deployment_name), metadata=client.V1ObjectMeta(name=f"{self.app_name}-deployment"),
spec=spec, spec=spec,
) )
return deployment return deployment

View File

@ -23,6 +23,15 @@ from stack_orchestrator.deploy.k8s.helpers import pods_in_deployment, log_stream
from stack_orchestrator.deploy.k8s.cluster_info import ClusterInfo from stack_orchestrator.deploy.k8s.cluster_info import ClusterInfo
from stack_orchestrator.opts import opts from stack_orchestrator.opts import opts
from stack_orchestrator.deploy.deployment_context import DeploymentContext from stack_orchestrator.deploy.deployment_context import DeploymentContext
from stack_orchestrator.util import error_exit
def _check_delete_exception(e: client.exceptions.ApiException):
if e.status == 404:
if opts.o.debug:
print("Failed to delete object, continuing")
else:
error_exit(f"k8s api error: {e}")
class K8sDeployer(Deployer): class K8sDeployer(Deployer):
@ -133,42 +142,59 @@ class K8sDeployer(Deployer):
for pv in pvs: for pv in pvs:
if opts.o.debug: if opts.o.debug:
print(f"Deleting this pv: {pv}") print(f"Deleting this pv: {pv}")
pv_resp = self.core_api.delete_persistent_volume(name=pv.metadata.name) try:
if opts.o.debug: pv_resp = self.core_api.delete_persistent_volume(name=pv.metadata.name)
print("PV deleted:") if opts.o.debug:
print(f"{pv_resp}") print("PV deleted:")
print(f"{pv_resp}")
except client.exceptions.ApiException as e:
_check_delete_exception(e)
# Figure out the PVCs for this deployment # Figure out the PVCs for this deployment
pvcs = self.cluster_info.get_pvcs() pvcs = self.cluster_info.get_pvcs()
for pvc in pvcs: for pvc in pvcs:
if opts.o.debug: if opts.o.debug:
print(f"Deleting this pvc: {pvc}") print(f"Deleting this pvc: {pvc}")
pvc_resp = self.core_api.delete_namespaced_persistent_volume_claim(name=pvc.metadata.name, namespace=self.k8s_namespace) try:
if opts.o.debug: pvc_resp = self.core_api.delete_namespaced_persistent_volume_claim(
print("PVCs deleted:") name=pvc.metadata.name, namespace=self.k8s_namespace
print(f"{pvc_resp}") )
if opts.o.debug:
print("PVCs deleted:")
print(f"{pvc_resp}")
except client.exceptions.ApiException as e:
_check_delete_exception(e)
deployment = self.cluster_info.get_deployment() deployment = self.cluster_info.get_deployment()
if opts.o.debug: if opts.o.debug:
print(f"Deleting this deployment: {deployment}") print(f"Deleting this deployment: {deployment}")
self.apps_api.delete_namespaced_deployment( try:
name=deployment.metadata.name, namespace=self.k8s_namespace self.apps_api.delete_namespaced_deployment(
) name=deployment.metadata.name, namespace=self.k8s_namespace
)
except client.exceptions.ApiException as e:
_check_delete_exception(e)
service: client.V1Service = self.cluster_info.get_service() service: client.V1Service = self.cluster_info.get_service()
if opts.o.debug: if opts.o.debug:
print(f"Deleting service: {service}") print(f"Deleting service: {service}")
self.core_api.delete_namespaced_service( try:
namespace=self.k8s_namespace, self.core_api.delete_namespaced_service(
body=service namespace=self.k8s_namespace,
) name=service.metadata.name
)
except client.exceptions.ApiException as e:
_check_delete_exception(e)
# TODO: disable ingress for kind # TODO: disable ingress for kind
ingress: client.V1Ingress = self.cluster_info.get_ingress() ingress: client.V1Ingress = self.cluster_info.get_ingress()
if opts.o.debug: if opts.o.debug:
print(f"Deleting this ingress: {ingress}") print(f"Deleting this ingress: {ingress}")
self.networking_api.delete_namespaced_ingress( try:
name=ingress.metadata.name, namespace=self.k8s_namespace self.networking_api.delete_namespaced_ingress(
) name=ingress.metadata.name, namespace=self.k8s_namespace
)
except client.exceptions.ApiException as e:
_check_delete_exception(e)
if self.is_kind(): if self.is_kind():
# Destroy the kind cluster # Destroy the kind cluster