Skip to content

Commit

Permalink
Use k8s.io/kubelet/pkg/cri/streaming instead
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Aug 16, 2023
1 parent eb20928 commit 08023a5
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 1,798 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
k8s.io/client-go v0.28.0
k8s.io/code-generator v0.28.0
k8s.io/cri-api v0.28.0
k8s.io/kubelet v0.28.0
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
mvdan.cc/sh/v3 v3.7.0
sigs.k8s.io/controller-runtime v0.15.1
Expand Down Expand Up @@ -109,7 +110,6 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apiextensions-apiserver v0.27.3 // indirect
k8s.io/component-base v0.28.0 // indirect
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
k8s.io/klog v0.2.0 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ k8s.io/client-go v0.28.0 h1:ebcPRDZsCjpj62+cMk1eGNX1QkMdRmQ6lmz5BLoFWeM=
k8s.io/client-go v0.28.0/go.mod h1:0Asy9Xt3U98RypWJmU1ZrRAGKhP6NqDPmptlAzK2kMc=
k8s.io/code-generator v0.28.0 h1:msdkRVJNVFgdiIJ8REl/d3cZsMB9HByFcWMmn13NyuE=
k8s.io/code-generator v0.28.0/go.mod h1:ueeSJZJ61NHBa0ccWLey6mwawum25vX61nRZ6WOzN9A=
k8s.io/component-base v0.28.0 h1:HQKy1enJrOeJlTlN4a6dU09wtmXaUvThC0irImfqyxI=
k8s.io/component-base v0.28.0/go.mod h1:Yyf3+ZypLfMydVzuLBqJ5V7Kx6WwDr/5cN+dFjw1FNk=
k8s.io/cri-api v0.28.0 h1:TVidtHNi425IaKF50oDD5hRvQuK7wB4NQAfTVOcr9QA=
k8s.io/cri-api v0.28.0/go.mod h1:xXygwvSOGcT/2KXg8sMYTHns2xFem3949kCQn5IS1k4=
k8s.io/gengo v0.0.0-20201203183100-97869a43a9d9/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
Expand All @@ -349,6 +347,8 @@ k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/kubelet v0.28.0 h1:H/3JAkLIungVF+WLpqrxhgJ4gzwsbN8VA8LOTYsEX3U=
k8s.io/kubelet v0.28.0/go.mod h1:i8jUg4ltbRusT3ExOhSAeqETuHdoHTZcTT2cPr9RTgc=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
mvdan.cc/editorconfig v0.2.0 h1:XL+7ys6ls/RKrkUNFQvEwIvNHh+JKx8Mj1pUV5wQxQE=
Expand Down
26 changes: 16 additions & 10 deletions pkg/kwok/server/debugging_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/emicklei/go-restful/v3"
"k8s.io/apimachinery/pkg/types"
clientremotecommand "k8s.io/client-go/tools/remotecommand"
remotecommandconsts "k8s.io/apimachinery/pkg/util/remotecommand"
remotecommandclient "k8s.io/client-go/tools/remotecommand"
remotecommandserver "k8s.io/kubelet/pkg/cri/streaming/remotecommand"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/kwok/server/remotecommand"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/slices"
)

// AttachContainer attaches to a container in a pod,
// copying data between in/out/err and the container's stdin/stdout/stderr.
func (s *Server) AttachContainer(ctx context.Context, podName, podNamespace string, uid types.UID, containerName string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan clientremotecommand.TerminalSize) error {
func (s *Server) AttachContainer(ctx context.Context, name string, uid types.UID, containerName string, in io.Reader, out, errOut io.WriteCloser, tty bool, resize <-chan remotecommandclient.TerminalSize) error {
pod := strings.Split(name, "/")
if len(pod) != 2 {
return fmt.Errorf("invalid pod name %q", name)
}
podName, podNamespace := pod[0], pod[1]
attach, err := s.getPodAttach(podName, podNamespace, containerName)
if err != nil {
return err
Expand All @@ -45,31 +52,30 @@ func (s *Server) AttachContainer(ctx context.Context, podName, podNamespace stri
follow: true,
timestamp: false,
}
return readLogs(ctx, attach.LogsFile, opts, stdout, stderr)
return readLogs(ctx, attach.LogsFile, opts, out, errOut)
}

func (s *Server) getAttach(req *restful.Request, resp *restful.Response) {
params := getExecRequestParams(req)

streamOpts, err := remotecommand.NewOptions(req.Request)
streamOpts, err := remotecommandserver.NewOptions(req.Request)
if err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)
return
}

remotecommand.ServeAttach(
req.Request.Context(),
remotecommandserver.ServeAttach(
resp.ResponseWriter,
req.Request,
s,
params.podName,
params.podNamespace,
params.podName+"/"+params.podNamespace,
params.podUID,
params.containerName,
streamOpts,
s.idleTimeout,
s.streamCreationTimeout,
remotecommand.SupportedStreamingProtocols)
remotecommandconsts.SupportedStreamingProtocols,
)
}

func (s *Server) getPodAttach(podName, podNamespace, containerName string) (*internalversion.AttachConfig, error) {
Expand Down
25 changes: 16 additions & 9 deletions pkg/kwok/server/debugging_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/emicklei/go-restful/v3"
"k8s.io/apimachinery/pkg/types"
clientremotecommand "k8s.io/client-go/tools/remotecommand"
remotecommandconsts "k8s.io/apimachinery/pkg/util/remotecommand"
remotecommandclient "k8s.io/client-go/tools/remotecommand"
remotecommandserver "k8s.io/kubelet/pkg/cri/streaming/remotecommand"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/kwok/server/remotecommand"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/exec"
"sigs.k8s.io/kwok/pkg/utils/slices"
)

// ExecInContainer executes a command in a container.
func (s *Server) ExecInContainer(ctx context.Context, podName, podNamespace string, uid types.UID, container string, cmd []string, in io.Reader, out, errOut io.WriteCloser, tty bool, resize <-chan clientremotecommand.TerminalSize) error {
func (s *Server) ExecInContainer(ctx context.Context, name string, uid types.UID, container string, cmd []string, in io.Reader, out, errOut io.WriteCloser, tty bool, resize <-chan remotecommandclient.TerminalSize, timeout time.Duration) error {
pod := strings.Split(name, "/")
if len(pod) != 2 {
return fmt.Errorf("invalid pod name %q", name)
}
podName, podNamespace := pod[0], pod[1]
execTarget, err := s.getExecTarget(podName, podNamespace, container)
if err != nil {
return err
Expand Down Expand Up @@ -137,24 +145,23 @@ func findContainerInExecs(containerName string, execs []internalversion.ExecTarg
func (s *Server) getExec(req *restful.Request, resp *restful.Response) {
params := getExecRequestParams(req)

streamOpts, err := remotecommand.NewOptions(req.Request)
streamOpts, err := remotecommandserver.NewOptions(req.Request)
if err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)
return
}

remotecommand.ServeExec(
req.Request.Context(),
remotecommandserver.ServeExec(
resp.ResponseWriter,
req.Request,
s,
params.podName,
params.podNamespace,
params.podName+"/"+params.podNamespace,
params.podUID,
params.containerName,
params.cmd,
streamOpts,
s.idleTimeout,
s.streamCreationTimeout,
remotecommand.SupportedStreamingProtocols)
remotecommandconsts.SupportedStreamingProtocols,
)
}
18 changes: 12 additions & 6 deletions pkg/kwok/server/debugging_port_forword.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,30 @@ import (
"io"
"net"
"net/http"
"strings"

"github.com/emicklei/go-restful/v3"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kubelet/pkg/cri/streaming/portforward"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/kwok/server/portforward"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/exec"
"sigs.k8s.io/kwok/pkg/utils/slices"
)

// PortForward handles a port forwarding request.
func (s *Server) PortForward(ctx context.Context, podName, podNamespace string, uid types.UID, port int32, stream io.ReadWriteCloser) error {
func (s *Server) PortForward(ctx context.Context, name string, uid types.UID, port int32, stream io.ReadWriteCloser) error {
defer func() {
_ = stream.Close()
}()

pod := strings.Split(name, "/")
if len(pod) != 2 {
return fmt.Errorf("invalid pod name %q", name)
}
podName, podNamespace := pod[0], pod[1]

forward, err := s.getPodsForward(podName, podNamespace, port)
if err != nil {
return err
Expand Down Expand Up @@ -87,17 +94,16 @@ func (s *Server) getPortForward(req *restful.Request, resp *restful.Response) {
}

portforward.ServePortForward(
req.Request.Context(),
resp.ResponseWriter,
req.Request,
s,
params.podName,
params.podNamespace,
params.podName+"/"+params.podNamespace,
params.podUID,
portForwardOptions,
s.idleTimeout,
s.streamCreationTimeout,
portforward.SupportedProtocols)
portforward.SupportedProtocols,
)
}

func (s *Server) getPodsForward(podName, podNamespace string, port int32) (*internalversion.Forward, error) {
Expand Down
24 changes: 0 additions & 24 deletions pkg/kwok/server/portforward/constants.go

This file was deleted.

Loading

0 comments on commit 08023a5

Please sign in to comment.