Skip to content

Commit 09cc5a7

Browse files
authored
Support debug session via k8s runtime to nginx (#81)
* Add start session keys * Add note about k8s list pods auto runtime behavior * Support kubernetes session to nginx
1 parent c295288 commit 09cc5a7

File tree

4 files changed

+301
-19
lines changed

4 files changed

+301
-19
lines changed

pkg/app/master/command/debug/cli.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Volume struct {
3232
}
3333

3434
type CommandParams struct {
35+
KubeComm *KubernetesHandlerComm
3536
/// the runtime environment type
3637
Runtime string
3738
/// the running container which we want to attach to
@@ -239,6 +240,8 @@ var CLI = &cli.Command{
239240
xc.Exit(-1)
240241
}
241242

243+
// NOTE -> this approach results in the default of `auto` erroring out
244+
// even if the user's system has a `k8s` runtime available`
242245
if commandParams.ActionListPods &&
243246
commandParams.Runtime != crt.KubernetesRuntime {
244247
xc.Out.Error("param", "unsupported runtime flag")

pkg/app/master/command/debug/handle_kubernetes_runtime.go

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io"
89
"io/ioutil"
910
"net/http"
1011
"os"
@@ -817,14 +818,27 @@ func HandleKubernetesRuntime(
817818

818819
fmt.Printf("\n")
819820
//note: blocks until done streaming or failure...
820-
err = attach.StreamWithContext(
821-
ctx,
822-
remotecommand.StreamOptions{
823-
Stdin: os.Stdin,
824-
Stdout: os.Stdout,
825-
Stderr: os.Stderr,
826-
Tty: doTTY,
827-
})
821+
if commandParams.TUI {
822+
// TODO - move KubeComm off of command params
823+
reader := &KubeReader{inputChan: commandParams.KubeComm.InputChan}
824+
err = attach.StreamWithContext(
825+
ctx,
826+
remotecommand.StreamOptions{
827+
Stdin: reader,
828+
Stdout: os.Stdout,
829+
Stderr: os.Stderr,
830+
Tty: true, // Later on we may parse this in TUI mode.
831+
})
832+
} else {
833+
err = attach.StreamWithContext(
834+
ctx,
835+
remotecommand.StreamOptions{
836+
Stdin: os.Stdin,
837+
Stdout: os.Stdout,
838+
Stderr: os.Stderr,
839+
Tty: doTTY,
840+
})
841+
}
828842

829843
if err != nil {
830844
if apierrors.IsNotFound(err) {
@@ -844,6 +858,49 @@ func HandleKubernetesRuntime(
844858
}
845859
}
846860

861+
// NOTE -> this input channel reader will be genericized
862+
// as per the comment in `debug/tui.go`.
863+
// An InputReader usable by Docker, Podman, Kubernetes, and Containerd
864+
// will be added to this directory.
865+
type KubeReader struct {
866+
inputChan chan InputKey
867+
}
868+
869+
func (kr *KubeReader) Read(p []byte) (n int, err error) {
870+
inputKey, ok := <-kr.inputChan
871+
if !ok {
872+
return 0, io.EOF
873+
}
874+
log.Debugf("KubeReader received inputKey %v", inputKey)
875+
switch inputKey.Special {
876+
case NotSpecial:
877+
p[0] = byte(inputKey.Rune)
878+
return 1, nil
879+
case Enter:
880+
p[0] = '\n'
881+
return 1, nil
882+
case Backspace:
883+
p[0] = 127
884+
return 1, nil
885+
case Up:
886+
copy(p, []byte{27, 91, 65}) // ESC [ A
887+
return 3, nil
888+
case Down:
889+
copy(p, []byte{27, 91, 66}) // ESC [ B
890+
return 3, nil
891+
case Left:
892+
copy(p, []byte{27, 91, 68}) // ESC [ D
893+
return 3, nil
894+
case Right:
895+
copy(p, []byte{27, 91, 67}) // ESC [ C
896+
return 3, nil
897+
default:
898+
log.Debugf("Unhandled inputKey %v", inputKey)
899+
// Handle other special keys or return an error
900+
return 0, fmt.Errorf("unsupported special key")
901+
}
902+
}
903+
847904
func listNamespaces(ctx context.Context, api *kubernetes.Clientset) ([]string, error) {
848905
namespaces, err := api.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
849906
if err != nil {

0 commit comments

Comments
 (0)