From c08dce3506634939f779de29e268785834d2d383 Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Tue, 30 May 2023 10:14:31 -0500 Subject: [PATCH 33/34] Move objects with force move label and no cluster tenants Signed-off-by: Vignesh Goutham Ganesh --- cmd/clusterctl/client/cluster/objectgraph.go | 8 ++++ .../client/cluster/objectgraph_test.go | 43 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/cmd/clusterctl/client/cluster/objectgraph.go b/cmd/clusterctl/client/cluster/objectgraph.go index a14de3ac6..d1b884d3a 100644 --- a/cmd/clusterctl/client/cluster/objectgraph.go +++ b/cmd/clusterctl/client/cluster/objectgraph.go @@ -503,6 +503,14 @@ func (o *objectGraph) filterCluster(clusterName string) error { object.identity.Name, strings.Join(clusterTenants, ",")) } + // CAPI has a force move label that can be used on CRD to identify objects to be moved CRDs with that label has + // `forceMove` set to true. Only move forceMove nodes if and only if they do not have a cluster Tenant. + // If an object has `forceMove` and also has a clusterTenant, the object has clear owner ref assigned and can be + // dropped if the cluster being filtered is not part of its tenants + if len(clusterTenants) == 0 && object.forceMove { + continue + } + if !hasFilterCluster { if _, ok := o.uidToNode[object.identity.UID]; ok { delete(o.uidToNode, object.identity.UID) diff --git a/cmd/clusterctl/client/cluster/objectgraph_test.go b/cmd/clusterctl/client/cluster/objectgraph_test.go index 46572d62f..9100880e1 100644 --- a/cmd/clusterctl/client/cluster/objectgraph_test.go +++ b/cmd/clusterctl/client/cluster/objectgraph_test.go @@ -1988,6 +1988,49 @@ func TestObjectGraph_DiscoveryByCluster(t *testing.T) { }, }, }, + { + name: "two clusters with external force object, read only 1 cluster & both external objects", + args: args{ + cluster: "cluster1", // read only from ns1 + objs: func() []client.Object { + objs := []client.Object{} + objs = append(objs, test.NewFakeCluster("ns1", "cluster1").Objs()...) + objs = append(objs, test.NewFakeExternalObject("ns1", "externalObject1").Objs()...) + objs = append(objs, test.NewFakeCluster("ns1", "cluster2").Objs()...) + objs = append(objs, test.NewFakeExternalObject("ns2", "externalObject2").Objs()...) + return objs + }(), + }, + want: wantGraph{ + nodes: map[string]wantGraphItem{ + "cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1": { + forceMove: true, + forceMoveHierarchy: true, + }, + "infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1": { + owners: []string{ + "cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1", + }, + }, + "/v1, Kind=Secret, ns1/cluster1-ca": { + softOwners: []string{ + "cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1", // NB. this secret is not linked to the cluster through owner ref + }, + }, + "/v1, Kind=Secret, ns1/cluster1-kubeconfig": { + owners: []string{ + "cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1", + }, + }, + "external.cluster.x-k8s.io/v1beta1, Kind=GenericExternalObject, ns1/externalObject1": { + forceMove: true, + }, + "external.cluster.x-k8s.io/v1beta1, Kind=GenericExternalObject, ns2/externalObject2": { + forceMove: true, + }, + }, + }, + }, } for _, tt := range tests { -- 2.40.0