Skip to content

Commit

Permalink
Store []ResolvedPortSpec in ReferencedMachineResources
Browse files Browse the repository at this point in the history
The purpose of this change is fix an issue where we are storing
unresolved references in ReferencedMachineResources. Specifically we are
storing a PortOpts, which is a user-intent struct. PortOpts can contain
unresolved references to both subnets and security groups, as well
fields requiring additional processing which reference external objects:
the port name, description, and tags.

We create a new type, ResolvedPortSpec, which can contain only fully
resolved data. This can be seen in the new signature of CreatePorts(),
which no longer requires any source of data other than the
[]ResolvedPortSpec from ReferencedMachineResources, and is now greatly
simplified.

Fully resolving the port name also allows a simplification in port
adoption.

All of the complexity now moves to ConstructPorts(), which is updated to
return []ResolvedPortSpec instead of []PortOpts. ConstructPorts() is
updated to resolve security groups, port name, description, and all
subnets referenced in FixedIPs.
  • Loading branch information
mdbooth committed Mar 20, 2024
1 parent 27b3cef commit 51b34bd
Show file tree
Hide file tree
Showing 29 changed files with 1,838 additions and 2,170 deletions.
60 changes: 10 additions & 50 deletions api/v1alpha5/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions api/v1alpha6/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ func TestPortOptsConvertTo(t *testing.T) {
SecurityGroups: uuids,
}},
hubPortOpts: []infrav1.PortOpts{{
Profile: &convertedPortProfile,
ResolvedPortSpecFields: infrav1.ResolvedPortSpecFields{
Profile: &convertedPortProfile,
},
SecurityGroups: securityGroupsUuids,
}},
},
Expand All @@ -582,7 +584,9 @@ func TestPortOptsConvertTo(t *testing.T) {
SecurityGroupFilters: securityGroupFilter,
}},
hubPortOpts: []infrav1.PortOpts{{
Profile: &convertedPortProfile,
ResolvedPortSpecFields: infrav1.ResolvedPortSpecFields{
Profile: &convertedPortProfile,
},
SecurityGroups: securityGroupFilterMerged,
}},
},
Expand Down
62 changes: 11 additions & 51 deletions api/v1alpha6/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions api/v1alpha7/types_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha7

import (
"errors"

apiconversion "k8s.io/apimachinery/pkg/conversion"
"k8s.io/utils/pointer"

Expand Down Expand Up @@ -253,6 +255,38 @@ func Convert_v1alpha7_PortOpts_To_v1beta1_PortOpts(in *PortOpts, out *infrav1.Po
return err
}

// Copy members of ResolvedPortSpecFields
var allowedAddressPairs []infrav1.AddressPair
if len(in.AllowedAddressPairs) > 0 {
allowedAddressPairs = make([]infrav1.AddressPair, len(in.AllowedAddressPairs))
for i := range in.AllowedAddressPairs {
aap := &in.AllowedAddressPairs[i]
allowedAddressPairs[i] = infrav1.AddressPair{
MACAddress: &aap.MACAddress,
IPAddress: aap.IPAddress,
}
}
}
var valueSpecs []infrav1.ValueSpec
if len(in.ValueSpecs) > 0 {
valueSpecs = make([]infrav1.ValueSpec, len(in.ValueSpecs))
for i, vs := range in.ValueSpecs {
valueSpecs[i] = infrav1.ValueSpec(vs)
}
}
out.AdminStateUp = in.AdminStateUp
out.AllowedAddressPairs = allowedAddressPairs
out.DisablePortSecurity = in.DisablePortSecurity
out.PropagateUplinkStatus = in.PropagateUplinkStatus
out.ValueSpecs = valueSpecs
if err := errors.Join(
optional.Convert_string_To_optional_String(&in.MACAddress, &out.MACAddress, s),
optional.Convert_string_To_optional_String(&in.HostID, &out.HostID, s),
optional.Convert_string_To_optional_String(&in.VNICType, &out.VNICType, s),
); err != nil {
return err
}

if len(in.SecurityGroupFilters) > 0 {
out.SecurityGroups = make([]infrav1.SecurityGroupFilter, len(in.SecurityGroupFilters))
for i := range in.SecurityGroupFilters {
Expand All @@ -277,6 +311,39 @@ func Convert_v1beta1_PortOpts_To_v1alpha7_PortOpts(in *infrav1.PortOpts, out *Po
return err
}

// Copy members of ResolvedPortSpecFields
var allowedAddressPairs []AddressPair
if len(in.AllowedAddressPairs) > 0 {
allowedAddressPairs = make([]AddressPair, len(in.AllowedAddressPairs))
for i := range in.AllowedAddressPairs {
inAAP := &in.AllowedAddressPairs[i]
outAAP := &allowedAddressPairs[i]
if err := optional.Convert_optional_String_To_string(&inAAP.MACAddress, &outAAP.MACAddress, s); err != nil {
return err
}
outAAP.IPAddress = inAAP.IPAddress
}
}
var valueSpecs []ValueSpec
if len(in.ValueSpecs) > 0 {
valueSpecs = make([]ValueSpec, len(in.ValueSpecs))
for i, vs := range in.ValueSpecs {
valueSpecs[i] = ValueSpec(vs)
}
}
out.AdminStateUp = in.AdminStateUp
out.AllowedAddressPairs = allowedAddressPairs
out.DisablePortSecurity = in.DisablePortSecurity
out.PropagateUplinkStatus = in.PropagateUplinkStatus
out.ValueSpecs = valueSpecs
if err := errors.Join(
optional.Convert_optional_String_To_string(&in.MACAddress, &out.MACAddress, s),
optional.Convert_optional_String_To_string(&in.HostID, &out.HostID, s),
optional.Convert_optional_String_To_string(&in.VNICType, &out.VNICType, s),
); err != nil {
return err
}

if len(in.SecurityGroups) > 0 {
out.SecurityGroupFilters = make([]SecurityGroupFilter, len(in.SecurityGroups))
for i := range in.SecurityGroups {
Expand Down
64 changes: 12 additions & 52 deletions api/v1alpha7/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 51b34bd

Please sign in to comment.