Skip to content

✨ Allow changing DNSNameservers in subnet config for OpenstackCluster #2511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/utils/ptr"

infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
Expand Down Expand Up @@ -201,6 +202,10 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl
} else if len(subnetList) == 1 {
subnet = &subnetList[0]
s.scope.Logger().V(6).Info("Reusing existing subnet", "name", subnet.Name, "id", subnet.ID)

if err := s.updateSubnetDNSNameservers(openStackCluster, subnet); err != nil {
return err
}
}

openStackCluster.Status.Network.Subnets = []infrav1.Subnet{
Expand Down Expand Up @@ -248,6 +253,39 @@ func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, clust
return subnet, nil
}

// updateSubnetDNSNameservers updates the DNS nameservers for an existing subnet if they differ from the desired configuration.
func (s *Service) updateSubnetDNSNameservers(openStackCluster *infrav1.OpenStackCluster, subnet *subnets.Subnet) error {
// Picking the first managed subnet since we only support one for now
desiredNameservers := openStackCluster.Spec.ManagedSubnets[0].DNSNameservers
currentNameservers := subnet.DNSNameservers

var needsUpdate bool
if len(desiredNameservers) != len(currentNameservers) {
needsUpdate = true
} else {
needsUpdate = !equality.Semantic.DeepEqual(currentNameservers, desiredNameservers)
}
Comment on lines +262 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not that big of a deal but I'm curious why don't we just drop needsUpdate and use the equality all the time. Is this an optimization or is it simply something I don't know? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, len is much quicker vs equality.Semantic.DeepEqual but for sure we can drop that optimization and keep code more simple

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Given that they're just slices of strings I'd personally do something a bit less voodoo than Semantic.DeepEqual like slices.Sort(); slices.Equal().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equality.Semantic.DeepEqual is basically what most of people use in K8s land (before it was regular reflect.DeepEqual), but sure we can switch to sort and equal approach

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equality.Semantic.DeepEqual is used in multiple places, so I guess it makes sense to have a wrapper function to compare slices, something like this

func EqualIgnoreOrder[T cmp.Ordered](a, b []T) bool {
	if len(a) != len(b) {
		return false
	}

	if len(a) == 0 {
		return true
	}

	aCopy := slices.Clone(a)
	bCopy := slices.Clone(b)

	slices.Sort(aCopy)
	slices.Sort(bCopy)

	return slices.Equal(aCopy, bCopy)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a nit, and mostly because I've spent time in the past reading Semantic.DeepEqual trying to determine the precise behaviour of its edge cases.

I only mention it at all because my personal preference would be to use something more explicit, especially when the data structure is simple as here. But this works and the PR has great test coverage, so there's no pressing need to update this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine replacing if we bring similar helper in, I would hate to replace 1 func call with len,copy,sort,equal checks in multiple places, this can be followup if that makes sense


if needsUpdate {
s.scope.Logger().Info("Updating subnet DNS nameservers", "id", subnet.ID, "from", currentNameservers, "to", desiredNameservers)

updateOpts := subnets.UpdateOpts{
DNSNameservers: &desiredNameservers,
}

updatedSubnet, err := s.client.UpdateSubnet(subnet.ID, updateOpts)
if err != nil {
record.Warnf(openStackCluster, "FailedUpdateSubnet", "Failed to update DNS nameservers for subnet %s: %v", subnet.ID, err)
return err
}

*subnet = *updatedSubnet
record.Eventf(openStackCluster, "SuccessfulUpdateSubnet", "Updated DNS nameservers for subnet %s", subnet.ID)
}

return nil
}

func (s *Service) getNetworkByName(networkName string) (networks.Network, error) {
opts := networks.ListOpts{
Name: networkName,
Expand Down
Loading