Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
fix: pull images
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Feb 7, 2024
1 parent 88bdd7c commit 96a487e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
39 changes: 38 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 96a487e

Please sign in to comment.