@@ -1013,16 +1013,25 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) {
1013
1013
}
1014
1014
1015
1015
func TestReconcileMachinePoolMachines (t * testing.T ) {
1016
+
1017
+ defaultCluster := & clusterv1.Cluster {
1018
+ ObjectMeta : metav1.ObjectMeta {
1019
+ Name : clusterName ,
1020
+ Namespace : metav1 .NamespaceDefault ,
1021
+ },
1022
+ }
1023
+
1016
1024
defaultMachinePool := expv1.MachinePool {
1017
1025
ObjectMeta : metav1.ObjectMeta {
1018
1026
Name : "machinepool-test" ,
1019
1027
Namespace : metav1 .NamespaceDefault ,
1020
1028
Labels : map [string ]string {
1021
- clusterv1 .ClusterNameLabel : clusterName ,
1029
+ clusterv1 .ClusterNameLabel : defaultCluster . Name ,
1022
1030
},
1023
1031
},
1024
1032
Spec : expv1.MachinePoolSpec {
1025
- Replicas : pointer .Int32 (1 ),
1033
+ ClusterName : defaultCluster .Name ,
1034
+ Replicas : pointer .Int32 (1 ),
1026
1035
Template : clusterv1.MachineTemplateSpec {
1027
1036
Spec : clusterv1.MachineSpec {
1028
1037
Bootstrap : clusterv1.Bootstrap {
@@ -1050,7 +1059,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1050
1059
"name" : "infra-machine1" ,
1051
1060
"namespace" : metav1 .NamespaceDefault ,
1052
1061
"labels" : map [string ]interface {}{
1053
- clusterv1 .ClusterNameLabel : clusterName ,
1062
+ clusterv1 .ClusterNameLabel : defaultCluster . Name ,
1054
1063
expv1 .MachinePoolNameLabel : "machinepool-test" ,
1055
1064
clusterv1 .MachinePoolOwnedLabel : "true" ,
1056
1065
},
@@ -1066,7 +1075,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1066
1075
"name" : "infra-machine2" ,
1067
1076
"namespace" : metav1 .NamespaceDefault ,
1068
1077
"labels" : map [string ]interface {}{
1069
- clusterv1 .ClusterNameLabel : clusterName ,
1078
+ clusterv1 .ClusterNameLabel : defaultCluster . Name ,
1070
1079
expv1 .MachinePoolNameLabel : "machinepool-test" ,
1071
1080
clusterv1 .MachinePoolOwnedLabel : "true" ,
1072
1081
},
@@ -1078,8 +1087,14 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1078
1087
ObjectMeta : metav1.ObjectMeta {
1079
1088
Name : "machine1" ,
1080
1089
Namespace : metav1 .NamespaceDefault ,
1090
+ Labels : map [string ]string {
1091
+ clusterv1 .ClusterNameLabel : defaultCluster .Name ,
1092
+ expv1 .MachinePoolNameLabel : "machinepool-test" ,
1093
+ clusterv1 .MachinePoolOwnedLabel : "true" ,
1094
+ },
1081
1095
},
1082
1096
Spec : clusterv1.MachineSpec {
1097
+ ClusterName : clusterName ,
1083
1098
InfrastructureRef : corev1.ObjectReference {
1084
1099
APIVersion : "infrastructure.cluster.x-k8s.io/v1beta1" ,
1085
1100
Kind : "InfrastructureMachine" ,
@@ -1091,10 +1106,16 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1091
1106
1092
1107
machine2 := clusterv1.Machine {
1093
1108
ObjectMeta : metav1.ObjectMeta {
1094
- Name : "machine1 " ,
1109
+ Name : "machine2 " ,
1095
1110
Namespace : metav1 .NamespaceDefault ,
1111
+ Labels : map [string ]string {
1112
+ clusterv1 .ClusterNameLabel : defaultCluster .Name ,
1113
+ expv1 .MachinePoolNameLabel : "machinepool-test" ,
1114
+ clusterv1 .MachinePoolOwnedLabel : "true" ,
1115
+ },
1096
1116
},
1097
1117
Spec : clusterv1.MachineSpec {
1118
+ ClusterName : clusterName ,
1098
1119
InfrastructureRef : corev1.ObjectReference {
1099
1120
APIVersion : "infrastructure.cluster.x-k8s.io/v1beta1" ,
1100
1121
Kind : "InfrastructureMachine" ,
@@ -1104,23 +1125,15 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1104
1125
},
1105
1126
}
1106
1127
1107
- defaultCluster := & clusterv1.Cluster {
1108
- ObjectMeta : metav1.ObjectMeta {
1109
- Name : clusterName ,
1110
- Namespace : metav1 .NamespaceDefault ,
1111
- },
1112
- }
1113
-
1114
1128
testCases := []struct {
1115
- name string
1116
- bootstrapConfig map [string ]interface {}
1117
- infraConfig map [string ]interface {}
1118
- machinepool * expv1.MachinePool
1119
- infraMachines []unstructured.Unstructured
1120
- machines []clusterv1.Machine
1121
- expectedMachines []clusterv1.Machine
1122
- expectError bool
1123
- expected func (g * WithT , m * expv1.MachinePool )
1129
+ name string
1130
+ bootstrapConfig map [string ]interface {}
1131
+ infraConfig map [string ]interface {}
1132
+ machinepool * expv1.MachinePool
1133
+ infraMachines []unstructured.Unstructured
1134
+ machines []clusterv1.Machine
1135
+ expectError bool
1136
+ expected func (g * WithT , m * expv1.MachinePool )
1124
1137
}{
1125
1138
{
1126
1139
name : "create two machinepool machines" ,
@@ -1163,25 +1176,105 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1163
1176
* infraMachine1 ,
1164
1177
* infraMachine2 ,
1165
1178
},
1166
- expectError : false ,
1167
- expectedMachines : []clusterv1.Machine {},
1179
+ expectError : false ,
1168
1180
expected : func (g * WithT , m * expv1.MachinePool ) {
1169
- g .Expect (m .Status .InfrastructureReady ).To (BeTrue ())
1181
+ g .Expect (env .Create (ctx , & machine1 )).To (Succeed ())
1182
+ g .Expect (env .Create (ctx , & machine2 )).To (Succeed ())
1183
+ },
1184
+ },
1185
+ {
1186
+ name : "machinepool machines already exist, nothing to do" ,
1187
+ infraConfig : map [string ]interface {}{
1188
+ "kind" : "InfrastructureConfig" ,
1189
+ "apiVersion" : "infrastructure.cluster.x-k8s.io/v1beta1" ,
1190
+ "metadata" : map [string ]interface {}{
1191
+ "name" : "infra-config1" ,
1192
+ "namespace" : metav1 .NamespaceDefault ,
1193
+ },
1194
+ "spec" : map [string ]interface {}{
1195
+ "providerIDList" : []interface {}{
1196
+ "test://id-1" ,
1197
+ },
1198
+ },
1199
+ "status" : map [string ]interface {}{
1200
+ "infrastructureMachineKind" : "InfrastructureMachine" ,
1201
+ "infrastructureMachineSelector" : map [string ]interface {}{
1202
+ "matchLabels" : map [string ]interface {}{
1203
+ // "cluster.x-k8s.io/cluster-name": clusterName,
1204
+ // "cluster.x-k8s.io/machinepool-owned": "true",
1205
+ "cluster.x-k8s.io/pool-name" : "machinepool-test" ,
1206
+ },
1207
+ },
1208
+ "ready" : true ,
1209
+ "addresses" : []interface {}{
1210
+ map [string ]interface {}{
1211
+ "type" : "InternalIP" ,
1212
+ "address" : "10.0.0.1" ,
1213
+ },
1214
+ map [string ]interface {}{
1215
+ "type" : "InternalIP" ,
1216
+ "address" : "10.0.0.2" ,
1217
+ },
1218
+ },
1219
+ },
1220
+ },
1221
+ machines : []clusterv1.Machine {},
1222
+ infraMachines : []unstructured.Unstructured {
1223
+ * infraMachine1 ,
1224
+ * infraMachine2 ,
1225
+ },
1226
+ expectError : false ,
1227
+ expected : func (g * WithT , m * expv1.MachinePool ) {
1228
+ },
1229
+ },
1230
+ {
1231
+ name : "delete dangling machinepool machine" ,
1232
+ infraConfig : map [string ]interface {}{
1233
+ "kind" : "InfrastructureConfig" ,
1234
+ "apiVersion" : "infrastructure.cluster.x-k8s.io/v1beta1" ,
1235
+ "metadata" : map [string ]interface {}{
1236
+ "name" : "infra-config1" ,
1237
+ "namespace" : metav1 .NamespaceDefault ,
1238
+ },
1239
+ "spec" : map [string ]interface {}{
1240
+ "providerIDList" : []interface {}{
1241
+ "test://id-1" ,
1242
+ },
1243
+ },
1244
+ "status" : map [string ]interface {}{
1245
+ "infrastructureMachineKind" : "InfrastructureMachine" ,
1246
+ "infrastructureMachineSelector" : map [string ]interface {}{
1247
+ "matchLabels" : map [string ]interface {}{
1248
+ // "cluster.x-k8s.io/cluster-name": clusterName,
1249
+ // "cluster.x-k8s.io/machinepool-owned": "true",
1250
+ "cluster.x-k8s.io/pool-name" : "machinepool-test" ,
1251
+ },
1252
+ },
1253
+ "ready" : true ,
1254
+ "addresses" : []interface {}{
1255
+ map [string ]interface {}{
1256
+ "type" : "InternalIP" ,
1257
+ "address" : "10.0.0.1" ,
1258
+ },
1259
+ map [string ]interface {}{
1260
+ "type" : "InternalIP" ,
1261
+ "address" : "10.0.0.2" ,
1262
+ },
1263
+ },
1264
+ },
1265
+ },
1266
+ machines : []clusterv1.Machine {
1267
+ machine1 ,
1268
+ machine2 ,
1269
+ },
1270
+ infraMachines : []unstructured.Unstructured {
1271
+ * infraMachine1 ,
1272
+ },
1273
+ expectError : false ,
1274
+ expected : func (g * WithT , m * expv1.MachinePool ) {
1275
+ g .Expect (env .Delete (ctx , & machine2 )).To (Succeed ())
1170
1276
},
1171
1277
},
1172
-
1173
- // expectError: false,
1174
- // expectRequeueAfter: false,
1175
- // expected: func(g *WithT, m *expv1.MachinePool) {
1176
- // g.Expect(m.Status.InfrastructureReady).To(BeTrue())
1177
- // g.Expect(m.Status.ReadyReplicas).To(Equal(int32(0)))
1178
- // g.Expect(m.Status.AvailableReplicas).To(Equal(int32(0)))
1179
- // g.Expect(m.Status.UnavailableReplicas).To(Equal(int32(0)))
1180
- // g.Expect(m.Status.FailureMessage).To(BeNil())
1181
- // g.Expect(m.Status.FailureReason).To(BeNil())
1182
- // g.Expect(m.Status.GetTypedPhase()).To(Equal(expv1.MachinePoolPhaseRunning))
1183
- // },
1184
- // },
1185
1278
}
1186
1279
1187
1280
for _ , tc := range testCases {
@@ -1197,13 +1290,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) {
1197
1290
Client : fake .NewClientBuilder ().WithObjects (tc .machinepool , infraConfig ).Build (),
1198
1291
}
1199
1292
1200
- res , err := r .reconcileInfrastructure (ctx , defaultCluster , tc .machinepool )
1201
- if tc .expectRequeueAfter {
1202
- g .Expect (res .RequeueAfter ).To (BeNumerically (">=" , 0 ))
1203
- } else {
1204
- g .Expect (res .RequeueAfter ).To (Equal (time .Duration (0 )))
1205
- }
1206
- r .reconcilePhase (tc .machinepool )
1293
+ err := r .reconcileMachinePoolMachines (ctx , tc .machinepool , infraConfig )
1207
1294
if tc .expectError {
1208
1295
g .Expect (err ).ToNot (BeNil ())
1209
1296
} else {
0 commit comments