mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 12:44:39 +00:00
runtime: connect guest debug console bypass kata-monitor
Parse agent socket address by conversation to improve usability of using guest debug console. Fixes: #1329 Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
parent
d6682e3168
commit
44cde6e464
@ -1,4 +1,5 @@
|
|||||||
// Copyright (c) 2017-2019 Intel Corporation
|
// Copyright (c) 2017-2019 Intel Corporation
|
||||||
|
// Copyright (c) 2020 Ant Group
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
@ -19,6 +20,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containerd/console"
|
"github.com/containerd/console"
|
||||||
|
kataMonitor "github.com/kata-containers/kata-containers/src/runtime/pkg/kata-monitor"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
|
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
|
||||||
clientUtils "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/client"
|
clientUtils "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/client"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -35,10 +37,10 @@ const (
|
|||||||
|
|
||||||
subCommandName = "exec"
|
subCommandName = "exec"
|
||||||
// command-line parameters name
|
// command-line parameters name
|
||||||
paramKataMonitorAddr = "kata-monitor-addr"
|
paramRuntimeNamespace = "runtime-namespace"
|
||||||
paramDebugConsolePort = "kata-debug-port"
|
paramDebugConsolePort = "kata-debug-port"
|
||||||
defaultKernelParamDebugConsoleVPortValue = 1026
|
defaultKernelParamDebugConsoleVPortValue = 1026
|
||||||
defaultParamKataMonitorAddr = "http://localhost:8090"
|
defaultRuntimeNamespace = "k8s.io"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -55,12 +57,12 @@ var kataExecCLICommand = cli.Command{
|
|||||||
Usage: "Enter into guest by debug console",
|
Usage: "Enter into guest by debug console",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: paramKataMonitorAddr,
|
Name: paramRuntimeNamespace,
|
||||||
Usage: "Kata monitor listen address.",
|
Usage: "Namespace that containerd or CRI-O are using for containers. (Default: k8s.io, only works for containerd)",
|
||||||
},
|
},
|
||||||
cli.Uint64Flag{
|
cli.Uint64Flag{
|
||||||
Name: paramDebugConsolePort,
|
Name: paramDebugConsolePort,
|
||||||
Usage: "Port that debug console is listening on.",
|
Usage: "Port that debug console is listening on. (Default: 1026)",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(context *cli.Context) error {
|
Action: func(context *cli.Context) error {
|
||||||
@ -71,11 +73,11 @@ var kataExecCLICommand = cli.Command{
|
|||||||
span, _ := katautils.Trace(ctx, subCommandName)
|
span, _ := katautils.Trace(ctx, subCommandName)
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
endPoint := context.String(paramKataMonitorAddr)
|
namespace := context.String(paramRuntimeNamespace)
|
||||||
if endPoint == "" {
|
if namespace == "" {
|
||||||
endPoint = defaultParamKataMonitorAddr
|
namespace = defaultRuntimeNamespace
|
||||||
}
|
}
|
||||||
span.SetAttributes(label.Key("endPoint").String(endPoint))
|
span.SetAttributes(label.Key("namespace").String(namespace))
|
||||||
|
|
||||||
port := context.Uint64(paramDebugConsolePort)
|
port := context.Uint64(paramDebugConsolePort)
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
@ -89,7 +91,7 @@ var kataExecCLICommand = cli.Command{
|
|||||||
}
|
}
|
||||||
span.SetAttributes(label.Key("sandbox").String(sandboxID))
|
span.SetAttributes(label.Key("sandbox").String(sandboxID))
|
||||||
|
|
||||||
conn, err := getConn(endPoint, sandboxID, port)
|
conn, err := getConn(namespace, sandboxID, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -172,15 +174,20 @@ func (s *iostream) Read(data []byte) (n int, err error) {
|
|||||||
return s.conn.Read(data)
|
return s.conn.Read(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConn(endPoint, sandboxID string, port uint64) (net.Conn, error) {
|
func getConn(namespace, sandboxID string, port uint64) (net.Conn, error) {
|
||||||
shimURL := fmt.Sprintf("%s/agent-url?sandbox=%s", endPoint, sandboxID)
|
socketAddr := fmt.Sprintf("/containerd-shim/%s/%s/shim-monitor.sock", namespace, sandboxID)
|
||||||
resp, err := http.Get(shimURL)
|
client, err := kataMonitor.BuildUnixSocketClient(socketAddr, defaultTimeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Get("http://shim/agent-url")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("Failed to get %s: %d", shimURL, resp.StatusCode)
|
return nil, fmt.Errorf("Failed to get %s: %d", socketAddr, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2020 Ant Financial
|
// Copyright (c) 2020-2021 Ant Group
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
@ -34,15 +34,19 @@ func getSandboxIdFromReq(r *http.Request) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (km *KataMonitor) buildShimClient(sandboxID, namespace string, timeout time.Duration) (*http.Client, error) {
|
func (km *KataMonitor) buildShimClient(sandboxID, namespace string, timeout time.Duration) (*http.Client, error) {
|
||||||
socket, err := km.getMonitorAddress(sandboxID, namespace)
|
socketAddr, err := km.getMonitorAddress(sandboxID, namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return BuildUnixSocketClient(socketAddr, timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildUnixSocketClient build http client for Unix socket
|
||||||
|
func BuildUnixSocketClient(socketAddr string, timeout time.Duration) (*http.Client, error) {
|
||||||
transport := &http.Transport{
|
transport := &http.Transport{
|
||||||
DisableKeepAlives: true,
|
DisableKeepAlives: true,
|
||||||
Dial: func(proto, addr string) (conn net.Conn, err error) {
|
Dial: func(proto, addr string) (conn net.Conn, err error) {
|
||||||
return net.Dial("unix", "\x00"+socket)
|
return net.Dial("unix", "\x00"+socketAddr)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user