Skip to content

Commit 006c946

Browse files
committed
cmd/docker: make feature detection lazy again
Commit 20ba591 fixed incorrect feature detection in the CLI, but introduced a regression; previously the "ping" would only be executed if needed (see b397391), but by not inlining the call to `ServerInfo()` would now always be called. This patch inlines the code again to only execute the "ping" conditionally, which allows it to be executed lazily (and omitted for commands that don't require a daemon connection). Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 8fc1444 commit 006c946

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

cmd/docker/docker.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,21 @@ func areFlagsSupported(cmd *cobra.Command, details versionDetails) error {
416416
func areSubcommandsSupported(cmd *cobra.Command, details versionDetails) error {
417417
// Check recursively so that, e.g., `docker stack ls` returns the same output as `docker stack`
418418
for curr := cmd; curr != nil; curr = curr.Parent() {
419+
// Important: in the code below, calls to "details.CurrentVersion()" and
420+
// "details.ServerInfo()" are deliberately executed inline to make them
421+
// be executed "lazily". This is to prevent making a connection with the
422+
// daemon to perform a "ping" (even for commands that do not require a
423+
// daemon connection).
424+
//
425+
// See commit b39739123b845f872549e91be184cc583f5b387c for details.
426+
419427
if cmdVersion, ok := curr.Annotations["version"]; ok && versions.LessThan(details.CurrentVersion(), cmdVersion) {
420428
return fmt.Errorf("%s requires API version %s, but the Docker daemon API version is %s", cmd.CommandPath(), cmdVersion, details.CurrentVersion())
421429
}
422-
si := details.ServerInfo()
423-
if ost, ok := curr.Annotations["ostype"]; ok && si.OSType != "" && ost != si.OSType {
424-
return fmt.Errorf("%s is only supported on a Docker daemon running on %s, but the Docker daemon is running on %s", cmd.CommandPath(), ost, si.OSType)
430+
if ost, ok := curr.Annotations["ostype"]; ok && details.ServerInfo().OSType != "" && ost != details.ServerInfo().OSType {
431+
return fmt.Errorf("%s is only supported on a Docker daemon running on %s, but the Docker daemon is running on %s", cmd.CommandPath(), ost, details.ServerInfo().OSType)
425432
}
426-
if _, ok := curr.Annotations["experimental"]; ok && !si.HasExperimental {
433+
if _, ok := curr.Annotations["experimental"]; ok && !details.ServerInfo().HasExperimental {
427434
return fmt.Errorf("%s is only supported on a Docker daemon with experimental features enabled", cmd.CommandPath())
428435
}
429436
}

0 commit comments

Comments
 (0)