Skip to content

Commit 438fe67

Browse files
alvaroalemank8s-ci-robot
authored andcommitted
Test MachineDeployment controller scale and update (#541)
1 parent 3d2b607 commit 438fe67

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

pkg/controller/machinedeployment/machinedeployment_controller_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/types"
2727
"k8s.io/apimachinery/pkg/util/intstr"
28+
"k8s.io/client-go/util/retry"
2829
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
2930
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -40,11 +41,13 @@ const timeout = time.Second * 5
4041

4142
func TestReconcile(t *testing.T) {
4243
g := gomega.NewGomegaWithT(t)
44+
labels := map[string]string{"foo": "bar"}
4345
instance := &clusterv1alpha1.MachineDeployment{
4446
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"},
4547
Spec: clusterv1alpha1.MachineDeploymentSpec{
4648
MinReadySeconds: int32Ptr(0),
4749
Replicas: int32Ptr(2),
50+
Selector: metav1.LabelSelector{MatchLabels: labels},
4851
Strategy: clusterv1alpha1.MachineDeploymentStrategy{
4952
Type: common.RollingUpdateMachineDeploymentStrategyType,
5053
RollingUpdate: &clusterv1alpha1.MachineRollingUpdateDeployment{
@@ -53,6 +56,9 @@ func TestReconcile(t *testing.T) {
5356
},
5457
},
5558
Template: clusterv1alpha1.MachineTemplateSpec{
59+
ObjectMeta: metav1.ObjectMeta{
60+
Labels: labels,
61+
},
5662
Spec: clusterv1alpha1.MachineSpec{
5763
Versions: clusterv1alpha1.MachineVersionInfo{Kubelet: "1.10.3"},
5864
},
@@ -94,9 +100,58 @@ func TestReconcile(t *testing.T) {
94100
g.Expect(c.Delete(context.TODO(), &ms)).NotTo(gomega.HaveOccurred())
95101
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))
96102
g.Eventually(errors, timeout).Should(gomega.Receive(gomega.BeNil()))
103+
g.Eventually(func() int {
104+
if err := c.List(context.TODO(), &client.ListOptions{}, machineSets); err != nil {
105+
return -1
106+
}
107+
return len(machineSets.Items)
108+
}, timeout).Should(gomega.BeEquivalentTo(1))
109+
110+
// Scale a MachineDeployment and expect Reconcile to be called
111+
err = updateMachineDeployment(c, instance, func(d *clusterv1alpha1.MachineDeployment) { d.Spec.Replicas = int32Ptr(5) })
112+
g.Expect(err).NotTo(gomega.HaveOccurred())
113+
g.Expect(c.Update(context.TODO(), instance)).NotTo(gomega.HaveOccurred())
114+
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))
115+
g.Eventually(errors, timeout).Should(gomega.Receive(gomega.BeNil()))
116+
g.Eventually(func() int32 {
117+
if err := c.List(context.TODO(), &client.ListOptions{}, machineSets); err != nil {
118+
return -1
119+
}
120+
if len(machineSets.Items) != 1 {
121+
return -1
122+
}
123+
return *machineSets.Items[0].Spec.Replicas
124+
}, timeout).Should(gomega.BeEquivalentTo(5))
125+
126+
// Update a MachineDeployment, expect Reconsile to be called and a new MachineSet to appear
127+
err = updateMachineDeployment(c, instance, func(d *clusterv1alpha1.MachineDeployment) { d.Spec.Template.Labels["updated"] = "true" })
128+
g.Expect(err).NotTo(gomega.HaveOccurred())
129+
g.Expect(c.Update(context.TODO(), instance)).NotTo(gomega.HaveOccurred())
130+
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))
131+
g.Eventually(errors, timeout).Should(gomega.Receive(gomega.BeNil()))
132+
g.Eventually(func() int {
133+
if err := c.List(context.TODO(), &client.ListOptions{}, machineSets); err != nil {
134+
return -1
135+
}
136+
return len(machineSets.Items)
137+
}, timeout).Should(gomega.BeEquivalentTo(2))
97138

98139
}
99140

141+
func updateMachineDeployment(c client.Client, d *clusterv1alpha1.MachineDeployment, modify func(*clusterv1alpha1.MachineDeployment)) error {
142+
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
143+
//Get latest version from API
144+
if err := c.Get(context.Background(), types.NamespacedName{Namespace: d.Namespace, Name: d.Name}, d); err != nil {
145+
return err
146+
}
147+
// Apply modifications
148+
modify(d)
149+
// Update the machineDeployment
150+
return c.Update(context.Background(), d)
151+
})
152+
return err
153+
}
154+
100155
func int32Ptr(i int32) *int32 {
101156
return &i
102157
}

0 commit comments

Comments
 (0)