Skip to content

Commit ea42fea

Browse files
authored
fix: only insert proxy service once it exists (#7424)
* maybe this is the fix? Signed-off-by: jukie <[email protected]> * fixes Signed-off-by: jukie <[email protected]> * cleanup Signed-off-by: jukie <[email protected]> * consolidate Signed-off-by: jukie <[email protected]> * fix Signed-off-by: jukie <[email protected]> --------- Signed-off-by: jukie <[email protected]>
1 parent 5a95a04 commit ea42fea

File tree

2 files changed

+89
-16
lines changed

2 files changed

+89
-16
lines changed

internal/provider/kubernetes/controller.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,11 +1605,7 @@ func (r *gatewayAPIReconciler) processServiceClusterForGatewayClass(ep *egv1a1.E
16051605
}
16061606
}
16071607

1608-
resourceMap.insertBackendRef(gwapiv1.BackendObjectReference{
1609-
Kind: ptr.To(gwapiv1.Kind("Service")),
1610-
Namespace: gatewayapi.NamespacePtr(proxySvcNamespace),
1611-
Name: gwapiv1.ObjectName(proxySvcName),
1612-
})
1608+
r.insertProxyServiceIfExists(proxySvcName, proxySvcNamespace, resourceMap)
16131609
}
16141610

16151611
// Called on a Gateway when merged gateways mode is not enabled for its parent GatewayClass.
@@ -1634,10 +1630,24 @@ func (r *gatewayAPIReconciler) processServiceClusterForGateway(ep *egv1a1.EnvoyP
16341630
}
16351631
}
16361632

1633+
r.insertProxyServiceIfExists(proxySvcName, proxySvcNamespace, resourceMap)
1634+
}
1635+
1636+
func (r *gatewayAPIReconciler) insertProxyServiceIfExists(name, namespace string, resourceMap *resourceMappings) {
1637+
svcNN := types.NamespacedName{Name: name, Namespace: namespace}
1638+
svc := new(corev1.Service)
1639+
err := r.client.Get(context.Background(), svcNN, svc)
1640+
// Only insert if service exists
1641+
if err != nil {
1642+
if !kerrors.IsNotFound(err) {
1643+
r.log.Error(err, "failed to get proxy service", "namespace", namespace, "name", name)
1644+
}
1645+
return
1646+
}
16371647
resourceMap.insertBackendRef(gwapiv1.BackendObjectReference{
1638-
Kind: ptr.To(gwapiv1.Kind("Service")),
1639-
Namespace: gatewayapi.NamespacePtr(proxySvcNamespace),
1640-
Name: gwapiv1.ObjectName(proxySvcName),
1648+
Kind: ptr.To(gwapiv1.Kind(resource.KindService)),
1649+
Namespace: gatewayapi.NamespacePtr(svc.Namespace),
1650+
Name: gwapiv1.ObjectName(svc.Name),
16411651
})
16421652
}
16431653

internal/provider/kubernetes/controller_test.go

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,32 +1249,43 @@ func TestProcessSecurityPolicyObjectRefs(t *testing.T) {
12491249
}
12501250

12511251
func TestProcessServiceClusterForGatewayClass(t *testing.T) {
1252+
gcName := "merged-gc"
1253+
nsName := "envoy-gateway-system"
12521254
testCases := []struct {
12531255
name string
12541256
gatewayClass *gwapiv1.GatewayClass
12551257
envoyProxy *egv1a1.EnvoyProxy
12561258
expectedSvcName string
1259+
serviceCluster []client.Object
12571260
}{
12581261
{
12591262
name: "when merged gateways and no hardcoded svc name is used",
12601263
gatewayClass: &gwapiv1.GatewayClass{
12611264
ObjectMeta: metav1.ObjectMeta{
1262-
Name: "merged-gc",
1265+
Name: gcName,
12631266
},
12641267
},
12651268
envoyProxy: nil,
1266-
expectedSvcName: proxy.ExpectedResourceHashedName("merged-gc"),
1269+
expectedSvcName: proxy.ExpectedResourceHashedName(gcName),
1270+
serviceCluster: []client.Object{
1271+
&corev1.Service{
1272+
ObjectMeta: metav1.ObjectMeta{
1273+
Name: proxy.ExpectedResourceHashedName(gcName),
1274+
Namespace: nsName,
1275+
},
1276+
},
1277+
},
12671278
},
12681279
{
12691280
name: "when merged gateways and a hardcoded svc name is used",
12701281
gatewayClass: &gwapiv1.GatewayClass{
12711282
ObjectMeta: metav1.ObjectMeta{
1272-
Name: "merged-gc",
1283+
Name: gcName,
12731284
},
12741285
},
12751286
envoyProxy: &egv1a1.EnvoyProxy{
12761287
ObjectMeta: metav1.ObjectMeta{
1277-
Name: "merged-gc",
1288+
Name: gcName,
12781289
},
12791290
Spec: egv1a1.EnvoyProxySpec{
12801291
Provider: &egv1a1.EnvoyProxyProvider{
@@ -1288,6 +1299,25 @@ func TestProcessServiceClusterForGatewayClass(t *testing.T) {
12881299
},
12891300
},
12901301
expectedSvcName: "merged-gc-svc",
1302+
serviceCluster: []client.Object{
1303+
&corev1.Service{
1304+
ObjectMeta: metav1.ObjectMeta{
1305+
Name: "merged-gc-svc",
1306+
Namespace: nsName,
1307+
},
1308+
},
1309+
},
1310+
},
1311+
{
1312+
name: "non-existent proxy service",
1313+
gatewayClass: &gwapiv1.GatewayClass{
1314+
ObjectMeta: metav1.ObjectMeta{
1315+
Name: gcName,
1316+
},
1317+
},
1318+
envoyProxy: nil,
1319+
expectedSvcName: proxy.ExpectedResourceHashedName(gcName),
1320+
serviceCluster: nil,
12911321
},
12921322
}
12931323

@@ -1299,6 +1329,10 @@ func TestProcessServiceClusterForGatewayClass(t *testing.T) {
12991329
resourceMap := newResourceMapping()
13001330

13011331
r := newGatewayAPIReconciler(logger)
1332+
r.client = fakeclient.NewClientBuilder().
1333+
WithScheme(envoygateway.GetScheme()).
1334+
WithObjects(tc.serviceCluster...).
1335+
Build()
13021336
r.namespace = "envoy-gateway-system"
13031337

13041338
r.processServiceClusterForGatewayClass(tc.envoyProxy, tc.gatewayClass, resourceMap)
@@ -1309,8 +1343,12 @@ func TestProcessServiceClusterForGatewayClass(t *testing.T) {
13091343
Name: gwapiv1.ObjectName(tc.expectedSvcName),
13101344
}
13111345
key := backendRefKey(&expectedRef)
1312-
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
1313-
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
1346+
if tc.serviceCluster != nil {
1347+
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
1348+
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
1349+
} else {
1350+
require.NotContains(t, resourceMap.allAssociatedBackendRefs, key)
1351+
}
13141352
})
13151353
}
13161354
}
@@ -1324,6 +1362,7 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
13241362
gatewayNamespacedMode bool
13251363
expectedSvcName string
13261364
expectedSvcNamespace string
1365+
serviceCluster []client.Object
13271366
}{
13281367
{
13291368
name: "no gateway namespaced mode with no hardcoded service name",
@@ -1377,6 +1416,14 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
13771416
gatewayNamespacedMode: true,
13781417
expectedSvcName: "my-gateway",
13791418
expectedSvcNamespace: "app-namespace",
1419+
serviceCluster: []client.Object{
1420+
&corev1.Service{
1421+
ObjectMeta: metav1.ObjectMeta{
1422+
Name: "my-gateway",
1423+
Namespace: "app-namespace",
1424+
},
1425+
},
1426+
},
13801427
},
13811428
{
13821429
name: "gateway namespaced mode with hardcoded service name",
@@ -1404,6 +1451,14 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
14041451
gatewayNamespacedMode: true,
14051452
expectedSvcName: "my-gateway-svc",
14061453
expectedSvcNamespace: "app-namespace",
1454+
serviceCluster: []client.Object{
1455+
&corev1.Service{
1456+
ObjectMeta: metav1.ObjectMeta{
1457+
Name: "my-gateway-svc",
1458+
Namespace: "app-namespace",
1459+
},
1460+
},
1461+
},
14071462
},
14081463
{
14091464
name: "no gateway namespaced mode with no hardcoded service name attached gatewayclass",
@@ -1443,6 +1498,10 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
14431498
resourceMap := newResourceMapping()
14441499

14451500
r := newGatewayAPIReconciler(logger)
1501+
r.client = fakeclient.NewClientBuilder().
1502+
WithScheme(envoygateway.GetScheme()).
1503+
WithObjects(tc.serviceCluster...).
1504+
Build()
14461505
r.namespace = "envoy-gateway-system"
14471506
r.gatewayNamespaceMode = tc.gatewayNamespacedMode
14481507

@@ -1466,8 +1525,12 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
14661525
Name: gwapiv1.ObjectName(tc.expectedSvcName),
14671526
}
14681527
key := backendRefKey(&expectedRef)
1469-
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
1470-
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
1528+
if tc.serviceCluster != nil {
1529+
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
1530+
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
1531+
} else {
1532+
require.NotContains(t, resourceMap.allAssociatedBackendRefs, key)
1533+
}
14711534
})
14721535
}
14731536
}

0 commit comments

Comments
 (0)