From 56e64270f39a17e12abde344e55ab123675b6af5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 5 Oct 2022 16:28:35 +0200 Subject: [PATCH] daemon: use strconv instead of fmt.Sprintf() Also cleaning up some errors Signed-off-by: Sebastiaan van Stijn --- daemon/config/config.go | 24 ++++++++++++------------ daemon/keys.go | 6 +++--- daemon/kill.go | 8 ++++---- daemon/links/links_test.go | 3 ++- daemon/reload_unix.go | 6 +++--- daemon/resize.go | 9 +++++---- daemon/runtime_unix.go | 3 +-- 7 files changed, 30 insertions(+), 29 deletions(-) diff --git a/daemon/config/config.go b/daemon/config/config.go index cf2f0f648e..a47515d161 100644 --- a/daemon/config/config.go +++ b/daemon/config/config.go @@ -224,7 +224,7 @@ type CommonConfig struct { DNSConfig LogConfig - BridgeConfig // bridgeConfig holds bridge network specific configuration. + BridgeConfig // BridgeConfig holds bridge network specific configuration. NetworkConfig registry.ServiceOptions @@ -317,7 +317,7 @@ func GetConflictFreeLabels(labels []string) ([]string, error) { if len(stringSlice) > 1 { // If there is a conflict we will return an error if v, ok := labelMap[stringSlice[0]]; ok && v != stringSlice[1] { - return nil, fmt.Errorf("conflict labels for %s=%s and %s=%s", stringSlice[0], stringSlice[1], stringSlice[0], v) + return nil, errors.Errorf("conflict labels for %s=%s and %s=%s", stringSlice[0], stringSlice[1], stringSlice[0], v) } labelMap[stringSlice[0]] = stringSlice[1] } @@ -325,7 +325,7 @@ func GetConflictFreeLabels(labels []string) ([]string, error) { newLabels := []string{} for k, v := range labelMap { - newLabels = append(newLabels, fmt.Sprintf("%s=%s", k, v)) + newLabels = append(newLabels, k+"="+v) } return newLabels, nil } @@ -521,7 +521,7 @@ func findConfigurationConflicts(config map[string]interface{}, flags *pflag.Flag for key := range unknownKeys { unknown = append(unknown, key) } - return fmt.Errorf("the following directives don't match any configuration option: %s", strings.Join(unknown, ", ")) + return errors.Errorf("the following directives don't match any configuration option: %s", strings.Join(unknown, ", ")) } var conflicts []string @@ -555,7 +555,7 @@ func findConfigurationConflicts(config map[string]interface{}, flags *pflag.Flag flags.Visit(duplicatedConflicts) if len(conflicts) > 0 { - return fmt.Errorf("the following directives are specified both as a flag and in the configuration file: %s", strings.Join(conflicts, ", ")) + return errors.Errorf("the following directives are specified both as a flag and in the configuration file: %s", strings.Join(conflicts, ", ")) } return nil } @@ -572,7 +572,7 @@ func Validate(config *Config) error { // validate log-level if config.LogLevel != "" { if _, err := logrus.ParseLevel(config.LogLevel); err != nil { - return fmt.Errorf("invalid logging level: %s", config.LogLevel) + return errors.Errorf("invalid logging level: %s", config.LogLevel) } } @@ -599,22 +599,22 @@ func Validate(config *Config) error { // TODO(thaJeztah) Validations below should not accept "0" to be valid; see Validate() for a more in-depth description of this problem if config.Mtu < 0 { - return fmt.Errorf("invalid default MTU: %d", config.Mtu) + return errors.Errorf("invalid default MTU: %d", config.Mtu) } if config.MaxConcurrentDownloads < 0 { - return fmt.Errorf("invalid max concurrent downloads: %d", config.MaxConcurrentDownloads) + return errors.Errorf("invalid max concurrent downloads: %d", config.MaxConcurrentDownloads) } if config.MaxConcurrentUploads < 0 { - return fmt.Errorf("invalid max concurrent uploads: %d", config.MaxConcurrentUploads) + return errors.Errorf("invalid max concurrent uploads: %d", config.MaxConcurrentUploads) } if config.MaxDownloadAttempts < 0 { - return fmt.Errorf("invalid max download attempts: %d", config.MaxDownloadAttempts) + return errors.Errorf("invalid max download attempts: %d", config.MaxDownloadAttempts) } // validate that "default" runtime is not reset if runtimes := config.GetAllRuntimes(); len(runtimes) > 0 { if _, ok := runtimes[StockRuntimeName]; ok { - return fmt.Errorf("runtime name '%s' is reserved", StockRuntimeName) + return errors.Errorf("runtime name '%s' is reserved", StockRuntimeName) } } @@ -626,7 +626,7 @@ func Validate(config *Config) error { if !builtinRuntimes[defaultRuntime] { runtimes := config.GetAllRuntimes() if _, ok := runtimes[defaultRuntime]; !ok && !IsPermissibleC8dRuntimeName(defaultRuntime) { - return fmt.Errorf("specified default runtime '%s' does not exist", defaultRuntime) + return errors.Errorf("specified default runtime '%s' does not exist", defaultRuntime) } } } diff --git a/daemon/keys.go b/daemon/keys.go index 3a494fef22..e736946355 100644 --- a/daemon/keys.go +++ b/daemon/keys.go @@ -4,7 +4,6 @@ package daemon // import "github.com/docker/docker/daemon" import ( - "fmt" "os" "strconv" "strings" @@ -38,7 +37,8 @@ func setRootKeyLimit(limit int) error { return err } defer keys.Close() - if _, err := fmt.Fprintf(keys, "%d", limit); err != nil { + _, err = keys.WriteString(strconv.Itoa(limit)) + if err != nil { return err } bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0) @@ -46,7 +46,7 @@ func setRootKeyLimit(limit int) error { return err } defer bytes.Close() - _, err = fmt.Fprintf(bytes, "%d", limit*rootKeyByteMultiplier) + _, err = bytes.WriteString(strconv.Itoa(limit * rootKeyByteMultiplier)) return err } diff --git a/daemon/kill.go b/daemon/kill.go index 953249c627..6020501844 100644 --- a/daemon/kill.go +++ b/daemon/kill.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "runtime" + "strconv" "syscall" "time" @@ -125,10 +126,9 @@ func (daemon *Daemon) killWithSignal(container *containerpkg.Container, stopSign } } - attributes := map[string]string{ - "signal": fmt.Sprintf("%d", stopSignal), - } - daemon.LogContainerEventWithAttributes(container, "kill", attributes) + daemon.LogContainerEventWithAttributes(container, "kill", map[string]string{ + "signal": strconv.Itoa(int(stopSignal)), + }) return nil } diff --git a/daemon/links/links_test.go b/daemon/links/links_test.go index e1b36dbbd9..2d624759fd 100644 --- a/daemon/links/links_test.go +++ b/daemon/links/links_test.go @@ -2,6 +2,7 @@ package links // import "github.com/docker/docker/daemon/links" import ( "fmt" + "strconv" "strings" "testing" @@ -200,7 +201,7 @@ func TestLinkPortRangeEnv(t *testing.T) { if env[tcpaddr] != "172.0.17.2" { t.Fatalf("Expected env %s = 172.0.17.2, got %s", tcpaddr, env[tcpaddr]) } - if env[tcpport] != fmt.Sprintf("%d", i) { + if env[tcpport] != strconv.Itoa(i) { t.Fatalf("Expected env %s = %d, got %s", tcpport, i, env[tcpport]) } if env[tcpproto] != "tcp" { diff --git a/daemon/reload_unix.go b/daemon/reload_unix.go index 590267c484..4adde34643 100644 --- a/daemon/reload_unix.go +++ b/daemon/reload_unix.go @@ -5,7 +5,7 @@ package daemon // import "github.com/docker/docker/daemon" import ( "bytes" - "fmt" + "strconv" "github.com/docker/docker/api/types" "github.com/docker/docker/daemon/config" @@ -49,12 +49,12 @@ func (daemon *Daemon) reloadPlatform(conf *config.Config, attributes map[string] if runtimeList.Len() > 0 { runtimeList.WriteRune(' ') } - runtimeList.WriteString(fmt.Sprintf("%s:%s", name, rt.Path)) + runtimeList.WriteString(name + ":" + rt.Path) } attributes["runtimes"] = runtimeList.String() attributes["default-runtime"] = daemon.configStore.DefaultRuntime - attributes["default-shm-size"] = fmt.Sprintf("%d", daemon.configStore.ShmSize) + attributes["default-shm-size"] = strconv.FormatInt(int64(daemon.configStore.ShmSize), 10) attributes["default-ipc-mode"] = daemon.configStore.IpcMode attributes["default-cgroupns-mode"] = daemon.configStore.CgroupNamespaceMode diff --git a/daemon/resize.go b/daemon/resize.go index 2fd427ae9e..d1325e6297 100644 --- a/daemon/resize.go +++ b/daemon/resize.go @@ -2,7 +2,8 @@ package daemon // import "github.com/docker/docker/daemon" import ( "context" - "fmt" + "errors" + "strconv" "time" ) @@ -23,8 +24,8 @@ func (daemon *Daemon) ContainerResize(name string, height, width int) error { if err = tsk.Resize(context.Background(), uint32(width), uint32(height)); err == nil { attributes := map[string]string{ - "height": fmt.Sprintf("%d", height), - "width": fmt.Sprintf("%d", width), + "height": strconv.Itoa(height), + "width": strconv.Itoa(width), } daemon.LogContainerEventWithAttributes(container, "resize", attributes) } @@ -49,6 +50,6 @@ func (daemon *Daemon) ContainerExecResize(name string, height, width int) error case <-ec.Started: return ec.Process.Resize(context.Background(), uint32(width), uint32(height)) case <-timeout.C: - return fmt.Errorf("timeout waiting for exec session ready") + return errors.New("timeout waiting for exec session ready") } } diff --git a/daemon/runtime_unix.go b/daemon/runtime_unix.go index df16aa14d7..52e976f75f 100644 --- a/daemon/runtime_unix.go +++ b/daemon/runtime_unix.go @@ -14,7 +14,6 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/daemon/config" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/ioutils" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -56,7 +55,7 @@ func (daemon *Daemon) initRuntimes(runtimes map[string]types.Runtime) (err error runtimeDir := filepath.Join(daemon.configStore.Root, "runtimes") // Remove old temp directory if any os.RemoveAll(runtimeDir + "-old") - tmpDir, err := ioutils.TempDir(daemon.configStore.Root, "gen-runtimes") + tmpDir, err := os.MkdirTemp(daemon.configStore.Root, "gen-runtimes") if err != nil { return errors.Wrap(err, "failed to get temp dir to generate runtime scripts") }