Skip to content

Commit 1fecd9f

Browse files
authored
Merge pull request #8836 from Jont828/mpm-clusterctl
✨ Add MachinePool Machine support in clusterctl discovery
2 parents da10c33 + 378d6a4 commit 1fecd9f

File tree

4 files changed

+150
-41
lines changed

4 files changed

+150
-41
lines changed

api/v1beta1/machine_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const (
4343
// MachineDeploymentNameLabel is the label set on machines if they're controlled by MachineDeployment.
4444
MachineDeploymentNameLabel = "cluster.x-k8s.io/deployment-name"
4545

46+
// MachinePoolNameLabel is the label indicating the name of the MachinePool a Machine is controlled by.
47+
// Note: The value of this label may be a hash if the MachinePool name is longer than 63 characters.
48+
MachinePoolNameLabel = "cluster.x-k8s.io/pool-name"
49+
4650
// MachineControlPlaneNameLabel is the label set on machines if they're controlled by a ControlPlane.
4751
// Note: The value of this label may be a hash if the control plane name is longer than 63 characters.
4852
MachineControlPlaneNameLabel = "cluster.x-k8s.io/control-plane-name"

cmd/clusterctl/client/tree/discovery.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
111111
machineMap[m.Name] = true
112112

113113
if visible {
114-
if machineInfra, err := external.Get(ctx, c, &m.Spec.InfrastructureRef, cluster.Namespace); err == nil {
115-
tree.Add(m, machineInfra, ObjectMetaName("MachineInfrastructure"), NoEcho(true))
114+
if (m.Spec.InfrastructureRef != corev1.ObjectReference{}) {
115+
if machineInfra, err := external.Get(ctx, c, &m.Spec.InfrastructureRef, cluster.Namespace); err == nil {
116+
tree.Add(m, machineInfra, ObjectMetaName("MachineInfrastructure"), NoEcho(true))
117+
}
116118
}
117119

118-
if machineBootstrap, err := external.Get(ctx, c, m.Spec.Bootstrap.ConfigRef, cluster.Namespace); err == nil {
119-
tree.Add(m, machineBootstrap, ObjectMetaName("BootstrapConfig"), NoEcho(true))
120+
if m.Spec.Bootstrap.ConfigRef != nil {
121+
if machineBootstrap, err := external.Get(ctx, c, m.Spec.Bootstrap.ConfigRef, cluster.Namespace); err == nil {
122+
tree.Add(m, machineBootstrap, ObjectMetaName("BootstrapConfig"), NoEcho(true))
123+
}
120124
}
121125
}
122126
}
@@ -146,25 +150,25 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
146150
if err != nil {
147151
return nil, err
148152
}
149-
150-
// Handles orphan machines.
151-
if len(machineMap) < len(machinesList.Items) {
152-
other := VirtualObject(cluster.Namespace, "OtherGroup", "Other")
153-
tree.Add(workers, other)
154-
155-
for i := range machinesList.Items {
156-
m := &machinesList.Items[i]
157-
if _, ok := machineMap[m.Name]; ok {
158-
continue
159-
}
160-
addMachineFunc(other, m)
161-
}
162-
}
163153
}
164154

165155
if len(machinePoolList.Items) > 0 { // Add MachinePool objects
166156
tree.Add(cluster, workers)
167-
addMachinePoolsToObjectTree(ctx, c, cluster.Namespace, workers, machinePoolList, tree)
157+
addMachinePoolsToObjectTree(ctx, c, cluster.Namespace, workers, machinePoolList, machinesList, tree, addMachineFunc)
158+
}
159+
160+
// Handles orphan machines.
161+
if len(machineMap) < len(machinesList.Items) {
162+
other := VirtualObject(cluster.Namespace, "OtherGroup", "Other")
163+
tree.Add(workers, other)
164+
165+
for i := range machinesList.Items {
166+
m := &machinesList.Items[i]
167+
if _, ok := machineMap[m.Name]; ok {
168+
continue
169+
}
170+
addMachineFunc(other, m)
171+
}
168172
}
169173

170174
return tree, nil
@@ -271,20 +275,25 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
271275
return nil
272276
}
273277

274-
func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, namespace string, workers *unstructured.Unstructured, machinePoolList *expv1.MachinePoolList, tree *ObjectTree) {
278+
func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, namespace string, workers *unstructured.Unstructured, machinePoolList *expv1.MachinePoolList, machinesList *clusterv1.MachineList, tree *ObjectTree, addMachineFunc func(parent client.Object, m *clusterv1.Machine)) {
275279
for i := range machinePoolList.Items {
276280
mp := &machinePoolList.Items[i]
277-
_, visible := tree.Add(workers, mp)
281+
_, visible := tree.Add(workers, mp, GroupingObject(true))
278282

279283
if visible {
280284
if machinePoolBootstrap, err := external.Get(ctx, c, mp.Spec.Template.Spec.Bootstrap.ConfigRef, namespace); err == nil {
281285
tree.Add(mp, machinePoolBootstrap, ObjectMetaName("BootstrapConfig"), NoEcho(true))
282286
}
283287

284288
if machinePoolInfra, err := external.Get(ctx, c, &mp.Spec.Template.Spec.InfrastructureRef, namespace); err == nil {
285-
tree.Add(mp, machinePoolInfra, ObjectMetaName("MachineInfrastructure"), NoEcho(true))
289+
tree.Add(mp, machinePoolInfra, ObjectMetaName("MachinePoolInfrastructure"), NoEcho(true))
286290
}
287291
}
292+
293+
machines := selectMachinesControlledBy(machinesList, mp)
294+
for _, m := range machines {
295+
addMachineFunc(mp, m)
296+
}
288297
}
289298
}
290299

cmd/clusterctl/client/tree/discovery_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,86 @@ func Test_Discovery(t *testing.T) {
215215
},
216216
},
217217
},
218+
{
219+
name: "Discovery with MachinePool Machines with echo enabled",
220+
args: args{
221+
discoverOptions: DiscoverOptions{
222+
Grouping: false,
223+
Echo: true,
224+
},
225+
objs: test.NewFakeCluster("ns1", "cluster1").
226+
WithControlPlane(
227+
test.NewFakeControlPlane("cp").
228+
WithMachines(
229+
test.NewFakeMachine("cp1"),
230+
),
231+
).
232+
WithMachinePools(
233+
test.NewFakeMachinePool("mp1").
234+
WithMachines(
235+
test.NewFakeMachine("mp1m1"),
236+
test.NewFakeMachine("mp1m2"),
237+
),
238+
).
239+
Objs(),
240+
},
241+
wantTree: map[string][]string{
242+
// Cluster should be parent of InfrastructureCluster, ControlPlane, and WorkerNodes
243+
"cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1": {
244+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1",
245+
"controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlane, ns1/cp",
246+
"virtual.cluster.x-k8s.io/v1beta1, Kind=WorkerGroup, ns1/Workers",
247+
},
248+
// InfrastructureCluster should be leaf
249+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1": {},
250+
// ControlPlane should have a machine
251+
"controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlane, ns1/cp": {
252+
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/cp1",
253+
},
254+
// Workers should have a machine deployment
255+
"virtual.cluster.x-k8s.io/v1beta1, Kind=WorkerGroup, ns1/Workers": {
256+
"cluster.x-k8s.io/v1beta1, Kind=MachinePool, ns1/mp1",
257+
},
258+
// Machine Pool should have a group of machines
259+
"cluster.x-k8s.io/v1beta1, Kind=MachinePool, ns1/mp1": {
260+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachineTemplate, ns1/mp1",
261+
"bootstrap.cluster.x-k8s.io/v1beta1, Kind=GenericBootstrapConfigTemplate, ns1/mp1",
262+
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m1",
263+
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m2",
264+
},
265+
// Machine should have infra machine and bootstrap (echo)
266+
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/cp1": {
267+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachine, ns1/cp1",
268+
"bootstrap.cluster.x-k8s.io/v1beta1, Kind=GenericBootstrapConfig, ns1/cp1",
269+
},
270+
// MachinePool Machine should only have infra machine
271+
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m1": {
272+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachine, ns1/mp1m1",
273+
},
274+
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m2": {
275+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachine, ns1/mp1m2",
276+
},
277+
},
278+
wantNodeCheck: map[string]nodeCheck{
279+
// InfrastructureCluster should have a meta name
280+
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1": func(g *WithT, obj client.Object) {
281+
g.Expect(GetMetaName(obj)).To(Equal("ClusterInfrastructure"))
282+
},
283+
// ControlPlane should have a meta name, should NOT be a grouping object
284+
"controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlane, ns1/cp": func(g *WithT, obj client.Object) {
285+
g.Expect(GetMetaName(obj)).To(Equal("ControlPlane"))
286+
g.Expect(IsGroupingObject(obj)).To(BeFalse())
287+
},
288+
// Workers should be a virtual node
289+
"virtual.cluster.x-k8s.io/v1beta1, Kind=WorkerGroup, ns1/Workers": func(g *WithT, obj client.Object) {
290+
g.Expect(IsVirtualObject(obj)).To(BeTrue())
291+
},
292+
// Machine pool should NOT be a grouping object
293+
"cluster.x-k8s.io/v1beta1, Kind=MachinePool, ns1/mp1": func(g *WithT, obj client.Object) {
294+
g.Expect(IsGroupingObject(obj)).To(BeFalse())
295+
},
296+
},
297+
},
218298
{
219299
name: "Discovery with grouping and no-echo disabled",
220300
args: args{

cmd/clusterctl/internal/test/fake_objects.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (f *FakeCluster) Objs() []client.Object {
269269
if f.controlPlane == nil && i == 0 {
270270
generateCerts = true
271271
}
272-
objs = append(objs, machine.Objs(cluster, generateCerts, nil, nil)...)
272+
objs = append(objs, machine.Objs(cluster, generateCerts, nil, nil, nil)...)
273273
}
274274

275275
// Ensure all the objects gets UID.
@@ -403,7 +403,7 @@ func (f *FakeControlPlane) Objs(cluster *clusterv1.Cluster) []client.Object {
403403

404404
// Adds the objects for the machines controlled by the controlPlane
405405
for _, machine := range f.machines {
406-
objs = append(objs, machine.Objs(cluster, false, nil, controlPlane)...)
406+
objs = append(objs, machine.Objs(cluster, false, nil, nil, controlPlane)...)
407407
}
408408

409409
return objs
@@ -412,6 +412,7 @@ func (f *FakeControlPlane) Objs(cluster *clusterv1.Cluster) []client.Object {
412412
type FakeMachinePool struct {
413413
name string
414414
bootstrapConfig *clusterv1.Bootstrap
415+
machines []*FakeMachine
415416
}
416417

417418
// NewFakeMachinePool return a FakeMachinePool that can generate a MachinePool object, all its own ancillary objects:
@@ -428,6 +429,11 @@ func (f *FakeMachinePool) WithStaticBootstrapConfig() *FakeMachinePool {
428429
return f
429430
}
430431

432+
func (f *FakeMachinePool) WithMachines(fakeMachine ...*FakeMachine) *FakeMachinePool {
433+
f.machines = append(f.machines, fakeMachine...)
434+
return f
435+
}
436+
431437
func (f *FakeMachinePool) Objs(cluster *clusterv1.Cluster) []client.Object {
432438
machinePoolInfrastructure := &fakeinfrastructure.GenericInfrastructureMachineTemplate{
433439
TypeMeta: metav1.TypeMeta{
@@ -523,6 +529,10 @@ func (f *FakeMachinePool) Objs(cluster *clusterv1.Cluster) []client.Object {
523529
objs = append(objs, machinePoolBootstrap)
524530
}
525531

532+
for _, machine := range f.machines {
533+
objs = append(objs, machine.Objs(cluster, false, nil, machinePool, nil)...)
534+
}
535+
526536
return objs
527537
}
528538

@@ -847,7 +857,7 @@ func (f *FakeMachineSet) Objs(cluster *clusterv1.Cluster, machineDeployment *clu
847857

848858
// Adds the objects for the machines controlled by the machineSet
849859
for _, machine := range f.machines {
850-
objs = append(objs, machine.Objs(cluster, false, machineSet, nil)...)
860+
objs = append(objs, machine.Objs(cluster, false, machineSet, nil, nil)...)
851861
}
852862

853863
return objs
@@ -873,7 +883,7 @@ func (f *FakeMachine) WithStaticBootstrapConfig() *FakeMachine {
873883
return f
874884
}
875885

876-
func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machineSet *clusterv1.MachineSet, controlPlane *fakecontrolplane.GenericControlPlane) []client.Object {
886+
func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machineSet *clusterv1.MachineSet, machinePool *expv1.MachinePool, controlPlane *fakecontrolplane.GenericControlPlane) []client.Object {
877887
machineInfrastructure := &fakeinfrastructure.GenericInfrastructureMachine{
878888
TypeMeta: metav1.TypeMeta{
879889
APIVersion: fakeinfrastructure.GroupVersion.String(),
@@ -955,8 +965,6 @@ func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machi
955965
},
956966
}
957967

958-
machine.Spec.Bootstrap = *bootstrapConfig
959-
960968
// Ensure the machine gets a UID to be used by dependant objects for creating OwnerReferences.
961969
setUID(machine)
962970

@@ -971,6 +979,11 @@ func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machi
971979
machine.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(controlPlane, controlPlane.GroupVersionKind())})
972980
// Sets the MachineControlPlane Label
973981
machine.Labels[clusterv1.MachineControlPlaneLabel] = ""
982+
case machinePool != nil:
983+
// If this machine belong to a machinePool, it is controlled by it / ownership set by the machinePool controller -- ** NOT RECONCILED **
984+
machine.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(machinePool, machinePool.GroupVersionKind())})
985+
// Sets the MachinePoolNameLabel
986+
machine.Labels[clusterv1.MachinePoolNameLabel] = machinePool.Name
974987
default:
975988
// If this machine does not belong to a machineSet or to a control plane, it is owned by the cluster / ownership set by the machine controller -- RECONCILED
976989
machine.SetOwnerReferences([]metav1.OwnerReference{{
@@ -1017,20 +1030,23 @@ func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machi
10171030
machineInfrastructure,
10181031
}
10191032

1020-
if machine.Spec.Bootstrap.ConfigRef != nil {
1021-
machineBootstrap.SetOwnerReferences([]metav1.OwnerReference{
1022-
{
1023-
APIVersion: machine.APIVersion,
1024-
Kind: machine.Kind,
1025-
Name: machine.Name,
1026-
UID: machine.UID,
1027-
},
1028-
})
1029-
machineBootstrap.SetLabels(map[string]string{
1030-
clusterv1.ClusterNameLabel: machine.Spec.ClusterName,
1031-
})
1033+
if machinePool == nil {
1034+
machine.Spec.Bootstrap = *bootstrapConfig
1035+
if machine.Spec.Bootstrap.ConfigRef != nil {
1036+
machineBootstrap.SetOwnerReferences([]metav1.OwnerReference{
1037+
{
1038+
APIVersion: machine.APIVersion,
1039+
Kind: machine.Kind,
1040+
Name: machine.Name,
1041+
UID: machine.UID,
1042+
},
1043+
})
1044+
machineBootstrap.SetLabels(map[string]string{
1045+
clusterv1.ClusterNameLabel: machine.Spec.ClusterName,
1046+
})
10321047

1033-
objs = append(objs, bootstrapDataSecret, machineBootstrap)
1048+
objs = append(objs, bootstrapDataSecret, machineBootstrap)
1049+
}
10341050
}
10351051

10361052
objs = append(objs, additionalObjs...)

0 commit comments

Comments
 (0)