Skip to content

Commit

Permalink
add helper for determining app protocol (#904)
Browse files Browse the repository at this point in the history
* add helper for determining app protocol

* use string pointer to match with ServicePort

* the ServicePort field for AppProtocol is a string pointer

* add test

* omit type declaration

* link to kep in variable comment

* refactor: change comparison style
  • Loading branch information
KauzClay authored Jan 10, 2024
1 parent 07c412e commit b48b31e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pkg/apis/networking/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
27 changes: 25 additions & 2 deletions pkg/apis/networking/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand All @@ -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)
}
})
Expand Down

0 comments on commit b48b31e

Please sign in to comment.