Skip to content

Commit e30cc4c

Browse files
committed
loadbalancer: resolve ControlPlaneEndpoint.Host when needed
`ControlPlaneEndpoint.Host` is not guaranteed to be an IP address, it can also be an hostname. Now we'll try to lookup the hostname if it's not an IP and set that for the LB VipAddress.
1 parent 821a1a2 commit e30cc4c

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

pkg/cloud/services/loadbalancer/loadbalancer.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package loadbalancer
1818

1919
import (
20+
"context"
2021
"errors"
2122
"fmt"
23+
"net"
2224
"reflect"
2325
"time"
2426

@@ -27,7 +29,7 @@ import (
2729
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
2830
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
2931
"k8s.io/apimachinery/pkg/util/wait"
30-
"k8s.io/utils/net"
32+
utilsnet "k8s.io/utils/net"
3133
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3234
"sigs.k8s.io/cluster-api/util"
3335

@@ -42,20 +44,39 @@ import (
4244
const (
4345
networkPrefix string = "k8s-clusterapi"
4446
kubeapiLBSuffix string = "kubeapi"
47+
resolvedMsg string = "ControlPlaneEndpoint.Host is not an IP address, using the first resolved IP address"
4548
)
4649

4750
const loadBalancerProvisioningStatusActive = "ACTIVE"
4851

52+
// We wrap the net.LookupHost function in a variable to allow overriding it in unit tests.
53+
//
54+
//nolint:gocritic
55+
var lookupHost = func(host string) ([]string, error) {
56+
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
57+
defer cancel()
58+
return net.DefaultResolver.LookupHost(ctx, host)
59+
}
60+
4961
func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackCluster, clusterName string, apiServerPort int) (bool, error) {
5062
loadBalancerName := getLoadBalancerName(clusterName)
5163
s.scope.Logger().Info("Reconciling load balancer", "name", loadBalancerName)
5264

5365
var fixedIPAddress string
66+
var err error
67+
5468
switch {
5569
case openStackCluster.Spec.APIServerFixedIP != "":
5670
fixedIPAddress = openStackCluster.Spec.APIServerFixedIP
5771
case openStackCluster.Spec.DisableAPIServerFloatingIP && openStackCluster.Spec.ControlPlaneEndpoint.IsValid():
58-
fixedIPAddress = openStackCluster.Spec.ControlPlaneEndpoint.Host
72+
ips, err := lookupHost(openStackCluster.Spec.ControlPlaneEndpoint.Host)
73+
if err != nil {
74+
return false, fmt.Errorf("lookup host: %w", err)
75+
}
76+
fixedIPAddress = ips[0]
77+
if net.ParseIP(fixedIPAddress) == nil {
78+
s.scope.Logger().Info(resolvedMsg, "host", openStackCluster.Spec.ControlPlaneEndpoint.Host, "ip", fixedIPAddress)
79+
}
5980
}
6081

6182
providers, err := s.loadbalancerClient.ListLoadBalancerProviders()
@@ -93,7 +114,14 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust
93114
case openStackCluster.Spec.APIServerFloatingIP != "":
94115
floatingIPAddress = openStackCluster.Spec.APIServerFloatingIP
95116
case openStackCluster.Spec.ControlPlaneEndpoint.IsValid():
96-
floatingIPAddress = openStackCluster.Spec.ControlPlaneEndpoint.Host
117+
ips, err := lookupHost(openStackCluster.Spec.ControlPlaneEndpoint.Host)
118+
if err != nil {
119+
return false, fmt.Errorf("lookup host: %w", err)
120+
}
121+
floatingIPAddress = ips[0]
122+
if net.ParseIP(floatingIPAddress) == nil {
123+
s.scope.Logger().Info(resolvedMsg, "host", openStackCluster.Spec.ControlPlaneEndpoint.Host, "ip", floatingIPAddress)
124+
}
97125
}
98126
fp, err := s.networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster, clusterName, floatingIPAddress)
99127
if err != nil {
@@ -294,9 +322,9 @@ func validateIPs(openStackCluster *infrav1.OpenStackCluster, definedCIDRs []stri
294322

295323
for _, v := range definedCIDRs {
296324
switch {
297-
case net.IsIPv4String(v):
325+
case utilsnet.IsIPv4String(v):
298326
marshaledCIDRs = append(marshaledCIDRs, v+"/32")
299-
case net.IsIPv4CIDRString(v):
327+
case utilsnet.IsIPv4CIDRString(v):
300328
marshaledCIDRs = append(marshaledCIDRs, v)
301329
default:
302330
record.Warnf(openStackCluster, "FailedIPAddressValidation", "%s is not a valid IPv4 nor CIDR address and will not get applied to allowed_cidrs", v)

pkg/cloud/services/loadbalancer/loadbalancer_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package loadbalancer
1818

1919
import (
20+
"errors"
21+
"net"
2022
"testing"
2123

2224
"github.com/go-logr/logr"
@@ -28,6 +30,7 @@ import (
2830
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
2931
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/providers"
3032
. "github.com/onsi/gomega"
33+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3134

3235
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
3336
"sigs.k8s.io/cluster-api-provider-openstack/pkg/clients/mock"
@@ -38,9 +41,24 @@ func Test_ReconcileLoadBalancer(t *testing.T) {
3841
mockCtrl := gomock.NewController(t)
3942
defer mockCtrl.Finish()
4043

44+
// Stub the call to net.LookupHost
45+
lookupHost = func(host string) (addrs []string, err error) {
46+
if net.ParseIP(host) != nil {
47+
return []string{host}, nil
48+
} else if host == "api.test-cluster.test" {
49+
ips := []string{"192.168.100.10"}
50+
return ips, nil
51+
}
52+
return nil, errors.New("Unknown Host " + host)
53+
}
54+
4155
openStackCluster := &infrav1.OpenStackCluster{
4256
Spec: infrav1.OpenStackClusterSpec{
4357
DisableAPIServerFloatingIP: true,
58+
ControlPlaneEndpoint: clusterv1.APIEndpoint{
59+
Host: "api.test-cluster.test",
60+
Port: 6443,
61+
},
4462
},
4563
Status: infrav1.OpenStackClusterStatus{
4664
ExternalNetwork: &infrav1.NetworkStatus{

0 commit comments

Comments
 (0)