Skip to content

Commit b1fe130

Browse files
committed
Resolve control plane endpoint when updating service
Signed-off-by: Lubron Zhan <[email protected]>
1 parent 3046d4a commit b1fe130

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

pkg/ako-operator/lib.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ func IsLoadBalancerProvider(cluster *clusterv1.Cluster) (bool, error) {
9797
return true, nil
9898
}
9999

100-
// GetControlPlaneEndpoint returns cluster's API server address
100+
// GetControlPlaneEndpoint returns cluster's API server address, this could be an FQDN, if
101+
// that's the case, need to resolve to IP before putting it as service's address.
101102
func GetControlPlaneEndpoint(cluster *clusterv1.Cluster) (string, error) {
102103
apiServerEndpoint, _ := cluster.ObjectMeta.Annotations[ClusterControlPlaneAnnotations]
103104
if IsClusterClassBasedCluster(cluster) {

pkg/haprovider/haprovider.go

+28-17
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package haprovider
55

66
import (
77
"context"
8-
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils"
98
"net"
109
"sync"
1110

11+
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils"
12+
1213
"github.com/pkg/errors"
1314

1415
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -37,9 +38,11 @@ type HAProvider struct {
3738
log logr.Logger
3839
}
3940

40-
var instance *HAProvider
41-
var once sync.Once
42-
var QueryFQDN = queryFQDNEndpoint
41+
var (
42+
instance *HAProvider
43+
once sync.Once
44+
QueryFQDN = queryFQDNEndpoint
45+
)
4346

4447
// NewProvider make HAProvider as a singleton
4548
func NewProvider(c client.Client, log logr.Logger) *HAProvider {
@@ -126,13 +129,14 @@ func (r *HAProvider) createService(
126129
},
127130
Spec: corev1.ServiceSpec{
128131
Type: corev1.ServiceTypeLoadBalancer,
129-
//TODO:(chenlin) Add two ip families after AKO fully supports dual-stack load balancer type of service
132+
// TODO:(chenlin) Add two ip families after AKO fully supports dual-stack load balancer type of service
130133
IPFamilies: []corev1.IPFamily{corev1.IPFamily(primaryIPFamily)},
131-
Ports: []corev1.ServicePort{{
132-
Protocol: "TCP",
133-
Port: port,
134-
TargetPort: intstr.FromInt(int(6443)),
135-
},
134+
Ports: []corev1.ServicePort{
135+
{
136+
Protocol: "TCP",
137+
Port: port,
138+
TargetPort: intstr.FromInt(int(6443)),
139+
},
136140
},
137141
},
138142
}
@@ -184,7 +188,7 @@ func (r *HAProvider) annotateService(ctx context.Context, cluster *clusterv1.Clu
184188
if err != nil {
185189
return serviceAnnotation, err
186190
}
187-
//no adc is selected for cluster, no annotation is needed.
191+
// no adc is selected for cluster, no annotation is needed.
188192
if adcForCluster == nil {
189193
// for the management cluster, it needs to requeue until the install-ako-for-management-cluster AKODeploymentConfig created
190194
if _, ok := cluster.Labels[akoov1alpha1.TKGManagememtClusterRoleLabel]; ok {
@@ -206,7 +210,7 @@ func (r *HAProvider) annotateService(ctx context.Context, cluster *clusterv1.Clu
206210
}
207211
}
208212
if aviInfraSetting != nil {
209-
//add AVIInfraSetting annotation when creating HA svc
213+
// add AVIInfraSetting annotation when creating HA svc
210214
serviceAnnotation[akoov1alpha1.HAAVIInfraSettingAnnotationsKey] = aviInfraSetting.Name
211215
}
212216
return serviceAnnotation, nil
@@ -224,7 +228,6 @@ func (r *HAProvider) getADCForCluster(ctx context.Context, cluster *clusterv1.Cl
224228
}
225229

226230
func (r *HAProvider) getAviInfraSettingFromAdc(ctx context.Context, adcForCluster *akoov1alpha1.AKODeploymentConfig) (*akov1beta1.AviInfraSetting, error) {
227-
228231
aviInfraSetting := &akov1beta1.AviInfraSetting{}
229232
aviInfraSettingName := GetAviInfraSettingName(adcForCluster)
230233
if err := r.Client.Get(ctx, client.ObjectKey{
@@ -261,11 +264,20 @@ func (r *HAProvider) updateClusterControlPlaneEndpoint(cluster *clusterv1.Cluste
261264
}
262265

263266
func (r *HAProvider) updateControlPlaneEndpointToService(ctx context.Context, cluster *clusterv1.Cluster, service *corev1.Service) error {
264-
service.Spec.LoadBalancerIP = cluster.Spec.ControlPlaneEndpoint.Host
267+
host := cluster.Spec.ControlPlaneEndpoint.Host
268+
var err error
269+
if net.ParseIP(host) == nil {
270+
host, err = QueryFQDN(host)
271+
if err != nil {
272+
r.log.Error(err, "Failed to resolve control plane endpoint ", "endpoint", host)
273+
return err
274+
}
275+
}
276+
service.Spec.LoadBalancerIP = host
265277
if service.Annotations == nil {
266278
service.Annotations = make(map[string]string)
267279
}
268-
service.Annotations[akoov1alpha1.AkoPreferredIPAnnotation] = cluster.Spec.ControlPlaneEndpoint.Host
280+
service.Annotations[akoov1alpha1.AkoPreferredIPAnnotation] = host
269281
if err := r.Update(ctx, service); err != nil {
270282
return errors.Wrapf(err, "Failed to update cluster endpoint to cluster control plane load balancer type of service <%s>\n", service.Name)
271283
}
@@ -354,7 +366,7 @@ func (r *HAProvider) addMachineIpToEndpoints(endpoints *corev1.Endpoints, machin
354366
IP: machineAddress.Address,
355367
NodeName: &machine.Name,
356368
}
357-
//Validate MachineIP before adding to Endpoint
369+
// Validate MachineIP before adding to Endpoint
358370
if ipFamily == "V6" {
359371
if net.ParseIP(machineAddress.Address).To4() == nil {
360372
endpoints.Subsets[0].Addresses = append(endpoints.Subsets[0].Addresses, newAddress)
@@ -404,7 +416,6 @@ func (r *HAProvider) CreateOrUpdateHAEndpoints(ctx context.Context, machine *clu
404416
ipFamily := "V4"
405417
if adcForCluster != nil && adcForCluster.Spec.ExtraConfigs.IpFamily != "" {
406418
ipFamily = adcForCluster.Spec.ExtraConfigs.IpFamily
407-
408419
}
409420
if !machine.DeletionTimestamp.IsZero() {
410421
r.log.Info("machine" + machine.Name + " is being deleted, remove the endpoint of the machine from " + r.getHAServiceName(cluster) + " Endpoints")

pkg/haprovider/haprovider_test.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var _ = Describe("Control Plane HA provider", func() {
4343
haProvider = *NewProvider(fc, logger)
4444
})
4545

46-
Context("Test_CreateOrUpdateHAService", func() {
46+
FContext("Test_CreateOrUpdateHAService", func() {
4747
var (
4848
cluster *clusterv1.Cluster
4949
svc *corev1.Service
@@ -134,6 +134,36 @@ var _ = Describe("Control Plane HA provider", func() {
134134
Expect(svc.Annotations[akoov1alpha1.AkoPreferredIPAnnotation]).Should(Equal("fd01:3:4:2877:250:56ff:feb4:adaf"))
135135
})
136136
})
137+
138+
When("ControlPlaneEndpoint.host has FQDN, it should be resolved before adding to service", func() {
139+
BeforeEach(func() {
140+
cluster.Spec.ControlPlaneEndpoint.Host = "google.com"
141+
svc = &corev1.Service{
142+
ObjectMeta: v1.ObjectMeta{
143+
Name: "default-test-cluster-control-plane",
144+
Namespace: "default",
145+
},
146+
Spec: corev1.ServiceSpec{},
147+
Status: corev1.ServiceStatus{
148+
LoadBalancer: corev1.LoadBalancerStatus{
149+
Ingress: []corev1.LoadBalancerIngress{},
150+
},
151+
},
152+
}
153+
key = client.ObjectKey{Name: haProvider.getHAServiceName(cluster), Namespace: cluster.Namespace}
154+
Expect(haProvider.Client.Create(ctx, svc)).ShouldNot(HaveOccurred())
155+
QueryFQDN = func(fqdn string) (string, error) {
156+
return "3.3.3.3", nil
157+
}
158+
})
159+
160+
It("test should pass without error", func() {
161+
Expect(err).ShouldNot(HaveOccurred())
162+
Expect(haProvider.Client.Get(ctx, key, svc)).ShouldNot(HaveOccurred())
163+
Expect(svc.Spec.LoadBalancerIP).Should(Equal("3.3.3.3"))
164+
Expect(svc.Annotations[akoov1alpha1.AkoPreferredIPAnnotation]).Should(Equal("3.3.3.3"))
165+
})
166+
})
137167
})
138168

139169
Describe("Test_CreateService", func() {
@@ -503,8 +533,6 @@ var _ = Describe("Control Plane HA provider", func() {
503533
Expect(ep.Subsets[0].Addresses[0].IP).Should(Equal("1.1.1.1"))
504534
})
505535
})
506-
507536
})
508-
509537
})
510538
})

0 commit comments

Comments
 (0)