Skip to content

Commit 06c6dc4

Browse files
committed
Implement autodetection of Windows buildkitd socket
Buildkit on Windows doesn't support rootless mode, and doesn't put the namespace into the pipe name currently, so the Windows version is near-trivial. Signed-off-by: Paul "TBBle" Hampson <[email protected]>
1 parent 91d419c commit 06c6dc4

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

pkg/buildkitutil/buildkitutil.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,14 @@ func BuildctlBaseArgs(buildkitHost string) []string {
5656
}
5757

5858
func GetBuildkitHost(namespace string) (string, error) {
59-
if namespace == "" {
60-
return "", fmt.Errorf("namespace must be specified")
61-
}
62-
// Try candidate locations of the current containerd namespace.
63-
run := "/run/"
64-
if rootlessutil.IsRootless() {
65-
var err error
66-
run, err = rootlessutil.XDGRuntimeDir()
67-
if err != nil {
68-
log.L.Warn(err)
69-
run = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID())
70-
}
71-
}
72-
var hostRel []string
73-
if namespace != "default" {
74-
hostRel = append(hostRel, fmt.Sprintf("buildkit-%s/buildkitd.sock", namespace))
59+
paths, err := getBuildkitHostCandidates(namespace)
60+
if err != nil {
61+
return "", err
7562
}
76-
hostRel = append(hostRel, "buildkit-default/buildkitd.sock", "buildkit/buildkitd.sock")
63+
7764
var errs []error //nolint:prealloc
78-
for _, p := range hostRel {
79-
log.L.Debugf("Choosing the buildkit host %q, candidates=%v (in %q)", p, hostRel, run)
80-
buildkitHost := "unix://" + filepath.Join(run, p)
65+
for _, buildkitHost := range paths {
66+
log.L.Debugf("Choosing the buildkit host %q, candidates=%v", buildkitHost, paths)
8167
_, err := pingBKDaemon(buildkitHost)
8268
if err == nil {
8369
log.L.Debugf("Chosen buildkit host %q", buildkitHost)
@@ -87,7 +73,7 @@ func GetBuildkitHost(namespace string) (string, error) {
8773
}
8874
allErr := errors.Join(errs...)
8975
log.L.WithError(allErr).Error(getHint())
90-
return "", fmt.Errorf("no buildkit host is available, tried %d candidates: %w", len(hostRel), allErr)
76+
return "", fmt.Errorf("no buildkit host is available, tried %d candidates: %w", len(paths), allErr)
9177
}
9278

9379
func GetWorkerLabels(buildkitHost string) (labels map[string]string, _ error) {

pkg/buildkitutil/buildkitutil_unix.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build freebsd || linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package buildkitutil
20+
21+
import (
22+
"fmt"
23+
"path/filepath"
24+
25+
"github.com/containerd/log"
26+
"github.com/containerd/nerdctl/pkg/rootlessutil"
27+
)
28+
29+
func getBuildkitHostCandidates(namespace string) ([]string, error) {
30+
if namespace == "" {
31+
return []string{}, fmt.Errorf("namespace must be specified")
32+
}
33+
// Try candidate locations of the current containerd namespace.
34+
run := "/run/"
35+
if rootlessutil.IsRootless() {
36+
var err error
37+
run, err = rootlessutil.XDGRuntimeDir()
38+
if err != nil {
39+
log.L.Warn(err)
40+
run = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID())
41+
}
42+
}
43+
var hostRel []string
44+
if namespace != "default" {
45+
hostRel = append(hostRel, fmt.Sprintf("buildkit-%s/buildkitd.sock", namespace))
46+
}
47+
hostRel = append(hostRel, "buildkit-default/buildkitd.sock", "buildkit/buildkitd.sock")
48+
49+
candidates := make([]string, len(hostRel))
50+
for _, p := range hostRel {
51+
candidates = append(candidates, "unix://"+filepath.Join(run, p))
52+
}
53+
54+
return candidates, nil
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package buildkitutil
18+
19+
func getBuildkitHostCandidates(namespace string) ([]string, error) {
20+
return []string{"npipe:////./pipe/buildkitd"}, nil
21+
}

0 commit comments

Comments
 (0)