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
funcCreatePod(podConfigPodConfig) (VCPod, error) {
41+
returncreatePodFromConfig(podConfig)
42+
}
43+
44+
funccreatePodFromConfig(podConfigPodConfig) (*Pod, error) {
4145
// Create the pod.
4246
p, err:=createPod(podConfig)
4347
iferr!=nil {
@@ -168,8 +172,12 @@ func StartPod(podID string) (VCPod, error) {
168172
returnnil, err
169173
}
170174

175+
returnstartPod(p)
176+
}
177+
178+
funcstartPod(p*Pod) (*Pod, error) {
171179
// Start it
172-
err=p.start()
180+
err:=p.start()
173181
iferr!=nil {
174182
returnnil, 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
funcRunPod(podConfigPodConfig) (VCPod, error) {
217-
// Create the pod.
218-
p, err:=createPod(podConfig)
219-
iferr!=nil {
220-
returnnil, err
221-
}
222-
223-
// Store it.
224-
err=p.storePod()
225+
p, err:=createPodFromConfig(podConfig)
225226
iferr!=nil {
226227
returnnil, err
227228
}
@@ -232,56 +233,7 @@ func RunPod(podConfig PodConfig) (VCPod, error) {
232233
}
233234
deferunlockPod(lockFile)
234235

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

0 commit comments

Comments
 (0)