Skip to content

Commit

Permalink
Merge pull request #2587 from TBBle/windows-builder
Browse files Browse the repository at this point in the history
Support for `nerdctl build` on Windows
  • Loading branch information
AkihiroSuda authored Apr 8, 2024
2 parents 822a3ff + b5117a2 commit 640d62d
Show file tree
Hide file tree
Showing 34 changed files with 162 additions and 75 deletions.
3 changes: 1 addition & 2 deletions cmd/nerdctl/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/containerd/log"
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/defaults"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func newBuilderPruneCommand() *cobra.Command {
SilenceErrors: true,
}

AddStringFlag(buildPruneCommand, "buildkit-host", nil, defaults.BuildKitHost(), "BUILDKIT_HOST", "BuildKit address")
AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
return buildPruneCommand
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/nerdctl/builder_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/clientutil"
"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
"github.com/containerd/nerdctl/v2/pkg/defaults"
"github.com/containerd/nerdctl/v2/pkg/strutil"

"github.com/spf13/cobra"
Expand All @@ -43,7 +42,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
SilenceUsage: true,
SilenceErrors: true,
}
AddStringFlag(buildCommand, "buildkit-host", nil, defaults.BuildKitHost(), "BUILDKIT_HOST", "BuildKit address")
AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
buildCommand.Flags().String("target", "", "Set the target build stage to build")
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/container_top_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin || freebsd || netbsd || openbsd
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/main_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/network_create_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
45 changes: 22 additions & 23 deletions pkg/buildkitutil/buildkitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/containerd/log"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
Expand All @@ -56,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 @@ -87,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 Expand Up @@ -143,9 +130,21 @@ func PingBKDaemon(buildkitHost string) error {
return nil
}

// contains open-codes slices.Contains (without generics) from Go 1.21.
// TODO: Replace once Go 1.21 is the minimum supported compiler.
func contains(haystack []string, needle string) bool {
for i := range haystack {
if needle == haystack[i] {
return true
}
}
return false
}

func pingBKDaemon(buildkitHost string) (output string, _ error) {
if runtime.GOOS != "linux" && runtime.GOOS != "freebsd" {
return "", errors.New("only linux and freebsd are supported")
supportedOses := []string{"linux", "freebsd", "windows"}
if !contains(supportedOses, runtime.GOOS) {
return "", fmt.Errorf("only %s are supported", strings.Join(supportedOses, ", "))
}
buildctlBinary, err := BuildctlBinary()
if err != nil {
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 unix

/*
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
}
2 changes: 1 addition & 1 deletion pkg/cioutil/container_io_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 0 additions & 2 deletions pkg/cioutil/container_io_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build windows

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/container/top_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin || freebsd || netbsd || openbsd
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/login/login_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/consoleutil/consoleutil_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/containerutil/container_network_manager_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || freebsd || netbsd || openbsd
//go:build !(linux || windows)

/*
Copyright The containerd Authors.
Expand Down
4 changes: 0 additions & 4 deletions pkg/defaults/defaults_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func CNIRuntimeDir() string {
return "/run/cni"
}

func BuildKitHost() string {
return "unix:///run/buildkit/buildkitd.sock"
}

func CgroupManager() string {
return ""
}
Expand Down
12 changes: 0 additions & 12 deletions pkg/defaults/defaults_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,6 @@ func CNIRuntimeDir() string {
return fmt.Sprintf("%s/cni", xdr)
}

func BuildKitHost() string {
if !rootlessutil.IsRootless() {
return "unix:///run/buildkit/buildkitd.sock"
}
xdr, err := rootlessutil.XDGRuntimeDir()
if err != nil {
log.L.Warn(err)
xdr = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID())
}
return fmt.Sprintf("unix://%s/buildkit/buildkitd.sock", xdr)
}

func NerdctlTOML() string {
if !rootlessutil.IsRootless() {
return "/etc/nerdctl/nerdctl.toml"
Expand Down
5 changes: 0 additions & 5 deletions pkg/defaults/defaults_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package defaults

import (
"fmt"
"os"
"path/filepath"
)
Expand All @@ -44,10 +43,6 @@ func CNIRuntimeDir() string {
return ""
}

func BuildKitHost() string {
return fmt.Sprint("\\\\.\\pipe\\buildkit")
}

func IsSystemdAvailable() bool {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/infoutil/infoutil_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/infoutil/infoutil_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 0 additions & 2 deletions pkg/ipcutil/ipcutil_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build linux

/*
Copyright The containerd Authors.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd
//go:build !(linux || windows)

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/lockutil/lockutil_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 0 additions & 2 deletions pkg/mountutil/mountutil_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build windows

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/netutil/cni_plugin_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
2 changes: 0 additions & 2 deletions pkg/netutil/netutil_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build linux

/*
Copyright The containerd Authors.
Expand Down
2 changes: 1 addition & 1 deletion pkg/netutil/netutil_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build freebsd || linux
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
Loading

0 comments on commit 640d62d

Please sign in to comment.