From e5602d72da89199790eaeefa7816e422758d656d Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Fri, 12 Jan 2024 14:38:02 -0500 Subject: [PATCH] Set AppProtocol on Services (#14757) * set app protocol field on services * fix serverlessservice test * set app protocol on endpoints * set app protocol in test service * fix expected service in test --- pkg/reconciler/route/resources/service.go | 19 +++++----- .../route/resources/service_test.go | 24 +++++++------ .../serverlessservice/resources/services.go | 16 +++++---- .../resources/services_test.go | 36 ++++++++++--------- .../serverlessservice_test.go | 2 ++ pkg/testing/functional.go | 7 ++-- 6 files changed, 60 insertions(+), 44 deletions(-) diff --git a/pkg/reconciler/route/resources/service.go b/pkg/reconciler/route/resources/service.go index 2a19da7581a6..5967cf2cdb72 100644 --- a/pkg/reconciler/route/resources/service.go +++ b/pkg/reconciler/route/resources/service.go @@ -60,9 +60,10 @@ func MakeK8sPlaceholderService(ctx context.Context, route *v1.Route, tagName str ExternalName: domainName, SessionAffinity: corev1.ServiceAffinityNone, Ports: []corev1.ServicePort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), - TargetPort: intstr.FromInt(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), + TargetPort: intstr.FromInt(80), }}, }, }, nil @@ -101,9 +102,10 @@ func MakeK8sService(ctx context.Context, route *v1.Route, tagName string, ingres ObjectMeta: makeServiceObjectMeta(hostname, route), Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), - TargetPort: intstr.FromInt(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), + TargetPort: intstr.FromInt(80), }}, }, }, @@ -128,8 +130,9 @@ func MakeK8sService(ctx context.Context, route *v1.Route, tagName string, ingres IP: balancer.IP, }}, Ports: []corev1.EndpointPort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), }}, }}, } diff --git a/pkg/reconciler/route/resources/service_test.go b/pkg/reconciler/route/resources/service_test.go index 1664fb46647d..36de58b19d12 100644 --- a/pkg/reconciler/route/resources/service_test.go +++ b/pkg/reconciler/route/resources/service_test.go @@ -54,9 +54,10 @@ var ( } expectedPorts = []corev1.ServicePort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), - TargetPort: intstr.FromInt(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), + TargetPort: intstr.FromInt(80), }} ) @@ -123,8 +124,9 @@ func TestMakeK8SService(t *testing.T) { IP: "some-ip", }}, Ports: []corev1.EndpointPort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), }}, }}, }, { @@ -200,8 +202,9 @@ func TestMakeK8SService(t *testing.T) { IP: "some-ip", }}, Ports: []corev1.EndpointPort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), }}, }}, }, { @@ -392,9 +395,10 @@ func TestMakePlaceholderService(t *testing.T) { ExternalName: tt.expectedExternalName, SessionAffinity: corev1.ServiceAffinityNone, Ports: []corev1.ServicePort{{ - Name: netapi.ServicePortNameH2C, - Port: int32(80), - TargetPort: intstr.FromInt(80), + Name: netapi.ServicePortNameH2C, + AppProtocol: &netapi.AppProtocolH2C, + Port: int32(80), + TargetPort: intstr.FromInt(80), }}, } diff --git a/pkg/reconciler/serverlessservice/resources/services.go b/pkg/reconciler/serverlessservice/resources/services.go index 7ad61ad674ed..5672fc12e3df 100644 --- a/pkg/reconciler/serverlessservice/resources/services.go +++ b/pkg/reconciler/serverlessservice/resources/services.go @@ -60,10 +60,11 @@ func MakePublicService(sks *v1alpha1.ServerlessService) *corev1.Service { func makePublicServicePorts(sks *v1alpha1.ServerlessService) []corev1.ServicePort { ports := []corev1.ServicePort{{ - Name: pkgnet.ServicePortName(sks.Spec.ProtocolType), - Protocol: corev1.ProtocolTCP, - Port: int32(pkgnet.ServicePort(sks.Spec.ProtocolType)), - TargetPort: targetPort(sks), + Name: pkgnet.ServicePortName(sks.Spec.ProtocolType), + Protocol: corev1.ProtocolTCP, + AppProtocol: pkgnet.AppProtocol(sks.Spec.ProtocolType), + Port: int32(pkgnet.ServicePort(sks.Spec.ProtocolType)), + TargetPort: targetPort(sks), }, { // The HTTPS port is used when activator-ca is enabled. // Although it is not used by default, we put it here as it should be harmless @@ -144,9 +145,10 @@ func MakePrivateService(sks *v1alpha1.ServerlessService, selector map[string]str }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{{ - Name: pkgnet.ServicePortName(sks.Spec.ProtocolType), - Protocol: corev1.ProtocolTCP, - Port: pkgnet.ServiceHTTPPort, + Name: pkgnet.ServicePortName(sks.Spec.ProtocolType), + Protocol: corev1.ProtocolTCP, + AppProtocol: pkgnet.AppProtocol(sks.Spec.ProtocolType), + Port: pkgnet.ServiceHTTPPort, // This one is matching the public one, since this is the // port queue-proxy listens on. TargetPort: targetPort(sks), diff --git a/pkg/reconciler/serverlessservice/resources/services_test.go b/pkg/reconciler/serverlessservice/resources/services_test.go index 61f7bad027ad..0690f52d3d07 100644 --- a/pkg/reconciler/serverlessservice/resources/services_test.go +++ b/pkg/reconciler/serverlessservice/resources/services_test.go @@ -182,10 +182,11 @@ func TestMakePublicService(t *testing.T) { }), want: svc(networking.ServiceTypePublic, func(s *corev1.Service) { s.Spec.Ports = []corev1.ServicePort{{ - Name: pkgnet.ServicePortNameH2C, - Protocol: corev1.ProtocolTCP, - Port: pkgnet.ServiceHTTP2Port, - TargetPort: intstr.FromInt(networking.BackendHTTP2Port), + Name: pkgnet.ServicePortNameH2C, + Protocol: corev1.ProtocolTCP, + AppProtocol: &pkgnet.AppProtocolH2C, + Port: pkgnet.ServiceHTTP2Port, + TargetPort: intstr.FromInt(networking.BackendHTTP2Port), }, { Name: pkgnet.ServicePortNameHTTPS, Protocol: corev1.ProtocolTCP, @@ -202,10 +203,11 @@ func TestMakePublicService(t *testing.T) { }), want: svc(networking.ServiceTypePublic, func(s *corev1.Service) { s.Spec.Ports = []corev1.ServicePort{{ - Name: pkgnet.ServicePortNameH2C, - Protocol: corev1.ProtocolTCP, - Port: pkgnet.ServiceHTTP2Port, - TargetPort: intstr.FromInt(networking.BackendHTTP2Port), + Name: pkgnet.ServicePortNameH2C, + Protocol: corev1.ProtocolTCP, + AppProtocol: &pkgnet.AppProtocolH2C, + Port: pkgnet.ServiceHTTP2Port, + TargetPort: intstr.FromInt(networking.BackendHTTP2Port), }, { Name: pkgnet.ServicePortNameHTTPS, Protocol: corev1.ProtocolTCP, @@ -222,10 +224,11 @@ func TestMakePublicService(t *testing.T) { }), want: svc(networking.ServiceTypePublic, func(s *corev1.Service) { s.Spec.Ports = []corev1.ServicePort{{ - Name: pkgnet.ServicePortNameH2C, - Protocol: corev1.ProtocolTCP, - Port: pkgnet.ServiceHTTP2Port, - TargetPort: intstr.FromInt(networking.BackendHTTP2Port), + Name: pkgnet.ServicePortNameH2C, + Protocol: corev1.ProtocolTCP, + AppProtocol: &pkgnet.AppProtocolH2C, + Port: pkgnet.ServiceHTTP2Port, + TargetPort: intstr.FromInt(networking.BackendHTTP2Port), }, { Name: pkgnet.ServicePortNameHTTPS, Protocol: corev1.ProtocolTCP, @@ -470,10 +473,11 @@ func TestMakePrivateService(t *testing.T) { }, privateSvcMod, func(s *corev1.Service) { // And now patch port to be http2. s.Spec.Ports[0] = corev1.ServicePort{ - Name: pkgnet.ServicePortNameH2C, - Protocol: corev1.ProtocolTCP, - Port: pkgnet.ServiceHTTPPort, - TargetPort: intstr.FromInt(networking.BackendHTTP2Port), + Name: pkgnet.ServicePortNameH2C, + Protocol: corev1.ProtocolTCP, + AppProtocol: &pkgnet.AppProtocolH2C, + Port: pkgnet.ServiceHTTPPort, + TargetPort: intstr.FromInt(networking.BackendHTTP2Port), } s.Spec.Ports[5] = corev1.ServicePort{ Name: pkgnet.ServicePortNameH2C + "-istio", diff --git a/pkg/reconciler/serverlessservice/serverlessservice_test.go b/pkg/reconciler/serverlessservice/serverlessservice_test.go index 2682f680667f..3a743236f8e2 100644 --- a/pkg/reconciler/serverlessservice/serverlessservice_test.go +++ b/pkg/reconciler/serverlessservice/serverlessservice_test.go @@ -803,6 +803,7 @@ func deploy(namespace, name string, opts ...deploymentOption) *appsv1.Deployment func withHTTP2Priv(svc *corev1.Service) { svc.Spec.Ports[0].Name = "http2" svc.Spec.Ports[0].TargetPort = intstr.FromInt(networking.BackendHTTP2Port) + svc.Spec.Ports[0].AppProtocol = &pkgnet.AppProtocolH2C svc.Spec.Ports[5].Name = "http2-istio" svc.Spec.Ports[5].Port = networking.BackendHTTP2Port @@ -812,6 +813,7 @@ func withHTTP2Priv(svc *corev1.Service) { func withHTTP2(svc *corev1.Service) { svc.Spec.Ports[0].Port = pkgnet.ServiceHTTP2Port svc.Spec.Ports[0].Name = "http2" + svc.Spec.Ports[0].AppProtocol = &pkgnet.AppProtocolH2C svc.Spec.Ports[0].TargetPort = intstr.FromInt(networking.BackendHTTP2Port) } diff --git a/pkg/testing/functional.go b/pkg/testing/functional.go index c04d5c9e30d7..f0ee41eb7658 100644 --- a/pkg/testing/functional.go +++ b/pkg/testing/functional.go @@ -256,9 +256,10 @@ func WithExternalName(name string) K8sServiceOption { return func(svc *corev1.Service) { svc.Spec.ExternalName = name svc.Spec.Ports = []corev1.ServicePort{{ - Name: networking.ServicePortNameH2C, - Port: int32(80), - TargetPort: intstr.FromInt(80), + Name: networking.ServicePortNameH2C, + AppProtocol: &networking.AppProtocolH2C, + Port: int32(80), + TargetPort: intstr.FromInt(80), }} } }