Skip to content

Commit

Permalink
Fix secret selection logic for ownerRef test
Browse files Browse the repository at this point in the history
  • Loading branch information
killianmuldoon committed Jan 23, 2023
1 parent 3b39d4a commit 11435e7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion cmd/clusterctl/client/cluster/ownergraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// OwnerGraph contains a graph with all the objects considered by clusterctl move as nodes and the OwnerReference relationship
Expand Down Expand Up @@ -57,8 +60,12 @@ func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) {

graph := newObjectGraph(p, invClient)

cl, err := p.NewClient()
if err != nil {
return OwnerGraph{}, errors.Wrap(err, "failed to create client for ownerGraph")
}
// Gets all the types defined by the CRDs installed by clusterctl plus the ConfigMap/Secret core types.
err := graph.getDiscoveryTypes()
err = graph.getDiscoveryTypes()
if err != nil {
return OwnerGraph{}, errors.Wrap(err, "failed to retrieve discovery types")
}
Expand All @@ -71,6 +78,16 @@ func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) {
}
owners := OwnerGraph{}
for _, v := range graph.uidToNode {
// If the object is a Secret but not part of the Cluster ignore it.
if v.identity.Kind == "Secret" {
clusterSecret, err := isClusterSecret(v.identity, cl)
if err != nil {
return OwnerGraph{}, err
}
if !clusterSecret {
continue
}
}
n := OwnerGraphNode{Object: v.identity, Owners: []metav1.OwnerReference{}}
for owner, attributes := range v.owners {
n.Owners = append(n.Owners, nodeToOwnerRef(owner, attributes))
Expand All @@ -79,3 +96,12 @@ func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) {
}
return owners, nil
}

// isClsuterSecret checks whether a Secret is related to a CAPI Cluster by checking if the secret type is ClusterSecretType.
func isClusterSecret(ref corev1.ObjectReference, c client.Client) (bool, error) {
s := &corev1.Secret{}
if err := c.Get(ctx, client.ObjectKey{Namespace: ref.Namespace, Name: ref.Name}, s); err != nil {
return false, err
}
return s.Type == clusterv1.ClusterSecretType, nil
}
2 changes: 1 addition & 1 deletion util/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Name(cluster string, suffix Purpose) string {
return fmt.Sprintf("%s-%s", cluster, suffix)
}

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

0 comments on commit 11435e7

Please sign in to comment.