diff --git a/virtcontainers/cc_proxy.go b/virtcontainers/cc_proxy.go index ef6cce5eb0..3d66e1fd5c 100644 --- a/virtcontainers/cc_proxy.go +++ b/virtcontainers/cc_proxy.go @@ -6,6 +6,7 @@ package virtcontainers import ( + "fmt" "os/exec" ) @@ -14,8 +15,12 @@ type ccProxy struct { // start is the proxy start implementation for ccProxy. func (p *ccProxy) start(sandbox *Sandbox, params proxyParams) (int, string, error) { - config, err := newProxyConfig(sandbox.config) - if err != nil { + if sandbox.config == nil { + return -1, "", fmt.Errorf("Sandbox config cannot be nil") + } + + config := sandbox.config.ProxyConfig + if err := validateProxyConfig(config); err != nil { return -1, "", err } diff --git a/virtcontainers/kata_proxy.go b/virtcontainers/kata_proxy.go index fe4b01e9c7..e68686295b 100644 --- a/virtcontainers/kata_proxy.go +++ b/virtcontainers/kata_proxy.go @@ -25,6 +25,10 @@ func (p *kataProxy) consoleWatched() bool { // start is kataProxy start implementation for proxy interface. func (p *kataProxy) start(sandbox *Sandbox, params proxyParams) (int, string, error) { sandbox.Logger().Info("Starting regular Kata proxy rather than built-in") + if sandbox.config == nil { + return -1, "", fmt.Errorf("Sandbox config cannot be nil") + } + if sandbox.agent == nil { return -1, "", fmt.Errorf("No agent") } @@ -33,8 +37,8 @@ func (p *kataProxy) start(sandbox *Sandbox, params proxyParams) (int, string, er return -1, "", fmt.Errorf("AgentURL cannot be empty") } - config, err := newProxyConfig(sandbox.config) - if err != nil { + config := sandbox.config.ProxyConfig + if err := validateProxyConfig(config); err != nil { return -1, "", err } diff --git a/virtcontainers/proxy.go b/virtcontainers/proxy.go index f2f55f8a82..29256e7349 100644 --- a/virtcontainers/proxy.go +++ b/virtcontainers/proxy.go @@ -119,28 +119,12 @@ func newProxy(pType ProxyType) (proxy, error) { } } -// newProxyConfig returns a proxy config from a generic SandboxConfig handler, -// after it properly checked the configuration was valid. -func newProxyConfig(sandboxConfig *SandboxConfig) (ProxyConfig, error) { - if sandboxConfig == nil { - return ProxyConfig{}, fmt.Errorf("Sandbox config cannot be nil") +func validateProxyConfig(proxyConfig ProxyConfig) error { + if len(proxyConfig.Path) == 0 { + return fmt.Errorf("Proxy path cannot be empty") } - var config ProxyConfig - switch sandboxConfig.ProxyType { - case KataProxyType: - fallthrough - case CCProxyType: - config = sandboxConfig.ProxyConfig - default: - return ProxyConfig{}, fmt.Errorf("unknown proxy type %v", sandboxConfig.ProxyType) - } - - if config.Path == "" { - return ProxyConfig{}, fmt.Errorf("Proxy path cannot be empty") - } - - return config, nil + return nil } func defaultProxyURL(sandbox *Sandbox, socketType string) (string, error) { diff --git a/virtcontainers/proxy_test.go b/virtcontainers/proxy_test.go index b796d30780..4f72a2212d 100644 --- a/virtcontainers/proxy_test.go +++ b/virtcontainers/proxy_test.go @@ -154,15 +154,15 @@ func TestNewProxyFromUnknownProxyType(t *testing.T) { } } -func testNewProxyConfigFromSandboxConfig(t *testing.T, sandboxConfig SandboxConfig, expected ProxyConfig) { - result, err := newProxyConfig(&sandboxConfig) - if err != nil { +func testNewProxyFromSandboxConfig(t *testing.T, sandboxConfig SandboxConfig) { + if _, err := newProxy(sandboxConfig.ProxyType); err != nil { t.Fatal(err) } - if reflect.DeepEqual(result, expected) == false { - t.Fatalf("Got %+v\nExpecting %+v", result, expected) + if err := validateProxyConfig(sandboxConfig.ProxyConfig); err != nil { + t.Fatal(err) } + } var testProxyPath = "proxy-path" @@ -177,7 +177,7 @@ func TestNewProxyConfigFromCCProxySandboxConfig(t *testing.T) { ProxyConfig: proxyConfig, } - testNewProxyConfigFromSandboxConfig(t, sandboxConfig, proxyConfig) + testNewProxyFromSandboxConfig(t, sandboxConfig) } func TestNewProxyConfigFromKataProxySandboxConfig(t *testing.T) { @@ -190,22 +190,11 @@ func TestNewProxyConfigFromKataProxySandboxConfig(t *testing.T) { ProxyConfig: proxyConfig, } - testNewProxyConfigFromSandboxConfig(t, sandboxConfig, proxyConfig) -} - -func TestNewProxyConfigNilSandboxConfigFailure(t *testing.T) { - if _, err := newProxyConfig(nil); err == nil { - t.Fatal("Should fail because SandboxConfig provided is nil") - } + testNewProxyFromSandboxConfig(t, sandboxConfig) } func TestNewProxyConfigNoPathFailure(t *testing.T) { - sandboxConfig := &SandboxConfig{ - ProxyType: CCProxyType, - ProxyConfig: ProxyConfig{}, - } - - if _, err := newProxyConfig(sandboxConfig); err == nil { + if err := validateProxyConfig(ProxyConfig{}); err == nil { t.Fatal("Should fail because ProxyConfig has no Path") } }