@@ -6,8 +6,10 @@ import (
66	. "github.com/onsi/gomega/gstruct" 
77
88	apierrors "k8s.io/apimachinery/pkg/api/errors" 
9+ 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 
910	"k8s.io/apimachinery/pkg/runtime" 
1011	"sigs.k8s.io/controller-runtime/pkg/client" 
12+ 	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" 
1113	"sigs.k8s.io/controller-runtime/pkg/reconcile" 
1214
1315	clustersv1alpha1 "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1" 
@@ -240,6 +242,55 @@ var _ = Describe("Cluster Controller", func() {
240242		Expect (env .Client (platformCluster ).Get (env .Ctx , client .ObjectKeyFromObject (c ), c )).To (MatchError (apierrors .IsNotFound , "cluster should be deleted" ))
241243	})
242244
245+ 	It ("should not start with shoot deletion if there are foreign finalizers on the Cluster" , func () {
246+ 		env  :=  defaultTestSetup (".." , "cluster" , "testdata" , "test-05" )
247+ 
248+ 		c  :=  & clustersv1alpha1.Cluster {}
249+ 		c .SetName ("advanced" )
250+ 		c .SetNamespace ("clusters" )
251+ 		env .ShouldReconcile (cRec , testutils .RequestFromObject (c ))
252+ 		Expect (env .Client (platformCluster ).Get (env .Ctx , client .ObjectKeyFromObject (c ), c )).To (Succeed ())
253+ 		c .Finalizers  =  append (c .Finalizers , "example.com/finalizer" )
254+ 		Expect (env .Client (platformCluster ).Update (env .Ctx , c )).To (Succeed ())
255+ 
256+ 		// verify shoot existence 
257+ 		Expect (c .Status .ProviderStatus ).ToNot (BeNil ())
258+ 		cs  :=  & providerv1alpha1.ClusterStatus {}
259+ 		Expect (c .Status .GetProviderStatus (cs )).To (Succeed ())
260+ 		Expect (cs .Shoot ).ToNot (BeNil ())
261+ 		shoot  :=  & gardenv1beta1.Shoot {}
262+ 		shoot .SetName (cs .Shoot .Name )
263+ 		shoot .SetNamespace (cs .Shoot .Namespace )
264+ 		Expect (env .Client (gardenCluster ).Get (env .Ctx , client .ObjectKeyFromObject (shoot ), shoot )).To (Succeed ())
265+ 
266+ 		// delete the cluster 
267+ 		Expect (env .Client (platformCluster ).Delete (env .Ctx , c )).To (Succeed ())
268+ 		env .ShouldReconcile (cRec , testutils .RequestFromObject (c ))
269+ 		Expect (env .Client (platformCluster ).Get (env .Ctx , client .ObjectKeyFromObject (c ), c )).To (Succeed ())
270+ 
271+ 		// shoot should not be deleted because of the unknown finalizer 
272+ 		Expect (env .Client (gardenCluster ).Get (env .Ctx , client .ObjectKeyFromObject (shoot ), shoot )).To (Succeed ())
273+ 		Expect (shoot .DeletionTimestamp .IsZero ()).To (BeTrue ())
274+ 
275+ 		// there should be a condition indicating that there are foreign finalizers 
276+ 		Expect (c .Status .Conditions ).To (ContainElement (MatchFields (IgnoreExtras , Fields {
277+ 			"Type" :    Equal (providerv1alpha1 .ClusterConditionForeignFinalizers ),
278+ 			"Status" :  Equal (metav1 .ConditionFalse ),
279+ 			"Message" : ContainSubstring ("example.com/finalizer" ),
280+ 		})))
281+ 
282+ 		// remove the foreign finalizer 
283+ 		Expect (controllerutil .RemoveFinalizer (c , "example.com/finalizer" )).To (BeTrue ())
284+ 		Expect (env .Client (platformCluster ).Update (env .Ctx , c )).To (Succeed ())
285+ 		env .ShouldReconcile (cRec , testutils .RequestFromObject (c ))
286+ 
287+ 		// verify shoot deletion 
288+ 		Expect (env .Client (gardenCluster ).Get (env .Ctx , client .ObjectKeyFromObject (shoot ), shoot )).To (MatchError (apierrors .IsNotFound , "shoot should be deleted" ))
289+ 		// shoot is gone, reconcile again to remove cluster finalizer 
290+ 		env .ShouldReconcile (cRec , testutils .RequestFromObject (c ))
291+ 		Expect (env .Client (platformCluster ).Get (env .Ctx , client .ObjectKeyFromObject (c ), c )).To (MatchError (apierrors .IsNotFound , "cluster should be deleted" ))
292+ 	})
293+ 
243294	It ("should handle cluster configs correctly" , func () {
244295		env  :=  defaultTestSetup (".." , "cluster" , "testdata" , "test-04" )
245296
0 commit comments