From 8ded1b354664ea17629782f0e78a14fc447e39bc Mon Sep 17 00:00:00 2001 From: Adyanth Hosavalike Date: Fri, 4 Nov 2022 05:23:01 +0000 Subject: [PATCH] Fix bug where incorrect response is returned When server is unreachable and docker checkpoint (or any command that needs to check the server type) is run, incorrect error was returned. When checking if the daemon had the right OS, we compared the OSType from the clients ServerInfo(). In situations where the client cannot connect to the daemon, a "stub" Info is used for this, in which we assume the daemon has experimental enabled, and is running the latest API version. However, we cannot fill in the correct OSType, so this field is empty in this situation. This patch only compares the OSType if the field is non-empty, otherwise assumes the platform matches. before this: docker -H unix:///no/such/socket.sock checkpoint create test test docker checkpoint create is only supported on a Docker daemon running on linux, but the Docker daemon is running on with this patch: docker -H unix:///no/such/socket.sock checkpoint create test test Cannot connect to the Docker daemon at unix:///no/such/socket.sock. Is the docker daemon running? Co-authored-by: Adyanth Hosavalike Signed-off-by: Sebastiaan van Stijn --- cmd/docker/builder.go | 2 +- cmd/docker/docker.go | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/docker/builder.go b/cmd/docker/builder.go index 7d463e4a767d..1353694c3284 100644 --- a/cmd/docker/builder.go +++ b/cmd/docker/builder.go @@ -86,7 +86,7 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st if useLegacy { // display warning if not wcow and continue - if dockerCli.ServerInfo().OSType != "windows" { + if dockerCli.ServerInfo().OSType != "" && dockerCli.ServerInfo().OSType != "windows" { _, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, nil)) } return args, osargs, nil, nil diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 0105c7fb5850..c06773195bde 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -306,7 +306,7 @@ func hideSubcommandIf(subcmd *cobra.Command, condition func(string) bool, annota func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) error { var ( notExperimental = func(_ string) bool { return !details.ServerInfo().HasExperimental } - notOSType = func(v string) bool { return v != details.ServerInfo().OSType } + notOSType = func(v string) bool { return details.ServerInfo().OSType != "" && v != details.ServerInfo().OSType } notSwarmStatus = func(v string) bool { s := details.ServerInfo().SwarmStatus if s == nil { @@ -375,7 +375,7 @@ func findCommand(cmd *cobra.Command, commands []string) bool { return findCommand(cmd.Parent(), commands) } -func isSupported(cmd *cobra.Command, details versionDetails) error { +func isSupported(cmd *cobra.Command, details command.Cli) error { if err := areSubcommandsSupported(cmd, details); err != nil { return err } @@ -419,10 +419,11 @@ func areSubcommandsSupported(cmd *cobra.Command, details versionDetails) error { if cmdVersion, ok := curr.Annotations["version"]; ok && versions.LessThan(details.CurrentVersion(), cmdVersion) { return fmt.Errorf("%s requires API version %s, but the Docker daemon API version is %s", cmd.CommandPath(), cmdVersion, details.CurrentVersion()) } - if ost, ok := curr.Annotations["ostype"]; ok && ost != details.ServerInfo().OSType { - 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) + si := details.ServerInfo() + if ost, ok := curr.Annotations["ostype"]; ok && si.OSType != "" && ost != si.OSType { + 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) } - if _, ok := curr.Annotations["experimental"]; ok && !details.ServerInfo().HasExperimental { + if _, ok := curr.Annotations["experimental"]; ok && !si.HasExperimental { return fmt.Errorf("%s is only supported on a Docker daemon with experimental features enabled", cmd.CommandPath()) } }