Skip to content

Commit dcb2b6f

Browse files
oyvindiodacofr
authored andcommitted
Skip updates on Application with deletionTimestamp in the past
metadata.deletionTimestamp is set when a resource is deleted with propagationPolicy: orphan or propagationPolicy: foreground. The resource may be updated after this, for example finalizers may be set or removed. If the application resource is in the process of being deleted (i.e. has deletionTimestamp set and in the past), skip deploy deploy to avoid interfering with the deletion process done by the garbage collector.
1 parent 85b5494 commit dcb2b6f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

fiaas_deploy_daemon/crd/watcher.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
17+
import datetime
1818
import logging
1919
from queue import Queue
2020
from typing import Union
@@ -90,6 +90,22 @@ def _skip_status_event(self, application: FiaasApplication):
9090
return True
9191
return False
9292

93+
def _skip_update_of_deleted_application(self, application: FiaasApplication):
94+
# metadata.deletionTimestamp is set when a resource is deleted with propagationPolicy: orphan or
95+
# propagationPolicy: foreground. The resource may be updated after this, for example finalizers may be set or
96+
# removed. If the application resource is in the process of being deleted, deploy should be skipped otherwise
97+
# it may interfere with the deletion process done by the garbage collector.
98+
deletion_timestamp = application.metadata.deletionTimestamp
99+
if isinstance(deletion_timestamp, datetime.datetime) and deletion_timestamp <= datetime.datetime.now(
100+
tz=datetime.timezone.utc
101+
):
102+
LOG.warning(
103+
"Skipping update watch event for app %s; it was marked for deletion at %s",
104+
application.spec.application,
105+
deletion_timestamp,
106+
)
107+
return True
108+
93109
def _deploy(self, application: FiaasApplication):
94110
app_name = application.spec.application
95111
LOG.debug("Deploying %s", app_name)
@@ -100,6 +116,8 @@ def _deploy(self, application: FiaasApplication):
100116
raise ValueError("The Application {} is missing the 'fiaas/deployment_id' label".format(app_name))
101117
if self._skip_status_event(application):
102118
return
119+
if self._skip_update_of_deleted_application(application):
120+
return
103121
if self._already_deployed(app_name, application.metadata.namespace, deployment_id):
104122
LOG.debug("Have already deployed %s for app %s", deployment_id, app_name)
105123
return

tests/fiaas_deploy_daemon/crd/test_crd_watcher.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# limitations under the License.
1717

1818

19+
import copy
1920
from queue import Queue
2021

2122
from unittest import mock
@@ -311,3 +312,12 @@ def test_deploy_save_status(self, crd_watcher, deploy_queue, watcher, status_get
311312
assert deploy_queue.qsize() == 0
312313
crd_watcher._watch(None)
313314
assert deploy_queue.qsize() == 0
315+
316+
def test_deploy_skip_deleted_app(self, crd_watcher, deploy_queue, watcher, status_get):
317+
event = copy.deepcopy(MODIFIED_EVENT)
318+
event['object']['metadata']['deletionTimestamp'] = '2000-01-01T00:00:00Z'
319+
watcher.watch.return_value = [WatchEvent(event, FiaasApplication)]
320+
321+
assert deploy_queue.qsize() == 0
322+
crd_watcher._watch(None)
323+
assert deploy_queue.qsize() == 0

0 commit comments

Comments
 (0)