Skip to content

Commit

Permalink
Remove code-path for responding to Delete events
Browse files Browse the repository at this point in the history
Now that we are setting ownerReferences on the created objects
kubernetes will take care of deleting them for us when the application
is deleted, so we can remove all the code related to that.

The delete methods for service, ingress and autoscaler are still needed
for the case where the config changes such that they are no longer
required, but now they can be private/internal methods.
  • Loading branch information
gregjones committed May 28, 2020
1 parent 7e89a35 commit f4cd341
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 63 deletions.
21 changes: 1 addition & 20 deletions fiaas_deploy_daemon/crd/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ def _create(kind, plural, short_names, group):
def _handle_watch_event(self, event):
if event.type in (WatchEvent.ADDED, WatchEvent.MODIFIED):
self._deploy(event.object)
elif event.type == WatchEvent.DELETED:
self._delete(event.object)
else:
elif event.type != WatchEvent.DELETED:
raise ValueError("Unknown WatchEvent type {}".format(event.type))

def _deploy(self, application):
Expand Down Expand Up @@ -127,23 +125,6 @@ def _deploy(self, application):
LOG.exception("Failed to create app spec from fiaas config file")
self._lifecycle.failed(lifecycle_subject)

def _delete(self, application):
app_spec = self._spec_factory(
uid=application.metadata.uid,
name=application.spec.application,
image=application.spec.image,
app_config=application.spec.config,
teams=[],
tags=[],
deployment_id="deletion",
namespace=application.metadata.namespace,
additional_labels=application.spec.additional_labels,
additional_annotations=application.spec.additional_annotations,
)
set_extras(app_spec)
self._deploy_queue.put(DeployerEvent("DELETE", app_spec, lifecycle_subject=None))
LOG.debug("Queued delete for %s", application.spec.application)

def _already_deployed(self, app_name, namespace, deployment_id):
try:
name = create_name(app_name, deployment_id)
Expand Down
6 changes: 0 additions & 6 deletions fiaas_deploy_daemon/deployer/kubernetes/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ def deploy(self, app_spec):
self._deployment_deployer.deploy(app_spec, selector, labels, _besteffort_qos_is_required(app_spec))
self._autoscaler_deployer.deploy(app_spec, labels)

def delete(self, app_spec):
self._ingress_deployer.delete(app_spec)
self._autoscaler_deployer.delete(app_spec)
self._service_deployer.delete(app_spec)
self._deployment_deployer.delete(app_spec)

def _make_labels(self, app_spec):
labels = {
"app": app_spec.name,
Expand Down
7 changes: 0 additions & 7 deletions fiaas_deploy_daemon/deployer/kubernetes/autoscaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ def deploy(self, app_spec, labels):
except NotFound:
pass

def delete(self, app_spec):
LOG.info("Deleting autoscaler for %s", app_spec.name)
try:
HorizontalPodAutoscaler.delete(app_spec.name, app_spec.namespace)
except NotFound:
pass


def should_have_autoscaler(app_spec):
if not _autoscaler_enabled(app_spec.autoscaler):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ def deploy(self, app_spec, selector, labels, besteffort_qos_is_required):
self._owner_references.apply(deployment, app_spec)
deployment.save()

def delete(self, app_spec):
LOG.info("Deleting deployment for %s", app_spec.name)
try:
body = {"kind": "DeleteOptions", "apiVersion": "v1", "propagationPolicy": "Foreground"}
Deployment.delete(app_spec.name, app_spec.namespace, body=body)
except NotFound:
pass

def _make_volumes(self, app_spec):
volumes = []
volumes.append(Volume(name="{}-config".format(app_spec.name),
Expand Down
4 changes: 2 additions & 2 deletions fiaas_deploy_daemon/deployer/kubernetes/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def deploy(self, app_spec, labels):
if self._should_have_ingress(app_spec):
self._create(app_spec, labels)
else:
self.delete(app_spec)
self._delete(app_spec)

def delete(self, app_spec):
def _delete(self, app_spec):
LOG.info("Deleting ingress for %s", app_spec.name)
try:
Ingress.delete(app_spec.name, app_spec.namespace)
Expand Down
4 changes: 2 additions & 2 deletions fiaas_deploy_daemon/deployer/kubernetes/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def deploy(self, app_spec, selector, labels):
if self._should_have_service(app_spec):
self._create(app_spec, selector, labels)
else:
self.delete(app_spec)
self._delete(app_spec)

def delete(self, app_spec):
def _delete(self, app_spec):
LOG.info("Deleting service for %s", app_spec.name)
try:
Service.delete(app_spec.name, app_spec.namespace)
Expand Down
26 changes: 8 additions & 18 deletions tests/fiaas_deploy_daemon/crd/test_crd_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
"type": WatchEvent.MODIFIED,
}

DELETED_EVENT = {
"object": ADD_EVENT["object"],
"type": WatchEvent.DELETED,
}


class TestWatcher(object):

Expand Down Expand Up @@ -166,7 +161,6 @@ def test_is_able_to_watch_custom_resource_definition(self, crd_watcher, deploy_q
(ADD_EVENT, "UPDATE", {"deployment": {"fiaas/source-repository": "xyz"}}, "xyz"),
(MODIFIED_EVENT, "UPDATE", None, None),
(MODIFIED_EVENT, "UPDATE", {"deployment": {"fiaas/source-repository": "xyz"}}, "xyz"),
(DELETED_EVENT, "DELETE", None, None),
])
def test_deploy(self, crd_watcher, deploy_queue, spec_factory, watcher, app_spec, event, deployer_event_type,
lifecycle, annotations, repository):
Expand All @@ -188,14 +182,13 @@ def test_deploy(self, crd_watcher, deploy_queue, spec_factory, watcher, app_spec

crd_watcher._watch(None)

if event in [ADD_EVENT, MODIFIED_EVENT]:
lifecycle.initiate.assert_called_once_with(uid=event["object"]["metadata"]["uid"],
app_name=event["object"]["spec"]["application"],
namespace=event["object"]["metadata"]["namespace"],
deployment_id='deployment_id',
repository=repository,
labels=None,
annotations=None)
lifecycle.initiate.assert_called_once_with(uid=event["object"]["metadata"]["uid"],
app_name=event["object"]["spec"]["application"],
namespace=event["object"]["metadata"]["namespace"],
deployment_id='deployment_id',
repository=repository,
labels=None,
annotations=None)

app_config = spec["config"]
additional_labels = AdditionalLabelsOrAnnotations()
Expand All @@ -210,10 +203,7 @@ def test_deploy(self, crd_watcher, deploy_queue, spec_factory, watcher, app_spec

assert deploy_queue.qsize() == 1
deployer_event = deploy_queue.get_nowait()
if event in [ADD_EVENT, MODIFIED_EVENT]:
assert deployer_event == DeployerEvent(deployer_event_type, app_spec, lifecycle_subject)
else:
assert deployer_event == DeployerEvent(deployer_event_type, app_spec, None)
assert deployer_event == DeployerEvent(deployer_event_type, app_spec, lifecycle_subject)
assert deploy_queue.empty()

@pytest.mark.parametrize("namespace", [None, "default"])
Expand Down

0 comments on commit f4cd341

Please sign in to comment.