Skip to content

Commit efeb191

Browse files
committed
Fix windows linter complaints
Signed-off-by: apostasie <[email protected]>
1 parent 30c63cf commit efeb191

22 files changed

+240
-299
lines changed

cmd/nerdctl/container/container_cp.go renamed to cmd/nerdctl/container/container_cp_unix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build unix
2+
13
/*
24
Copyright The containerd Authors.
35

cmd/nerdctl/container/container_list_windows_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
type psTestContainer struct {
3333
name string
3434
labels map[string]string
35-
volumes []string
3635
network string
3736
}
3837

cmd/nerdctl/container/container_run_network_base_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"io"
2424
"net"
25-
"regexp"
2625
"strings"
2726
"testing"
2827

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

225224
}
226-
227-
func valuesOfMapStringString(m map[string]string) map[string]struct{} {
228-
res := make(map[string]struct{})
229-
for _, v := range m {
230-
res[v] = struct{}{}
231-
}
232-
return res
233-
}
234-
235-
func extractHostPort(portMapping string, port string) (string, error) {
236-
// Regular expression to extract host port from port mapping information
237-
re := regexp.MustCompile(`(?P<containerPort>\d{1,5})/tcp ->.*?0.0.0.0:(?P<hostPort>\d{1,5}).*?`)
238-
portMappingLines := strings.Split(portMapping, "\n")
239-
for _, portMappingLine := range portMappingLines {
240-
// Find the matches
241-
matches := re.FindStringSubmatch(portMappingLine)
242-
// Check if there is a match
243-
if len(matches) >= 3 && matches[1] == port {
244-
// Extract the host port number
245-
hostPort := matches[2]
246-
return hostPort, nil
247-
}
248-
}
249-
return "", fmt.Errorf("could not extract host port from port mapping: %s", portMapping)
250-
}

cmd/nerdctl/container/container_run_network_linux_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ import (
3737
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
3838
)
3939

40+
func extractHostPort(portMapping string, port string) (string, error) {
41+
// Regular expression to extract host port from port mapping information
42+
re := regexp.MustCompile(`(?P<containerPort>\d{1,5})/tcp ->.*?0.0.0.0:(?P<hostPort>\d{1,5}).*?`)
43+
portMappingLines := strings.Split(portMapping, "\n")
44+
for _, portMappingLine := range portMappingLines {
45+
// Find the matches
46+
matches := re.FindStringSubmatch(portMappingLine)
47+
// Check if there is a match
48+
if len(matches) >= 3 && matches[1] == port {
49+
// Extract the host port number
50+
hostPort := matches[2]
51+
return hostPort, nil
52+
}
53+
}
54+
return "", fmt.Errorf("could not extract host port from port mapping: %s", portMapping)
55+
}
56+
57+
func valuesOfMapStringString(m map[string]string) map[string]struct{} {
58+
res := make(map[string]struct{})
59+
for _, v := range m {
60+
res[v] = struct{}{}
61+
}
62+
return res
63+
}
64+
4065
// TestRunInternetConnectivity tests Internet connectivity with `apk update`
4166
func TestRunInternetConnectivity(t *testing.T) {
4267
base := testutil.NewBase(t)

cmd/nerdctl/container/container_run_network_windows_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,22 @@ func TestHnsEndpointsExistDuringContainerLifecycle(t *testing.T) {
126126
"tail", "-f",
127127
)
128128
t.Logf("Creating HNS lifecycle test container with command: %q", strings.Join(cmd.Command, " "))
129-
containerId := strings.TrimSpace(cmd.Run().Stdout())
130-
t.Logf("HNS endpoint lifecycle test container ID: %q", containerId)
129+
containerID := strings.TrimSpace(cmd.Run().Stdout())
130+
t.Logf("HNS endpoint lifecycle test container ID: %q", containerID)
131131

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

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

139-
base.Cmd("stop", containerId).AssertOK()
140-
assertHnsEndpointsExistence(t, true, containerId, testNet.Name)
139+
base.Cmd("stop", containerID).AssertOK()
140+
assertHnsEndpointsExistence(t, true, containerID, testNet.Name)
141141

142142
// Removing the container should remove the HNS endpoints.
143-
base.Cmd("rm", containerId).AssertOK()
144-
assertHnsEndpointsExistence(t, false, containerId, testNet.Name)
143+
base.Cmd("rm", containerID).AssertOK()
144+
assertHnsEndpointsExistence(t, false, containerID, testNet.Name)
145145
}
146146

147147
// Returns a network to be used for testing.

cmd/nerdctl/image/image_encrypt_linux_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ func TestImageEncryptJWE(t *testing.T) {
2929
testutil.RequiresBuild(t)
3030
testutil.DockerIncompatible(t)
3131
keyPair := helpers.NewJWEKeyPair(t)
32-
defer keyPair.Cleanup()
3332
base := testutil.NewBase(t)
3433
tID := testutil.Identifier(t)
3534
reg := testregistry.NewWithNoAuth(base, 0, false)
35+
36+
defer keyPair.Cleanup()
3637
defer reg.Cleanup(nil)
38+
3739
base.Cmd("pull", testutil.CommonImage).AssertOK()
3840
encryptImageRef := fmt.Sprintf("127.0.0.1:%d/%s:encrypted", reg.Port, tID)
39-
defer base.Cmd("rmi", encryptImageRef).Run()
4041
base.Cmd("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, testutil.CommonImage, encryptImageRef).AssertOK()
4142
base.Cmd("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", encryptImageRef).AssertOutExactly("1\n")
4243
base.Cmd("image", "inspect", "--mode=native", "--format={{json .Manifest.Layers}}", encryptImageRef).AssertOutContains("org.opencontainers.image.enc.keys.jwe")
4344
base.Cmd("push", encryptImageRef).AssertOK()
45+
46+
defer base.Cmd("rmi", encryptImageRef).Run()
47+
4448
// remove all local images (in the nerdctl-test namespace), to ensure that we do not have blobs of the original image.
4549
helpers.RmiAll(base)
4650
base.Cmd("pull", encryptImageRef).AssertFail() // defaults to --unpack=true, and fails due to missing prv key

pkg/cmd/container/run_cgroup_freebsd.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

pkg/cmd/container/run_cgroup_windows.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

pkg/cmd/container/run_gpus.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ import (
2222
"fmt"
2323
"strconv"
2424
"strings"
25-
26-
"github.com/containerd/containerd/v2/contrib/nvidia"
27-
"github.com/containerd/containerd/v2/pkg/oci"
28-
29-
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3025
)
3126

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

39-
func parseGPUOpts(value []string) (res []oci.SpecOpts, _ error) {
40-
for _, gpu := range value {
41-
gpuOpt, err := parseGPUOpt(gpu)
42-
if err != nil {
43-
return nil, err
44-
}
45-
res = append(res, gpuOpt)
46-
}
47-
return res, nil
48-
}
49-
50-
func parseGPUOpt(value string) (oci.SpecOpts, error) {
51-
req, err := ParseGPUOptCSV(value)
52-
if err != nil {
53-
return nil, err
54-
}
55-
56-
var gpuOpts []nvidia.Opts
57-
58-
if len(req.DeviceIDs) > 0 {
59-
gpuOpts = append(gpuOpts, nvidia.WithDeviceUUIDs(req.DeviceIDs...))
60-
} else if req.Count > 0 {
61-
var devices []int
62-
for i := 0; i < req.Count; i++ {
63-
devices = append(devices, i)
64-
}
65-
gpuOpts = append(gpuOpts, nvidia.WithDevices(devices...))
66-
} else if req.Count < 0 {
67-
gpuOpts = append(gpuOpts, nvidia.WithAllDevices)
68-
}
69-
70-
str2cap := make(map[string]nvidia.Capability)
71-
for _, c := range nvidia.AllCaps() {
72-
str2cap[string(c)] = c
73-
}
74-
var nvidiaCaps []nvidia.Capability
75-
for _, c := range req.Capabilities {
76-
if cp, isNvidiaCap := str2cap[c]; isNvidiaCap {
77-
nvidiaCaps = append(nvidiaCaps, cp)
78-
}
79-
}
80-
if len(nvidiaCaps) != 0 {
81-
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidiaCaps...))
82-
} else {
83-
// Add "utility", "compute" capability if unset.
84-
// Please see also: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#driver-capabilities
85-
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidia.Utility, nvidia.Compute))
86-
}
87-
88-
if rootlessutil.IsRootless() {
89-
// "--no-cgroups" option is needed to nvidia-container-cli in rootless environment
90-
// Please see also: https://github.com/moby/moby/issues/38729#issuecomment-463493866
91-
gpuOpts = append(gpuOpts, nvidia.WithNoCgroups)
92-
}
93-
94-
return nvidia.WithGPUs(gpuOpts...), nil
95-
}
96-
9734
// ParseGPUOptCSV parses a GPU option from CSV.
9835
func ParseGPUOptCSV(value string) (*GPUReq, error) {
9936
csvReader := csv.NewReader(strings.NewReader(value))

pkg/cmd/container/run_linux.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/opencontainers/runtime-spec/specs-go"
2626

2727
containerd "github.com/containerd/containerd/v2/client"
28+
"github.com/containerd/containerd/v2/contrib/nvidia"
2829
"github.com/containerd/containerd/v2/core/containers"
2930
"github.com/containerd/containerd/v2/pkg/oci"
3031
"github.com/containerd/log"
@@ -260,3 +261,61 @@ func withOOMScoreAdj(score int) oci.SpecOpts {
260261
return nil
261262
}
262263
}
264+
265+
func parseGPUOpts(value []string) (res []oci.SpecOpts, _ error) {
266+
for _, gpu := range value {
267+
gpuOpt, err := parseGPUOpt(gpu)
268+
if err != nil {
269+
return nil, err
270+
}
271+
res = append(res, gpuOpt)
272+
}
273+
return res, nil
274+
}
275+
276+
func parseGPUOpt(value string) (oci.SpecOpts, error) {
277+
req, err := ParseGPUOptCSV(value)
278+
if err != nil {
279+
return nil, err
280+
}
281+
282+
var gpuOpts []nvidia.Opts
283+
284+
if len(req.DeviceIDs) > 0 {
285+
gpuOpts = append(gpuOpts, nvidia.WithDeviceUUIDs(req.DeviceIDs...))
286+
} else if req.Count > 0 {
287+
var devices []int
288+
for i := 0; i < req.Count; i++ {
289+
devices = append(devices, i)
290+
}
291+
gpuOpts = append(gpuOpts, nvidia.WithDevices(devices...))
292+
} else if req.Count < 0 {
293+
gpuOpts = append(gpuOpts, nvidia.WithAllDevices)
294+
}
295+
296+
str2cap := make(map[string]nvidia.Capability)
297+
for _, c := range nvidia.AllCaps() {
298+
str2cap[string(c)] = c
299+
}
300+
var nvidiaCaps []nvidia.Capability
301+
for _, c := range req.Capabilities {
302+
if cp, isNvidiaCap := str2cap[c]; isNvidiaCap {
303+
nvidiaCaps = append(nvidiaCaps, cp)
304+
}
305+
}
306+
if len(nvidiaCaps) != 0 {
307+
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidiaCaps...))
308+
} else {
309+
// Add "utility", "compute" capability if unset.
310+
// Please see also: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#driver-capabilities
311+
gpuOpts = append(gpuOpts, nvidia.WithCapabilities(nvidia.Utility, nvidia.Compute))
312+
}
313+
314+
if rootlessutil.IsRootless() {
315+
// "--no-cgroups" option is needed to nvidia-container-cli in rootless environment
316+
// Please see also: https://github.com/moby/moby/issues/38729#issuecomment-463493866
317+
gpuOpts = append(gpuOpts, nvidia.WithNoCgroups)
318+
}
319+
320+
return nvidia.WithGPUs(gpuOpts...), nil
321+
}

0 commit comments

Comments
 (0)