Skip to content

Commit 4f4b730

Browse files
Fix secret selection logic for ownerRef test
1 parent 3b39d4a commit 4f4b730

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

cmd/clusterctl/client/cluster/ownergraph.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"github.com/pkg/errors"
2121
corev1 "k8s.io/api/core/v1"
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
25+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2326
)
2427

2528
// OwnerGraph contains a graph with all the objects considered by clusterctl move as nodes and the OwnerReference relationship
@@ -50,15 +53,20 @@ func nodeToOwnerRef(n *node, attributes ownerReferenceAttributes) metav1.OwnerRe
5053

5154
// GetOwnerGraph returns a graph with all the objects considered by clusterctl move as nodes and the OwnerReference relationship between those objects as edges.
5255
// NOTE: this data structure is exposed to allow implementation of E2E tests verifying that CAPI can properly rebuild its
53-
// own owner references; there is no guarantee about the stability of this API.
56+
// own owner references; there is no guarantee about the stability of this API. Using this test with providers may require
57+
// a custom implementation of this function, or the OwnerGraph it returns.
5458
func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) {
5559
p := newProxy(Kubeconfig{Path: kubeconfigPath, Context: ""})
5660
invClient := newInventoryClient(p, nil)
5761

5862
graph := newObjectGraph(p, invClient)
5963

64+
cl, err := p.NewClient()
65+
if err != nil {
66+
return OwnerGraph{}, errors.Wrap(err, "failed to create client for ownerGraph")
67+
}
6068
// Gets all the types defined by the CRDs installed by clusterctl plus the ConfigMap/Secret core types.
61-
err := graph.getDiscoveryTypes()
69+
err = graph.getDiscoveryTypes()
6270
if err != nil {
6371
return OwnerGraph{}, errors.Wrap(err, "failed to retrieve discovery types")
6472
}
@@ -71,6 +79,17 @@ func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) {
7179
}
7280
owners := OwnerGraph{}
7381
for _, v := range graph.uidToNode {
82+
// The Discovery function returns all secrets in the Cluster namespace. Ensure a Secret that is not part of the
83+
// Cluster is not added to the OwnerGraph.
84+
if v.identity.Kind == "Secret" {
85+
clusterSecret, err := isClusterSecret(v.identity, cl)
86+
if err != nil {
87+
return OwnerGraph{}, err
88+
}
89+
if !clusterSecret {
90+
continue
91+
}
92+
}
7493
n := OwnerGraphNode{Object: v.identity, Owners: []metav1.OwnerReference{}}
7594
for owner, attributes := range v.owners {
7695
n.Owners = append(n.Owners, nodeToOwnerRef(owner, attributes))
@@ -79,3 +98,12 @@ func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) {
7998
}
8099
return owners, nil
81100
}
101+
102+
// isClusterSecret checks whether a Secret is related to a CAPI Cluster by checking if the secret type is ClusterSecretType.
103+
func isClusterSecret(ref corev1.ObjectReference, c client.Client) (bool, error) {
104+
s := &corev1.Secret{}
105+
if err := c.Get(ctx, client.ObjectKey{Namespace: ref.Namespace, Name: ref.Name}, s); err != nil {
106+
return false, err
107+
}
108+
return s.Type == clusterv1.ClusterSecretType, nil
109+
}

util/secret/secret.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Name(cluster string, suffix Purpose) string {
5353
return fmt.Sprintf("%s-%s", cluster, suffix)
5454
}
5555

56-
// ParseSecretName return the cluster name and the suffix Purpose in name is a valid cluster secrets,
56+
// ParseSecretName return the cluster name and the suffix Purpose in name is a valid cluster secret,
5757
// otherwise it return error.
5858
func ParseSecretName(name string) (string, Purpose, error) {
5959
separatorPos := strings.LastIndex(name, "-")

0 commit comments

Comments
 (0)