|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/pkg/errors" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | +) |
| 24 | + |
| 25 | +// OwnerGraph contains a graph with all the objects considered by clusterctl move as nodes and the OwnerReference relationship |
| 26 | +// between those objects as edges. |
| 27 | +type OwnerGraph map[string]OwnerGraphNode |
| 28 | + |
| 29 | +// OwnerGraphNode is a single node linking an ObjectReference to its OwnerReferences. |
| 30 | +type OwnerGraphNode struct { |
| 31 | + Object corev1.ObjectReference |
| 32 | + Owners []metav1.OwnerReference |
| 33 | +} |
| 34 | + |
| 35 | +func nodeToOwnerRef(n *node, attributes ownerReferenceAttributes) metav1.OwnerReference { |
| 36 | + ref := metav1.OwnerReference{ |
| 37 | + Name: n.identity.Name, |
| 38 | + APIVersion: n.identity.APIVersion, |
| 39 | + Kind: n.identity.Kind, |
| 40 | + UID: n.identity.UID, |
| 41 | + } |
| 42 | + if attributes.BlockOwnerDeletion != nil { |
| 43 | + ref.BlockOwnerDeletion = attributes.BlockOwnerDeletion |
| 44 | + } |
| 45 | + if attributes.Controller != nil { |
| 46 | + ref.Controller = attributes.Controller |
| 47 | + } |
| 48 | + return ref |
| 49 | +} |
| 50 | + |
| 51 | +// GetOwnerGraph returns a graph with all the objects considered by clusterctl move as nodes and the OwnerReference relationship between those objects as edges. |
| 52 | +// 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. |
| 54 | +func GetOwnerGraph(namespace, kubeconfigPath string) (OwnerGraph, error) { |
| 55 | + p := newProxy(Kubeconfig{Path: kubeconfigPath, Context: ""}) |
| 56 | + invClient := newInventoryClient(p, nil) |
| 57 | + |
| 58 | + graph := newObjectGraph(p, invClient) |
| 59 | + |
| 60 | + // Gets all the types defined by the CRDs installed by clusterctl plus the ConfigMap/Secret core types. |
| 61 | + err := graph.getDiscoveryTypes() |
| 62 | + if err != nil { |
| 63 | + return OwnerGraph{}, errors.Wrap(err, "failed to retrieve discovery types") |
| 64 | + } |
| 65 | + |
| 66 | + // Discovery the object graph for the selected types: |
| 67 | + // - Nodes are defined the Kubernetes objects (Clusters, Machines etc.) identified during the discovery process. |
| 68 | + // - Edges are derived by the OwnerReferences between nodes. |
| 69 | + if err := graph.Discovery(namespace); err != nil { |
| 70 | + return OwnerGraph{}, errors.Wrap(err, "failed to discover the object graph") |
| 71 | + } |
| 72 | + owners := OwnerGraph{} |
| 73 | + for _, v := range graph.uidToNode { |
| 74 | + n := OwnerGraphNode{Object: v.identity, Owners: []metav1.OwnerReference{}} |
| 75 | + for owner, attributes := range v.owners { |
| 76 | + n.Owners = append(n.Owners, nodeToOwnerRef(owner, attributes)) |
| 77 | + } |
| 78 | + owners[string(v.identity.UID)] = n |
| 79 | + } |
| 80 | + return owners, nil |
| 81 | +} |
0 commit comments