From 304959b3f5a2cc5921c30b999447afa828b07605 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Mon, 19 Jul 2021 16:26:18 +0200 Subject: [PATCH] automatically detect vendored buildkit version Signed-off-by: Andrea Luzzardi --- .github/workflows/ci.yml | 2 +- Makefile | 9 +-------- util/buildkitd/buildkitd.go | 40 +++++++++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e7737f2d..5782d24a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,7 +38,7 @@ jobs: - name: Lint run: | - make shellcheck cuelint check-buildkit-version docslint + make shellcheck cuelint docslint - name: Markdown Lint uses: avto-dev/markdown-lint@v1 diff --git a/Makefile b/Makefile index 00c5e1e8..1c0bbb8c 100644 --- a/Makefile +++ b/Makefile @@ -31,14 +31,7 @@ shellcheck: shellcheck ./universe/*.bats ./universe/*.bash .PHONY: lint -lint: shellcheck cuelint golint check-buildkit-version docslint - -.PHONY: check-buildkit-version -check-buildkit-version: - @test \ - "$(shell grep buildkit ./go.mod | cut -d' ' -f2)" = \ - "$(shell grep ' = "v' ./util/buildkitd/buildkitd.go | sed -E 's/^.*version.*=.*\"(v.*)\"/\1/' )" \ - || { echo buildkit version mismatch go.mod != util/buildkitd/buildkitd.go ; exit 1; } +lint: shellcheck cuelint golint docslint .PHONY: integration integration: core-integration universe-test diff --git a/util/buildkitd/buildkitd.go b/util/buildkitd/buildkitd.go index 8587d0a3..f314c5da 100644 --- a/util/buildkitd/buildkitd.go +++ b/util/buildkitd/buildkitd.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os/exec" + "runtime/debug" "strings" "time" @@ -14,14 +15,33 @@ import ( "github.com/rs/zerolog/log" ) +var ( + // vendoredVersion is filled in by init() + vendoredVersion string +) + const ( image = "moby/buildkit" - version = "v0.9.0-rc1" - imageVersion = image + ":" + version containerName = "dagger-buildkitd" volumeName = "dagger-buildkitd" ) +func init() { + bi, ok := debug.ReadBuildInfo() + if !ok { + panic("unable to retrieve build info") + } + for _, d := range bi.Deps { + if d.Path == "github.com/moby/buildkit" { + vendoredVersion = d.Version + break + } + } + if vendoredVersion == "" { + panic("failed to solve vendored buildkit version") + } +} + func Start(ctx context.Context) (string, error) { lg := log.Ctx(ctx) @@ -40,11 +60,11 @@ func Start(ctx context.Context) (string, error) { lg.Debug().Str("version", currentVersion).Msg("detected buildkit version") } - if currentVersion != version { + if currentVersion != vendoredVersion { if currentVersion != "" { lg. Info(). - Str("version", version). + Str("version", vendoredVersion). Msg("upgrading buildkit") if err := remvoveBuildkit(ctx); err != nil { return "", err @@ -52,7 +72,7 @@ func Start(ctx context.Context) (string, error) { } else { lg. Info(). - Str("version", version). + Str("version", vendoredVersion). Msg("starting buildkit") } if err := startBuildkit(ctx); err != nil { @@ -85,14 +105,14 @@ func startBuildkit(ctx context.Context) error { lg := log. Ctx(ctx). With(). - Str("version", version). + Str("version", vendoredVersion). Logger() lg.Debug().Msg("pulling buildkit image") cmd := exec.CommandContext(ctx, "docker", "pull", - imageVersion, + image+":"+vendoredVersion, ) output, err := cmd.CombinedOutput() if err != nil { @@ -117,7 +137,7 @@ func startBuildkit(ctx context.Context) error { "-v", volumeName+":/var/lib/buildkit", "--name", containerName, "--privileged", - imageVersion, + image+":"+vendoredVersion, ) output, err = cmd.CombinedOutput() if err != nil { @@ -145,10 +165,10 @@ func waitBuildkit(ctx context.Context) error { } defer c.Close() - // Try to connect every 100ms up to 50 times (5 seconds total) + // Try to connect every 100ms up to 100 times (10 seconds total) const ( retryPeriod = 100 * time.Millisecond - retryAttempts = 50 + retryAttempts = 100 ) for retry := 0; retry < retryAttempts; retry++ {