diff --git a/pkg/apis/networking/ports.go b/pkg/apis/networking/ports.go index 1c5b5301b..6bd1a65f2 100644 --- a/pkg/apis/networking/ports.go +++ b/pkg/apis/networking/ports.go @@ -40,6 +40,11 @@ const ( ServicePortNameHTTPS = "https" ) +var ( + // 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" +) + // ServicePortName returns the port for the app level protocol. func ServicePortName(proto ProtocolType) string { if proto == ProtocolH2C { @@ -55,3 +60,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 nil +} diff --git a/pkg/apis/networking/ports_test.go b/pkg/apis/networking/ports_test.go index a15cda54f..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,30 @@ 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) + } + }) + } +} + +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) } })