Skip to content

Commit 6b58c06

Browse files
authored
Merge pull request #6768 from jackfrancis/e2e-kind-local-image-priming
🌱 pull non-existent images when building kind bootstrap cluster
2 parents e53cab0 + 2e2148f commit 6b58c06

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

test/framework/bootstrap/kind_util.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func LoadImagesToKindCluster(ctx context.Context, input LoadImagesToKindClusterI
134134
}
135135

136136
// LoadImage will put a local image onto the kind node.
137+
// If the image doesn't exist locally we will attempt to pull it remotely.
137138
func loadImage(ctx context.Context, cluster, image string) error {
138139
// Save the image into a tar
139140
dir, err := os.MkdirTemp("", "image-tar")
@@ -147,7 +148,21 @@ func loadImage(ctx context.Context, cluster, image string) error {
147148
if err != nil {
148149
return errors.Wrap(err, "failed to access container runtime")
149150
}
150-
151+
// in the nominal E2E scenario images have been locally built and added to cache
152+
exists, err := containerRuntime.ImageExistsLocally(ctx, image)
153+
if err != nil {
154+
return errors.Wrapf(err, "error listing local image %s", image)
155+
}
156+
// in some scenarios we refer to a real reference image which may not have been pre-downloaded
157+
if !exists {
158+
log.Logf("Image %s not present in local container image cache, will pull", image)
159+
err := containerRuntime.PullContainerImage(ctx, image)
160+
if err != nil {
161+
return errors.Wrapf(err, "error pulling image %q", image)
162+
}
163+
} else {
164+
log.Logf("Image %s is present in local container image cache", image)
165+
}
151166
err = containerRuntime.SaveContainerImage(ctx, image, imageTarPath)
152167
if err != nil {
153168
return errors.Wrapf(err, "error saving image %q to %q", image, imageTarPath)

test/infrastructure/container/docker.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
9898
func (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").
128144
func (d *dockerRuntime) GetHostPort(ctx context.Context, containerName, portAndProtocol string) (string, error) {
129145
// Get details about the container

test/infrastructure/container/fake.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ func (f *FakeRuntime) PullContainerImageIfNotExists(ctx context.Context, image s
6666
return nil
6767
}
6868

69+
// PullContainerImage triggers the Docker engine to pull an image.
70+
func (f *FakeRuntime) PullContainerImage(ctx context.Context, image string) error {
71+
return nil
72+
}
73+
74+
// ImageExistsLocally returns if the specified image exists in local container image cache.
75+
func (f *FakeRuntime) ImageExistsLocally(ctx context.Context, image string) (bool, error) {
76+
return false, nil
77+
}
78+
6979
// GetHostPort looks up the host port bound for the port and protocol (e.g. "6443/tcp").
7080
func (f *FakeRuntime) GetHostPort(ctx context.Context, containerName, portAndProtocol string) (string, error) {
7181
return "", nil

test/infrastructure/container/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type providerKey struct{}
3131
type Runtime interface {
3232
SaveContainerImage(ctx context.Context, image, dest string) error
3333
PullContainerImageIfNotExists(ctx context.Context, image string) error
34+
PullContainerImage(ctx context.Context, image string) error
35+
ImageExistsLocally(ctx context.Context, image string) (bool, error)
3436
GetHostPort(ctx context.Context, containerName, portAndProtocol string) (string, error)
3537
GetContainerIPs(ctx context.Context, containerName string) (string, string, error)
3638
ExecContainer(ctx context.Context, containerName string, config *ExecContainerInput, command string, args ...string) error

0 commit comments

Comments
 (0)