Skip to content
This repository was archived by the owner on Apr 3, 2018. It is now read-only.

Commit d449ece

Browse files
author
James O. D. Hunt
committed
api: Refactor RunPod()
Introduce some private functions to allow RunPod() to behave exactly as if CreatePod() + StartPod() had been called instead. Removing the duplicated code ensures the behaviour between the two methods for creating and starting a pod is consistent. Note that this change modifies the RunPod() behaviour very slightly - the locking is now handled later, as was previously being done by CreatePod() + StartPod(). Fixes #485. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
1 parent c3663b2 commit d449ece

2 files changed

Lines changed: 12 additions & 60 deletions

File tree

api.go

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func SetLogger(logger logrus.FieldLogger) {
3838
// CreatePod is the virtcontainers pod creation entry point.
3939
// CreatePod creates a pod and its containers. It does not start them.
4040
func CreatePod(podConfig PodConfig) (VCPod, error) {
41+
return createPodFromConfig(podConfig)
42+
}
43+
44+
func createPodFromConfig(podConfig PodConfig) (*Pod, error) {
4145
// Create the pod.
4246
p, err := createPod(podConfig)
4347
if err != nil {
@@ -168,8 +172,12 @@ func StartPod(podID string) (VCPod, error) {
168172
return nil, err
169173
}
170174

175+
return startPod(p)
176+
}
177+
178+
func startPod(p *Pod) (*Pod, error) {
171179
// Start it
172-
err = p.start()
180+
err := p.start()
173181
if err != nil {
174182
return nil, err
175183
}
@@ -214,14 +222,7 @@ func StopPod(podID string) (VCPod, error) {
214222
// RunPod is the virtcontainers pod running entry point.
215223
// RunPod creates a pod and its containers and then it starts them.
216224
func RunPod(podConfig PodConfig) (VCPod, error) {
217-
// Create the pod.
218-
p, err := createPod(podConfig)
219-
if err != nil {
220-
return nil, err
221-
}
222-
223-
// Store it.
224-
err = p.storePod()
225+
p, err := createPodFromConfig(podConfig)
225226
if err != nil {
226227
return nil, err
227228
}
@@ -232,56 +233,7 @@ func RunPod(podConfig PodConfig) (VCPod, error) {
232233
}
233234
defer unlockPod(lockFile)
234235

235-
// Initialize the network.
236-
netNsPath, netNsCreated, err := p.network.init(p.config.NetworkConfig)
237-
if err != nil {
238-
return nil, err
239-
}
240-
241-
// Execute prestart hooks inside netns
242-
err = p.network.run(netNsPath, func() error {
243-
return p.config.Hooks.preStartHooks()
244-
})
245-
if err != nil {
246-
return nil, err
247-
}
248-
249-
// Add the network
250-
networkNS, err := p.network.add(*p, p.config.NetworkConfig, netNsPath, netNsCreated)
251-
if err != nil {
252-
return nil, err
253-
}
254-
255-
// Store the network
256-
err = p.storage.storePodNetwork(p.id, networkNS)
257-
if err != nil {
258-
return nil, err
259-
}
260-
261-
// Start the VM
262-
err = p.startVM(netNsPath)
263-
if err != nil {
264-
return nil, err
265-
}
266-
267-
// Start shims
268-
if err := p.startShims(); err != nil {
269-
return nil, err
270-
}
271-
272-
// Start the pod
273-
err = p.start()
274-
if err != nil {
275-
p.delete()
276-
return nil, err
277-
}
278-
279-
// Execute poststart hooks
280-
if err := p.config.Hooks.postStartHooks(); err != nil {
281-
return nil, err
282-
}
283-
284-
return p, nil
236+
return startPod(p)
285237
}
286238

287239
// ListPod is the virtcontainers pod listing entry point.

api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func TestCreatePodFailing(t *testing.T) {
274274
config := PodConfig{}
275275

276276
p, err := CreatePod(config)
277-
if p != nil || err == nil {
277+
if p.(*Pod) != nil || err == nil {
278278
t.Fatal()
279279
}
280280
}

0 commit comments

Comments
 (0)