Skip to content

Commit

Permalink
NetworkFilter to NetworkParam
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbooth committed Mar 26, 2024
1 parent 59689c9 commit 14fe5d4
Show file tree
Hide file tree
Showing 31 changed files with 1,331 additions and 896 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/openstackfloatingippool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type OpenStackFloatingIPPoolSpec struct {

// FloatingIPNetwork is the external network to use for floating ips, if there's only one external network it will be used by default
// +optional
FloatingIPNetwork infrav1.NetworkFilter `json:"floatingIPNetwork"`
FloatingIPNetwork infrav1.NetworkParam `json:"floatingIPNetwork"`

// The stratergy to use for reclaiming floating ips when they are released from a machine
// +kubebuilder:validation:Optional
Expand Down
54 changes: 43 additions & 11 deletions api/v1alpha5/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha5

import (
"errors"
"strings"

conversion "k8s.io/apimachinery/pkg/conversion"
Expand Down Expand Up @@ -205,8 +206,8 @@ func Convert_v1beta1_OpenStackClusterSpec_To_v1alpha5_OpenStackClusterSpec(in *i
return err
}

if in.ExternalNetwork != nil && in.ExternalNetwork.ID != "" {
out.ExternalNetworkID = in.ExternalNetwork.ID
if in.ExternalNetwork != nil && in.ExternalNetwork.ID != nil {
out.ExternalNetworkID = *in.ExternalNetwork.ID
}

if len(in.ManagedSubnets) > 0 {
Expand Down Expand Up @@ -246,8 +247,8 @@ func Convert_v1alpha5_OpenStackClusterSpec_To_v1beta1_OpenStackClusterSpec(in *O
}

if in.ExternalNetworkID != "" {
out.ExternalNetwork = &infrav1.NetworkFilter{
ID: in.ExternalNetworkID,
out.ExternalNetwork = &infrav1.NetworkParam{
ID: &in.ExternalNetworkID,
}
}

Expand Down Expand Up @@ -746,18 +747,49 @@ func Convert_v1beta1_SecurityGroupFilter_To_v1alpha5_SecurityGroupFilter(in *inf
return nil
}

func Convert_v1alpha5_NetworkFilter_To_v1beta1_NetworkFilter(in *NetworkFilter, out *infrav1.NetworkFilter, s conversion.Scope) error {
if err := autoConvert_v1alpha5_NetworkFilter_To_v1beta1_NetworkFilter(in, out, s); err != nil {
func Convert_v1alpha5_NetworkFilter_To_v1beta1_NetworkParam(in *NetworkFilter, out *infrav1.NetworkParam, s conversion.Scope) error {
if in.ID != "" {
out.ID = &in.ID
return nil
}
outFilter := &infrav1.NetworkFilter{}
if err := autoConvert_v1alpha5_NetworkFilter_To_v1beta1_NetworkFilter(in, outFilter, s); err != nil {
return err
}
infrav1.ConvertAllTagsTo(in.Tags, in.TagsAny, in.NotTags, in.NotTagsAny, &out.FilterByNeutronTags)
infrav1.ConvertAllTagsTo(in.Tags, in.TagsAny, in.NotTags, in.NotTagsAny, &outFilter.FilterByNeutronTags)
if !outFilter.IsZero() {
out.Filter = outFilter
}
return nil
}

func Convert_v1beta1_NetworkFilter_To_v1alpha5_NetworkFilter(in *infrav1.NetworkFilter, out *NetworkFilter, s conversion.Scope) error {
if err := autoConvert_v1beta1_NetworkFilter_To_v1alpha5_NetworkFilter(in, out, s); err != nil {
return err
func Convert_v1beta1_NetworkParam_To_v1alpha5_NetworkFilter(in *infrav1.NetworkParam, out *NetworkFilter, s conversion.Scope) error {
if in.ID != nil {
out.ID = *in.ID
return nil
}
if in.Filter != nil {
if err := autoConvert_v1beta1_NetworkFilter_To_v1alpha5_NetworkFilter(in.Filter, out, s); err != nil {
return err
}
infrav1.ConvertAllTagsFrom(&in.Filter.FilterByNeutronTags, &out.Tags, &out.TagsAny, &out.NotTags, &out.NotTagsAny)
}
infrav1.ConvertAllTagsFrom(&in.FilterByNeutronTags, &out.Tags, &out.TagsAny, &out.NotTags, &out.NotTagsAny)
return nil
}

// conversion-gen registers the following functions so we have to define them, but nothing should ever call them.
func Convert_v1alpha5_NetworkFilter_To_v1beta1_NetworkFilter(_ *NetworkFilter, _ *infrav1.NetworkFilter, _ conversion.Scope) error {
return errors.New("Convert_v1alpha6_NetworkFilter_To_v1beta1_NetworkFilter should not be called")
}

func Convert_v1beta1_NetworkFilter_To_v1alpha5_NetworkFilter(_ *infrav1.NetworkFilter, _ *NetworkFilter, _ conversion.Scope) error {
return errors.New("Convert_v1beta1_NetworkFilter_To_v1alpha6_NetworkFilter should not be called")
}

func Convert_v1alpha5_NetworkParam_To_v1beta1_NetworkParam(_ *NetworkParam, _ *infrav1.NetworkParam, _ conversion.Scope) error {
return errors.New("Convert_v1alpha6_NetworkParam_To_v1beta1_NetworkParam should not be called")
}

func Convert_v1beta1_NetworkParam_To_v1alpha5_NetworkParam(_ *infrav1.NetworkParam, _ *NetworkParam, _ conversion.Scope) error {
return errors.New("Convert_v1beta1_NetworkParam_To_v1alpha6_NetworkParam should not be called")
}
47 changes: 40 additions & 7 deletions api/v1alpha5/zz_generated.conversion.go

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

64 changes: 45 additions & 19 deletions api/v1alpha6/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ func TestFuzzyConversion(t *testing.T) {
return ret
}

nonEmptyString := func(c fuzz.Continue) string {
for {
if s := c.RandString(); s != "" {
return s
}
}
}

fuzzerFuncs := func(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
func(instance *Instance, c fuzz.Continue) {
Expand Down Expand Up @@ -206,6 +214,22 @@ func TestFuzzyConversion(t *testing.T) {
filter.NotTags = filterInvalidTags(filter.NotTags)
filter.NotTagsAny = filterInvalidTags(filter.NotTagsAny)
},

// v1beta1 network param contains exactly one of ID or filter
func(param *infrav1.NetworkParam, c fuzz.Continue) {
if c.RandBool() {
id := nonEmptyString(c)
param.ID = &id
param.Filter = nil
} else {
filter := infrav1.NetworkFilter{}
for filter.IsZero() {
c.FuzzNoCustom(&filter)
}
param.Filter = &filter
param.ID = nil
}
},
}
}

Expand Down Expand Up @@ -303,8 +327,8 @@ func TestNetworksToPorts(t *testing.T) {
afterMachineSpec: infrav1.OpenStackMachineSpec{
Ports: []infrav1.PortOpts{
{
Network: &infrav1.NetworkFilter{
ID: networkuuid,
Network: &infrav1.NetworkParam{
ID: pointer.String(networkuuid),
},
},
},
Expand All @@ -330,15 +354,17 @@ func TestNetworksToPorts(t *testing.T) {
afterMachineSpec: infrav1.OpenStackMachineSpec{
Ports: []infrav1.PortOpts{
{
Network: &infrav1.NetworkFilter{
Name: "network-name",
Description: "network-description",
ProjectID: "project-id",
FilterByNeutronTags: infrav1.FilterByNeutronTags{
Tags: []infrav1.NeutronTag{"tags"},
TagsAny: []infrav1.NeutronTag{"tags-any"},
NotTags: []infrav1.NeutronTag{"not-tags"},
NotTagsAny: []infrav1.NeutronTag{"not-tags-any"},
Network: &infrav1.NetworkParam{
Filter: &infrav1.NetworkFilter{
Name: "network-name",
Description: "network-description",
ProjectID: "project-id",
FilterByNeutronTags: infrav1.FilterByNeutronTags{
Tags: []infrav1.NeutronTag{"tags"},
TagsAny: []infrav1.NeutronTag{"tags-any"},
NotTags: []infrav1.NeutronTag{"not-tags"},
NotTagsAny: []infrav1.NeutronTag{"not-tags-any"},
},
},
},
},
Expand All @@ -362,8 +388,8 @@ func TestNetworksToPorts(t *testing.T) {
afterMachineSpec: infrav1.OpenStackMachineSpec{
Ports: []infrav1.PortOpts{
{
Network: &infrav1.NetworkFilter{
ID: networkuuid,
Network: &infrav1.NetworkParam{
ID: pointer.String(networkuuid),
},
FixedIPs: []infrav1.FixedIP{
{
Expand Down Expand Up @@ -406,8 +432,8 @@ func TestNetworksToPorts(t *testing.T) {
afterMachineSpec: infrav1.OpenStackMachineSpec{
Ports: []infrav1.PortOpts{
{
Network: &infrav1.NetworkFilter{
ID: networkuuid,
Network: &infrav1.NetworkParam{
ID: pointer.String(networkuuid),
},
FixedIPs: []infrav1.FixedIP{
{
Expand Down Expand Up @@ -466,8 +492,8 @@ func TestNetworksToPorts(t *testing.T) {
afterMachineSpec: infrav1.OpenStackMachineSpec{
Ports: []infrav1.PortOpts{
{
Network: &infrav1.NetworkFilter{
ID: networkuuid,
Network: &infrav1.NetworkParam{
ID: pointer.String(networkuuid),
},
FixedIPs: []infrav1.FixedIP{
{
Expand All @@ -478,8 +504,8 @@ func TestNetworksToPorts(t *testing.T) {
},
},
{
Network: &infrav1.NetworkFilter{
ID: networkuuid,
Network: &infrav1.NetworkParam{
ID: pointer.String(networkuuid),
},
FixedIPs: []infrav1.FixedIP{
{
Expand Down
31 changes: 11 additions & 20 deletions api/v1alpha6/openstackcluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,14 @@ func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackCl
func restorev1beta1ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *infrav1.OpenStackClusterSpec) {
// Bastion is restored separately

if dst.Network.IsEmpty() {
dst.Network = previous.Network
}
restorev1beta1NetworkParam(previous.Network, dst.Network)

// Restore all fields except ID, which should have been copied over in conversion
// ExternalNetwork by filter will be been lost in down-conversion
if previous.ExternalNetwork != nil {
if dst.ExternalNetwork == nil {
dst.ExternalNetwork = &infrav1.NetworkFilter{}
dst.ExternalNetwork = &infrav1.NetworkParam{}
}

dst.ExternalNetwork.Name = previous.ExternalNetwork.Name
dst.ExternalNetwork.Description = previous.ExternalNetwork.Description
dst.ExternalNetwork.ProjectID = previous.ExternalNetwork.ProjectID
dst.ExternalNetwork.Tags = previous.ExternalNetwork.Tags
dst.ExternalNetwork.TagsAny = previous.ExternalNetwork.TagsAny
dst.ExternalNetwork.NotTags = previous.ExternalNetwork.NotTags
dst.ExternalNetwork.NotTagsAny = previous.ExternalNetwork.NotTagsAny
dst.ExternalNetwork.Filter = previous.ExternalNetwork.Filter
}

// Restore fields not present in v1alpha6
Expand Down Expand Up @@ -227,15 +218,15 @@ func Convert_v1alpha6_OpenStackClusterSpec_To_v1beta1_OpenStackClusterSpec(in *O
}

if in.Network != (NetworkFilter{}) {
out.Network = &infrav1.NetworkFilter{}
if err := Convert_v1alpha6_NetworkFilter_To_v1beta1_NetworkFilter(&in.Network, out.Network, s); err != nil {
out.Network = &infrav1.NetworkParam{}
if err := Convert_v1alpha6_NetworkFilter_To_v1beta1_NetworkParam(&in.Network, out.Network, s); err != nil {
return err
}
}

if in.ExternalNetworkID != "" {
out.ExternalNetwork = &infrav1.NetworkFilter{
ID: in.ExternalNetworkID,
out.ExternalNetwork = &infrav1.NetworkParam{
ID: &in.ExternalNetworkID,
}
}

Expand Down Expand Up @@ -294,13 +285,13 @@ func Convert_v1beta1_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *i
}

if in.Network != nil {
if err := Convert_v1beta1_NetworkFilter_To_v1alpha6_NetworkFilter(in.Network, &out.Network, s); err != nil {
if err := Convert_v1beta1_NetworkParam_To_v1alpha6_NetworkFilter(in.Network, &out.Network, s); err != nil {
return err
}
}

if in.ExternalNetwork != nil && in.ExternalNetwork.ID != "" {
out.ExternalNetworkID = in.ExternalNetwork.ID
if in.ExternalNetwork != nil && in.ExternalNetwork.ID != nil {
out.ExternalNetworkID = *in.ExternalNetwork.ID
}

if len(in.Subnets) >= 1 {
Expand Down
Loading

0 comments on commit 14fe5d4

Please sign in to comment.