Skip to content

Commit 5e729a7

Browse files
committed
Refactor into helper functions
1 parent b43f4ee commit 5e729a7

File tree

1 file changed

+69
-51
lines changed

1 file changed

+69
-51
lines changed

cmd/clusterctl/client/tree/discovery.go

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -83,44 +83,13 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
8383

8484
if options.ShowAllResources {
8585
// Adds resource sets and resource set bindings
86-
if resourceSetBindings, err := getResourceSetBindingsInCluster(ctx, c, namespace, name); err == nil {
87-
// Should this be in it's own group?
88-
addons := VirtualObject(cluster.Namespace, "AddonGroup", "Addons")
89-
tree.Add(cluster, addons)
90-
for i, resourceSetBinding := range resourceSetBindings {
91-
tree.Add(addons, &resourceSetBindings[i], ObjectMetaName("ClusterResourceSetBinding"))
92-
for _, binding := range resourceSetBinding.Spec.Bindings {
93-
if resourceSet, err := getResourceSetInCluster(ctx, c, namespace, binding.ClusterResourceSetName); err == nil {
94-
tree.Add(addons, resourceSet, ObjectMetaName("ClusterResourceSet"))
95-
}
96-
}
97-
}
98-
}
86+
addAddonsToObjectTree(ctx, c, cluster, tree)
9987
}
10088

10189
// Adds control plane
10290
controlPlane, err := external.Get(ctx, c, cluster.Spec.ControlPlaneRef, cluster.Namespace)
10391
if err == nil {
104-
tree.Add(cluster, controlPlane, ObjectMetaName("ControlPlane"), GroupingObject(true))
105-
106-
if options.ShowAllResources {
107-
// Add control plane infrastructure ref using spec fields guaranteed in contract
108-
infrastructureRef, found, err := unstructured.NestedMap(controlPlane.UnstructuredContent(), "spec", "machineTemplate", "infrastructureRef")
109-
if err != nil {
110-
return nil, err
111-
}
112-
if found {
113-
infrastructureObjectRef := &corev1.ObjectReference{
114-
Kind: infrastructureRef["kind"].(string),
115-
Namespace: infrastructureRef["namespace"].(string),
116-
Name: infrastructureRef["name"].(string),
117-
APIVersion: infrastructureRef["apiVersion"].(string),
118-
}
119-
if machineTemplate, err := external.Get(ctx, c, infrastructureObjectRef, cluster.Namespace); err == nil {
120-
tree.Add(controlPlane, machineTemplate, ObjectMetaName("MachineInfrastructureTemplate"))
121-
}
122-
}
123-
}
92+
addControlPlane(ctx, c, cluster, controlPlane, tree, options)
12493
}
12594

12695
// Adds control plane machines.
@@ -163,15 +132,76 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
163132
workers := VirtualObject(cluster.Namespace, "WorkerGroup", "Workers")
164133
tree.Add(cluster, workers)
165134

135+
err = addMachineDeploymentToObjectTree(ctx, c, cluster, workers, machinesList, tree, options, addMachineFunc)
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
addMachinePoolsToObjectTree(ctx, c, cluster, workers, machinePoolList, tree, options)
141+
142+
// Handles orphan machines.
143+
if len(machineMap) < len(machinesList.Items) {
144+
other := VirtualObject(cluster.Namespace, "OtherGroup", "Other")
145+
tree.Add(workers, other)
146+
147+
for i := range machinesList.Items {
148+
m := &machinesList.Items[i]
149+
if _, ok := machineMap[m.Name]; ok {
150+
continue
151+
}
152+
addMachineFunc(other, m)
153+
}
154+
}
155+
156+
return tree, nil
157+
}
158+
159+
func addAddonsToObjectTree(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, tree *ObjectTree) {
160+
if resourceSetBindings, err := getResourceSetBindingsInCluster(ctx, c, cluster.Namespace, cluster.Name); err == nil {
161+
// Should this be in it's own group?
162+
addons := VirtualObject(cluster.Namespace, "AddonGroup", "Addons")
163+
tree.Add(cluster, addons)
164+
for i, resourceSetBinding := range resourceSetBindings {
165+
tree.Add(addons, &resourceSetBindings[i], ObjectMetaName("ClusterResourceSetBinding"))
166+
for _, binding := range resourceSetBinding.Spec.Bindings {
167+
if resourceSet, err := getResourceSetInCluster(ctx, c, cluster.Namespace, binding.ClusterResourceSetName); err == nil {
168+
tree.Add(addons, resourceSet, ObjectMetaName("ClusterResourceSet"))
169+
}
170+
}
171+
}
172+
}
173+
}
174+
175+
func addControlPlane(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, controlPlane *unstructured.Unstructured, tree *ObjectTree, options DiscoverOptions) {
176+
tree.Add(cluster, controlPlane, ObjectMetaName("ControlPlane"), GroupingObject(true))
177+
178+
if options.ShowAllResources {
179+
// Add control plane infrastructure ref using spec fields guaranteed in contract
180+
infrastructureRef, found, err := unstructured.NestedMap(controlPlane.UnstructuredContent(), "spec", "machineTemplate", "infrastructureRef")
181+
if err != nil && found {
182+
infrastructureObjectRef := &corev1.ObjectReference{
183+
Kind: infrastructureRef["kind"].(string),
184+
Namespace: infrastructureRef["namespace"].(string),
185+
Name: infrastructureRef["name"].(string),
186+
APIVersion: infrastructureRef["apiVersion"].(string),
187+
}
188+
if machineTemplate, err := external.Get(ctx, c, infrastructureObjectRef, cluster.Namespace); err == nil {
189+
tree.Add(controlPlane, machineTemplate, ObjectMetaName("MachineInfrastructureTemplate"))
190+
}
191+
}
192+
}
193+
}
194+
195+
func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, workers *unstructured.Unstructured, machinesList *clusterv1.MachineList, tree *ObjectTree, options DiscoverOptions, addMachineFunc func(parent client.Object, m *clusterv1.Machine)) error {
166196
// Adds worker machines.
167197
machinesDeploymentList, err := getMachineDeploymentsInCluster(ctx, c, cluster.Namespace, cluster.Name)
168198
if err != nil {
169-
return nil, err
199+
return err
170200
}
171201

172202
machineSetList, err := getMachineSetsInCluster(ctx, c, cluster.Namespace, cluster.Name)
173203
if err != nil {
174-
return nil, err
204+
return err
175205
}
176206

177207
for i := range machinesDeploymentList.Items {
@@ -209,6 +239,10 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
209239
}
210240
}
211241

242+
return nil
243+
}
244+
245+
func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, cluster *clusterv1.Cluster, workers *unstructured.Unstructured, machinePoolList *expv1.MachinePoolList, tree *ObjectTree, options DiscoverOptions) {
212246
for i := range machinePoolList.Items {
213247
mp := &machinePoolList.Items[i]
214248
addOpts := make([]AddObjectOption, 0)
@@ -229,22 +263,6 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
229263

230264
// TODO: Add machinepoolmachines when it's implemented
231265
}
232-
233-
// Handles orphan machines.
234-
if len(machineMap) < len(machinesList.Items) {
235-
other := VirtualObject(cluster.Namespace, "OtherGroup", "Other")
236-
tree.Add(workers, other)
237-
238-
for i := range machinesList.Items {
239-
m := &machinesList.Items[i]
240-
if _, ok := machineMap[m.Name]; ok {
241-
continue
242-
}
243-
addMachineFunc(other, m)
244-
}
245-
}
246-
247-
return tree, nil
248266
}
249267

250268
func getResourceSetBindingsInCluster(ctx context.Context, c client.Client, namespace string, name string) ([]addonsv1.ClusterResourceSetBinding, error) {

0 commit comments

Comments
 (0)