@@ -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