proxy: remove newProxyConfig

The proxy config does not depend on proxy type. Let's not misture them.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
Peng Tao 2018-08-14 16:51:23 +08:00
parent c41c9de839
commit f39fa5d489
4 changed files with 25 additions and 43 deletions

View File

@ -6,6 +6,7 @@
package virtcontainers package virtcontainers
import ( import (
"fmt"
"os/exec" "os/exec"
) )
@ -14,8 +15,12 @@ type ccProxy struct {
// start is the proxy start implementation for ccProxy. // start is the proxy start implementation for ccProxy.
func (p *ccProxy) start(sandbox *Sandbox, params proxyParams) (int, string, error) { func (p *ccProxy) start(sandbox *Sandbox, params proxyParams) (int, string, error) {
config, err := newProxyConfig(sandbox.config) if sandbox.config == nil {
if err != nil { return -1, "", fmt.Errorf("Sandbox config cannot be nil")
}
config := sandbox.config.ProxyConfig
if err := validateProxyConfig(config); err != nil {
return -1, "", err return -1, "", err
} }

View File

@ -25,6 +25,10 @@ func (p *kataProxy) consoleWatched() bool {
// start is kataProxy start implementation for proxy interface. // start is kataProxy start implementation for proxy interface.
func (p *kataProxy) start(sandbox *Sandbox, params proxyParams) (int, string, error) { func (p *kataProxy) start(sandbox *Sandbox, params proxyParams) (int, string, error) {
sandbox.Logger().Info("Starting regular Kata proxy rather than built-in") 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 { if sandbox.agent == nil {
return -1, "", fmt.Errorf("No agent") 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") return -1, "", fmt.Errorf("AgentURL cannot be empty")
} }
config, err := newProxyConfig(sandbox.config) config := sandbox.config.ProxyConfig
if err != nil { if err := validateProxyConfig(config); err != nil {
return -1, "", err return -1, "", err
} }

View File

@ -119,28 +119,12 @@ func newProxy(pType ProxyType) (proxy, error) {
} }
} }
// newProxyConfig returns a proxy config from a generic SandboxConfig handler, func validateProxyConfig(proxyConfig ProxyConfig) error {
// after it properly checked the configuration was valid. if len(proxyConfig.Path) == 0 {
func newProxyConfig(sandboxConfig *SandboxConfig) (ProxyConfig, error) { return fmt.Errorf("Proxy path cannot be empty")
if sandboxConfig == nil {
return ProxyConfig{}, fmt.Errorf("Sandbox config cannot be nil")
} }
var config ProxyConfig return nil
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
} }
func defaultProxyURL(sandbox *Sandbox, socketType string) (string, error) { func defaultProxyURL(sandbox *Sandbox, socketType string) (string, error) {

View File

@ -154,15 +154,15 @@ func TestNewProxyFromUnknownProxyType(t *testing.T) {
} }
} }
func testNewProxyConfigFromSandboxConfig(t *testing.T, sandboxConfig SandboxConfig, expected ProxyConfig) { func testNewProxyFromSandboxConfig(t *testing.T, sandboxConfig SandboxConfig) {
result, err := newProxyConfig(&sandboxConfig) if _, err := newProxy(sandboxConfig.ProxyType); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if reflect.DeepEqual(result, expected) == false { if err := validateProxyConfig(sandboxConfig.ProxyConfig); err != nil {
t.Fatalf("Got %+v\nExpecting %+v", result, expected) t.Fatal(err)
} }
} }
var testProxyPath = "proxy-path" var testProxyPath = "proxy-path"
@ -177,7 +177,7 @@ func TestNewProxyConfigFromCCProxySandboxConfig(t *testing.T) {
ProxyConfig: proxyConfig, ProxyConfig: proxyConfig,
} }
testNewProxyConfigFromSandboxConfig(t, sandboxConfig, proxyConfig) testNewProxyFromSandboxConfig(t, sandboxConfig)
} }
func TestNewProxyConfigFromKataProxySandboxConfig(t *testing.T) { func TestNewProxyConfigFromKataProxySandboxConfig(t *testing.T) {
@ -190,22 +190,11 @@ func TestNewProxyConfigFromKataProxySandboxConfig(t *testing.T) {
ProxyConfig: proxyConfig, ProxyConfig: proxyConfig,
} }
testNewProxyConfigFromSandboxConfig(t, sandboxConfig, proxyConfig) testNewProxyFromSandboxConfig(t, sandboxConfig)
}
func TestNewProxyConfigNilSandboxConfigFailure(t *testing.T) {
if _, err := newProxyConfig(nil); err == nil {
t.Fatal("Should fail because SandboxConfig provided is nil")
}
} }
func TestNewProxyConfigNoPathFailure(t *testing.T) { func TestNewProxyConfigNoPathFailure(t *testing.T) {
sandboxConfig := &SandboxConfig{ if err := validateProxyConfig(ProxyConfig{}); err == nil {
ProxyType: CCProxyType,
ProxyConfig: ProxyConfig{},
}
if _, err := newProxyConfig(sandboxConfig); err == nil {
t.Fatal("Should fail because ProxyConfig has no Path") t.Fatal("Should fail because ProxyConfig has no Path")
} }
} }