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.

This also corrects the autodetection path on FreeBSD to start in
/var/run instead of current Linux FHS's /run, and doesn't bother trying
to support Rootless mode and related path guessing on FreeBSD.

Signed-off-by: Paul "TBBle" Hampson <[email protected]>
  • Loading branch information
TBBle committed Apr 8, 2024
1 parent 6e6aa4a commit 5fac99b
Show file tree
Hide file tree
Showing 5 changed files with 128 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 @@ -57,28 +57,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 @@ -88,7 +74,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
22 changes: 22 additions & 0 deletions pkg/buildkitutil/buildkitutil_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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 getRuntimeVariableDataDir() string {
// Per hier(7) dated July 6, 2023.
return "/var/run"
}
39 changes: 39 additions & 0 deletions pkg/buildkitutil/buildkitutil_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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"

"github.com/containerd/log"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
)

func getRuntimeVariableDataDir() string {
// Per Linux Foundation "Filesystem Hierarchy Standard" version 3.0 section 3.15.
// Under version 2.3, this was "/var/run".
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())
}
}
return run
}
39 changes: 39 additions & 0 deletions pkg/buildkitutil/buildkitutil_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//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"
)

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 := getRuntimeVariableDataDir()
var candidates []string
if namespace != "default" {
candidates = append(candidates, "unix://"+filepath.Join(run, fmt.Sprintf("buildkit-%s/buildkitd.sock", namespace)))
}
candidates = append(candidates, "unix://"+filepath.Join(run, "buildkit-default/buildkitd.sock"), "unix://"+filepath.Join(run, "buildkit/buildkitd.sock"))

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 5fac99b

Please sign in to comment.