Skip to content

Commit

Permalink
virtconainers: add SetFactory API
Browse files Browse the repository at this point in the history
Add SetFactory to allow virtcontainers consumers to set a vm factory.
And use it to create new VMs whenever the factory is set.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Jul 19, 2018
1 parent bdd5c66 commit a7d888f
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 54 deletions.
12 changes: 6 additions & 6 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func SetLogger(logger logrus.FieldLogger) {

// CreateSandbox is the virtcontainers sandbox creation entry point.
// CreateSandbox creates a sandbox and its containers. It does not start them.
func CreateSandbox(sandboxConfig SandboxConfig) (VCSandbox, error) {
return createSandboxFromConfig(sandboxConfig)
func CreateSandbox(sandboxConfig SandboxConfig, factory Factory) (VCSandbox, error) {
return createSandboxFromConfig(sandboxConfig, factory)
}

func createSandboxFromConfig(sandboxConfig SandboxConfig) (*Sandbox, error) {
func createSandboxFromConfig(sandboxConfig SandboxConfig, factory Factory) (*Sandbox, error) {
// Create the sandbox.
s, err := createSandbox(sandboxConfig)
s, err := createSandbox(sandboxConfig, factory)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -206,8 +206,8 @@ func StopSandbox(sandboxID string) (VCSandbox, error) {

// RunSandbox is the virtcontainers sandbox running entry point.
// RunSandbox creates a sandbox and its containers and then it starts them.
func RunSandbox(sandboxConfig SandboxConfig) (VCSandbox, error) {
s, err := createSandboxFromConfig(sandboxConfig)
func RunSandbox(sandboxConfig SandboxConfig, factory Factory) (VCSandbox, error) {
s, err := createSandboxFromConfig(sandboxConfig, factory)
if err != nil {
return nil, err
}
Expand Down
70 changes: 35 additions & 35 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestCreateSandboxNoopAgentSuccessful(t *testing.T) {

config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestCreateSandboxHyperstartAgentSuccessful(t *testing.T) {
proxy.Start()
defer proxy.Stop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestCreateSandboxKataAgentSuccessful(t *testing.T) {
}
defer kataProxyMock.Stop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand All @@ -347,7 +347,7 @@ func TestCreateSandboxFailing(t *testing.T) {

config := SandboxConfig{}

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p.(*Sandbox) != nil || err == nil {
t.Fatal()
}
Expand All @@ -358,7 +358,7 @@ func TestDeleteSandboxNoopAgentSuccessful(t *testing.T) {

config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestDeleteSandboxHyperstartAgentSuccessful(t *testing.T) {
proxy.Start()
defer proxy.Stop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -452,7 +452,7 @@ func TestDeleteSandboxKataAgentSuccessful(t *testing.T) {
}
defer kataProxyMock.Stop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -751,7 +751,7 @@ func TestRunSandboxNoopAgentSuccessful(t *testing.T) {

config := newTestSandboxConfigNoop()

p, err := RunSandbox(config)
p, err := RunSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -787,7 +787,7 @@ func TestRunSandboxHyperstartAgentSuccessful(t *testing.T) {
hyperConfig := config.AgentConfig.(HyperConfig)
config.AgentConfig = hyperConfig

p, err := RunSandbox(config)
p, err := RunSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -833,7 +833,7 @@ func TestRunSandboxKataAgentSuccessful(t *testing.T) {
}
defer kataProxyMock.Stop()

p, err := RunSandbox(config)
p, err := RunSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand All @@ -855,7 +855,7 @@ func TestRunSandboxFailing(t *testing.T) {

config := SandboxConfig{}

p, err := RunSandbox(config)
p, err := RunSandbox(config, nil)
if p != nil || err == nil {
t.Fatal()
}
Expand All @@ -868,7 +868,7 @@ func TestListSandboxSuccessful(t *testing.T) {

config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -928,7 +928,7 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) {
},
}

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -985,7 +985,7 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) {
},
}

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1014,7 +1014,7 @@ func TestStatusSandboxFailingFetchSandboxConfig(t *testing.T) {

config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand All @@ -1034,7 +1034,7 @@ func TestStatusPodSandboxFailingFetchSandboxState(t *testing.T) {

config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ func TestCreateContainerSuccessful(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ func TestCreateContainerFailingNoSandbox(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1130,7 +1130,7 @@ func TestDeleteContainerSuccessful(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func TestDeleteContainerFailingNoContainer(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1249,7 +1249,7 @@ func TestStartContainerFailingNoContainer(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand All @@ -1272,7 +1272,7 @@ func TestStartContainerFailingSandboxNotStarted(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1493,7 +1493,7 @@ func TestStopContainerFailingNoContainer(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1674,7 +1674,7 @@ func TestEnterContainerFailingNoContainer(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1731,7 +1731,7 @@ func TestStatusContainerSuccessful(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1782,7 +1782,7 @@ func TestStatusContainerStateReady(t *testing.T) {
contID := "101"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1845,7 +1845,7 @@ func TestStatusContainerStateRunning(t *testing.T) {
contID := "101"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1917,7 +1917,7 @@ func TestStatusContainerFailing(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand All @@ -1940,7 +1940,7 @@ func TestStatsContainerFailing(t *testing.T) {
contID := "100"
config := newTestSandboxConfigNoop()

p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
if p == nil || err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1973,7 +1973,7 @@ func TestStatsContainer(t *testing.T) {
assert.Error(err)

config := newTestSandboxConfigNoop()
p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
assert.NoError(err)
assert.NotNil(p)

Expand Down Expand Up @@ -2023,7 +2023,7 @@ func TestProcessListContainer(t *testing.T) {
assert.Error(err)

config := newTestSandboxConfigNoop()
p, err := CreateSandbox(config)
p, err := CreateSandbox(config, nil)
assert.NoError(err)
assert.NotNil(p)

Expand Down Expand Up @@ -2117,7 +2117,7 @@ func createAndStartSandbox(config SandboxConfig) (sandbox VCSandbox, sandboxDir
err error) {

// Create sandbox
sandbox, err = CreateSandbox(config)
sandbox, err = CreateSandbox(config, nil)
if sandbox == nil || err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -2158,7 +2158,7 @@ func createStartStopDeleteSandbox(b *testing.B, sandboxConfig SandboxConfig) {

func createStartStopDeleteContainers(b *testing.B, sandboxConfig SandboxConfig, contConfigs []ContainerConfig) {
// Create sandbox
p, err := CreateSandbox(sandboxConfig)
p, err := CreateSandbox(sandboxConfig, nil)
if err != nil {
b.Fatalf("Could not create sandbox: %s", err)
}
Expand Down Expand Up @@ -2326,7 +2326,7 @@ func TestFetchSandbox(t *testing.T) {

config := newTestSandboxConfigNoop()

s, err := CreateSandbox(config)
s, err := CreateSandbox(config, nil)
if s == nil || err != nil {
t.Fatal(err)
}
Expand All @@ -2348,7 +2348,7 @@ func TestReleaseSandbox(t *testing.T) {

config := newTestSandboxConfigNoop()

s, err := CreateSandbox(config)
s, err := CreateSandbox(config, nil)
if s == nil || err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/example_pod_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Example_createAndStartSandbox() {
Containers: []vc.ContainerConfig{container},
}

_, err := vc.RunSandbox(sandboxConfig)
_, err := vc.RunSandbox(sandboxConfig, nil)
if err != nil {
fmt.Printf("Could not run sandbox: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/hack/virtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func runSandbox(context *cli.Context) error {
return fmt.Errorf("Could not build sandbox config: %s", err)
}

_, err = vc.RunSandbox(sandboxConfig)
_, err = vc.RunSandbox(sandboxConfig, nil)
if err != nil {
return fmt.Errorf("Could not run sandbox: %s", err)
}
Expand All @@ -338,7 +338,7 @@ func createSandbox(context *cli.Context) error {
return fmt.Errorf("Could not build sandbox config: %s", err)
}

p, err := vc.CreateSandbox(sandboxConfig)
p, err := vc.CreateSandbox(sandboxConfig, nil)
if err != nil {
return fmt.Errorf("Could not create sandbox: %s", err)
}
Expand Down
Loading

0 comments on commit a7d888f

Please sign in to comment.