From 87090288b5ff67d56555a8eb35f94dbd63c3fc80 Mon Sep 17 00:00:00 2001 From: Luis Tomas Bolivar Date: Fri, 25 Sep 2020 17:15:42 +0200 Subject: [PATCH] Avoid race when pod is deleted before finalizer is added It may be possible that add_finalizer function first fails due to a Conflict updating the object as it got updated (in this case triggered the deletion) from the Kubernetes side, and then when trying to get the new object this is actually gone as the deletion was completed in between both actions. If that is the case we just need to return false as there is no need to add a finalizer for it Change-Id: I118a7c01d98722af30435f4d091820c81e4e95e4 --- kuryr_kubernetes/k8s_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kuryr_kubernetes/k8s_client.py b/kuryr_kubernetes/k8s_client.py index 5fe5ea65d..38354c54f 100644 --- a/kuryr_kubernetes/k8s_client.py +++ b/kuryr_kubernetes/k8s_client.py @@ -271,7 +271,11 @@ class K8sClient(object): # Object is being deleting or gone. Return. return False except exc.K8sConflict: - obj = self.get(path) + try: + obj = self.get(path) + except exc.K8sResourceNotFound: + # Object got removed before finalizer was set + return False if finalizer in obj['metadata'].get('finalizers', []): # Finalizer is there, return. return True