From f6b7746400b474d60366b4c52d4f7fa9a719ff25 Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Wed, 3 Jan 2024 10:56:14 -0500 Subject: [PATCH 1/6] add helper for determining app protocol --- pkg/apis/networking/ports.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/apis/networking/ports.go b/pkg/apis/networking/ports.go index 1c5b5301b..32a0e2b3c 100644 --- a/pkg/apis/networking/ports.go +++ b/pkg/apis/networking/ports.go @@ -38,6 +38,9 @@ const ( // ServicePortNameHTTPS is the name of the external port of the service for HTTPS ServicePortNameHTTPS = "https" + + // AppProtocolH2C is the name of the external port of the service for HTTP/2 + AppProtocolH2C = "kubernetes.io/h2c" ) // ServicePortName returns the port for the app level protocol. @@ -55,3 +58,11 @@ func ServicePort(proto ProtocolType) int { } return ServiceHTTPPort } + +// AppProtocol returns the value for app level protocol based on the ProtocolType +func AppProtocol(proto ProtocolType) string { + if proto == ProtocolH2C { + return AppProtocolH2C + } + return "" +} From 986261340d738fd8d152309e27ebc7aee4954caf Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Wed, 3 Jan 2024 11:27:22 -0500 Subject: [PATCH 2/6] use string pointer to match with ServicePort * the ServicePort field for AppProtocol is a string pointer --- pkg/apis/networking/ports.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/apis/networking/ports.go b/pkg/apis/networking/ports.go index 32a0e2b3c..abe80d05e 100644 --- a/pkg/apis/networking/ports.go +++ b/pkg/apis/networking/ports.go @@ -60,9 +60,9 @@ func ServicePort(proto ProtocolType) int { } // AppProtocol returns the value for app level protocol based on the ProtocolType -func AppProtocol(proto ProtocolType) string { +func AppProtocol(proto ProtocolType) (ap *string) { if proto == ProtocolH2C { - return AppProtocolH2C + *ap = AppProtocolH2C } - return "" + return } From cb6eb7b8297b9899b8bc90ca6ecbec33816caf4d Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Wed, 3 Jan 2024 12:51:31 -0500 Subject: [PATCH 3/6] add test --- pkg/apis/networking/ports.go | 10 ++++++---- pkg/apis/networking/ports_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pkg/apis/networking/ports.go b/pkg/apis/networking/ports.go index abe80d05e..f3a6f5a34 100644 --- a/pkg/apis/networking/ports.go +++ b/pkg/apis/networking/ports.go @@ -38,9 +38,11 @@ const ( // ServicePortNameHTTPS is the name of the external port of the service for HTTPS ServicePortNameHTTPS = "https" +) +var ( // AppProtocolH2C is the name of the external port of the service for HTTP/2 - AppProtocolH2C = "kubernetes.io/h2c" + AppProtocolH2C string = "kubernetes.io/h2c" ) // ServicePortName returns the port for the app level protocol. @@ -60,9 +62,9 @@ func ServicePort(proto ProtocolType) int { } // AppProtocol returns the value for app level protocol based on the ProtocolType -func AppProtocol(proto ProtocolType) (ap *string) { +func AppProtocol(proto ProtocolType) *string { if proto == ProtocolH2C { - *ap = AppProtocolH2C + return &AppProtocolH2C } - return + return nil } diff --git a/pkg/apis/networking/ports_test.go b/pkg/apis/networking/ports_test.go index a15cda54f..6518c2cb1 100644 --- a/pkg/apis/networking/ports_test.go +++ b/pkg/apis/networking/ports_test.go @@ -65,3 +65,26 @@ func TestServicePort(t *testing.T) { }) } } + +func TestAppProtocol(t *testing.T) { + cases := []struct { + name string + proto ProtocolType + expect *string + }{{ + name: "pass h2c protocol to get Serving and Activator K8s services for HTTP/2 endpoints", + proto: ProtocolH2C, + expect: &AppProtocolH2C, + }, { + name: "other protocols result in nil", + proto: ProtocolHTTP1, + expect: nil, + }} + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got, want := AppProtocol(c.proto), c.expect; !(got == want) { + t.Errorf("got = %d, want: %d", got, want) + } + }) + } +} From 99c292ac8072cb35f795b21adf74b83ae4f41d29 Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Wed, 3 Jan 2024 13:37:13 -0500 Subject: [PATCH 4/6] omit type declaration --- pkg/apis/networking/ports.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/apis/networking/ports.go b/pkg/apis/networking/ports.go index f3a6f5a34..cd8626956 100644 --- a/pkg/apis/networking/ports.go +++ b/pkg/apis/networking/ports.go @@ -42,7 +42,7 @@ const ( var ( // AppProtocolH2C is the name of the external port of the service for HTTP/2 - AppProtocolH2C string = "kubernetes.io/h2c" + AppProtocolH2C = "kubernetes.io/h2c" ) // ServicePortName returns the port for the app level protocol. From ec467be8d64b9845e26afb5f55c300983eec8c38 Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Tue, 9 Jan 2024 13:34:33 -0500 Subject: [PATCH 5/6] link to kep in variable comment --- pkg/apis/networking/ports.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/apis/networking/ports.go b/pkg/apis/networking/ports.go index cd8626956..6bd1a65f2 100644 --- a/pkg/apis/networking/ports.go +++ b/pkg/apis/networking/ports.go @@ -41,7 +41,7 @@ const ( ) var ( - // AppProtocolH2C is the name of the external port of the service for HTTP/2 + // AppProtocolH2C is the name of the external port of the service for HTTP/2, from https://github.com/kubernetes/enhancements/tree/master/keps/sig-network/3726-standard-application-protocols#new-standard-protocols AppProtocolH2C = "kubernetes.io/h2c" ) From f03650e8fbffb11cf7107e1090dacaa810d75dc2 Mon Sep 17 00:00:00 2001 From: Clay Kauzlaric Date: Wed, 10 Jan 2024 12:47:35 -0500 Subject: [PATCH 6/6] refactor: change comparison style --- pkg/apis/networking/ports_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/apis/networking/ports_test.go b/pkg/apis/networking/ports_test.go index 6518c2cb1..3257caf5c 100644 --- a/pkg/apis/networking/ports_test.go +++ b/pkg/apis/networking/ports_test.go @@ -36,7 +36,7 @@ func TestServicePortName(t *testing.T) { }} for _, c := range cases { t.Run(c.name, func(t *testing.T) { - if got, want := ServicePortName(c.proto), c.expect; !(got == want) { + if got, want := ServicePortName(c.proto), c.expect; got != want { t.Errorf("got = %s, want: %s", got, want) } }) @@ -59,7 +59,7 @@ func TestServicePort(t *testing.T) { }} for _, c := range cases { t.Run(c.name, func(t *testing.T) { - if got, want := ServicePort(c.proto), c.expect; !(got == want) { + if got, want := ServicePort(c.proto), c.expect; got != want { t.Errorf("got = %d, want: %d", got, want) } }) @@ -82,7 +82,7 @@ func TestAppProtocol(t *testing.T) { }} for _, c := range cases { t.Run(c.name, func(t *testing.T) { - if got, want := AppProtocol(c.proto), c.expect; !(got == want) { + if got, want := AppProtocol(c.proto), c.expect; got != want { t.Errorf("got = %d, want: %d", got, want) } })