@@ -86,6 +86,12 @@ func restorev1alpha6MachineSpec(previous *OpenStackMachineSpec, dst *OpenStackMa
86
86
// Subnet is removed from v1alpha7 with no replacement, so can't be
87
87
// losslessly converted. Restore the previously stored value on down-conversion.
88
88
dst .Subnet = previous .Subnet
89
+
90
+ // Strictly speaking this is lossy because we lose changes in
91
+ // down-conversion which were made to the up-converted object. However
92
+ // it isn't worth implementing this as the fields are immutable.
93
+ dst .Networks = previous .Networks
94
+ dst .Ports = previous .Ports
89
95
}
90
96
91
97
func restorev1alpha7MachineSpec (previous * infrav1.OpenStackMachineSpec , dst * infrav1.OpenStackMachineSpec ) {
@@ -288,7 +294,80 @@ func (r *OpenStackMachineTemplateList) ConvertFrom(srcRaw ctrlconversion.Hub) er
288
294
}
289
295
290
296
func Convert_v1alpha6_OpenStackMachineSpec_To_v1alpha7_OpenStackMachineSpec (in * OpenStackMachineSpec , out * infrav1.OpenStackMachineSpec , s conversion.Scope ) error {
291
- return autoConvert_v1alpha6_OpenStackMachineSpec_To_v1alpha7_OpenStackMachineSpec (in , out , s )
297
+ err := autoConvert_v1alpha6_OpenStackMachineSpec_To_v1alpha7_OpenStackMachineSpec (in , out , s )
298
+ if err != nil {
299
+ return err
300
+ }
301
+
302
+ if len (in .Networks ) > 0 {
303
+ ports := convertNetworksToPorts (in .Networks )
304
+ // Networks were previously created first, so need to come before ports
305
+ out .Ports = append (ports , out .Ports ... )
306
+ }
307
+ return nil
308
+ }
309
+
310
+ func convertNetworksToPorts (networks []NetworkParam ) []infrav1.PortOpts {
311
+ var ports []infrav1.PortOpts
312
+
313
+ for _ , network := range networks {
314
+ // This will remain null if the network is not specified in NetworkParam
315
+ var networkFilter * infrav1.NetworkFilter
316
+
317
+ // In v1alpha6, if network.Filter resolved to multiple networks
318
+ // then we would add multiple ports. It is not possible to
319
+ // support this behaviour during k8s API conversion as it
320
+ // requires an OpenStack API call. A network filter returning
321
+ // multiple networks now becomes an error when we attempt to
322
+ // create the port.
323
+ switch {
324
+ case network .UUID != "" :
325
+ networkFilter = & infrav1.NetworkFilter {
326
+ ID : network .UUID ,
327
+ }
328
+ case network .Filter != (NetworkFilter {}):
329
+ networkFilter = (* infrav1 .NetworkFilter )(& network .Filter )
330
+ }
331
+
332
+ // Note that network.FixedIP was unused in v1alpha6 so we also ignore it here.
333
+
334
+ // In v1alpha6, specifying multiple subnets created multiple
335
+ // ports. We maintain this behaviour in conversion by adding
336
+ // multiple portOpts in this case.
337
+ //
338
+ // Also, similar to network.Filter above, if a subnet filter
339
+ // resolved to multiple subnets then we would add a port for
340
+ // each subnet. Again, it is not possible to support this
341
+ // behaviour during k8s API conversion as it requires an
342
+ // OpenStack API call. A subnet filter returning multiple
343
+ // subnets now becomes an error when we attempt to create the
344
+ // port.
345
+ if len (network .Subnets ) == 0 {
346
+ // If the network has no explicit subnets then we create a single port with no subnets.
347
+ ports = append (ports , infrav1.PortOpts {Network : networkFilter })
348
+ } else {
349
+ // If the network has explicit subnets then we create a separate port for each subnet.
350
+ for _ , subnet := range network .Subnets {
351
+ if subnet .UUID != "" {
352
+ ports = append (ports , infrav1.PortOpts {
353
+ Network : networkFilter ,
354
+ FixedIPs : []infrav1.FixedIP {
355
+ {Subnet : & infrav1.SubnetFilter {ID : subnet .UUID }},
356
+ },
357
+ })
358
+ } else {
359
+ ports = append (ports , infrav1.PortOpts {
360
+ Network : networkFilter ,
361
+ FixedIPs : []infrav1.FixedIP {
362
+ {Subnet : (* infrav1 .SubnetFilter )(& subnet .Filter )},
363
+ },
364
+ })
365
+ }
366
+ }
367
+ }
368
+ }
369
+
370
+ return ports
292
371
}
293
372
294
373
func Convert_v1alpha7_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec (in * infrav1.OpenStackClusterSpec , out * OpenStackClusterSpec , s conversion.Scope ) error {
0 commit comments