Skip to content

Commit f8b3307

Browse files
committed
Fix conversion of v1alpha6 PortOpts
v1alpha6 PortOpts conversion was missing many fields, but this was hidden in the unit tests because the v1alpha6 restore function for the machine spec unconditionally restored all Ports. This change turns the unconditional restore of all ports in the machine spec into individual conversion of each port, then fixes all the resulting test failures by adding individual conversions and restores as required.
1 parent 8fc78f1 commit f8b3307

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

api/v1alpha6/openstackmachine_conversion.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ func restorev1alpha6MachineSpec(previous *OpenStackMachineSpec, dst *OpenStackMa
184184

185185
func restorev1beta1MachineSpec(previous *infrav1.OpenStackMachineSpec, dst *infrav1.OpenStackMachineSpec) {
186186
// PropagateUplinkStatus has been added in v1beta1.
187-
// We restore the whole Ports since they are anyway immutable.
188-
dst.Ports = previous.Ports
189187
dst.AdditionalBlockDevices = previous.AdditionalBlockDevices
190188
dst.ServerGroup = previous.ServerGroup
191189
dst.Image = previous.Image
@@ -198,6 +196,12 @@ func restorev1beta1MachineSpec(previous *infrav1.OpenStackMachineSpec, dst *infr
198196
}
199197
}
200198

199+
if len(dst.Ports) == len(previous.Ports) {
200+
for i := range dst.Ports {
201+
restorev1beta1Port(&previous.Ports[i], &dst.Ports[i])
202+
}
203+
}
204+
201205
if dst.RootVolume != nil && previous.RootVolume != nil {
202206
restorev1beta1BlockDeviceVolume(
203207
&previous.RootVolume.BlockDeviceVolume,

api/v1alpha6/types_conversion.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,94 @@ func restorev1alpha6Port(previous *PortOpts, dst *PortOpts) {
330330
}
331331
}
332332

333+
func restorev1beta1Port(previous *infrav1.PortOpts, dst *infrav1.PortOpts) {
334+
// PropagateUplinkStatus was not present in v1alpha6
335+
dst.PropagateUplinkStatus = previous.PropagateUplinkStatus
336+
337+
optional.RestoreString(&previous.NameSuffix, &dst.NameSuffix)
338+
optional.RestoreString(&previous.Description, &dst.Description)
339+
optional.RestoreString(&previous.MACAddress, &dst.MACAddress)
340+
341+
if len(dst.FixedIPs) == len(previous.FixedIPs) {
342+
for j := range dst.FixedIPs {
343+
prevFixedIP := &previous.FixedIPs[j]
344+
dstFixedIP := &dst.FixedIPs[j]
345+
346+
optional.RestoreString(&prevFixedIP.IPAddress, &dstFixedIP.IPAddress)
347+
restorev1beta1SubnetParam(prevFixedIP.Subnet, dstFixedIP.Subnet)
348+
}
349+
}
350+
351+
if len(dst.AllowedAddressPairs) == len(previous.AllowedAddressPairs) {
352+
for j := range dst.AllowedAddressPairs {
353+
prevAAP := &previous.AllowedAddressPairs[j]
354+
dstAAP := &dst.AllowedAddressPairs[j]
355+
356+
optional.RestoreString(&prevAAP.MACAddress, &dstAAP.MACAddress)
357+
}
358+
}
359+
360+
optional.RestoreString(&previous.HostID, &dst.HostID)
361+
optional.RestoreString(&previous.VNICType, &dst.VNICType)
362+
363+
if previous.Profile != nil {
364+
// A binding profile of {&false, &false} will be converted to a nil map.
365+
// We still need to restore it, so substitute an empty BindProfile.
366+
var dstProfile *infrav1.BindingProfile
367+
if dst.Profile != nil {
368+
dstProfile = dst.Profile
369+
} else {
370+
dstProfile = &infrav1.BindingProfile{}
371+
dst.Profile = dstProfile
372+
}
373+
prevProfile := previous.Profile
374+
375+
if dstProfile.OVSHWOffload == nil || !*dstProfile.OVSHWOffload {
376+
dstProfile.OVSHWOffload = prevProfile.OVSHWOffload
377+
}
378+
379+
if dstProfile.TrustedVF == nil || !*dstProfile.TrustedVF {
380+
dstProfile.TrustedVF = prevProfile.TrustedVF
381+
}
382+
}
383+
}
384+
333385
func Convert_v1alpha6_PortOpts_To_v1beta1_PortOpts(in *PortOpts, out *infrav1.PortOpts, s apiconversion.Scope) error {
334386
if err := autoConvert_v1alpha6_PortOpts_To_v1beta1_PortOpts(in, out, s); err != nil {
335387
return err
336388
}
337389

390+
// Copy members of ResolvedPortSpecFields
391+
var allowedAddressPairs []infrav1.AddressPair
392+
if len(in.AllowedAddressPairs) > 0 {
393+
allowedAddressPairs = make([]infrav1.AddressPair, len(in.AllowedAddressPairs))
394+
for i := range in.AllowedAddressPairs {
395+
aap := &in.AllowedAddressPairs[i]
396+
allowedAddressPairs[i] = infrav1.AddressPair{
397+
MACAddress: &aap.MACAddress,
398+
IPAddress: aap.IPAddress,
399+
}
400+
}
401+
}
402+
var valueSpecs []infrav1.ValueSpec
403+
if len(in.ValueSpecs) > 0 {
404+
valueSpecs = make([]infrav1.ValueSpec, len(in.ValueSpecs))
405+
for i, vs := range in.ValueSpecs {
406+
valueSpecs[i] = infrav1.ValueSpec(vs)
407+
}
408+
}
409+
out.AdminStateUp = in.AdminStateUp
410+
out.AllowedAddressPairs = allowedAddressPairs
411+
out.DisablePortSecurity = in.DisablePortSecurity
412+
out.ValueSpecs = valueSpecs
413+
if err := errors.Join(
414+
optional.Convert_string_To_optional_String(&in.MACAddress, &out.MACAddress, s),
415+
optional.Convert_string_To_optional_String(&in.HostID, &out.HostID, s),
416+
optional.Convert_string_To_optional_String(&in.VNICType, &out.VNICType, s),
417+
); err != nil {
418+
return err
419+
}
420+
338421
if len(in.SecurityGroups) > 0 || len(in.SecurityGroupFilters) > 0 {
339422
out.SecurityGroups = make([]infrav1.SecurityGroupParam, len(in.SecurityGroups)+len(in.SecurityGroupFilters))
340423
for i := range in.SecurityGroupFilters {
@@ -373,6 +456,38 @@ func Convert_v1beta1_PortOpts_To_v1alpha6_PortOpts(in *infrav1.PortOpts, out *Po
373456
return err
374457
}
375458

459+
// Copy members of ResolvedPortSpecFields
460+
var allowedAddressPairs []AddressPair
461+
if len(in.AllowedAddressPairs) > 0 {
462+
allowedAddressPairs = make([]AddressPair, len(in.AllowedAddressPairs))
463+
for i := range in.AllowedAddressPairs {
464+
inAAP := &in.AllowedAddressPairs[i]
465+
outAAP := &allowedAddressPairs[i]
466+
if err := optional.Convert_optional_String_To_string(&inAAP.MACAddress, &outAAP.MACAddress, s); err != nil {
467+
return err
468+
}
469+
outAAP.IPAddress = inAAP.IPAddress
470+
}
471+
}
472+
var valueSpecs []ValueSpec
473+
if len(in.ValueSpecs) > 0 {
474+
valueSpecs = make([]ValueSpec, len(in.ValueSpecs))
475+
for i, vs := range in.ValueSpecs {
476+
valueSpecs[i] = ValueSpec(vs)
477+
}
478+
}
479+
out.AdminStateUp = in.AdminStateUp
480+
out.AllowedAddressPairs = allowedAddressPairs
481+
out.DisablePortSecurity = in.DisablePortSecurity
482+
out.ValueSpecs = valueSpecs
483+
if err := errors.Join(
484+
optional.Convert_optional_String_To_string(&in.MACAddress, &out.MACAddress, s),
485+
optional.Convert_optional_String_To_string(&in.HostID, &out.HostID, s),
486+
optional.Convert_optional_String_To_string(&in.VNICType, &out.VNICType, s),
487+
); err != nil {
488+
return err
489+
}
490+
376491
// The auto-generated function converts v1beta1 SecurityGroup to
377492
// v1alpha6 SecurityGroup, but v1alpha6 SecurityGroupFilter is more
378493
// appropriate. Unset them and convert to SecurityGroupFilter instead.

0 commit comments

Comments
 (0)