From 313cd6d1c205cc7daebdbc37ecb9d018146ace14 Mon Sep 17 00:00:00 2001 From: "Paul \"TBBle\" Hampson" Date: Sun, 22 Oct 2023 12:33:45 +0900 Subject: [PATCH] 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 --- pkg/buildkitutil/buildkitutil.go | 28 +++--------- pkg/buildkitutil/buildkitutil_unix.go | 55 ++++++++++++++++++++++++ pkg/buildkitutil/buildkitutil_windows.go | 21 +++++++++ 3 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 pkg/buildkitutil/buildkitutil_unix.go create mode 100644 pkg/buildkitutil/buildkitutil_windows.go diff --git a/pkg/buildkitutil/buildkitutil.go b/pkg/buildkitutil/buildkitutil.go index 865a0bbded9..31a08dcd84b 100644 --- a/pkg/buildkitutil/buildkitutil.go +++ b/pkg/buildkitutil/buildkitutil.go @@ -56,28 +56,14 @@ func BuildctlBaseArgs(buildkitHost string) []string { } func GetBuildkitHost(namespace string) (string, error) { - if namespace == "" { - return "", fmt.Errorf("namespace must be specified") - } - // Try candidate locations of the current containerd namespace. - run := "/run/" - if rootlessutil.IsRootless() { - var err error - run, err = rootlessutil.XDGRuntimeDir() - if err != nil { - log.L.Warn(err) - run = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID()) - } - } - var hostRel []string - if namespace != "default" { - hostRel = append(hostRel, fmt.Sprintf("buildkit-%s/buildkitd.sock", namespace)) + paths, err := getBuildkitHostCandidates(namespace) + if err != nil { + return "", err } - hostRel = append(hostRel, "buildkit-default/buildkitd.sock", "buildkit/buildkitd.sock") + var errs []error //nolint:prealloc - for _, p := range hostRel { - log.L.Debugf("Choosing the buildkit host %q, candidates=%v (in %q)", p, hostRel, run) - buildkitHost := "unix://" + filepath.Join(run, p) + for _, buildkitHost := range paths { + log.L.Debugf("Choosing the buildkit host %q, candidates=%v", buildkitHost, paths) _, err := pingBKDaemon(buildkitHost) if err == nil { log.L.Debugf("Chosen buildkit host %q", buildkitHost) @@ -87,7 +73,7 @@ func GetBuildkitHost(namespace string) (string, error) { } allErr := errors.Join(errs...) log.L.WithError(allErr).Error(getHint()) - return "", fmt.Errorf("no buildkit host is available, tried %d candidates: %w", len(hostRel), allErr) + return "", fmt.Errorf("no buildkit host is available, tried %d candidates: %w", len(paths), allErr) } func GetWorkerLabels(buildkitHost string) (labels map[string]string, _ error) { diff --git a/pkg/buildkitutil/buildkitutil_unix.go b/pkg/buildkitutil/buildkitutil_unix.go new file mode 100644 index 00000000000..d96405652d3 --- /dev/null +++ b/pkg/buildkitutil/buildkitutil_unix.go @@ -0,0 +1,55 @@ +//go:build freebsd || linux + +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package buildkitutil + +import ( + "fmt" + "path/filepath" + + "github.com/containerd/log" + "github.com/containerd/nerdctl/pkg/rootlessutil" +) + +func getBuildkitHostCandidates(namespace string) ([]string, error) { + if namespace == "" { + return []string{}, fmt.Errorf("namespace must be specified") + } + // Try candidate locations of the current containerd namespace. + run := "/run/" + if rootlessutil.IsRootless() { + var err error + run, err = rootlessutil.XDGRuntimeDir() + if err != nil { + log.L.Warn(err) + run = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID()) + } + } + var hostRel []string + if namespace != "default" { + hostRel = append(hostRel, fmt.Sprintf("buildkit-%s/buildkitd.sock", namespace)) + } + hostRel = append(hostRel, "buildkit-default/buildkitd.sock", "buildkit/buildkitd.sock") + + candidates := make([]string, len(hostRel)) + for _, p := range hostRel { + candidates = append(candidates, "unix://"+filepath.Join(run, p)) + } + + return candidates, nil +} diff --git a/pkg/buildkitutil/buildkitutil_windows.go b/pkg/buildkitutil/buildkitutil_windows.go new file mode 100644 index 00000000000..dd38470c068 --- /dev/null +++ b/pkg/buildkitutil/buildkitutil_windows.go @@ -0,0 +1,21 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package buildkitutil + +func getBuildkitHostCandidates(namespace string) ([]string, error) { + return []string{"npipe:////./pipe/buildkitd"}, nil +}