From 57156ee95cccc172e3757520283b04d09f1646e0 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Mon, 9 May 2022 16:17:01 +0200 Subject: [PATCH] ls: adds fallback if buildkit version info unimplemented Signed-off-by: CrazyMax --- commands/util.go | 7 ++++--- driver/docker-container/driver.go | 17 +++++++++++++++++ driver/docker/driver.go | 8 ++++++++ driver/driver.go | 1 + driver/kubernetes/driver.go | 4 ++++ driver/remote/driver.go | 4 ++++ 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/commands/util.go b/commands/util.go index f2339a7a..a75d3420 100644 --- a/commands/util.go +++ b/commands/util.go @@ -314,9 +314,10 @@ func loadInfoData(ctx context.Context, d *dinfo) error { inf, err := c.Info(ctx) if err != nil { if st, ok := grpcerrors.AsGRPCStatus(err); ok && st.Code() == codes.Unimplemented { - d.version = "N/A" - } else { - return errors.Wrap(err, "getting info") + d.version, err = d.di.Driver.Version(ctx) + if err != nil { + return errors.Wrap(err, "getting version") + } } } else { d.version = inf.BuildkitVersion.Version diff --git a/driver/docker-container/driver.go b/driver/docker-container/driver.go index 86b35b3f..ac99d61a 100644 --- a/driver/docker-container/driver.go +++ b/driver/docker-container/driver.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "strings" "time" "github.com/docker/buildx/driver" @@ -286,6 +287,22 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { }, nil } +func (d *Driver) Version(ctx context.Context) (string, error) { + bufStdout := &bytes.Buffer{} + bufStderr := &bytes.Buffer{} + if err := d.run(ctx, []string{"buildkitd", "--version"}, bufStdout, bufStderr); err != nil { + if bufStderr.Len() > 0 { + return "", errors.Wrap(err, bufStderr.String()) + } + return "", err + } + version := strings.Fields(bufStdout.String()) + if len(version) != 4 { + return "", errors.Errorf("unexpected version format: %s", bufStdout.String()) + } + return version[2], nil +} + func (d *Driver) Stop(ctx context.Context, force bool) error { info, err := d.Info(ctx) if err != nil { diff --git a/driver/docker/driver.go b/driver/docker/driver.go index 26b6198b..d3d05d48 100644 --- a/driver/docker/driver.go +++ b/driver/docker/driver.go @@ -29,6 +29,14 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { }, nil } +func (d *Driver) Version(ctx context.Context) (string, error) { + v, err := d.DockerAPI.ServerVersion(ctx) + if err != nil { + return "", errors.Wrapf(driver.ErrNotConnecting, err.Error()) + } + return v.Version, nil +} + func (d *Driver) Stop(ctx context.Context, force bool) error { return nil } diff --git a/driver/driver.go b/driver/driver.go index 1eceb5aa..ed108031 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -53,6 +53,7 @@ type Driver interface { Factory() Factory Bootstrap(context.Context, progress.Logger) error Info(context.Context) (*Info, error) + Version(context.Context) (string, error) Stop(ctx context.Context, force bool) error Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error Client(ctx context.Context) (*client.Client, error) diff --git a/driver/kubernetes/driver.go b/driver/kubernetes/driver.go index 92725dff..7fae8170 100644 --- a/driver/kubernetes/driver.go +++ b/driver/kubernetes/driver.go @@ -160,6 +160,10 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { }, nil } +func (d *Driver) Version(ctx context.Context) (string, error) { + return "", nil +} + func (d *Driver) Stop(ctx context.Context, force bool) error { // future version may scale the replicas to zero here return nil diff --git a/driver/remote/driver.go b/driver/remote/driver.go index ed52bea0..df2bbcdb 100644 --- a/driver/remote/driver.go +++ b/driver/remote/driver.go @@ -41,6 +41,10 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { }, nil } +func (d *Driver) Version(ctx context.Context) (string, error) { + return "", nil +} + func (d *Driver) Stop(ctx context.Context, force bool) error { return nil }