diff --git a/pkg/api/conversion.go b/pkg/api/conversion.go index db3b0cb5241..f21eed92bef 100644 --- a/pkg/api/conversion.go +++ b/pkg/api/conversion.go @@ -18,6 +18,8 @@ package api import ( "fmt" + "strconv" + "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/conversion" @@ -67,6 +69,8 @@ func addConversionFuncs(scheme *runtime.Scheme) error { Convert_map_to_unversioned_LabelSelector, Convert_unversioned_LabelSelector_to_map, + + Convert_Slice_string_To_Slice_int32, ) } @@ -248,3 +252,19 @@ func Convert_unversioned_LabelSelector_to_map(in *metav1.LabelSelector, out *map } return err } + +// Convert_Slice_string_To_Slice_int32 converts multiple query parameters or +// a single query parameter with a comma delimited value to multiple int32. +// This is used for port forwarding which needs the ports as int32. +func Convert_Slice_string_To_Slice_int32(in *[]string, out *[]int32, s conversion.Scope) error { + for _, s := range *in { + for _, v := range strings.Split(s, ",") { + x, err := strconv.ParseUint(v, 10, 16) + if err != nil { + return fmt.Errorf("cannot convert to []int32: %v", err) + } + *out = append(*out, int32(x)) + } + } + return nil +} diff --git a/pkg/api/install/install.go b/pkg/api/install/install.go index 9fcaa1b9984..0bbe3d81de9 100644 --- a/pkg/api/install/install.go +++ b/pkg/api/install/install.go @@ -102,6 +102,7 @@ func newRESTMapper(externalVersions []schema.GroupVersion) meta.RESTMapper { "PodLogOptions", "PodExecOptions", "PodAttachOptions", + "PodPortForwardOptions", "PodProxyOptions", "NodeProxyOptions", "ServiceProxyOptions", diff --git a/pkg/api/register.go b/pkg/api/register.go index 1847e0c057a..8988cef184b 100644 --- a/pkg/api/register.go +++ b/pkg/api/register.go @@ -125,6 +125,7 @@ func addKnownTypes(scheme *runtime.Scheme) error { &PodAttachOptions{}, &PodLogOptions{}, &PodExecOptions{}, + &PodPortForwardOptions{}, &PodProxyOptions{}, &ComponentStatus{}, &ComponentStatusList{}, diff --git a/pkg/api/testing/fuzzer.go b/pkg/api/testing/fuzzer.go index 5fe1e082665..133dc979943 100644 --- a/pkg/api/testing/fuzzer.go +++ b/pkg/api/testing/fuzzer.go @@ -117,6 +117,14 @@ func FuzzerFor(t *testing.T, version schema.GroupVersion, src rand.Source) *fuzz j.Stdout = true j.Stderr = true }, + func(j *api.PodPortForwardOptions, c fuzz.Continue) { + if c.RandBool() { + j.Ports = make([]int32, c.Intn(10)) + for i := range j.Ports { + j.Ports[i] = c.Int31n(65535) + } + } + }, func(s *api.PodSpec, c fuzz.Continue) { c.FuzzNoCustom(s) // has a default value diff --git a/pkg/api/types.go b/pkg/api/types.go index 710168b306c..1d4a8f70c70 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -2953,6 +2953,15 @@ type PodExecOptions struct { Command []string } +// PodPortForwardOptions is the query options to a Pod's port forward call +type PodPortForwardOptions struct { + metav1.TypeMeta + + // The list of ports to forward + // +optional + Ports []int32 +} + // PodProxyOptions is the query options to a Pod's proxy call type PodProxyOptions struct { metav1.TypeMeta diff --git a/pkg/api/v1/register.go b/pkg/api/v1/register.go index 915a66f6f15..2ad0c4a975f 100644 --- a/pkg/api/v1/register.go +++ b/pkg/api/v1/register.go @@ -81,6 +81,7 @@ func addKnownTypes(scheme *runtime.Scheme) error { &PodAttachOptions{}, &PodLogOptions{}, &PodExecOptions{}, + &PodPortForwardOptions{}, &PodProxyOptions{}, &ComponentStatus{}, &ComponentStatusList{}, diff --git a/pkg/api/v1/types.go b/pkg/api/v1/types.go index 55af32adb40..8323657191e 100644 --- a/pkg/api/v1/types.go +++ b/pkg/api/v1/types.go @@ -3423,6 +3423,21 @@ type PodExecOptions struct { Command []string `json:"command" protobuf:"bytes,6,rep,name=command"` } +// PodPortForwardOptions is the query options to a Pod's port forward call +// when using WebSockets. +// The `port` query parameter must specify the port or +// ports (comma separated) to forward over. +// Port forwarding over SPDY does not use these options. It requires the port +// to be passed in the `port` header as part of request. +type PodPortForwardOptions struct { + metav1.TypeMeta `json:",inline"` + + // List of ports to forward + // Required when using WebSockets + // +optional + Ports []int32 `json:"ports,omitempty" protobuf:"varint,1,rep,name=ports"` +} + // PodProxyOptions is the query options to a Pod's proxy call. type PodProxyOptions struct { metav1.TypeMeta `json:",inline"`