@@ -20,6 +20,7 @@ import (
2020 "github.com/rancher/wrangler/v3/pkg/genericcondition"
2121 batchv1 "k8s.io/api/batch/v1"
2222 v1 "k8s.io/api/core/v1"
23+ apierrors "k8s.io/apimachinery/pkg/api/errors"
2324 "k8s.io/apimachinery/pkg/api/meta"
2425 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526 "k8s.io/apimachinery/pkg/types"
@@ -1375,4 +1376,156 @@ var _ = Describe("Deployment controller", func() {
13751376 })
13761377 })
13771378 })
1379+
1380+ When ("cleaning up removed target clusters" , func () {
1381+ var (
1382+ ctx context.Context
1383+ r * Reconciler
1384+ c client.WithWatch
1385+ d v1beta1.Deployment
1386+ dc1 v1beta1.DeploymentCluster
1387+ dc2 v1beta1.DeploymentCluster
1388+ dc3 v1beta1.DeploymentCluster
1389+ )
1390+
1391+ BeforeEach (func () {
1392+ ctx = context .Background ()
1393+
1394+ d = v1beta1.Deployment {
1395+ ObjectMeta : metav1.ObjectMeta {
1396+ Name : "test-deployment" ,
1397+ Namespace : "default" ,
1398+ UID : "test-uid-12345" ,
1399+ },
1400+ Spec : v1beta1.DeploymentSpec {
1401+ DeploymentType : v1beta1 .Targeted ,
1402+ Applications : []v1beta1.Application {
1403+ {
1404+ Name : "app1" ,
1405+ Version : "1.0.0" ,
1406+ Namespace : "default" ,
1407+ Targets : []map [string ]string {
1408+ {string (v1beta1 .ClusterName ): "cluster-1" },
1409+ {string (v1beta1 .ClusterName ): "cluster-2" },
1410+ },
1411+ },
1412+ },
1413+ },
1414+ }
1415+
1416+ dc1 = v1beta1.DeploymentCluster {
1417+ ObjectMeta : metav1.ObjectMeta {
1418+ Name : "test-deployment-cluster-1" ,
1419+ Namespace : "default" ,
1420+ Labels : map [string ]string {
1421+ string (v1beta1 .BundleName ): "test-deployment" ,
1422+ string (v1beta1 .DeploymentID ): "test-uid-12345" ,
1423+ },
1424+ OwnerReferences : []metav1.OwnerReference {
1425+ {APIVersion : "app.edge-orchestrator.intel.com/v1beta1" , Kind : "Deployment" , Name : "test-deployment" , UID : "test-uid-12345" },
1426+ },
1427+ },
1428+ Spec : v1beta1.DeploymentClusterSpec {ClusterID : "cluster-1" },
1429+ }
1430+
1431+ dc2 = v1beta1.DeploymentCluster {
1432+ ObjectMeta : metav1.ObjectMeta {
1433+ Name : "test-deployment-cluster-2" ,
1434+ Namespace : "default" ,
1435+ Labels : map [string ]string {
1436+ string (v1beta1 .BundleName ): "test-deployment" ,
1437+ string (v1beta1 .DeploymentID ): "test-uid-12345" ,
1438+ },
1439+ OwnerReferences : []metav1.OwnerReference {
1440+ {APIVersion : "app.edge-orchestrator.intel.com/v1beta1" , Kind : "Deployment" , Name : "test-deployment" , UID : "test-uid-12345" },
1441+ },
1442+ },
1443+ Spec : v1beta1.DeploymentClusterSpec {ClusterID : "cluster-2" },
1444+ }
1445+
1446+ dc3 = v1beta1.DeploymentCluster {
1447+ ObjectMeta : metav1.ObjectMeta {
1448+ Name : "test-deployment-cluster-3" ,
1449+ Namespace : "default" ,
1450+ Labels : map [string ]string {
1451+ string (v1beta1 .BundleName ): "test-deployment" ,
1452+ string (v1beta1 .DeploymentID ): "test-uid-12345" ,
1453+ },
1454+ OwnerReferences : []metav1.OwnerReference {
1455+ {APIVersion : "app.edge-orchestrator.intel.com/v1beta1" , Kind : "Deployment" , Name : "test-deployment" , UID : "test-uid-12345" },
1456+ },
1457+ },
1458+ Spec : v1beta1.DeploymentClusterSpec {ClusterID : "cluster-3" },
1459+ }
1460+
1461+ c = fake .NewClientBuilder ().
1462+ WithScheme (scheme .Scheme ).
1463+ WithObjects (& d , & dc1 , & dc2 , & dc3 ).
1464+ WithStatusSubresource (& v1beta1.Deployment {}).
1465+ Build ()
1466+
1467+ r = & Reconciler {
1468+ Client : c ,
1469+ Scheme : scheme .Scheme ,
1470+ recorder : record .NewFakeRecorder (1024 ),
1471+ }
1472+ })
1473+
1474+ Context ("when cluster is removed from targeted deployment" , func () {
1475+ It ("should delete the DeploymentCluster for removed cluster" , func () {
1476+ err := r .cleanupRemovedTargetClusters (ctx , & d )
1477+ Expect (err ).NotTo (HaveOccurred ())
1478+
1479+ err = r .Get (ctx , types.NamespacedName {Name : dc1 .Name , Namespace : dc1 .Namespace }, & dc1 )
1480+ Expect (err ).NotTo (HaveOccurred ())
1481+
1482+ err = r .Get (ctx , types.NamespacedName {Name : dc2 .Name , Namespace : dc2 .Namespace }, & dc2 )
1483+ Expect (err ).NotTo (HaveOccurred ())
1484+
1485+ err = r .Get (ctx , types.NamespacedName {Name : dc3 .Name , Namespace : dc3 .Namespace }, & dc3 )
1486+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
1487+ })
1488+ })
1489+
1490+ Context ("when deployment is auto-scaling type" , func () {
1491+ It ("should not delete any DeploymentClusters" , func () {
1492+ d .Spec .DeploymentType = v1beta1 .AutoScaling
1493+ err := c .Update (ctx , & d )
1494+ Expect (err ).NotTo (HaveOccurred ())
1495+
1496+ err = r .cleanupRemovedTargetClusters (ctx , & d )
1497+ Expect (err ).NotTo (HaveOccurred ())
1498+
1499+ err = r .Get (ctx , types.NamespacedName {Name : dc1 .Name , Namespace : dc1 .Namespace }, & dc1 )
1500+ Expect (err ).NotTo (HaveOccurred ())
1501+
1502+ err = r .Get (ctx , types.NamespacedName {Name : dc2 .Name , Namespace : dc2 .Namespace }, & dc2 )
1503+ Expect (err ).NotTo (HaveOccurred ())
1504+
1505+ err = r .Get (ctx , types.NamespacedName {Name : dc3 .Name , Namespace : dc3 .Namespace }, & dc3 )
1506+ Expect (err ).NotTo (HaveOccurred ())
1507+ })
1508+ })
1509+
1510+ Context ("when all clusters are removed from deployment" , func () {
1511+ It ("should delete all DeploymentClusters" , func () {
1512+ d .Spec .Applications [0 ].Targets = []map [string ]string {}
1513+ err := c .Update (ctx , & d )
1514+ Expect (err ).NotTo (HaveOccurred ())
1515+
1516+ err = r .cleanupRemovedTargetClusters (ctx , & d )
1517+ Expect (err ).NotTo (HaveOccurred ())
1518+
1519+ err = r .Get (ctx , types.NamespacedName {Name : dc1 .Name , Namespace : dc1 .Namespace }, & dc1 )
1520+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
1521+
1522+ err = r .Get (ctx , types.NamespacedName {Name : dc2 .Name , Namespace : dc2 .Namespace }, & dc2 )
1523+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
1524+
1525+ err = r .Get (ctx , types.NamespacedName {Name : dc3 .Name , Namespace : dc3 .Namespace }, & dc3 )
1526+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
1527+ })
1528+ })
1529+ })
1530+
13781531})
0 commit comments