From 539a2cbb770504510277a21578ec9cb1090c983c Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Thu, 9 Apr 2020 12:05:12 -0400 Subject: [PATCH] Add an owner reference to the orchestrator pod for all created objects This will allow anything we create to be automatically removed when the orchestrator pod is removed for any reason. If the pod is cleanly scaled down we will continue to tell the workers to exit cleanly, but if the orchestrator is abruptly removed this will ensure everything is cleaned up properly. Fixes https://github.com/ManageIQ/manageiq-pods/issues/428 --- .../object_definition.rb | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/container_orchestrator/object_definition.rb b/lib/container_orchestrator/object_definition.rb index fdc04d1580e..5dec1b19774 100644 --- a/lib/container_orchestrator/object_definition.rb +++ b/lib/container_orchestrator/object_definition.rb @@ -5,9 +5,10 @@ module ObjectDefinition def deployment_definition(name) { :metadata => { - :name => name, - :labels => {:app => app_name}, - :namespace => my_namespace, + :name => name, + :labels => {:app => app_name}, + :namespace => my_namespace, + :ownerReferences => owner_references }, :spec => { :selector => {:matchLabels => {:name => name}}, @@ -30,9 +31,10 @@ def deployment_definition(name) def service_definition(name, selector, port) { :metadata => { - :name => name, - :labels => {:app => app_name}, - :namespace => my_namespace + :name => name, + :labels => {:app => app_name}, + :namespace => my_namespace, + :ownerReferences => owner_references }, :spec => { :selector => selector, @@ -48,9 +50,10 @@ def service_definition(name, selector, port) def secret_definition(name, string_data) { :metadata => { - :name => name, - :labels => {:app => app_name}, - :namespace => my_namespace + :name => name, + :labels => {:app => app_name}, + :namespace => my_namespace, + :ownerReferences => owner_references }, :stringData => string_data } @@ -101,5 +104,16 @@ def my_namespace def app_name ENV["APP_NAME"] end + + def owner_references + [{ + :apiVersion => "v1", + :blockOwnerDeletion => true, + :controller => true, + :kind => "Pod", + :name => ENV["POD_NAME"], + :uid => ENV["POD_UID"] + }] + end end end