Skip to content

Commit

Permalink
Updates to build on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jsturtevant committed Apr 24, 2021
1 parent e0ee259 commit 552f67c
Show file tree
Hide file tree
Showing 39 changed files with 1,156 additions and 405 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
# -----------------------------------------------------------------------------

GO ?= go

GOOS ?= $(shell go env GOOS)
ifeq ($(GOOS),windows)
BIN_EXT := .exe
endif

PACKAGE := github.com/containerd/nerdctl
BINDIR ?= /usr/local/bin
Expand All @@ -28,7 +31,7 @@ VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)
VERSION_TRIMMED := $(VERSION:v%=%)
REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)

export GO_BUILD=GO111MODULE=on CGO_ENABLED=0 $(GO) build -ldflags "-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"
export GO_BUILD=GO111MODULE=on CGO_ENABLED=0 GOOS=$(GOOS) $(GO) build -ldflags "-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"

all: binaries

Expand All @@ -40,7 +43,7 @@ help:
@echo " * 'clean' - Clean artifacts."

nerdctl:
$(GO_BUILD) -o $(CURDIR)/_output/nerdctl $(PACKAGE)
$(GO_BUILD) -o $(CURDIR)/_output/nerdctl$(BIN_EXT) $(PACKAGE)

clean:
find . -name \*~ -delete
Expand Down Expand Up @@ -72,6 +75,9 @@ artifacts: clean
GOOS=linux GOARCH=s390x make -C $(CURDIR) binaries
tar $(TAR_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-s390x.tar.gz _output/nerdctl extras/rootless/*

GOOS=windows GOARCH=amd64 make -C $(CURDIR) binaries
tar $(TAR_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-windows-amd64.tar.gz

rm -f $(CURDIR)/_output/nerdctl

DOCKER_BUILDKIT=1 docker build --output type=tar,dest=$(CURDIR)/_output/nerdctl-full-$(VERSION_TRIMMED)-linux-amd64.tar --target out-full $(CURDIR)
Expand Down
File renamed without changes.
70 changes: 70 additions & 0 deletions client_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
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 main

import (
"context"
"os"
"path/filepath"

"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/opencontainers/go-digest"
"github.com/urfave/cli/v2"
)

func newClient(clicontext *cli.Context) (*containerd.Client, context.Context, context.CancelFunc, error) {
ctx := context.Background()
namespace := clicontext.String("namespace")
ctx = namespaces.WithNamespace(ctx, namespace)
address := clicontext.String("address")
const dockerContainerdaddress = "\\\\.\\pipe\\containerd-containerd"
//TODO fall back?
client, err := containerd.New(address)
if err != nil {
return nil, nil, nil, err
}
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
return client, ctx, cancel, nil
}

// getDataStore returns a string like "/var/lib/nerdctl/1935db59".
// "1935db9" is from `$(echo -n "/run/containerd/containerd.sock" | sha256sum | cut -c1-8)``
func getDataStore(clicontext *cli.Context) (string, error) {
dataRoot := clicontext.String("data-root")
if err := os.MkdirAll(dataRoot, 0700); err != nil {
return "", err
}
addrHash, err := getAddrHash(clicontext.String("address"))
if err != nil {
return "", err
}
dataStore := filepath.Join(dataRoot, addrHash)
if err := os.MkdirAll(dataStore, 0700); err != nil {
return "", err
}
return dataStore, nil
}

func getAddrHash(addr string) (string, error) {
const addrHashLen = 8

d := digest.SHA256.FromString(addr)
h := d.Encoded()[0:addrHashLen]
return h, nil
}
14 changes: 1 addition & 13 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
"github.com/containerd/containerd/pkg/cap"
"github.com/containerd/nerdctl/pkg/idgen"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/strutil"
Expand Down Expand Up @@ -224,21 +223,10 @@ func generateExecProcessSpec(ctx context.Context, clicontext *cli.Context, conta
}

if clicontext.Bool("privileged") {
if pspec.Capabilities == nil {
pspec.Capabilities = &specs.LinuxCapabilities{}
}
allCaps, err := cap.Current()
err = setExecCapabilities(pspec)
if err != nil {
return nil, err
}
pspec.Capabilities.Bounding = allCaps
pspec.Capabilities.Permitted = pspec.Capabilities.Bounding
pspec.Capabilities.Inheritable = pspec.Capabilities.Bounding
pspec.Capabilities.Effective = pspec.Capabilities.Bounding

// https://github.com/moby/moby/pull/36466/files
// > `docker exec --privileged` does not currently disable AppArmor
// > profiles. Privileged configuration of the container is inherited
}

return pspec, nil
Expand Down
41 changes: 41 additions & 0 deletions exec_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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 main

import (
"github.com/containerd/containerd/pkg/cap"
"github.com/opencontainers/runtime-spec/specs-go"
)

func setExecCapabilities(pspec *specs.Process) error {
if pspec.Capabilities == nil {
pspec.Capabilities = &specs.LinuxCapabilities{}
}
allCaps, err := cap.Current()
if err != nil {
return err
}
pspec.Capabilities.Bounding = allCaps
pspec.Capabilities.Permitted = pspec.Capabilities.Bounding
pspec.Capabilities.Inheritable = pspec.Capabilities.Bounding
pspec.Capabilities.Effective = pspec.Capabilities.Bounding

// https://github.com/moby/moby/pull/36466/files
// > `docker exec --privileged` does not currently disable AppArmor
// > profiles. Privileged configuration of the container is inherited
return nil
}
26 changes: 26 additions & 0 deletions exec_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
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 main

import (
"github.com/opencontainers/runtime-spec/specs-go"
)

func setExecCapabilities(pspec *specs.Process) error {
//no op windows
return nil
}
20 changes: 3 additions & 17 deletions login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"syscall"

"github.com/containerd/nerdctl/pkg/version"
dockercliconfig "github.com/docker/cli/cli/config"
Expand All @@ -35,7 +33,6 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)

type loginOptions struct {
Expand Down Expand Up @@ -210,22 +207,11 @@ func ConfigureAuthentification(clicontext *cli.Context, authConfig *types.AuthCo
if options.password == "" {

fmt.Print("Enter Password: ")
var fd int
if term.IsTerminal(syscall.Stdin) {
fd = syscall.Stdin
} else {
tty, err := os.Open("/dev/tty")
if err != nil {
return errors.Wrap(err, "error allocating terminal")
}
defer tty.Close()
fd = int(tty.Fd())
}
bytePassword, err := term.ReadPassword(fd)
pwd, err := readPassword()
if err != nil {
return errors.Wrap(err, "error reading password")
return err
}
options.password = string(bytePassword)
options.password = pwd
}

if options.password == "" {
Expand Down
45 changes: 45 additions & 0 deletions login_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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 main

import (
"os"
"syscall"

"github.com/pkg/errors"
"golang.org/x/term"
)

func readPassword() (string, error) {
var fd int
if term.IsTerminal(syscall.Stdin) {
fd = syscall.Stdin
} else {
tty, err := os.Open("/dev/tty")
if err != nil {
return "", errors.Wrap(err, "error allocating terminal")
}
defer tty.Close()
fd = int(tty.Fd())
}
bytePassword, err := term.ReadPassword(fd)
if err != nil {
return "", errors.Wrap(err, "error reading password")
}

return string(bytePassword), nil
}
29 changes: 29 additions & 0 deletions login_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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 main

import (
"bufio"
"os"
)

func readPassword() (string, error) {
reader := bufio.NewReader(os.Stdin)
pwd, _ := reader.ReadString('\n')

return pwd, nil
}
Loading

0 comments on commit 552f67c

Please sign in to comment.