@@ -5,10 +5,11 @@ package haprovider
5
5
6
6
import (
7
7
"context"
8
- "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils"
9
8
"net"
10
9
"sync"
11
10
11
+ "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils"
12
+
12
13
"github.com/pkg/errors"
13
14
14
15
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -37,9 +38,11 @@ type HAProvider struct {
37
38
log logr.Logger
38
39
}
39
40
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
+ )
43
46
44
47
// NewProvider make HAProvider as a singleton
45
48
func NewProvider (c client.Client , log logr.Logger ) * HAProvider {
@@ -126,13 +129,14 @@ func (r *HAProvider) createService(
126
129
},
127
130
Spec : corev1.ServiceSpec {
128
131
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
130
133
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
+ },
136
140
},
137
141
},
138
142
}
@@ -184,7 +188,7 @@ func (r *HAProvider) annotateService(ctx context.Context, cluster *clusterv1.Clu
184
188
if err != nil {
185
189
return serviceAnnotation , err
186
190
}
187
- //no adc is selected for cluster, no annotation is needed.
191
+ // no adc is selected for cluster, no annotation is needed.
188
192
if adcForCluster == nil {
189
193
// for the management cluster, it needs to requeue until the install-ako-for-management-cluster AKODeploymentConfig created
190
194
if _ , ok := cluster .Labels [akoov1alpha1 .TKGManagememtClusterRoleLabel ]; ok {
@@ -206,7 +210,7 @@ func (r *HAProvider) annotateService(ctx context.Context, cluster *clusterv1.Clu
206
210
}
207
211
}
208
212
if aviInfraSetting != nil {
209
- //add AVIInfraSetting annotation when creating HA svc
213
+ // add AVIInfraSetting annotation when creating HA svc
210
214
serviceAnnotation [akoov1alpha1 .HAAVIInfraSettingAnnotationsKey ] = aviInfraSetting .Name
211
215
}
212
216
return serviceAnnotation , nil
@@ -224,7 +228,6 @@ func (r *HAProvider) getADCForCluster(ctx context.Context, cluster *clusterv1.Cl
224
228
}
225
229
226
230
func (r * HAProvider ) getAviInfraSettingFromAdc (ctx context.Context , adcForCluster * akoov1alpha1.AKODeploymentConfig ) (* akov1beta1.AviInfraSetting , error ) {
227
-
228
231
aviInfraSetting := & akov1beta1.AviInfraSetting {}
229
232
aviInfraSettingName := GetAviInfraSettingName (adcForCluster )
230
233
if err := r .Client .Get (ctx , client.ObjectKey {
@@ -261,11 +264,20 @@ func (r *HAProvider) updateClusterControlPlaneEndpoint(cluster *clusterv1.Cluste
261
264
}
262
265
263
266
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
265
277
if service .Annotations == nil {
266
278
service .Annotations = make (map [string ]string )
267
279
}
268
- service .Annotations [akoov1alpha1 .AkoPreferredIPAnnotation ] = cluster . Spec . ControlPlaneEndpoint . Host
280
+ service .Annotations [akoov1alpha1 .AkoPreferredIPAnnotation ] = host
269
281
if err := r .Update (ctx , service ); err != nil {
270
282
return errors .Wrapf (err , "Failed to update cluster endpoint to cluster control plane load balancer type of service <%s>\n " , service .Name )
271
283
}
@@ -354,7 +366,7 @@ func (r *HAProvider) addMachineIpToEndpoints(endpoints *corev1.Endpoints, machin
354
366
IP : machineAddress .Address ,
355
367
NodeName : & machine .Name ,
356
368
}
357
- //Validate MachineIP before adding to Endpoint
369
+ // Validate MachineIP before adding to Endpoint
358
370
if ipFamily == "V6" {
359
371
if net .ParseIP (machineAddress .Address ).To4 () == nil {
360
372
endpoints .Subsets [0 ].Addresses = append (endpoints .Subsets [0 ].Addresses , newAddress )
@@ -404,7 +416,6 @@ func (r *HAProvider) CreateOrUpdateHAEndpoints(ctx context.Context, machine *clu
404
416
ipFamily := "V4"
405
417
if adcForCluster != nil && adcForCluster .Spec .ExtraConfigs .IpFamily != "" {
406
418
ipFamily = adcForCluster .Spec .ExtraConfigs .IpFamily
407
-
408
419
}
409
420
if ! machine .DeletionTimestamp .IsZero () {
410
421
r .log .Info ("machine" + machine .Name + " is being deleted, remove the endpoint of the machine from " + r .getHAServiceName (cluster ) + " Endpoints" )
0 commit comments