automatically detect vendored buildkit version

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-07-19 16:26:18 +02:00
parent 57754bebb8
commit 304959b3f5
3 changed files with 32 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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++ {