Skip to content

Commit

Permalink
Implement autodetection of Windows buildkitd socket
Browse files Browse the repository at this point in the history
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 <Paul.Hampson@Pobox.com>
  • Loading branch information
TBBle committed Oct 22, 2023
1 parent 91d419c commit 313cd6d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 21 deletions.
28 changes: 7 additions & 21 deletions pkg/buildkitutil/buildkitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
55 changes: 55 additions & 0 deletions pkg/buildkitutil/buildkitutil_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions pkg/buildkitutil/buildkitutil_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 313cd6d

Please sign in to comment.