Skip to content

Commit f250ec3

Browse files
authored
Set proper IP family policy on NGINX LB Service (#3475) (#3476)
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 00e246f commit f250ec3

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

internal/controller/provisioner/objects.go

Lines changed: 14 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,17 @@ 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+
if *nProxyCfg.IPFamily == ngfAPIv1alpha2.IPv4 {
489+
svc.Spec.IPFamilies = []corev1.IPFamily{corev1.IPv4Protocol}
490+
} else {
491+
svc.Spec.IPFamilies = []corev1.IPFamily{corev1.IPv6Protocol}
492+
}
493+
}
494+
}
495+
482496
func (p *NginxProvisioner) buildNginxDeployment(
483497
objectMeta metav1.ObjectMeta,
484498
nProxyCfg *graph.EffectiveNginxProxy,

internal/controller/provisioner/objects_test.go

Lines changed: 41 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,8 @@ 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))
327+
g.Expect(svc.Spec.IPFamilies).To(Equal([]corev1.IPFamily{corev1.IPv4Protocol}))
324328

325329
depObj := objects[5]
326330
dep, ok := depObj.(*appsv1.Deployment)
@@ -961,3 +965,40 @@ func TestBuildNginxResourceObjectsForDeletion_OpenShift(t *testing.T) {
961965
g.Expect(ok).To(BeTrue())
962966
validateMeta(roleBinding, deploymentNSName.Name)
963967
}
968+
969+
func TestSetIPFamily(t *testing.T) {
970+
t.Parallel()
971+
g := NewWithT(t)
972+
973+
newSvc := func() *corev1.Service {
974+
return &corev1.Service{
975+
Spec: corev1.ServiceSpec{},
976+
}
977+
}
978+
979+
// nProxyCfg is nil, should not set anything
980+
svc := newSvc()
981+
setIPFamily(nil, svc)
982+
g.Expect(svc.Spec.IPFamilyPolicy).To(BeNil())
983+
g.Expect(svc.Spec.IPFamilies).To(BeNil())
984+
985+
// nProxyCfg.IPFamily is nil, should not set anything
986+
svc = newSvc()
987+
setIPFamily(&graph.EffectiveNginxProxy{}, svc)
988+
g.Expect(svc.Spec.IPFamilyPolicy).To(BeNil())
989+
g.Expect(svc.Spec.IPFamilies).To(BeNil())
990+
991+
// nProxyCfg.IPFamily is IPv4, should set SingleStack and IPFamilies to IPv4
992+
svc = newSvc()
993+
ipFamily := ngfAPIv1alpha2.IPv4
994+
setIPFamily(&graph.EffectiveNginxProxy{IPFamily: &ipFamily}, svc)
995+
g.Expect(svc.Spec.IPFamilyPolicy).To(Equal(helpers.GetPointer(corev1.IPFamilyPolicySingleStack)))
996+
g.Expect(svc.Spec.IPFamilies).To(Equal([]corev1.IPFamily{corev1.IPv4Protocol}))
997+
998+
// nProxyCfg.IPFamily is IPv6, should set SingleStack and IPFamilies to IPv6
999+
svc = newSvc()
1000+
ipFamily = ngfAPIv1alpha2.IPv6
1001+
setIPFamily(&graph.EffectiveNginxProxy{IPFamily: &ipFamily}, svc)
1002+
g.Expect(svc.Spec.IPFamilyPolicy).To(Equal(helpers.GetPointer(corev1.IPFamilyPolicySingleStack)))
1003+
g.Expect(svc.Spec.IPFamilies).To(Equal([]corev1.IPFamily{corev1.IPv6Protocol}))
1004+
}

0 commit comments

Comments
 (0)