Skip to content

Commit 1adb6f9

Browse files
committed
Set proper IP family policy on NGINX LB Service
Problem: When provisioning the NGINX LoadBalancer Service, the IPFamily that's set in the NginxProxy resource (default dual) was not honored. Solution: By default, set the IPFamily to PreferDualStack. If a user has specified otherwise in the NginxProxy resource, then set to SingleStack. The IPFamilies list is populated automatically by k8s based on the policy.
1 parent dd86fec commit 1adb6f9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

internal/controller/provisioner/objects.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,12 @@ func buildNginxService(
463463
Ports: servicePorts,
464464
ExternalTrafficPolicy: servicePolicy,
465465
Selector: selectorLabels,
466+
IPFamilyPolicy: helpers.GetPointer(corev1.IPFamilyPolicyPreferDualStack),
466467
},
467468
}
468469

470+
setIPFamily(nProxyCfg, svc)
471+
469472
if serviceCfg.LoadBalancerIP != nil {
470473
svc.Spec.LoadBalancerIP = *serviceCfg.LoadBalancerIP
471474
}
@@ -479,6 +482,12 @@ func buildNginxService(
479482
return svc
480483
}
481484

485+
func setIPFamily(nProxyCfg *graph.EffectiveNginxProxy, svc *corev1.Service) {
486+
if nProxyCfg != nil && nProxyCfg.IPFamily != nil && *nProxyCfg.IPFamily != ngfAPIv1alpha2.Dual {
487+
svc.Spec.IPFamilyPolicy = helpers.GetPointer(corev1.IPFamilyPolicySingleStack)
488+
}
489+
}
490+
482491
func (p *NginxProvisioner) buildNginxDeployment(
483492
objectMeta metav1.ObjectMeta,
484493
nProxyCfg *graph.EffectiveNginxProxy,

internal/controller/provisioner/objects_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func TestBuildNginxResourceObjects(t *testing.T) {
161161
validateMeta(svc)
162162
g.Expect(svc.Spec.Type).To(Equal(defaultServiceType))
163163
g.Expect(svc.Spec.ExternalTrafficPolicy).To(Equal(defaultServicePolicy))
164+
g.Expect(*svc.Spec.IPFamilyPolicy).To(Equal(corev1.IPFamilyPolicyPreferDualStack))
164165

165166
// service ports is sorted in ascending order by port number when we make the nginx object
166167
g.Expect(svc.Spec.Ports).To(Equal([]corev1.ServicePort{
@@ -260,6 +261,7 @@ func TestBuildNginxResourceObjects_NginxProxyConfig(t *testing.T) {
260261

261262
resourceName := "gw-nginx"
262263
nProxyCfg := &graph.EffectiveNginxProxy{
264+
IPFamily: helpers.GetPointer(ngfAPIv1alpha2.IPv4),
263265
Logging: &ngfAPIv1alpha2.NginxLogging{
264266
ErrorLevel: helpers.GetPointer(ngfAPIv1alpha2.NginxLogLevelDebug),
265267
AgentLevel: helpers.GetPointer(ngfAPIv1alpha2.AgentLogLevelDebug),
@@ -321,6 +323,7 @@ func TestBuildNginxResourceObjects_NginxProxyConfig(t *testing.T) {
321323
g.Expect(svc.Spec.LoadBalancerIP).To(Equal("1.2.3.4"))
322324
g.Expect(*svc.Spec.LoadBalancerClass).To(Equal("myLoadBalancerClass"))
323325
g.Expect(svc.Spec.LoadBalancerSourceRanges).To(Equal([]string{"5.6.7.8"}))
326+
g.Expect(*svc.Spec.IPFamilyPolicy).To(Equal(corev1.IPFamilyPolicySingleStack))
324327

325328
depObj := objects[5]
326329
dep, ok := depObj.(*appsv1.Deployment)

0 commit comments

Comments
 (0)