@@ -272,40 +272,51 @@ func (r *AtlasDeploymentReconciler) handleDeletion(
272272 return true , workflow .Terminate (workflow .Internal , err .Error ())
273273 }
274274 }
275+
276+ return false , workflow .OK ()
275277 }
276278
277- if ! deployment .GetDeletionTimestamp ().IsZero () {
278- if customresource .HaveFinalizer (deployment , customresource .FinalizerLabel ) {
279- if err := r .cleanupBindings (workflowCtx .Context , deployment ); err != nil {
280- result := workflow .Terminate (workflow .Internal , err .Error ())
281- log .Errorw ("failed to cleanup deployment bindings (backups)" , "error" , err )
282- return true , result
283- }
284- isProtected := customresource .IsResourceProtected (deployment , r .ObjectDeletionProtection )
285- if isProtected {
286- log .Info ("Not removing Atlas deployment from Atlas as per configuration" )
287- } else {
288- if customresource .ResourceShouldBeLeftInAtlas (deployment ) {
289- log .Infof ("Not removing Atlas Deployment from Atlas as the '%s' annotation is set" , customresource .ResourcePolicyAnnotation )
290- } else {
291- if err := r .deleteDeploymentFromAtlas (workflowCtx , log , project , deployment ); err != nil {
292- log .Errorf ("failed to remove deployment from Atlas: %s" , err )
293- result := workflow .Terminate (workflow .Internal , err .Error ())
294- workflowCtx .SetConditionFromResult (status .DeploymentReadyType , result )
295- return true , result
296- }
297- }
298- }
299- err := customresource .ManageFinalizer (workflowCtx .Context , r .Client , deployment , customresource .UnsetFinalizer )
300- if err != nil {
301- result := workflow .Terminate (workflow .Internal , err .Error ())
302- log .Errorw ("failed to remove finalizer" , "error" , err )
303- return true , result
304- }
305- }
279+ if ! customresource .HaveFinalizer (deployment , customresource .FinalizerLabel ) {
306280 return true , prevResult
307281 }
308- return false , workflow .OK ()
282+
283+ if err := r .cleanupBindings (workflowCtx .Context , deployment ); err != nil {
284+ result := workflow .Terminate (workflow .Internal , err .Error ())
285+ log .Errorw ("failed to cleanup deployment bindings (backups)" , "error" , err )
286+ return true , result
287+ }
288+
289+ switch {
290+ case customresource .IsResourcePolicyKeepOrDefault (deployment , r .ObjectDeletionProtection ):
291+ log .Info ("Not removing Atlas deployment from Atlas as per configuration" )
292+ case customresource .IsResourcePolicyKeep (deployment ):
293+ log .Infof ("Not removing Atlas deployment from Atlas as the '%s' annotation is set" , customresource .ResourcePolicyAnnotation )
294+ case isTerminationProtectionEnabled (deployment ):
295+ msg := fmt .Sprintf ("Termination protection for %s deployment enabled. Deployment in Atlas won't be removed" , deployment .GetName ())
296+ log .Info (msg )
297+ r .EventRecorder .Event (deployment , "Warning" , "AtlasDeploymentTermination" , msg )
298+ default :
299+ if err := r .deleteDeploymentFromAtlas (workflowCtx , log , project , deployment ); err != nil {
300+ log .Errorf ("failed to remove deployment from Atlas: %s" , err )
301+ result := workflow .Terminate (workflow .Internal , err .Error ())
302+ workflowCtx .SetConditionFromResult (status .DeploymentReadyType , result )
303+ return true , result
304+ }
305+ }
306+
307+ if err := customresource .ManageFinalizer (workflowCtx .Context , r .Client , deployment , customresource .UnsetFinalizer ); err != nil {
308+ result := workflow .Terminate (workflow .Internal , err .Error ())
309+ log .Errorw ("failed to remove finalizer" , "error" , err )
310+ return true , result
311+ }
312+
313+ return true , prevResult
314+ }
315+
316+ func isTerminationProtectionEnabled (deployment * mdbv1.AtlasDeployment ) bool {
317+ return (deployment .Spec .DeploymentSpec != nil &&
318+ deployment .Spec .DeploymentSpec .TerminationProtectionEnabled ) || (deployment .Spec .ServerlessSpec != nil &&
319+ deployment .Spec .ServerlessSpec .TerminationProtectionEnabled )
309320}
310321
311322func (r * AtlasDeploymentReconciler ) cleanupBindings (context context.Context , deployment * mdbv1.AtlasDeployment ) error {
0 commit comments