@@ -25,6 +25,7 @@ import (
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
26
"k8s.io/apimachinery/pkg/types"
27
27
"k8s.io/apimachinery/pkg/util/intstr"
28
+ "k8s.io/client-go/util/retry"
28
29
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
29
30
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
30
31
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -40,11 +41,13 @@ const timeout = time.Second * 5
40
41
41
42
func TestReconcile (t * testing.T ) {
42
43
g := gomega .NewGomegaWithT (t )
44
+ labels := map [string ]string {"foo" : "bar" }
43
45
instance := & clusterv1alpha1.MachineDeployment {
44
46
ObjectMeta : metav1.ObjectMeta {Name : "foo" , Namespace : "default" },
45
47
Spec : clusterv1alpha1.MachineDeploymentSpec {
46
48
MinReadySeconds : int32Ptr (0 ),
47
49
Replicas : int32Ptr (2 ),
50
+ Selector : metav1.LabelSelector {MatchLabels : labels },
48
51
Strategy : clusterv1alpha1.MachineDeploymentStrategy {
49
52
Type : common .RollingUpdateMachineDeploymentStrategyType ,
50
53
RollingUpdate : & clusterv1alpha1.MachineRollingUpdateDeployment {
@@ -53,6 +56,9 @@ func TestReconcile(t *testing.T) {
53
56
},
54
57
},
55
58
Template : clusterv1alpha1.MachineTemplateSpec {
59
+ ObjectMeta : metav1.ObjectMeta {
60
+ Labels : labels ,
61
+ },
56
62
Spec : clusterv1alpha1.MachineSpec {
57
63
Versions : clusterv1alpha1.MachineVersionInfo {Kubelet : "1.10.3" },
58
64
},
@@ -94,9 +100,58 @@ func TestReconcile(t *testing.T) {
94
100
g .Expect (c .Delete (context .TODO (), & ms )).NotTo (gomega .HaveOccurred ())
95
101
g .Eventually (requests , timeout ).Should (gomega .Receive (gomega .Equal (expectedRequest )))
96
102
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 ))
97
138
98
139
}
99
140
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
+
100
155
func int32Ptr (i int32 ) * int32 {
101
156
return & i
102
157
}
0 commit comments