From 96a487e3d3bd75855e1ee23d6f342019841236b6 Mon Sep 17 00:00:00 2001 From: Raffael Sahli Date: Wed, 7 Feb 2024 13:57:02 +0100 Subject: [PATCH] fix: pull images --- go.mod | 1 + go.sum | 1 + main.go | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 7b98351..35cc436 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 go.uber.org/zap v1.26.0 + golang.org/x/sync v0.4.0 k8s.io/api v0.28.3 k8s.io/apimachinery v0.28.3 k8s.io/client-go v0.28.3 diff --git a/go.sum b/go.sum index 0c1c700..4a6d58b 100644 --- a/go.sum +++ b/go.sum @@ -178,6 +178,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index 1fde800..b7740d1 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ import ( "github.com/sethvargo/go-envconfig" flag "github.com/spf13/pflag" "go.uber.org/zap" + "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -109,6 +110,18 @@ func main() { ) must(err) + g := new(errgroup.Group) + g.Go(func() error { + tag, _ := strings.CutPrefix(config.EtcdVersion, "v") + return pullImage(ctx, dockerClient, logger, fmt.Sprintf("%s:v%s", config.EtcdRegistry, tag)) + }) + g.Go(func() error { + tag, _ := strings.CutPrefix(config.KubeVersion, "v") + return pullImage(ctx, dockerClient, logger, fmt.Sprintf("%s:v%s", config.ApiServerRegistry, tag)) + }) + + must(g.Wait()) + logger.V(1).Info("starting etcd") etcdSpecs, err := startEtcd(ctx, dockerClient) must(err) @@ -389,13 +402,37 @@ func resetContainer(ctx context.Context, dockerClient *dockerclient.Client, id s return nil } +func pullImage(ctx context.Context, dockerClient *dockerclient.Client, logger logr.Logger, image string) error { + images, err := dockerClient.ImageList(ctx, types.ImageListOptions{}) + if err != nil { + return err + } + + for _, img := range images { + if slices.Contains(img.RepoTags, image) { + logger.V(1).Info("image already exists", "tag", image) + return nil + } + } + + logger.Info("pulling image", "tag", image) + w, err := dockerClient.ImagePull(ctx, image, types.ImagePullOptions{}) + if err != nil { + return err + } + + defer w.Close() + _, err = io.Copy(io.Discard, w) + return err +} + func startEtcd(ctx context.Context, dockerClient *dockerclient.Client) (types.ContainerJSON, error) { + tag, _ := strings.CutPrefix(config.EtcdVersion, "v") err := resetContainer(ctx, dockerClient, "/yakmv-etcd") if err != nil { return types.ContainerJSON{}, err } - tag, _ := strings.CutPrefix(config.EtcdVersion, "v") cont, err := dockerClient.ContainerCreate( ctx, &container.Config{