Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions internal/provider/kubernetes/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,11 +1605,7 @@ func (r *gatewayAPIReconciler) processServiceClusterForGatewayClass(ep *egv1a1.E
}
}

resourceMap.insertBackendRef(gwapiv1.BackendObjectReference{
Kind: ptr.To(gwapiv1.Kind("Service")),
Namespace: gatewayapi.NamespacePtr(proxySvcNamespace),
Name: gwapiv1.ObjectName(proxySvcName),
})
r.insertProxyServiceIfExists(proxySvcName, proxySvcNamespace, resourceMap)
}

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

r.insertProxyServiceIfExists(proxySvcName, proxySvcNamespace, resourceMap)
}

func (r *gatewayAPIReconciler) insertProxyServiceIfExists(name, namespace string, resourceMap *resourceMappings) {
svcNN := types.NamespacedName{Name: name, Namespace: namespace}
svc := new(corev1.Service)
err := r.client.Get(context.Background(), svcNN, svc)
// Only insert if service exists
if err != nil {
if !kerrors.IsNotFound(err) {
r.log.Error(err, "failed to get proxy service", "namespace", namespace, "name", name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be info, since the system is eventually consistent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update with a follow-up

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, can we also use existing ctx instead of context.Backgound() here

}
return
}
resourceMap.insertBackendRef(gwapiv1.BackendObjectReference{
Kind: ptr.To(gwapiv1.Kind("Service")),
Namespace: gatewayapi.NamespacePtr(proxySvcNamespace),
Name: gwapiv1.ObjectName(proxySvcName),
Kind: ptr.To(gwapiv1.Kind(resource.KindService)),
Namespace: gatewayapi.NamespacePtr(svc.Namespace),
Name: gwapiv1.ObjectName(svc.Name),
})
}

Expand Down
79 changes: 71 additions & 8 deletions internal/provider/kubernetes/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,32 +1249,43 @@ func TestProcessSecurityPolicyObjectRefs(t *testing.T) {
}

func TestProcessServiceClusterForGatewayClass(t *testing.T) {
gcName := "merged-gc"
nsName := "envoy-gateway-system"
testCases := []struct {
name string
gatewayClass *gwapiv1.GatewayClass
envoyProxy *egv1a1.EnvoyProxy
expectedSvcName string
serviceCluster []client.Object
}{
{
name: "when merged gateways and no hardcoded svc name is used",
gatewayClass: &gwapiv1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: "merged-gc",
Name: gcName,
},
},
envoyProxy: nil,
expectedSvcName: proxy.ExpectedResourceHashedName("merged-gc"),
expectedSvcName: proxy.ExpectedResourceHashedName(gcName),
serviceCluster: []client.Object{
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: proxy.ExpectedResourceHashedName(gcName),
Namespace: nsName,
},
},
},
},
{
name: "when merged gateways and a hardcoded svc name is used",
gatewayClass: &gwapiv1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: "merged-gc",
Name: gcName,
},
},
envoyProxy: &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "merged-gc",
Name: gcName,
},
Spec: egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Expand All @@ -1288,6 +1299,25 @@ func TestProcessServiceClusterForGatewayClass(t *testing.T) {
},
},
expectedSvcName: "merged-gc-svc",
serviceCluster: []client.Object{
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "merged-gc-svc",
Namespace: nsName,
},
},
},
},
{
name: "non-existent proxy service",
gatewayClass: &gwapiv1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: gcName,
},
},
envoyProxy: nil,
expectedSvcName: proxy.ExpectedResourceHashedName(gcName),
serviceCluster: nil,
},
}

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

r := newGatewayAPIReconciler(logger)
r.client = fakeclient.NewClientBuilder().
WithScheme(envoygateway.GetScheme()).
WithObjects(tc.serviceCluster...).
Build()
r.namespace = "envoy-gateway-system"

r.processServiceClusterForGatewayClass(tc.envoyProxy, tc.gatewayClass, resourceMap)
Expand All @@ -1309,8 +1343,12 @@ func TestProcessServiceClusterForGatewayClass(t *testing.T) {
Name: gwapiv1.ObjectName(tc.expectedSvcName),
}
key := backendRefKey(&expectedRef)
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
if tc.serviceCluster != nil {
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
} else {
require.NotContains(t, resourceMap.allAssociatedBackendRefs, key)
}
})
}
}
Expand All @@ -1324,6 +1362,7 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
gatewayNamespacedMode bool
expectedSvcName string
expectedSvcNamespace string
serviceCluster []client.Object
}{
{
name: "no gateway namespaced mode with no hardcoded service name",
Expand Down Expand Up @@ -1377,6 +1416,14 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
gatewayNamespacedMode: true,
expectedSvcName: "my-gateway",
expectedSvcNamespace: "app-namespace",
serviceCluster: []client.Object{
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "my-gateway",
Namespace: "app-namespace",
},
},
},
},
{
name: "gateway namespaced mode with hardcoded service name",
Expand Down Expand Up @@ -1404,6 +1451,14 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
gatewayNamespacedMode: true,
expectedSvcName: "my-gateway-svc",
expectedSvcNamespace: "app-namespace",
serviceCluster: []client.Object{
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "my-gateway-svc",
Namespace: "app-namespace",
},
},
},
},
{
name: "no gateway namespaced mode with no hardcoded service name attached gatewayclass",
Expand Down Expand Up @@ -1443,6 +1498,10 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
resourceMap := newResourceMapping()

r := newGatewayAPIReconciler(logger)
r.client = fakeclient.NewClientBuilder().
WithScheme(envoygateway.GetScheme()).
WithObjects(tc.serviceCluster...).
Build()
r.namespace = "envoy-gateway-system"
r.gatewayNamespaceMode = tc.gatewayNamespacedMode

Expand All @@ -1466,8 +1525,12 @@ func TestProcessServiceClusterForGateway(t *testing.T) {
Name: gwapiv1.ObjectName(tc.expectedSvcName),
}
key := backendRefKey(&expectedRef)
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
if tc.serviceCluster != nil {
require.Contains(t, resourceMap.allAssociatedBackendRefs, key)
require.Equal(t, expectedRef, resourceMap.allAssociatedBackendRefs[key])
} else {
require.NotContains(t, resourceMap.allAssociatedBackendRefs, key)
}
})
}
}
Expand Down