mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
driver: resolve BuildKit version for docker driver
Buildx currently returns the Docker Engine version for the docker driver and it can be confusing. This is because BuildKit before 0.11 version does not support the Info API that returns the BuildKit version. This change resolves the BuildKit version from the Docker engine one. Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@ -3,6 +3,7 @@ package docker
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/buildx/driver"
|
||||
"github.com/docker/buildx/util/progress"
|
||||
@ -34,7 +35,11 @@ func (d *Driver) Version(ctx context.Context) (string, error) {
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(driver.ErrNotConnecting, err.Error())
|
||||
}
|
||||
return v.Version, nil
|
||||
if bkversion, _ := resolveBuildKitVersion(v.Version); bkversion != "" {
|
||||
return bkversion, nil
|
||||
}
|
||||
// https://github.com/moby/moby/blob/efc7a2abc3ab6dfa7d8d5d8c1c3b99138989b0f1/builder/builder-next/worker/worker.go#L176
|
||||
return strings.TrimSuffix(v.Version, "-moby"), nil
|
||||
}
|
||||
|
||||
func (d *Driver) Stop(ctx context.Context, force bool) error {
|
||||
|
186
driver/docker/version.go
Normal file
186
driver/docker/version.go
Normal file
@ -0,0 +1,186 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/semver/v3"
|
||||
)
|
||||
|
||||
type mobyBuildkitVersion struct {
|
||||
MobyVersionConstraint string
|
||||
BuildkitVersion string
|
||||
}
|
||||
|
||||
// https://gist.github.com/crazy-max/780cb6878c37cb79ec3f7699706cf83f
|
||||
// constraint syntax: https://github.com/Masterminds/semver#checking-version-constraints
|
||||
var mobyBuildkitVersions = []mobyBuildkitVersion{
|
||||
{
|
||||
MobyVersionConstraint: ">= 18.06.0-0, < 18.06.1-0",
|
||||
BuildkitVersion: "v0.0.0+9acf51e",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 18.06.1-0, < 18.09.0-0",
|
||||
BuildkitVersion: "v0.0.0+98f1604",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 18.09.0-0, < 18.09.1-0",
|
||||
BuildkitVersion: "v0.0.0+c7bb575",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "~18.09.1-0",
|
||||
BuildkitVersion: "v0.3.3",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "> 18.09.1-0, < 18.09.6-0",
|
||||
BuildkitVersion: "v0.3.3+d9f7592",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 18.09.6-0, < 18.09.7-0",
|
||||
BuildkitVersion: "v0.4.0+ed4da8b",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 18.09.7-0, < 19.03.0-0",
|
||||
BuildkitVersion: "v0.4.0+05766c5",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.0-beta2",
|
||||
BuildkitVersion: "v0.4.0+b302896",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.0-beta3",
|
||||
BuildkitVersion: "v0.4.0+8818c67",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.0-beta5",
|
||||
BuildkitVersion: "v0.5.1+f238f1e",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "< 19.03.2-0",
|
||||
BuildkitVersion: "v0.5.1+1f89ec1",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.2-beta1",
|
||||
BuildkitVersion: "v0.6.1",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 19.03.2-0, < 19.03.3-0",
|
||||
BuildkitVersion: "v0.6.1+588c73e",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 19.03.3-0, < 19.03.5-beta2",
|
||||
BuildkitVersion: "v0.6.2",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.5-rc1",
|
||||
BuildkitVersion: "v0.6.2+ff93519",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.5",
|
||||
BuildkitVersion: "v0.6.3+928f3b4",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.6-rc1",
|
||||
BuildkitVersion: "v0.6.3+926935b",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 19.03.6-rc2, < 19.03.7-0",
|
||||
BuildkitVersion: "v0.6.3+57e8ad5",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 19.03.7-0, < 19.03.9-0",
|
||||
BuildkitVersion: "v0.6.4",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 19.03.9-0, < 19.03.13-0",
|
||||
BuildkitVersion: "v0.6.4+a7d7b7f",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.13-beta2",
|
||||
BuildkitVersion: "v0.6.4+da1f4bf",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "<= 19.03.14",
|
||||
BuildkitVersion: "v0.6.4+df89d4d",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "< 20.10.0",
|
||||
BuildkitVersion: "v0.6.4+396bfe2",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "20.10.0-0 - 20.10.2-0",
|
||||
BuildkitVersion: "v0.8.1",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 20.10.3-0, < 20.10.4-0",
|
||||
BuildkitVersion: "v0.8.1+68bb095",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "20.10.4-0 - 20.10.6",
|
||||
BuildkitVersion: "v0.8.2",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "20.10.7-0 - 20.10.10-0",
|
||||
BuildkitVersion: "v0.8.2+244e8cde",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "20.10.11-0 - 20.10.18-0",
|
||||
BuildkitVersion: "v0.8.2+bc07b2b8",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 20.10.19-0, < 20.10.20-0",
|
||||
BuildkitVersion: "v0.8.2+3a1eeca5",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 20.10.20-0, < 20.10.21-0",
|
||||
BuildkitVersion: "v0.8.2+c0149372",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 20.10.21-0, <= 20.10.23",
|
||||
BuildkitVersion: "v0.8.2+eeb7b65",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "~20.10-0",
|
||||
BuildkitVersion: "v0.8+unknown",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "~22.06-0",
|
||||
BuildkitVersion: "v0.10.3",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: ">= 23.0.0-0, < 23.0.1-0",
|
||||
BuildkitVersion: "v0.10.6",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "23.0.1",
|
||||
BuildkitVersion: "v0.10.6+4f0ee09",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "23.0.2",
|
||||
BuildkitVersion: "v0.10.6+70f2ad5",
|
||||
},
|
||||
{
|
||||
MobyVersionConstraint: "~23-0",
|
||||
BuildkitVersion: "v0.10+unknown",
|
||||
},
|
||||
}
|
||||
|
||||
func resolveBuildKitVersion(ver string) (string, error) {
|
||||
mobyVersion, err := semver.NewVersion(ver)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, m := range mobyBuildkitVersions {
|
||||
c, err := semver.NewConstraint(m.MobyVersionConstraint)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
//if _, errs := c.Validate(mobyVersion); len(errs) > 0 {
|
||||
// for _, err := range errs {
|
||||
// fmt.Printf("%s: %v\n", m.MobyVersionConstraint, err)
|
||||
// }
|
||||
//}
|
||||
if !c.Check(mobyVersion) {
|
||||
continue
|
||||
}
|
||||
return m.BuildkitVersion, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
127
driver/docker/version_test.go
Normal file
127
driver/docker/version_test.go
Normal file
@ -0,0 +1,127 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConstraint(t *testing.T) {
|
||||
for _, tt := range mobyBuildkitVersions {
|
||||
tt := tt
|
||||
t.Run(tt.MobyVersionConstraint, func(t *testing.T) {
|
||||
_, err := semver.NewConstraint(tt.MobyVersionConstraint)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveBuildKitVersion(t *testing.T) {
|
||||
cases := []struct {
|
||||
mobyVersion string
|
||||
expected string
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
mobyVersion: "18.06.1-ce",
|
||||
expected: "v0.0.0+98f1604",
|
||||
},
|
||||
{
|
||||
mobyVersion: "18.09.1-beta1",
|
||||
expected: "v0.3.3",
|
||||
},
|
||||
{
|
||||
mobyVersion: "19.03.0-beta1",
|
||||
expected: "v0.4.0+b302896",
|
||||
},
|
||||
{
|
||||
mobyVersion: "19.03.5-beta2",
|
||||
expected: "v0.6.2+ff93519",
|
||||
},
|
||||
{
|
||||
mobyVersion: "19.03.13-beta1",
|
||||
expected: "v0.6.4+da1f4bf",
|
||||
},
|
||||
{
|
||||
mobyVersion: "19.03.13-beta2",
|
||||
expected: "v0.6.4+da1f4bf",
|
||||
},
|
||||
{
|
||||
mobyVersion: "19.03.13",
|
||||
expected: "v0.6.4+df89d4d",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.3-rc.1",
|
||||
expected: "v0.8.1+68bb095",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.3",
|
||||
expected: "v0.8.1+68bb095",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.4",
|
||||
expected: "v0.8.2",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.16",
|
||||
expected: "v0.8.2+bc07b2b8",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.19",
|
||||
expected: "v0.8.2+3a1eeca5",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.23",
|
||||
expected: "v0.8.2+eeb7b65",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.24",
|
||||
expected: "v0.8+unknown",
|
||||
},
|
||||
{
|
||||
mobyVersion: "20.10.50",
|
||||
expected: "v0.8+unknown",
|
||||
},
|
||||
{
|
||||
mobyVersion: "22.06.0-beta.0",
|
||||
expected: "v0.10.3",
|
||||
},
|
||||
{
|
||||
mobyVersion: "22.06.0",
|
||||
expected: "v0.10.3",
|
||||
},
|
||||
{
|
||||
mobyVersion: "23.0.0-rc.4",
|
||||
expected: "v0.10.6",
|
||||
},
|
||||
{
|
||||
mobyVersion: "23.0.0",
|
||||
expected: "v0.10.6",
|
||||
},
|
||||
{
|
||||
mobyVersion: "23.0.1",
|
||||
expected: "v0.10.6+4f0ee09",
|
||||
},
|
||||
{
|
||||
mobyVersion: "23.0.2-rc.1",
|
||||
expected: "v0.10+unknown",
|
||||
},
|
||||
{
|
||||
mobyVersion: "23.0.4",
|
||||
expected: "v0.10+unknown",
|
||||
},
|
||||
}
|
||||
for _, tt := range cases {
|
||||
tt := tt
|
||||
t.Run(tt.mobyVersion, func(t *testing.T) {
|
||||
bkVersion, err := resolveBuildKitVersion(tt.mobyVersion)
|
||||
if tt.err {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected, bkVersion)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user