Skip to content

Commit 7f13c20

Browse files
committed
Fix nerdctl build logs error even when succeeds
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent 825fa28 commit 7f13c20

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

pkg/buildkitutil/buildkitutil.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"runtime"
2828

2929
"github.com/containerd/nerdctl/pkg/rootlessutil"
30-
30+
"github.com/hashicorp/go-multierror"
3131
"github.com/sirupsen/logrus"
3232
)
3333

@@ -65,15 +65,19 @@ func GetBuildkitHost(namespace string) (string, error) {
6565
hostRel = append(hostRel, fmt.Sprintf("buildkit-%s/buildkitd.sock", namespace))
6666
}
6767
hostRel = append(hostRel, "buildkit-default/buildkitd.sock", "buildkit/buildkitd.sock")
68+
var allErr error
6869
for _, p := range hostRel {
70+
logrus.Debugf("Choosing the buildkit host %q, candidates=%v (in %q)", p, hostRel, run)
6971
buildkitHost := "unix://" + filepath.Join(run, p)
70-
err := PingBKDaemon(buildkitHost)
72+
_, err := pingBKDaemon(buildkitHost)
7173
if err == nil {
74+
logrus.Debugf("Chosen buildkit host %q", buildkitHost)
7275
return buildkitHost, nil
7376
}
74-
logrus.WithField("host", buildkitHost).Warn(err)
77+
allErr = multierror.Append(allErr, fmt.Errorf("failed to ping to host %s: %w", buildkitHost, err))
7578
}
76-
return "", fmt.Errorf("no buildkit host is available")
79+
logrus.WithError(allErr).Error(getHint())
80+
return "", fmt.Errorf("no buildkit host is available, tried %d candidates: %w", len(hostRel), allErr)
7781
}
7882

7983
func GetWorkerLabels(buildkitHost string) (labels map[string]string, _ error) {
@@ -111,27 +115,40 @@ func GetWorkerLabels(buildkitHost string) (labels map[string]string, _ error) {
111115
return labels, nil
112116
}
113117

114-
func PingBKDaemon(buildkitHost string) error {
115-
if runtime.GOOS != "linux" {
116-
return errors.New("only linux is supported")
117-
}
118+
func getHint() string {
118119
hint := "`buildctl` needs to be installed and `buildkitd` needs to be running, see https://github.com/moby/buildkit"
119120
if rootlessutil.IsRootless() {
120121
hint += " , and `containerd-rootless-setuptool.sh install-buildkit` for OCI worker or `containerd-rootless-setuptool.sh install-buildkit-containerd` for containerd worker"
121122
}
123+
return hint
124+
}
125+
126+
func PingBKDaemon(buildkitHost string) error {
127+
if out, err := pingBKDaemon(buildkitHost); err != nil {
128+
if out != "" {
129+
logrus.Error(string(out))
130+
}
131+
return fmt.Errorf(getHint()+": %w", err)
132+
}
133+
return nil
134+
}
135+
136+
func pingBKDaemon(buildkitHost string) (output string, _ error) {
137+
if runtime.GOOS != "linux" {
138+
return "", errors.New("only linux is supported")
139+
}
122140
buildctlBinary, err := BuildctlBinary()
123141
if err != nil {
124-
return fmt.Errorf(hint+": %w", err)
142+
return "", err
125143
}
126144
args := BuildctlBaseArgs(buildkitHost)
127145
args = append(args, "debug", "workers")
128146
buildctlCheckCmd := exec.Command(buildctlBinary, args...)
129147
buildctlCheckCmd.Env = os.Environ()
130148
if out, err := buildctlCheckCmd.CombinedOutput(); err != nil {
131-
logrus.Error(string(out))
132-
return fmt.Errorf(hint+": %w", err)
149+
return string(out), err
133150
}
134-
return nil
151+
return "", nil
135152
}
136153

137154
// WriteTempDockerfile is from https://github.com/docker/cli/blob/v20.10.9/cli/command/image/build/context.go#L118

0 commit comments

Comments
 (0)