agent: Return an error, not just an interface

Make `newAgentConfig()` return an explicit error rather than handling
the error scenario by simply returning the `error` object in the
`interface{}` return type. The old behaviour was confusing and
inconsistent with the other functions creating a new config type (shim,
proxy, etc).

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2019-04-15 09:44:57 +01:00
parent 63e1c440a1
commit e803a7f870
3 changed files with 16 additions and 7 deletions

View File

@ -91,19 +91,19 @@ func newAgent(agentType AgentType) agent {
} }
// newAgentConfig returns an agent config from a generic SandboxConfig interface. // newAgentConfig returns an agent config from a generic SandboxConfig interface.
func newAgentConfig(agentType AgentType, agentConfig interface{}) interface{} { func newAgentConfig(agentType AgentType, agentConfig interface{}) (interface{}, error) {
switch agentType { switch agentType {
case NoopAgentType: case NoopAgentType:
return nil return nil, nil
case KataContainersAgent: case KataContainersAgent:
var kataAgentConfig KataAgentConfig var kataAgentConfig KataAgentConfig
err := mapstructure.Decode(agentConfig, &kataAgentConfig) err := mapstructure.Decode(agentConfig, &kataAgentConfig)
if err != nil { if err != nil {
return err return nil, err
} }
return kataAgentConfig return kataAgentConfig, nil
default: default:
return nil return nil, nil
} }
} }

View File

@ -86,7 +86,12 @@ func TestNewAgentFromUnknownAgentType(t *testing.T) {
} }
func testNewAgentConfig(t *testing.T, config SandboxConfig, expected interface{}) { func testNewAgentConfig(t *testing.T, config SandboxConfig, expected interface{}) {
agentConfig := newAgentConfig(config.AgentType, config.AgentConfig) agentConfig, err := newAgentConfig(config.AgentType, config.AgentConfig)
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(agentConfig, expected) == false { if reflect.DeepEqual(agentConfig, expected) == false {
t.Fatal() t.Fatal()
} }

View File

@ -583,7 +583,11 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor
return nil, err return nil, err
} }
agentConfig := newAgentConfig(sandboxConfig.AgentType, sandboxConfig.AgentConfig) agentConfig, err := newAgentConfig(sandboxConfig.AgentType, sandboxConfig.AgentConfig)
if err != nil {
return nil, err
}
if err = s.agent.init(ctx, s, agentConfig); err != nil { if err = s.agent.init(ctx, s, agentConfig); err != nil {
return nil, err return nil, err
} }