This is an automated email from the ASF dual-hosted git repository.

AlinsRan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new 058cb0d2 fix(httproute): read appProtocol for ExternalName services 
(#2798)
058cb0d2 is described below

commit 058cb0d291ec23dcf6fbd1a79f7fa3fc7ac3021e
Author: Arunesh Dwivedi <[email protected]>
AuthorDate: Thu Jul 30 08:29:36 2026 +0530

    fix(httproute): read appProtocol for ExternalName services (#2798)
---
 internal/adc/translator/httproute.go      |  6 ++
 internal/adc/translator/httproute_test.go | 96 +++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git a/internal/adc/translator/httproute.go 
b/internal/adc/translator/httproute.go
index 8b05364a..f8fc66ba 100644
--- a/internal/adc/translator/httproute.go
+++ b/internal/adc/translator/httproute.go
@@ -423,6 +423,12 @@ func (t *Translator) translateBackendRef(tctx 
*provider.TranslateContext, ref ga
                port := 80
                if ref.Port != nil {
                        port = int(*ref.Port)
+                       for _, p := range service.Spec.Ports {
+                               if int(p.Port) == port {
+                                       protocol = ptr.Deref(p.AppProtocol, "")
+                                       break
+                               }
+                       }
                }
                return adctypes.UpstreamNodes{
                        {
diff --git a/internal/adc/translator/httproute_test.go 
b/internal/adc/translator/httproute_test.go
index 001f3c22..727334b4 100644
--- a/internal/adc/translator/httproute_test.go
+++ b/internal/adc/translator/httproute_test.go
@@ -402,6 +402,102 @@ func TestTranslateHTTPRouteUpstreamScheme(t *testing.T) {
        }
 }
 
+func TestTranslateHTTPRouteExternalNameAppProtocol(t *testing.T) {
+       tests := []struct {
+               name          string
+               appProtocol   string
+               wantScheme    string
+               wantWebsocket *bool
+       }{
+               {
+                       name:          "ExternalName with wss appProtocol",
+                       appProtocol:   internaltypes.AppProtocolWSS,
+                       wantScheme:    apiv2.SchemeHTTPS,
+                       wantWebsocket: ptr.To(true),
+               },
+               {
+                       name:          "ExternalName with ws appProtocol",
+                       appProtocol:   internaltypes.AppProtocolWS,
+                       wantScheme:    apiv2.SchemeHTTP,
+                       wantWebsocket: ptr.To(true),
+               },
+               {
+                       name:        "ExternalName with http appProtocol",
+                       appProtocol: internaltypes.AppProtocolHTTP,
+                       wantScheme:  apiv2.SchemeHTTP,
+               },
+               {
+                       name:        "ExternalName without appProtocol",
+                       appProtocol: "",
+                       wantScheme:  "",
+               },
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       translator := NewTranslator(logr.Discard(), "")
+                       tctx := 
provider.NewDefaultTranslateContext(context.Background())
+
+                       const (
+                               namespace   = "default"
+                               serviceName = "external-backend"
+                               portNumber  = 5000
+                       )
+
+                       var appProtocol *string
+                       if tt.appProtocol != "" {
+                               appProtocol = ptr.To(tt.appProtocol)
+                       }
+
+                       serviceKey := types.NamespacedName{Namespace: 
namespace, Name: serviceName}
+                       tctx.Services[serviceKey] = &corev1.Service{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Name:      serviceName,
+                                       Namespace: namespace,
+                               },
+                               Spec: corev1.ServiceSpec{
+                                       Type:         
corev1.ServiceTypeExternalName,
+                                       ExternalName: "example.com",
+                                       Ports: []corev1.ServicePort{{
+                                               Name:        "web",
+                                               Port:        portNumber,
+                                               AppProtocol: appProtocol,
+                                       }},
+                               },
+                       }
+
+                       route := &gatewayv1.HTTPRoute{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Name:      "demo",
+                                       Namespace: namespace,
+                               },
+                               Spec: gatewayv1.HTTPRouteSpec{
+                                       Rules: []gatewayv1.HTTPRouteRule{{
+                                               BackendRefs: 
[]gatewayv1.HTTPBackendRef{{
+                                                       BackendRef: 
gatewayv1.BackendRef{
+                                                               
BackendObjectReference: gatewayv1.BackendObjectReference{
+                                                                       Name: 
gatewayv1.ObjectName(serviceName),
+                                                                       Port: 
ptr.To(gatewayv1.PortNumber(portNumber)),
+                                                               },
+                                                       },
+                                               }},
+                                       }},
+                               },
+                       }
+
+                       result, err := translator.TranslateHTTPRoute(tctx, 
route)
+                       require.NoError(t, err)
+                       require.Len(t, result.Services, 1)
+                       require.NotNil(t, result.Services[0].Upstream)
+                       require.Len(t, result.Services[0].Routes, 1)
+
+                       assert.Equal(t, tt.wantScheme, 
result.Services[0].Upstream.Scheme)
+                       assert.Equal(t, "example.com", 
result.Services[0].Upstream.Nodes[0].Host)
+                       assert.Equal(t, tt.wantWebsocket, 
result.Services[0].Routes[0].EnableWebsocket)
+               })
+       }
+}
+
 func TestAttachBackendTrafficPolicyHealthCheck(t *testing.T) {
        trueVal := true
        falseVal := false

Reply via email to