@@ -96,19 +96,19 @@ func (d *dockerRuntime) SaveContainerImage(ctx context.Context, image, dest stri
9696// already exist. This is important when we're using locally build images in CI which
9797// do not exist remotely.
9898func (d * dockerRuntime ) PullContainerImageIfNotExists (ctx context.Context , image string ) error {
99- filters := dockerfilters .NewArgs ()
100- filters .Add ("reference" , image )
101- images , err := d .dockerClient .ImageList (ctx , types.ImageListOptions {
102- Filters : filters ,
103- })
99+ imageExistsLocally , err := d .ImageExistsLocally (ctx , image )
104100 if err != nil {
105- return fmt . Errorf ( "failure listing container images : %v " , err )
101+ return errors . Wrapf ( err , "failure determining if the image exists in local cache : %s " , image )
106102 }
107- // Nothing to do as the image already exists locally.
108- if len (images ) > 0 {
103+ if imageExistsLocally {
109104 return nil
110105 }
111106
107+ return d .PullContainerImage (ctx , image )
108+ }
109+
110+ // PullContainerImage triggers the Docker engine to pull an image.
111+ func (d * dockerRuntime ) PullContainerImage (ctx context.Context , image string ) error {
112112 pullResp , err := d .dockerClient .ImagePull (ctx , image , types.ImagePullOptions {})
113113 if err != nil {
114114 return fmt .Errorf ("failure pulling container image: %v" , err )
@@ -124,6 +124,22 @@ func (d *dockerRuntime) PullContainerImageIfNotExists(ctx context.Context, image
124124 return nil
125125}
126126
127+ // ImageExistsLocally returns if the specified image exists in local container image cache.
128+ func (d * dockerRuntime ) ImageExistsLocally (ctx context.Context , image string ) (bool , error ) {
129+ filters := dockerfilters .NewArgs ()
130+ filters .Add ("reference" , image )
131+ images , err := d .dockerClient .ImageList (ctx , types.ImageListOptions {
132+ Filters : filters ,
133+ })
134+ if err != nil {
135+ return false , errors .Wrapf (err , "failure listing container image: %s" , image )
136+ }
137+ if len (images ) > 0 {
138+ return true , nil
139+ }
140+ return false , nil
141+ }
142+
127143// GetHostPort looks up the host port bound for the port and protocol (e.g. "6443/tcp").
128144func (d * dockerRuntime ) GetHostPort (ctx context.Context , containerName , portAndProtocol string ) (string , error ) {
129145 // Get details about the container
0 commit comments