Skip to content

Commit

Permalink
Refactor to add test for MPMs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Jun 30, 2023
1 parent a38bc89 commit 5060ce9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 19 deletions.
4 changes: 4 additions & 0 deletions api/v1beta1/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const (
// MachineDeploymentNameLabel is the label set on machines if they're controlled by MachineDeployment.
MachineDeploymentNameLabel = "cluster.x-k8s.io/deployment-name"

// MachinePoolNameLabel is the label indicating the name of the MachinePool a Machine is controlled by.
// Note: The value of this label may be a hash if the MachinePool name is longer than 63 characters.
MachinePoolNameLabel = "cluster.x-k8s.io/pool-name"

// MachineControlPlaneNameLabel is the label set on machines if they're controlled by a ControlPlane.
// Note: The value of this label may be a hash if the control plane name is longer than 63 characters.
MachineControlPlaneNameLabel = "cluster.x-k8s.io/control-plane-name"
Expand Down
80 changes: 80 additions & 0 deletions cmd/clusterctl/client/tree/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,86 @@ func Test_Discovery(t *testing.T) {
},
},
},
{
name: "Discovery with MachinePool Machines with echo enabled",
args: args{
discoverOptions: DiscoverOptions{
Grouping: false,
Echo: true,
},
objs: test.NewFakeCluster("ns1", "cluster1").
WithControlPlane(
test.NewFakeControlPlane("cp").
WithMachines(
test.NewFakeMachine("cp1"),
),
).
WithMachinePools(
test.NewFakeMachinePool("mp1").
WithMachines(
test.NewFakeMachine("mp1m1"),
test.NewFakeMachine("mp1m2"),
),
).
Objs(),
},
wantTree: map[string][]string{
// Cluster should be parent of InfrastructureCluster, ControlPlane, and WorkerNodes
"cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1": {
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1",
"controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlane, ns1/cp",
"virtual.cluster.x-k8s.io/v1beta1, Kind=WorkerGroup, ns1/Workers",
},
// InfrastructureCluster should be leaf
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1": {},
// ControlPlane should have a machine
"controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlane, ns1/cp": {
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/cp1",
},
// Workers should have a machine deployment
"virtual.cluster.x-k8s.io/v1beta1, Kind=WorkerGroup, ns1/Workers": {
"cluster.x-k8s.io/v1beta1, Kind=MachinePool, ns1/mp1",
},
// Machine Pool should have a group of machines
"cluster.x-k8s.io/v1beta1, Kind=MachinePool, ns1/mp1": {
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachineTemplate, ns1/mp1",
"bootstrap.cluster.x-k8s.io/v1beta1, Kind=GenericBootstrapConfigTemplate, ns1/mp1",
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m1",
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m2",
},
// Machine should have infra machine and bootstrap (echo)
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/cp1": {
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachine, ns1/cp1",
"bootstrap.cluster.x-k8s.io/v1beta1, Kind=GenericBootstrapConfig, ns1/cp1",
},
// MachinePool Machine should only have infra machine
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m1": {
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachine, ns1/mp1m1",
},
"cluster.x-k8s.io/v1beta1, Kind=Machine, ns1/mp1m2": {
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachine, ns1/mp1m2",
},
},
wantNodeCheck: map[string]nodeCheck{
// InfrastructureCluster should have a meta name
"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/cluster1": func(g *WithT, obj client.Object) {
g.Expect(GetMetaName(obj)).To(Equal("ClusterInfrastructure"))
},
// ControlPlane should have a meta name, should NOT be a grouping object
"controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlane, ns1/cp": func(g *WithT, obj client.Object) {
g.Expect(GetMetaName(obj)).To(Equal("ControlPlane"))
g.Expect(IsGroupingObject(obj)).To(BeFalse())
},
// Workers should be a virtual node
"virtual.cluster.x-k8s.io/v1beta1, Kind=WorkerGroup, ns1/Workers": func(g *WithT, obj client.Object) {
g.Expect(IsVirtualObject(obj)).To(BeTrue())
},
// Machine pool should NOT be a grouping object
"cluster.x-k8s.io/v1beta1, Kind=MachinePool, ns1/mp1": func(g *WithT, obj client.Object) {
g.Expect(IsGroupingObject(obj)).To(BeFalse())
},
},
},
{
name: "Discovery with grouping and no-echo disabled",
args: args{
Expand Down
54 changes: 35 additions & 19 deletions cmd/clusterctl/internal/test/fake_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (f *FakeCluster) Objs() []client.Object {
if f.controlPlane == nil && i == 0 {
generateCerts = true
}
objs = append(objs, machine.Objs(cluster, generateCerts, nil, nil)...)
objs = append(objs, machine.Objs(cluster, generateCerts, nil, nil, nil)...)
}

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

// Adds the objects for the machines controlled by the controlPlane
for _, machine := range f.machines {
objs = append(objs, machine.Objs(cluster, false, nil, controlPlane)...)
objs = append(objs, machine.Objs(cluster, false, nil, nil, controlPlane)...)
}

return objs
Expand All @@ -412,6 +412,7 @@ func (f *FakeControlPlane) Objs(cluster *clusterv1.Cluster) []client.Object {
type FakeMachinePool struct {
name string
bootstrapConfig *clusterv1.Bootstrap
machines []*FakeMachine
}

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

func (f *FakeMachinePool) WithMachines(fakeMachine ...*FakeMachine) *FakeMachinePool {
f.machines = append(f.machines, fakeMachine...)
return f
}

func (f *FakeMachinePool) Objs(cluster *clusterv1.Cluster) []client.Object {
machinePoolInfrastructure := &fakeinfrastructure.GenericInfrastructureMachineTemplate{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -523,6 +529,10 @@ func (f *FakeMachinePool) Objs(cluster *clusterv1.Cluster) []client.Object {
objs = append(objs, machinePoolBootstrap)
}

for _, machine := range f.machines {
objs = append(objs, machine.Objs(cluster, false, nil, machinePool, nil)...)
}

return objs
}

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

// Adds the objects for the machines controlled by the machineSet
for _, machine := range f.machines {
objs = append(objs, machine.Objs(cluster, false, machineSet, nil)...)
objs = append(objs, machine.Objs(cluster, false, machineSet, nil, nil)...)
}

return objs
Expand All @@ -873,7 +883,7 @@ func (f *FakeMachine) WithStaticBootstrapConfig() *FakeMachine {
return f
}

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

machine.Spec.Bootstrap = *bootstrapConfig

// Ensure the machine gets a UID to be used by dependant objects for creating OwnerReferences.
setUID(machine)

Expand All @@ -971,6 +979,11 @@ func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machi
machine.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(controlPlane, controlPlane.GroupVersionKind())})
// Sets the MachineControlPlane Label
machine.Labels[clusterv1.MachineControlPlaneLabel] = ""
case machinePool != nil:
// If this machine belong to a machinePool, it is controlled by it / ownership set by the machinePool controller -- ** NOT RECONCILED ?? **
machine.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(machinePool, machinePool.GroupVersionKind())})
// Sets the MachineControlPlane Label
machine.Labels[clusterv1.MachinePoolNameLabel] = machinePool.Name
default:
// 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
machine.SetOwnerReferences([]metav1.OwnerReference{{
Expand Down Expand Up @@ -1017,20 +1030,23 @@ func (f *FakeMachine) Objs(cluster *clusterv1.Cluster, generateCerts bool, machi
machineInfrastructure,
}

if machine.Spec.Bootstrap.ConfigRef != nil {
machineBootstrap.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: machine.APIVersion,
Kind: machine.Kind,
Name: machine.Name,
UID: machine.UID,
},
})
machineBootstrap.SetLabels(map[string]string{
clusterv1.ClusterNameLabel: machine.Spec.ClusterName,
})
if machinePool == nil {
machine.Spec.Bootstrap = *bootstrapConfig
if machine.Spec.Bootstrap.ConfigRef != nil {
machineBootstrap.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: machine.APIVersion,
Kind: machine.Kind,
Name: machine.Name,
UID: machine.UID,
},
})
machineBootstrap.SetLabels(map[string]string{
clusterv1.ClusterNameLabel: machine.Spec.ClusterName,
})
}

objs = append(objs, bootstrapDataSecret, machineBootstrap)
additionalObjs = append(additionalObjs, machineBootstrap, bootstrapDataSecret)
}

objs = append(objs, additionalObjs...)
Expand Down

0 comments on commit 5060ce9

Please sign in to comment.