Skip to content

daemon/links: assorted bug fixes and cleanup#49300

Merged
thaJeztah merged 8 commits intomoby:masterfrom
thaJeztah:links_fixes
Jan 20, 2025
Merged

daemon/links: assorted bug fixes and cleanup#49300
thaJeztah merged 8 commits intomoby:masterfrom
thaJeztah:links_fixes

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

daemon: Daemon.setupLinkedContainers: don't fetch linked containers if not used

This function was unconditionally trying to fetch linked container, even
if the container was not using the default bridge (the only network that
supports legacy links).

Also removing the intermediate variable, as daemon.children, through
daemon.linkindex.children already returns a variable with a copy of these
links.

daemon/links: NewLink: simplify map to string conversion

daemon/links: add EnvVars function

Encapsulate the "create link -> link.ToEnv" process.

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

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 SCP) port, but
only if they had the same port-number. This could result in range-detection
incorrectly combining TCP and UDP (or SCP) ports in the same range.

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.

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
PAS

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added status/2-code-review area/networking Networking area/daemon Core Engine kind/refactor PR's that refactor, or clean-up code labels Jan 19, 2025
@thaJeztah thaJeztah requested a review from robmry January 19, 2025 14:56
@thaJeztah
Copy link
Copy Markdown
Member Author

Looks like I still had this in a local branch, and forgot to push after #49232 was merged 😂

Comment thread daemon/links/links.go
Comment on lines 31 to 32
// NewLink initializes a new Link struct with the provided options.
func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.Port]struct{}) *Link {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might open a PR to deprecate and/or move these internal (NewLink and Link) because nothing is using these, and they're only there to assist the "EnvVars" function now

@thaJeztah thaJeztah self-assigned this Jan 20, 2025
Copy link
Copy Markdown
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread daemon/links/links_test.go Outdated
"6379/tcp",
"6376/udp",
"6380/tcp",
"6376/scp",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it makes much difference - but these should probably be sctp?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, good catch; yes, they should've been.

Let me change those to prevent confusing ourselves!

Comment thread daemon/links/links.go Outdated
} else {
i++

// These env-vars are produces for every port, regardless if they're
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sub-nit - produces -> produced.

@thaJeztah thaJeztah added this to the 28.0.0 milestone Jan 20, 2025
…f not used

This function was unconditionally trying to fetch linked container, even
if the container was not using the default bridge (the only network that
supports legacy links).

Also removing the intermediate variable, as daemon.children, through
daemon.linkindex.children already returns a variable with a copy of these
links.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Encapsulate the "create link -> link.ToEnv" process.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
    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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
@thaJeztah thaJeztah merged commit cb4cc87 into moby:master Jan 20, 2025
@thaJeztah thaJeztah deleted the links_fixes branch January 20, 2025 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/daemon Core Engine area/networking Networking kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants