Skip to content

Commit

Permalink
Move filter conversions out of /api
Browse files Browse the repository at this point in the history
Removes a gophercloud dependency for consumers of the API.
  • Loading branch information
mdbooth committed Mar 6, 2024
1 parent 9fb6072 commit d33fee8
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 264 deletions.
53 changes: 0 additions & 53 deletions api/v1alpha6/filter_convert.go

This file was deleted.

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

import (
"strings"

"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -69,3 +71,47 @@ func LegacyCalicoSecurityGroupRules() []SecurityGroupRuleSpec {
},
}
}

// splitTags splits a comma separated list of tags into a slice of tags.
// If the input is an empty string, it returns nil representing no list rather
// than an empty list.
func splitTags(tags string) []NeutronTag {
if tags == "" {
return nil
}

var ret []NeutronTag
for _, tag := range strings.Split(tags, ",") {
if tag != "" {
ret = append(ret, NeutronTag(tag))
}
}

return ret
}

// JoinTags joins a slice of tags into a comma separated list of tags.
func JoinTags(tags []NeutronTag) string {
var b strings.Builder
for i := range tags {
if i > 0 {
b.WriteString(",")
}
b.WriteString(string(tags[i]))
}
return b.String()
}

func ConvertAllTagsTo(tags, tagsAny, notTags, notTagsAny string, neutronTags *FilterByNeutronTags) {
neutronTags.Tags = splitTags(tags)
neutronTags.TagsAny = splitTags(tagsAny)
neutronTags.NotTags = splitTags(notTags)
neutronTags.NotTagsAny = splitTags(notTagsAny)
}

func ConvertAllTagsFrom(neutronTags *FilterByNeutronTags, tags, tagsAny, notTags, notTagsAny *string) {
*tags = JoinTags(neutronTags.Tags)
*tagsAny = JoinTags(neutronTags.TagsAny)
*notTags = JoinTags(neutronTags.NotTags)
*notTagsAny = JoinTags(neutronTags.NotTagsAny)
}
147 changes: 0 additions & 147 deletions api/v1beta1/filter_convert.go

This file was deleted.

11 changes: 11 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ type NetworkFilter struct {
FilterByNeutronTags `json:",inline"`
}

func (networkFilter *NetworkFilter) IsEmpty() bool {
return networkFilter.Name == "" &&
networkFilter.Description == "" &&
networkFilter.ProjectID == "" &&
networkFilter.ID == "" &&
len(networkFilter.Tags) == 0 &&
len(networkFilter.TagsAny) == 0 &&
len(networkFilter.NotTags) == 0 &&
len(networkFilter.NotTagsAny) == 0
}

type SubnetFilter struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Expand Down
11 changes: 6 additions & 5 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
utils "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/controllers"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/filterconvert"
)

const (
Expand Down Expand Up @@ -633,7 +634,7 @@ func reconcilePreExistingNetworkComponents(scope *scope.WithLogger, networkingSe
}

if !openStackCluster.Spec.Network.IsEmpty() {
netOpts := openStackCluster.Spec.Network.ToListOpt()
netOpts := filterconvert.NetworkFilterToListOpts(&openStackCluster.Spec.Network)
networkList, err := networkingService.GetNetworksByFilter(&netOpts)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to find network: %w", err))
Expand Down Expand Up @@ -859,10 +860,10 @@ func getClusterSubnets(networkingService *networking.Service, openStackCluster *
return nil, fmt.Errorf("no network or subnets specified in OpenStackCluster spec")
}

empty := &infrav1.SubnetFilter{}
listOpt := empty.ToListOpt()
listOpt.NetworkID = networkID
clusterSubnets, err = networkingService.GetSubnetsByFilter(listOpt)
listOpts := subnets.ListOpts{
NetworkID: networkID,
}
clusterSubnets, err = networkingService.GetSubnetsByFilter(listOpts)
if err != nil {
err = fmt.Errorf("failed to find subnets: %w", err)
if errors.Is(err, networking.ErrFilterMatch) {
Expand Down
3 changes: 2 additions & 1 deletion controllers/openstackfloatingippool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
filterconvert "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/filterconvert/v1alpha7"
)

const (
Expand Down Expand Up @@ -401,7 +402,7 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileFloatingIPNetwork(scope *sc
}

netListOpts := external.ListOptsExt{
ListOptsBuilder: pool.Spec.FloatingIPNetwork.ToListOpt(),
ListOptsBuilder: filterconvert.NetworkFilterToListOpt(&pool.Spec.FloatingIPNetwork),
External: pointer.Bool(true),
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/cluster-api-provider-openstack/pkg/clients"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/filterconvert"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/hash"
)

Expand Down Expand Up @@ -336,7 +337,8 @@ func (s *Service) GetImageID(image infrav1.ImageFilter) (string, error) {
return image.ID, nil
}

allImages, err := s.getImageClient().ListImages(image.ToListOpt())
listOpts := filterconvert.ImageFilterToListOpts(&image)
allImages, err := s.getImageClient().ListImages(listOpts)
if err != nil {
return "", err
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/cluster-api-provider-openstack/pkg/metrics"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/filterconvert"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
)

Expand Down Expand Up @@ -77,7 +78,6 @@ func (c createOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
// - the user has set OpenStackCluster.Spec.DisableExternalNetwork to true.
func (s *Service) ReconcileExternalNetwork(openStackCluster *infrav1.OpenStackCluster) error {
var listOpts external.ListOptsExt
var emptyExternalnetwork infrav1.NetworkFilter
var isAutoDetecting bool

if openStackCluster.Spec.DisableExternalNetwork {
Expand All @@ -86,9 +86,10 @@ func (s *Service) ReconcileExternalNetwork(openStackCluster *infrav1.OpenStackCl
return nil
}

if openStackCluster.Spec.ExternalNetwork.ToListOpt() != emptyExternalnetwork.ToListOpt() {
externalNetworkListOpts := filterconvert.NetworkFilterToListOpts(&openStackCluster.Spec.ExternalNetwork)
if externalNetworkListOpts != (networks.ListOpts{}) {
listOpts = external.ListOptsExt{
ListOptsBuilder: openStackCluster.Spec.ExternalNetwork.ToListOpt(),
ListOptsBuilder: externalNetworkListOpts,
}
} else {
// ExternalNetwork is not given so we'll list all networks and filter for external networks
Expand Down Expand Up @@ -345,16 +346,17 @@ func (s *Service) GetSubnetsByFilter(opts subnets.ListOptsBuilder) ([]subnets.Su
// GetSubnetByFilter gets a single subnet specified by the given SubnetFilter.
// It returns an ErrFilterMatch if no or multiple subnets are found.
func (s *Service) GetSubnetByFilter(filter *infrav1.SubnetFilter) (*subnets.Subnet, error) {
return s.getSubnetByFilter(filter.ToListOpt())
listOpts := filterconvert.SubnetFilterToListOpts(filter)
return s.getSubnetByFilter(listOpts)
}

// GetNetworkSubnetByFilter gets a single subnet of the given network, specified by the given SubnetFilter.
// It returns an ErrFilterMatch if no or multiple subnets are found.
func (s *Service) GetNetworkSubnetByFilter(networkID string, filter *infrav1.SubnetFilter) (*subnets.Subnet, error) {
listOpt := filter.ToListOpt()
listOpt.NetworkID = networkID
listOpts := filterconvert.SubnetFilterToListOpts(filter)
listOpts.NetworkID = networkID

return s.getSubnetByFilter(listOpt)
return s.getSubnetByFilter(listOpts)
}

// getSubnetByFilter gets a single subnet specified by the given gophercloud ListOpts.
Expand Down
Loading

0 comments on commit d33fee8

Please sign in to comment.