From 2171bb65d93e1f6cfdf3771393ed8ad61f695123 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sat, 16 Nov 2024 16:49:06 +0100 Subject: [PATCH 1/2] env_kind.go: load image into kind cluster This commit load the image into the kind cluster with ``` kind load docker-image ``` We need to do this if the image is not in a public registry. Honestly, the purpose of the `preLoadImage()` function is not clear to me. Signed-off-by: leonnicolas --- env_kind.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/env_kind.go b/env_kind.go index 30a0882..f91b4ef 100644 --- a/env_kind.go +++ b/env_kind.go @@ -55,7 +55,6 @@ func validateKindName(name string) error { } if !kindNamePattern.MatchString(name) { return errors.Newf("name can have only %v characters due to kind cluster name constraints, got: %v", kindNamePattern.String(), name) - } return nil } @@ -768,7 +767,7 @@ func (r *kindRunnable) prePullImage(ctx context.Context) (err error) { } if _, err = r.env.execContext(ctx, "docker", "image", "inspect", r.opts.Image).CombinedOutput(); err == nil { - return nil + return r.loadImageIntoKindCluster(ctx) } // Assuming Error: No such image: . @@ -780,7 +779,15 @@ func (r *kindRunnable) prePullImage(ctx context.Context) (err error) { return errors.Wrapf(err, "docker image %q failed to download", r.opts.Image) } - return nil + return r.loadImageIntoKindCluster(ctx) +} + +func (r *kindRunnable) loadImageIntoKindCluster(ctx context.Context) error { + cmd := r.env.execContext(ctx, "kind", "load", "docker-image", "--name", r.env.clusterName, r.opts.Image) + l := &LinePrefixLogger{prefix: r.Name() + ": ", logger: r.logger} + cmd.Stdout = l + cmd.Stderr = l + return cmd.Run() } func (r *kindRunnable) WaitReady() (err error) { From bd1e26e8cb502e29aad732cf4bd7447f7b015536 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Fri, 6 Dec 2024 22:27:48 +0100 Subject: [PATCH 2/2] pull and load image Pull only the image if it isn't present on the machine and always load it into the cluster. --- env_kind.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/env_kind.go b/env_kind.go index f91b4ef..77755ab 100644 --- a/env_kind.go +++ b/env_kind.go @@ -766,17 +766,16 @@ func (r *kindRunnable) prePullImage(ctx context.Context) (err error) { return errors.Newf("service %q is running; expected stopped", r.Name()) } - if _, err = r.env.execContext(ctx, "docker", "image", "inspect", r.opts.Image).CombinedOutput(); err == nil { - return r.loadImageIntoKindCluster(ctx) - } - - // Assuming Error: No such image: . - cmd := r.env.execContext(ctx, "docker", "pull", r.opts.Image) - l := &LinePrefixLogger{prefix: r.Name() + ": ", logger: r.logger} - cmd.Stdout = l - cmd.Stderr = l - if err = cmd.Run(); err != nil { - return errors.Wrapf(err, "docker image %q failed to download", r.opts.Image) + if image, err := r.env.execContext(ctx, "docker", "image", "ls", r.opts.Image, "--format", "{{.Repository}}:{{.Tag}}").CombinedOutput(); err != nil { + return fmt.Errorf("error listing docker images: %w", err) + } else if strings.TrimSpace(string(image)) != r.opts.Image { + cmd := r.env.execContext(ctx, "docker", "pull", r.opts.Image) + l := &LinePrefixLogger{prefix: r.Name() + ": ", logger: r.logger} + cmd.Stdout = l + cmd.Stderr = l + if err = cmd.Run(); err != nil { + return errors.Wrapf(err, "docker image %q failed to download", r.opts.Image) + } } return r.loadImageIntoKindCluster(ctx)