Skip to content

Commit 906244c

Browse files
committed
Pull image on container creation
Signed-off-by: Sascha Grunert <[email protected]>
1 parent 24f0bb5 commit 906244c

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

cmd/crictl/container.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ var createContainerCommand = cli.Command{
6969
if err := getRuntimeClient(context); err != nil {
7070
return err
7171
}
72+
if err := getImageClient(context); err != nil {
73+
return err
74+
}
7275

7376
opts := createOptions{
7477
podID: context.Args().Get(0),
@@ -78,7 +81,7 @@ var createContainerCommand = cli.Command{
7881
},
7982
}
8083

81-
ctrID, err := CreateContainer(runtimeClient, opts)
84+
ctrID, err := CreateContainer(imageClient, runtimeClient, opts)
8285
if err != nil {
8386
return fmt.Errorf("Creating container failed: %v", err)
8487
}
@@ -374,13 +377,16 @@ var runContainerCommand = cli.Command{
374377
if err := getRuntimeClient(context); err != nil {
375378
return err
376379
}
380+
if err := getImageClient(context); err != nil {
381+
return err
382+
}
377383

378384
opts := runOptions{
379385
configPath: context.Args().Get(0),
380386
podConfig: context.Args().Get(1),
381387
}
382388

383-
err := RunContainer(runtimeClient, opts, context.String("runtime"))
389+
err := RunContainer(imageClient, runtimeClient, opts, context.String("runtime"))
384390
if err != nil {
385391
return fmt.Errorf("Running container failed: %v", err)
386392
}
@@ -390,27 +396,30 @@ var runContainerCommand = cli.Command{
390396

391397
// RunContainer starts a container in the provided sandbox
392398
func RunContainer(
393-
client pb.RuntimeServiceClient, opts runOptions, runtime string,
399+
iClient pb.ImageServiceClient,
400+
rClient pb.RuntimeServiceClient,
401+
opts runOptions,
402+
runtime string,
394403
) error {
395404
// Create the pod
396405
podSandboxConfig, err := loadPodSandboxConfig(opts.podConfig)
397406
if err != nil {
398407
return fmt.Errorf("load podSandboxConfig failed: %v", err)
399408
}
400-
podID, err := RunPodSandbox(runtimeClient, podSandboxConfig, runtime)
409+
podID, err := RunPodSandbox(rClient, podSandboxConfig, runtime)
401410
if err != nil {
402411
return fmt.Errorf("run pod sandbox failed: %v", err)
403412
}
404413

405414
// Create the container
406415
containerOptions := createOptions{podID, &opts}
407-
ctrID, err := CreateContainer(runtimeClient, containerOptions)
416+
ctrID, err := CreateContainer(iClient, rClient, containerOptions)
408417
if err != nil {
409418
return fmt.Errorf("creating container failed: %v", err)
410419
}
411420

412421
// Start the container
413-
err = StartContainer(runtimeClient, ctrID)
422+
err = StartContainer(rClient, ctrID)
414423
if err != nil {
415424
return fmt.Errorf("starting the container %q failed: %v", ctrID, err)
416425
}
@@ -419,7 +428,12 @@ func RunContainer(
419428

420429
// CreateContainer sends a CreateContainerRequest to the server, and parses
421430
// the returned CreateContainerResponse.
422-
func CreateContainer(client pb.RuntimeServiceClient, opts createOptions) (string, error) {
431+
func CreateContainer(
432+
iClient pb.ImageServiceClient,
433+
rClient pb.RuntimeServiceClient,
434+
opts createOptions,
435+
) (string, error) {
436+
423437
config, err := loadContainerConfig(opts.configPath)
424438
if err != nil {
425439
return "", err
@@ -432,13 +446,19 @@ func CreateContainer(client pb.RuntimeServiceClient, opts createOptions) (string
432446
}
433447
}
434448

449+
// Try to pull the image before container creation
450+
image := config.GetImage().GetImage()
451+
if _, err := PullImage(iClient, image, nil); err != nil {
452+
return "", err
453+
}
454+
435455
request := &pb.CreateContainerRequest{
436456
PodSandboxId: opts.podID,
437457
Config: config,
438458
SandboxConfig: podConfig,
439459
}
440460
logrus.Debugf("CreateContainerRequest: %v", request)
441-
r, err := client.CreateContainer(context.Background(), request)
461+
r, err := rClient.CreateContainer(context.Background(), request)
442462
logrus.Debugf("CreateContainerResponse: %v", r)
443463
if err != nil {
444464
return "", err

0 commit comments

Comments
 (0)