27 Commits

Author SHA1 Message Date
Paweł Gronowski
c9b0a21bb1 modernize: Use b.Loop (introduced in Go 1.24)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-12-15 18:56:33 +01:00
Austin Vazquez
c646091d57 api: move container port type to network package
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
2025-10-03 17:30:42 -05:00
Austin Vazquez
cb3abacc52 api/types/container: add network port and port range types
Co-authored-by: Sebastiaan van Stijn <github@gone.nl>
Co-authored-by: Cory Snider <csnider@mirantis.com>
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
2025-10-02 13:59:34 -05:00
Sebastiaan van Stijn
d58dc493fe replace direct uses of nat types for api/types/container aliases
Follow-up to 494677f93f, which added
the aliases, but did not yet replace our own use of the nat types.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-07-31 02:57:39 +02:00
Sebastiaan van Stijn
076e98e8f3 daemon/links, daemon/network: remove // import comments
These comments were added to enforce using the correct import path for
our packages ("github.com/docker/docker", not "github.com/moby/moby").
However, when working in go module mode (not GOPATH / vendor), they have
no effect, so their impact is limited.

Remove these imports in preparation of migrating our code to become an
actual go module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-30 15:59:12 +02:00
Sebastiaan van Stijn
76a496a482 daemon/links: fix duplicate env-vars and cleanup range-detection
Rewrite the range-detection logic to prevent duplicate env-vars,
and to avoid looping over the same values multiple times.

Benchmark before / after:

    goos: darwin
    goarch: arm64
    pkg: github.com/docker/docker/daemon/links
    cpu: Apple M1 Pro
    BenchmarkLinkMultipleEnv
    BenchmarkLinkMultipleEnvOld-10       92817     12072 ns/op    8516 B/op     316 allocs/op
    BenchmarkLinkMultipleEnvNew-10      149493      7792 ns/op    6435 B/op     213 allocs/op
    PASS

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:52:41 +01:00
Sebastiaan van Stijn
fcdd6f4ad2 daemon/links: fix port-ranges with mixed protocols
The code incorrectly created env-vars for consecutive port numbers with
a different protocol; we should only consider ports to be part of a range
if they have consecutive port-numbers and have the same protocol.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:51:17 +01:00
Sebastiaan van Stijn
251c68c647 daemon/links: fix port-sorting with mixed protocols
The intent of this sorting was twofold;

- the "default" port of the container to be the first TCP port (if present)
- consecutive port-mappings with the same protocol to be together so
  that port-ranges would produce an env-var describing the range.

The current sorting would sort TCP ports before UDP (or SCTP) port, but
only if they had the same port-number. This could result in range-detection
incorrectly combining TCP and UDP (or SCTP) ports in the same range.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:50:54 +01:00
Sebastiaan van Stijn
3d37d54b8c daemon/links: Link.ToEnv: simplify adding default port env-var
There's no need to loop and sort multiple times; this code picked
the first port after sorting, which we already did in this function.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:50:00 +01:00
Sebastiaan van Stijn
c508919b7c daemon/links: add BenchmarkLinkMultipleEnv
go test -v -bench ^\QBenchmarkLinkMultipleEnv\E$ -run ^$
    goos: darwin
    goarch: arm64
    pkg: github.com/docker/docker/daemon/links
    cpu: Apple M1 Pro
    BenchmarkLinkMultipleEnv
    BenchmarkLinkMultipleEnv-10    	   92817	     12072 ns/op	    8516 B/op	     316 allocs/op
    PASS

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:49:59 +01:00
Sebastiaan van Stijn
3b27e36d67 daemon/links: add EnvVars function
Encapsulate the "create link -> link.ToEnv" process.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:49:59 +01:00
Sebastiaan van Stijn
b54053d5da daemon/links: NewLink: simplify map to string conversion
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-20 11:49:59 +01:00
Sebastiaan van Stijn
34ab9570ad daemon/links: TestLinkMultipleEnv: assert with gotest.tools, remove TestLinkPortRangeEnv
Simplify the test by testing the result, instead of manually checking
specific values. This makes sure we check the actual results, and don't
miss values, or ignore unexpected values.

TestLinkPortRangeEnv was added in 611a23aa7f
to test for port-ranges to produce the expected env-vars, but used the
same input as TestLinkMultipleEnv. Now that we assert all env-vars produced,
it became a duplicate of TestLinkMultipleEnv, so we can remove that test.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-08 10:04:56 +01:00
Sebastiaan van Stijn
080d8e7d63 daemon/links: TestLinkEnv: assert with gotest.tools
Simplify the test by testing the result, instead of manually checking
specific values. This makes sure we check the actual results, and don't
miss values, or ignore unexpected values.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-08 09:59:17 +01:00
Sebastiaan van Stijn
702df89d9b daemon/links: TestLinkNaming: assert with gotest.tools
Simplify the test by testing the result, instead of manually checking
specific values. This makes sure we check the actual results, and don't
miss values, or ignore unexpected values.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-08 09:58:26 +01:00
Sebastiaan van Stijn
51eb0c6a24 daemon/links: TestLinkNew: assert with gotest.tools
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-08 09:58:22 +01:00
Sebastiaan van Stijn
382fb41563 daemon/links: remove newPortNoError utility
This utility was added in 12b6083c8f as a
replacement for nat.NewPort(), which before that patch would panic on
invalid values, but was changed to return an error.

Given that the utility ignores any error, and these values are fixed values
for the test, let's remove it to simplify constructing the tests.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-08 09:37:23 +01:00
Sebastiaan van Stijn
39f59c3289 daemon/links: use strings.Cut()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 11:09:01 +01:00
Sebastiaan van Stijn
56e64270f3 daemon: use strconv instead of fmt.Sprintf()
Also cleaning up some errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-08 17:41:39 +02:00
Eng Zer Jun
7873c27cfb all: replace strings.Replace with strings.ReplaceAll
strings.ReplaceAll(s, old, new) is a wrapper function for
strings.Replace(s, old, new, -1). But strings.ReplaceAll is more
readable and removes the hardcoded -1.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-05-09 19:45:40 +08:00
Sebastiaan van Stijn
f4f56b1197 daemon: normalize comment formatting
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-11-27 15:43:53 +01:00
jliao
7427fe12d8 fix typo
fix typo

Signed-off-by: jian liao <jliao@alauda.io>
2018-09-08 08:13:30 +08:00
Daniel Nephin
4f0d95fa6e Add canonical import comment
Signed-off-by: Daniel Nephin <dnephin@docker.com>
2018-02-05 16:51:57 -05:00
fate-grand-order
2a8d6368d4 use t.Fatal() to output the err message where the values used for formatting
text does not appear to contain a placeholder

Signed-off-by: Helen Xie <chenjg@harmonycloud.cn>
2017-02-23 10:16:22 +08:00
lixiaobing10051267
cf4f5b4771 check typos and fix in daemon directory
Signed-off-by: lixiaobing10051267 <li.xiaobing1@zte.com.cn>

Signed-off-by: lixiaobing10051267 <li.xiaobing1@zte.com.cn>
2016-11-26 17:23:21 +08:00
David Calavera
056e744903 Replace usage of pkg/nat with go-connections/nat.
Signed-off-by: David Calavera <david.calavera@gmail.com>
2015-12-22 13:31:46 -05:00
Antonio Murdaca
0e0c7e521c Cleanup links top level pkg
- Move top level pkg links under daemon
- Refactor code accordingly
- golint pkg

Signed-off-by: Antonio Murdaca <runcom@linux.com>
2015-07-29 23:39:18 +02:00