Skip to content

Commit

Permalink
Fix windows linter complaints
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
  • Loading branch information
apostasie committed Sep 4, 2024
1 parent 30c63cf commit efeb191
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 299 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unix

/*
Copyright The containerd Authors.
Expand Down
1 change: 0 additions & 1 deletion cmd/nerdctl/container/container_list_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
type psTestContainer struct {
name string
labels map[string]string
volumes []string
network string
}

Expand Down
26 changes: 0 additions & 26 deletions cmd/nerdctl/container/container_run_network_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"io"
"net"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -223,28 +222,3 @@ func baseTestRunPort(t *testing.T, nginxImage string, nginxIndexHTMLSnippet stri
}

}

func valuesOfMapStringString(m map[string]string) map[string]struct{} {
res := make(map[string]struct{})
for _, v := range m {
res[v] = struct{}{}
}
return res
}

func extractHostPort(portMapping string, port string) (string, error) {
// Regular expression to extract host port from port mapping information
re := regexp.MustCompile(`(?P<containerPort>\d{1,5})/tcp ->.*?0.0.0.0:(?P<hostPort>\d{1,5}).*?`)
portMappingLines := strings.Split(portMapping, "\n")
for _, portMappingLine := range portMappingLines {
// Find the matches
matches := re.FindStringSubmatch(portMappingLine)
// Check if there is a match
if len(matches) >= 3 && matches[1] == port {
// Extract the host port number
hostPort := matches[2]
return hostPort, nil
}
}
return "", fmt.Errorf("could not extract host port from port mapping: %s", portMapping)
}
25 changes: 25 additions & 0 deletions cmd/nerdctl/container/container_run_network_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ import (
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
)

func extractHostPort(portMapping string, port string) (string, error) {
// Regular expression to extract host port from port mapping information
re := regexp.MustCompile(`(?P<containerPort>\d{1,5})/tcp ->.*?0.0.0.0:(?P<hostPort>\d{1,5}).*?`)
portMappingLines := strings.Split(portMapping, "\n")
for _, portMappingLine := range portMappingLines {
// Find the matches
matches := re.FindStringSubmatch(portMappingLine)
// Check if there is a match
if len(matches) >= 3 && matches[1] == port {
// Extract the host port number
hostPort := matches[2]
return hostPort, nil
}
}
return "", fmt.Errorf("could not extract host port from port mapping: %s", portMapping)
}

func valuesOfMapStringString(m map[string]string) map[string]struct{} {
res := make(map[string]struct{})
for _, v := range m {
res[v] = struct{}{}
}
return res
}

// TestRunInternetConnectivity tests Internet connectivity with `apk update`
func TestRunInternetConnectivity(t *testing.T) {
base := testutil.NewBase(t)
Expand Down
18 changes: 9 additions & 9 deletions cmd/nerdctl/container/container_run_network_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,22 @@ func TestHnsEndpointsExistDuringContainerLifecycle(t *testing.T) {
"tail", "-f",
)
t.Logf("Creating HNS lifecycle test container with command: %q", strings.Join(cmd.Command, " "))
containerId := strings.TrimSpace(cmd.Run().Stdout())
t.Logf("HNS endpoint lifecycle test container ID: %q", containerId)
containerID := strings.TrimSpace(cmd.Run().Stdout())
t.Logf("HNS endpoint lifecycle test container ID: %q", containerID)

// HNS endpoints should be allocated on container creation.
assertHnsEndpointsExistence(t, true, containerId, testNet.Name)
assertHnsEndpointsExistence(t, true, containerID, testNet.Name)

// Starting and stopping the container should NOT affect/change the endpoints.
base.Cmd("start", containerId).AssertOK()
assertHnsEndpointsExistence(t, true, containerId, testNet.Name)
base.Cmd("start", containerID).AssertOK()
assertHnsEndpointsExistence(t, true, containerID, testNet.Name)

base.Cmd("stop", containerId).AssertOK()
assertHnsEndpointsExistence(t, true, containerId, testNet.Name)
base.Cmd("stop", containerID).AssertOK()
assertHnsEndpointsExistence(t, true, containerID, testNet.Name)

// Removing the container should remove the HNS endpoints.
base.Cmd("rm", containerId).AssertOK()
assertHnsEndpointsExistence(t, false, containerId, testNet.Name)
base.Cmd("rm", containerID).AssertOK()
assertHnsEndpointsExistence(t, false, containerID, testNet.Name)
}

// Returns a network to be used for testing.
Expand Down
8 changes: 6 additions & 2 deletions cmd/nerdctl/image/image_encrypt_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ func TestImageEncryptJWE(t *testing.T) {
testutil.RequiresBuild(t)
testutil.DockerIncompatible(t)
keyPair := helpers.NewJWEKeyPair(t)
defer keyPair.Cleanup()
base := testutil.NewBase(t)
tID := testutil.Identifier(t)
reg := testregistry.NewWithNoAuth(base, 0, false)

defer keyPair.Cleanup()
defer reg.Cleanup(nil)

base.Cmd("pull", testutil.CommonImage).AssertOK()
encryptImageRef := fmt.Sprintf("127.0.0.1:%d/%s:encrypted", reg.Port, tID)
defer base.Cmd("rmi", encryptImageRef).Run()
base.Cmd("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, testutil.CommonImage, encryptImageRef).AssertOK()
base.Cmd("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", encryptImageRef).AssertOutExactly("1\n")
base.Cmd("image", "inspect", "--mode=native", "--format={{json .Manifest.Layers}}", encryptImageRef).AssertOutContains("org.opencontainers.image.enc.keys.jwe")
base.Cmd("push", encryptImageRef).AssertOK()

defer base.Cmd("rmi", encryptImageRef).Run()

// remove all local images (in the nerdctl-test namespace), to ensure that we do not have blobs of the original image.
helpers.RmiAll(base)
base.Cmd("pull", encryptImageRef).AssertFail() // defaults to --unpack=true, and fails due to missing prv key
Expand Down
27 changes: 0 additions & 27 deletions pkg/cmd/container/run_cgroup_freebsd.go

This file was deleted.

27 changes: 0 additions & 27 deletions pkg/cmd/container/run_cgroup_windows.go

This file was deleted.

63 changes: 0 additions & 63 deletions pkg/cmd/container/run_gpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"fmt"
"strconv"
"strings"

"github.com/containerd/containerd/v2/contrib/nvidia"
"github.com/containerd/containerd/v2/pkg/oci"

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

// GPUReq is a request for GPUs.
Expand All @@ -36,64 +31,6 @@ type GPUReq struct {
Capabilities []string
}

func parseGPUOpts(value []string) (res []oci.SpecOpts, _ error) {
for _, gpu := range value {
gpuOpt, err := parseGPUOpt(gpu)
if err != nil {
return nil, err
}
res = append(res, gpuOpt)
}
return res, nil
}

func parseGPUOpt(value string) (oci.SpecOpts, error) {
req, err := ParseGPUOptCSV(value)
if err != nil {
return nil, err
}

var gpuOpts []nvidia.Opts

if len(req.DeviceIDs) > 0 {
gpuOpts = append(gpuOpts, nvidia.WithDeviceUUIDs(req.DeviceIDs...))
} else if req.Count > 0 {
var devices []int
for i := 0; i < req.Count; i++ {
devices = append(devices, i)
}
gpuOpts = append(gpuOpts, nvidia.WithDevices(devices...))
} else if req.Count < 0 {
gpuOpts = append(gpuOpts, nvidia.WithAllDevices)
}

str2cap := make(map[string]nvidia.Capability)
for _, c := range nvidia.AllCaps() {
str2cap[string(c)] = c
}
var nvidiaCaps []nvidia.Capability
for _, c := range req.Capabilities {
if cp, isNvidiaCap := str2cap[c]; isNvidiaCap {
nvidiaCaps = append(nvidiaCaps, cp)
}
}
if len(nvidiaCaps) != 0 {
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidiaCaps...))
} else {
// Add "utility", "compute" capability if unset.
// Please see also: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#driver-capabilities
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidia.Utility, nvidia.Compute))
}

if rootlessutil.IsRootless() {
// "--no-cgroups" option is needed to nvidia-container-cli in rootless environment
// Please see also: https://github.com/moby/moby/issues/38729#issuecomment-463493866
gpuOpts = append(gpuOpts, nvidia.WithNoCgroups)
}

return nvidia.WithGPUs(gpuOpts...), nil
}

// ParseGPUOptCSV parses a GPU option from CSV.
func ParseGPUOptCSV(value string) (*GPUReq, error) {
csvReader := csv.NewReader(strings.NewReader(value))
Expand Down
59 changes: 59 additions & 0 deletions pkg/cmd/container/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/contrib/nvidia"
"github.com/containerd/containerd/v2/core/containers"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/log"
Expand Down Expand Up @@ -260,3 +261,61 @@ func withOOMScoreAdj(score int) oci.SpecOpts {
return nil
}
}

func parseGPUOpts(value []string) (res []oci.SpecOpts, _ error) {
for _, gpu := range value {
gpuOpt, err := parseGPUOpt(gpu)
if err != nil {
return nil, err
}
res = append(res, gpuOpt)
}
return res, nil
}

func parseGPUOpt(value string) (oci.SpecOpts, error) {
req, err := ParseGPUOptCSV(value)
if err != nil {
return nil, err
}

var gpuOpts []nvidia.Opts

if len(req.DeviceIDs) > 0 {
gpuOpts = append(gpuOpts, nvidia.WithDeviceUUIDs(req.DeviceIDs...))
} else if req.Count > 0 {
var devices []int
for i := 0; i < req.Count; i++ {
devices = append(devices, i)
}
gpuOpts = append(gpuOpts, nvidia.WithDevices(devices...))
} else if req.Count < 0 {
gpuOpts = append(gpuOpts, nvidia.WithAllDevices)
}

str2cap := make(map[string]nvidia.Capability)
for _, c := range nvidia.AllCaps() {
str2cap[string(c)] = c
}
var nvidiaCaps []nvidia.Capability
for _, c := range req.Capabilities {
if cp, isNvidiaCap := str2cap[c]; isNvidiaCap {
nvidiaCaps = append(nvidiaCaps, cp)
}
}
if len(nvidiaCaps) != 0 {
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidiaCaps...))
} else {
// Add "utility", "compute" capability if unset.
// Please see also: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#driver-capabilities
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidia.Utility, nvidia.Compute))
}

if rootlessutil.IsRootless() {
// "--no-cgroups" option is needed to nvidia-container-cli in rootless environment
// Please see also: https://github.com/moby/moby/issues/38729#issuecomment-463493866
gpuOpts = append(gpuOpts, nvidia.WithNoCgroups)
}

return nvidia.WithGPUs(gpuOpts...), nil
}
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/cmd/container/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func setPlatformOptions(
internalLabels *internalLabels,
options types.ContainerCreateOptions,
) ([]oci.SpecOpts, error) {
var opts []oci.SpecOpts
opts := []oci.SpecOpts{}
if options.CPUs > 0.0 {
opts = append(opts, oci.WithWindowsCPUCount(uint64(options.CPUs)))
}
Expand Down
Loading

0 comments on commit efeb191

Please sign in to comment.