add portforwarding support to node/api (#1102)
Co-authored-by: Pablo Borrelli <pablo.borrelli0@gmail.com> Co-authored-by: windyear <1280027646@qq.com>
This commit is contained in:
116
node/api/portforward.go
Normal file
116
node/api/portforward.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright © 2017 The virtual-kubelet authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/internal/kubernetes/portforward"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
// PortForwardHandlerFunc defines the handler function used to
|
||||
// portforward, passing through the original dataStream
|
||||
type PortForwardHandlerFunc func(ctx context.Context, namespace, pod string, port int32, stream io.ReadWriteCloser) error
|
||||
|
||||
// PortForwardHandlerConfig is used to pass options to options to the container exec handler.
|
||||
type PortForwardHandlerConfig struct {
|
||||
// StreamIdleTimeout is the maximum time a streaming connection
|
||||
// can be idle before the connection is automatically closed.
|
||||
StreamIdleTimeout time.Duration
|
||||
// StreamCreationTimeout is the maximum time for streaming connection
|
||||
StreamCreationTimeout time.Duration
|
||||
}
|
||||
|
||||
// PortForwardHandlerOption configures a PortForwardHandlerConfig
|
||||
// It is used as functional options passed to `HandlePortForward`
|
||||
type PortForwardHandlerOption func(*PortForwardHandlerConfig)
|
||||
|
||||
// WithPortForwardStreamIdleTimeout sets the idle timeout for a container port forward streaming
|
||||
func WithPortForwardStreamIdleTimeout(dur time.Duration) PortForwardHandlerOption {
|
||||
return func(cfg *PortForwardHandlerConfig) {
|
||||
cfg.StreamIdleTimeout = dur
|
||||
}
|
||||
}
|
||||
|
||||
// WithPortForwardCreationTimeout sets the creation timeout for a container exec stream
|
||||
func WithPortForwardCreationTimeout(dur time.Duration) PortForwardHandlerOption {
|
||||
return func(cfg *PortForwardHandlerConfig) {
|
||||
cfg.StreamCreationTimeout = dur
|
||||
}
|
||||
}
|
||||
|
||||
// HandlePortForward makes an http handler func from a Provider which forward ports to a container
|
||||
// Note that this handler currently depends on gorrilla/mux to get url parts as variables.
|
||||
func HandlePortForward(h PortForwardHandlerFunc, opts ...PortForwardHandlerOption) http.HandlerFunc {
|
||||
if h == nil {
|
||||
return NotImplemented
|
||||
}
|
||||
|
||||
var cfg PortForwardHandlerConfig
|
||||
for _, o := range opts {
|
||||
o(&cfg)
|
||||
}
|
||||
|
||||
if cfg.StreamIdleTimeout == 0 {
|
||||
cfg.StreamIdleTimeout = 30 * time.Second
|
||||
}
|
||||
if cfg.StreamCreationTimeout == 0 {
|
||||
cfg.StreamCreationTimeout = 30 * time.Second
|
||||
}
|
||||
|
||||
return handleError(func(w http.ResponseWriter, req *http.Request) error {
|
||||
vars := mux.Vars(req)
|
||||
|
||||
namespace := vars["namespace"]
|
||||
|
||||
pod := vars["pod"]
|
||||
|
||||
supportedStreamProtocols := strings.Split(req.Header.Get("X-Stream-Protocol-Version"), ",")
|
||||
|
||||
portfwd := &portForwardContext{h: h, pod: pod, namespace: namespace}
|
||||
portforward.ServePortForward(
|
||||
w,
|
||||
req,
|
||||
portfwd,
|
||||
pod,
|
||||
"",
|
||||
&portforward.V4Options{}, // This is only used for websocket connection
|
||||
cfg.StreamIdleTimeout,
|
||||
cfg.StreamCreationTimeout,
|
||||
supportedStreamProtocols,
|
||||
)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
type portForwardContext struct {
|
||||
h PortForwardHandlerFunc
|
||||
pod string
|
||||
namespace string
|
||||
}
|
||||
|
||||
// PortForward Implements portforward.Portforwarder
|
||||
// This is called by portforward.ServePortForward
|
||||
func (p *portForwardContext) PortForward(ctx context.Context, name string, uid types.UID, port int32, stream io.ReadWriteCloser) error {
|
||||
return p.h(ctx, p.namespace, p.pod, port, stream)
|
||||
}
|
||||
@@ -36,6 +36,7 @@ type ServeMux interface {
|
||||
type PodHandlerConfig struct { //nolint:golint
|
||||
RunInContainer ContainerExecHandlerFunc
|
||||
AttachToContainer ContainerAttachHandlerFunc
|
||||
PortForward PortForwardHandlerFunc
|
||||
GetContainerLogs ContainerLogsHandlerFunc
|
||||
// GetPods is meant to enumerate the pods that the provider knows about
|
||||
GetPods PodListerFunc
|
||||
@@ -58,7 +59,6 @@ func PodHandler(p PodHandlerConfig, debug bool) http.Handler {
|
||||
if debug {
|
||||
r.HandleFunc("/runningpods/", HandleRunningPods(p.GetPods)).Methods("GET")
|
||||
}
|
||||
|
||||
r.HandleFunc("/pods", HandleRunningPods(p.GetPodsFromKubernetes)).Methods("GET")
|
||||
r.HandleFunc("/containerLogs/{namespace}/{pod}/{container}", HandleContainerLogs(p.GetContainerLogs)).Methods("GET")
|
||||
r.HandleFunc(
|
||||
@@ -77,6 +77,14 @@ func PodHandler(p PodHandlerConfig, debug bool) http.Handler {
|
||||
WithExecStreamIdleTimeout(p.StreamIdleTimeout),
|
||||
),
|
||||
).Methods("POST", "GET")
|
||||
r.HandleFunc(
|
||||
"/portForward/{namespace}/{pod}",
|
||||
HandlePortForward(
|
||||
p.PortForward,
|
||||
WithPortForwardStreamIdleTimeout(p.StreamCreationTimeout),
|
||||
WithPortForwardCreationTimeout(p.StreamIdleTimeout),
|
||||
),
|
||||
).Methods("POST", "GET")
|
||||
|
||||
if p.GetStatsSummary != nil {
|
||||
f := HandlePodStatsSummary(p.GetStatsSummary)
|
||||
|
||||
@@ -37,6 +37,9 @@ type Provider interface {
|
||||
|
||||
// GetMetricsResource gets the metrics for the node, including running pods
|
||||
GetMetricsResource(context.Context) ([]*dto.MetricFamily, error)
|
||||
|
||||
// PortForward forwards a local port to a port on the pod
|
||||
PortForward(ctx context.Context, namespace, pod string, port int32, stream io.ReadWriteCloser) error
|
||||
}
|
||||
|
||||
// ProviderConfig holds objects created by NewNodeFromClient that a provider may need to bootstrap itself.
|
||||
@@ -73,6 +76,7 @@ func AttachProviderRoutes(mux api.ServeMux) NodeOpt {
|
||||
GetMetricsResource: p.GetMetricsResource,
|
||||
StreamIdleTimeout: cfg.StreamIdleTimeout,
|
||||
StreamCreationTimeout: cfg.StreamCreationTimeout,
|
||||
PortForward: p.PortForward,
|
||||
}, true))
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user