Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 261 additions & 0 deletions controlplane/kubeadm/internal/control_plane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import (
"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

controlplanev1 "sigs.k8s.io/cluster-api/api/controlplane/kubeadm/v1beta2"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/patch"
)

func TestControlPlane(t *testing.T) {
Expand Down Expand Up @@ -62,6 +65,12 @@ func TestControlPlane(t *testing.T) {
controlPlane.Machines.Insert(machine("machine-5", withFailureDomain("unknown")))
g.Expect(controlPlane.FailureDomainWithMostMachines(ctx, controlPlane.Machines)).To(Equal("unknown"))
})

t.Run("With failure Domains is set empty", func(*testing.T) {
g := NewWithT(t)
controlPlane.Cluster.Status.FailureDomains = nil
g.Expect(controlPlane.FailureDomainWithMostMachines(ctx, controlPlane.Machines)).To(Equal("one"))
})
})

t.Run("MachinesUpToDate", func(t *testing.T) {
Expand Down Expand Up @@ -142,6 +151,169 @@ func TestControlPlane(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(fd).To(Equal("two")) // deleted up-to-date machines (m4) should not be counted when picking the next failure domain for scale up
})

t.Run("Next Failure Domains", func(t *testing.T) {
g := NewWithT(t)
cluster := clusterv1.Cluster{
Status: clusterv1.ClusterStatus{
FailureDomains: []clusterv1.FailureDomain{
failureDomain("one", false),
},
},
}
kcp := &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.31.0",
},
}
machines := collections.Machines{
"machine-1": &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "m1", DeletionTimestamp: ptr.To(metav1.Now())},
Spec: clusterv1.MachineSpec{
Version: "v1.31.0",
FailureDomain: "one",
InfrastructureRef: clusterv1.ContractVersionedObjectReference{Kind: "GenericInfrastructureMachine", APIGroup: clusterv1.GroupVersionInfrastructure.Group, Name: "m1"},
}},
}
controlPlane, err := NewControlPlane(ctx, nil, env.GetClient(), &cluster, kcp, machines)
g.Expect(err).NotTo(HaveOccurred())
fd, err := controlPlane.NextFailureDomainForScaleUp(ctx)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(fd).To(BeEmpty())
})

t.Run("ControlPlane returns error when getting infra resources", func(t *testing.T) {
g := NewWithT(t)
cluster := clusterv1.Cluster{
Status: clusterv1.ClusterStatus{
FailureDomains: []clusterv1.FailureDomain{
failureDomain("one", true),
},
},
}
kcp := &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.31.0",
},
}
machines := collections.Machines{
"machine-1": &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "m1"},
Spec: clusterv1.MachineSpec{
Version: "v1.31.0",
FailureDomain: "one",
InfrastructureRef: clusterv1.ContractVersionedObjectReference{Name: "m1"},
}},
}
_, err := NewControlPlane(ctx, nil, env.GetClient(), &cluster, kcp, machines)
g.Expect(err).To(HaveOccurred())
})

t.Run("When infra and bootstrap config exists", func(t *testing.T) {
g := NewWithT(t)
ns, err := env.CreateNamespace(ctx, "test-machine-watches")
kcp := &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.31.0",
},
}

g.Expect(err).ToNot(HaveOccurred())

infraMachine := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "GenericInfrastructureMachine",
"apiVersion": clusterv1.GroupVersionInfrastructure.String(),
"metadata": map[string]interface{}{
"name": "infra-config1",
"namespace": ns.Name,
},
"spec": map[string]interface{}{
"providerID": "test://id-1",
},
"status": map[string]interface{}{
"ready": true,
"addresses": []interface{}{
map[string]interface{}{
"type": "InternalIP",
"address": "10.0.0.1",
},
},
},
},
}

bootstrap := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "KubeadmConfig",
"apiVersion": "bootstrap.cluster.x-k8s.io/v1beta1",
"metadata": map[string]interface{}{
"name": "bootstrap-config-machinereconcile",
"namespace": ns.Name,
},
"spec": map[string]interface{}{
"providerID": "test://id-1",
},
"status": map[string]interface{}{
"ready": true,
},
},
}

testCluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster", Namespace: ns.Name},
Status: clusterv1.ClusterStatus{
FailureDomains: []clusterv1.FailureDomain{
failureDomain("one", true),
failureDomain("two", true),
failureDomain("three", true),
},
},
}

g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
g.Expect(env.Create(ctx, bootstrap)).To(Succeed())

defer func(do ...client.Object) {
g.Expect(env.Cleanup(ctx, do...)).To(Succeed())
}(ns, bootstrap, infraMachine)

// Patch infra machine ready
patchHelper, err := patch.NewHelper(infraMachine, env)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(unstructured.SetNestedField(infraMachine.Object, true, "status", "ready")).To(Succeed())
g.Expect(patchHelper.Patch(ctx, infraMachine, patch.WithStatusObservedGeneration{})).To(Succeed())

// Patch bootstrap ready
patchHelper, err = patch.NewHelper(bootstrap, env)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(unstructured.SetNestedField(bootstrap.Object, true, "status", "ready")).To(Succeed())
g.Expect(patchHelper.Patch(ctx, bootstrap, patch.WithStatusObservedGeneration{})).To(Succeed())

machines := collections.Machines{
"machine-1": &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "m1",
Namespace: ns.Name},
Spec: clusterv1.MachineSpec{
InfrastructureRef: clusterv1.ContractVersionedObjectReference{
APIGroup: clusterv1.GroupVersionInfrastructure.Group,
Kind: "GenericInfrastructureMachine",
Name: "infra-config1",
},
Bootstrap: clusterv1.Bootstrap{
ConfigRef: &clusterv1.ContractVersionedObjectReference{
APIGroup: clusterv1.GroupVersionBootstrap.Group,
Kind: "KubeadmConfig",
Name: "bootstrap-config-machinereconcile",
},
},
},
},
}

_, err = NewControlPlane(ctx, nil, env.GetClient(), testCluster, kcp, machines)
g.Expect(err).NotTo(HaveOccurred())
})
}

func TestHasMachinesToBeRemediated(t *testing.T) {
Expand Down Expand Up @@ -356,6 +528,95 @@ func TestStatusToLogKeyAndValues(t *testing.T) {
g.Expect(got[3]).To(Equal("m1, m2, m3"))
}

func TestMachineInFailureDomainWithMostMachines(t *testing.T) {
t.Run("Machines in Failure Domain", func(t *testing.T) {
machines := collections.Machines{
"machine-3": &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "m3"},
Spec: clusterv1.MachineSpec{
Version: "v1.31.0",
FailureDomain: "three",
InfrastructureRef: clusterv1.ContractVersionedObjectReference{Kind: "GenericInfrastructureMachine", APIGroup: clusterv1.GroupVersionInfrastructure.Group, Name: "m3"},
}},
}

c := &ControlPlane{
KCP: &controlplanev1.KubeadmControlPlane{},
Cluster: &clusterv1.Cluster{
Status: clusterv1.ClusterStatus{
FailureDomains: []clusterv1.FailureDomain{
failureDomain("three", false),
},
},
},
Machines: collections.Machines{
"machine-3": machine("machine-3", withFailureDomain("three")),
},
}

g := NewWithT(t)
_, err := c.MachineInFailureDomainWithMostMachines(ctx, machines)
g.Expect(err).NotTo(HaveOccurred())
})
t.Run("Return error when no controlplane machine found", func(t *testing.T) {
machines := collections.Machines{}

c := &ControlPlane{
KCP: &controlplanev1.KubeadmControlPlane{},
Cluster: &clusterv1.Cluster{
Status: clusterv1.ClusterStatus{
FailureDomains: []clusterv1.FailureDomain{},
},
},
Machines: collections.Machines{},
}

g := NewWithT(t)
_, err := c.MachineInFailureDomainWithMostMachines(ctx, machines)
g.Expect(err).To(HaveOccurred())
})
}
func TestMachineWithDeleteAnnotation(t *testing.T) {
t.Run("Machines having delete annotation set", func(t *testing.T) {
machines := collections.Machines{
"machine-1": &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "m1",
Annotations: map[string]string{
"cluster.x-k8s.io/delete-machine": "",
},
},
Spec: clusterv1.MachineSpec{
Version: "v1.31.0",
FailureDomain: "one",
InfrastructureRef: clusterv1.ContractVersionedObjectReference{Kind: "GenericInfrastructureMachine", APIGroup: clusterv1.GroupVersionInfrastructure.Group, Name: "m1"},
}},
"machine-2": &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{Name: "m2",
Annotations: map[string]string{
"cluster.x-k8s.io/delete-machine": "",
},
},
Spec: clusterv1.MachineSpec{
Version: "v1.31.0",
FailureDomain: "two",
InfrastructureRef: clusterv1.ContractVersionedObjectReference{Kind: "GenericInfrastructureMachine", APIGroup: clusterv1.GroupVersionInfrastructure.Group, Name: "m2"},
}},
}

c := ControlPlane{
Machines: machines,
Cluster: &clusterv1.Cluster{
Status: clusterv1.ClusterStatus{},
},
}

g := NewWithT(t)
annotatedMachines := c.MachineWithDeleteAnnotation(machines)
g.Expect(annotatedMachines).NotTo(BeNil())
g.Expect(annotatedMachines.Len()).To(BeEquivalentTo(2))
})
}

type machineOpt func(*clusterv1.Machine)

func failureDomain(name string, controlPlane bool) clusterv1.FailureDomain {
Expand Down
Loading