Skip to content

Commit

Permalink
virtcontainers: add watchconsole for no_proxy type
Browse files Browse the repository at this point in the history
For no proxy type, we also need the feature
of watch hypervisor's console to help debug.

Fixes:kata-containers#1932

Signed-off-by: lifupan <[email protected]>
  • Loading branch information
lifupan authored and vijaydhanraj committed Sep 3, 2019
1 parent b65b589 commit 7239970
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 127 deletions.
2 changes: 1 addition & 1 deletion pkg/katautils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ func LoadConfiguration(configPath string, ignoreLogging, builtIn bool) (resolved
if config.HypervisorConfig.UseVSock {
kataUtilsLogger.Info("VSOCK supported, configure to not use proxy")
config.ProxyType = vc.NoProxyType
config.ProxyConfig = vc.ProxyConfig{}
config.ProxyConfig = vc.ProxyConfig{Debug: config.Debug}
}

config.DisableNewNetNs = tomlConf.Runtime.DisableNewNetNs
Expand Down
96 changes: 3 additions & 93 deletions virtcontainers/kata_builtin_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,19 @@

package virtcontainers

import (
"bufio"
"fmt"
"io"
"net"

"github.com/sirupsen/logrus"
)

var buildinProxyConsoleProto = consoleProtoUnix
import "fmt"

// This is a kata builtin proxy implementation of the proxy interface. Kata proxy
// functionality is implemented inside the virtcontainers library.
type kataBuiltInProxy struct {
sandboxID string
conn net.Conn
}

// check if the proxy has watched the vm console.
func (p *kataBuiltInProxy) consoleWatched() bool {
return p.conn != nil
proxyBuiltin
}

func (p *kataBuiltInProxy) validateParams(params proxyParams) error {
if len(params.id) == 0 || len(params.agentURL) == 0 || len(params.consoleURL) == 0 {
return fmt.Errorf("Invalid proxy parameters %+v", params)
}

if params.logger == nil {
return fmt.Errorf("Invalid proxy parameter: proxy logger is not set")
}

return nil
}

Expand All @@ -48,76 +29,5 @@ func (p *kataBuiltInProxy) start(params proxyParams) (int, string, error) {
return -1, "", err
}

if p.consoleWatched() {
return -1, "", fmt.Errorf("kata builtin proxy running for sandbox %s", params.id)
}

params.logger.Debug("Starting builtin kata proxy")

p.sandboxID = params.id

if params.debug {
err := p.watchConsole(buildinProxyConsoleProto, params.consoleURL, params.logger)
if err != nil {
p.sandboxID = ""
return -1, "", err
}
}

return params.hid, params.agentURL, nil
}

// stop is the proxy stop implementation for kata builtin proxy.
func (p *kataBuiltInProxy) stop(pid int) error {
if p.conn != nil {
p.conn.Close()
p.conn = nil
p.sandboxID = ""
}
return nil
}

func (p *kataBuiltInProxy) watchConsole(proto, console string, logger *logrus.Entry) (err error) {
var (
scanner *bufio.Scanner
conn net.Conn
)

switch proto {
case consoleProtoUnix:
conn, err = net.Dial("unix", console)
if err != nil {
return err
}
// TODO: add pty console support for kvmtools
case consoleProtoPty:
fallthrough
default:
return fmt.Errorf("unknown console proto %s", proto)
}

p.conn = conn

go func() {
scanner = bufio.NewScanner(conn)
for scanner.Scan() {
logger.WithFields(logrus.Fields{
"sandbox": p.sandboxID,
"vmconsole": scanner.Text(),
}).Debug("reading guest console")
}

if err := scanner.Err(); err != nil {
if err == io.EOF {
logger.Info("console watcher quits")
} else {
logger.WithError(err).WithFields(logrus.Fields{
"console-protocol": proto,
"console-socket": console,
}).Error("Failed to read agent logs")
}
}
}()

return nil
return p.proxyBuiltin.start(params)
}
5 changes: 1 addition & 4 deletions virtcontainers/kata_builtin_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ func TestKataBuiltinProxy(t *testing.T) {

params.consoleURL = "foobarconsole"
err = p.validateParams(params)
assert.NotNil(err)

params.logger = logrus.WithField("proxy", params.id)
err = p.validateParams(params)
assert.Nil(err)

params.logger = logrus.WithField("proxy", params.id)
buildinProxyConsoleProto = "foobarproto"
_, _, err = p.start(params)
assert.NotNil(err)
Expand Down
30 changes: 1 addition & 29 deletions virtcontainers/no_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

package virtcontainers

import (
"fmt"
)

// This is the no proxy implementation of the proxy interface. This
// is a generic implementation for any case (basically any agent),
// where no actual proxy is needed. This happens when the combination
Expand All @@ -20,29 +16,5 @@ import (
// is to provide both shim and runtime the correct URL to connect
// directly to the VM.
type noProxy struct {
}

// start is noProxy start implementation for proxy interface.
func (p *noProxy) start(params proxyParams) (int, string, error) {
if params.logger == nil {
return -1, "", fmt.Errorf("proxy logger is not set")
}

params.logger.Debug("No proxy started because of no-proxy implementation")

if params.agentURL == "" {
return -1, "", fmt.Errorf("AgentURL cannot be empty")
}

return params.hid, params.agentURL, nil
}

// stop is noProxy stop implementation for proxy interface.
func (p *noProxy) stop(pid int) error {
return nil
}

// The noproxy doesn't need to watch the vm console, thus return false always.
func (p *noProxy) consoleWatched() bool {
return false
proxyBuiltin
}
98 changes: 98 additions & 0 deletions virtcontainers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
package virtcontainers

import (
"bufio"
"fmt"
"io"
"net"
"path/filepath"

"github.com/kata-containers/runtime/virtcontainers/store"
"github.com/sirupsen/logrus"
)

var buildinProxyConsoleProto = consoleProtoUnix

type proxyBuiltin struct {
sandboxID string
conn net.Conn
}

// ProxyConfig is a structure storing information needed from any
// proxy in order to be properly initialized.
type ProxyConfig struct {
Expand Down Expand Up @@ -161,3 +171,91 @@ type proxy interface {
//check if the proxy has watched the vm console.
consoleWatched() bool
}

func (p *proxyBuiltin) watchConsole(proto, console string, logger *logrus.Entry) (err error) {
var (
scanner *bufio.Scanner
conn net.Conn
)

switch proto {
case consoleProtoUnix:
conn, err = net.Dial("unix", console)
if err != nil {
return err
}
// TODO: please see
// https://github.com/kata-containers/runtime/issues/1940.
case consoleProtoPty:
fallthrough
default:
return fmt.Errorf("unknown console proto %s", proto)
}

p.conn = conn

go func() {
scanner = bufio.NewScanner(conn)
for scanner.Scan() {
logger.WithFields(logrus.Fields{
"sandbox": p.sandboxID,
"vmconsole": scanner.Text(),
}).Debug("reading guest console")
}

if err := scanner.Err(); err != nil {
if err == io.EOF {
logger.Info("console watcher quits")
} else {
logger.WithError(err).WithFields(logrus.Fields{
"console-protocol": proto,
"console-socket": console,
}).Error("Failed to read agent logs")
}
}
}()

return nil
}

// check if the proxy has watched the vm console.
func (p *proxyBuiltin) consoleWatched() bool {
return p.conn != nil
}

// start is the proxy start implementation for builtin proxy.
// It starts the console watcher for the guest.
// It returns agentURL to let agent connect directly.
func (p *proxyBuiltin) start(params proxyParams) (int, string, error) {
if params.logger == nil {
return -1, "", fmt.Errorf("Invalid proxy parameter: proxy logger is not set")
}

if p.consoleWatched() {
return -1, "", fmt.Errorf("The console has been watched for sandbox %s", params.id)
}

params.logger.Debug("Start to watch the console")

p.sandboxID = params.id

if params.debug {
err := p.watchConsole(buildinProxyConsoleProto, params.consoleURL, params.logger)
if err != nil {
p.sandboxID = ""
return -1, "", err
}
}

return params.hid, params.agentURL, nil
}

// stop is the proxy stop implementation for builtin proxy.
func (p *proxyBuiltin) stop(pid int) error {
if p.conn != nil {
p.conn.Close()
p.conn = nil
p.sandboxID = ""
}
return nil
}

0 comments on commit 7239970

Please sign in to comment.