Ver Fonte

daemon: use strconv instead of fmt.Sprintf()

Also cleaning up some errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn há 2 anos atrás
pai
commit
56e64270f3

+ 12 - 12
daemon/config/config.go

@@ -224,7 +224,7 @@ type CommonConfig struct {
 
 
 	DNSConfig
 	DNSConfig
 	LogConfig
 	LogConfig
-	BridgeConfig // bridgeConfig holds bridge network specific configuration.
+	BridgeConfig // BridgeConfig holds bridge network specific configuration.
 	NetworkConfig
 	NetworkConfig
 	registry.ServiceOptions
 	registry.ServiceOptions
 
 
@@ -317,7 +317,7 @@ func GetConflictFreeLabels(labels []string) ([]string, error) {
 		if len(stringSlice) > 1 {
 		if len(stringSlice) > 1 {
 			// If there is a conflict we will return an error
 			// If there is a conflict we will return an error
 			if v, ok := labelMap[stringSlice[0]]; ok && v != stringSlice[1] {
 			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]
 			labelMap[stringSlice[0]] = stringSlice[1]
 		}
 		}
@@ -325,7 +325,7 @@ func GetConflictFreeLabels(labels []string) ([]string, error) {
 
 
 	newLabels := []string{}
 	newLabels := []string{}
 	for k, v := range labelMap {
 	for k, v := range labelMap {
-		newLabels = append(newLabels, fmt.Sprintf("%s=%s", k, v))
+		newLabels = append(newLabels, k+"="+v)
 	}
 	}
 	return newLabels, nil
 	return newLabels, nil
 }
 }
@@ -521,7 +521,7 @@ func findConfigurationConflicts(config map[string]interface{}, flags *pflag.Flag
 		for key := range unknownKeys {
 		for key := range unknownKeys {
 			unknown = append(unknown, key)
 			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
 	var conflicts []string
@@ -555,7 +555,7 @@ func findConfigurationConflicts(config map[string]interface{}, flags *pflag.Flag
 	flags.Visit(duplicatedConflicts)
 	flags.Visit(duplicatedConflicts)
 
 
 	if len(conflicts) > 0 {
 	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
 	return nil
 }
 }
@@ -572,7 +572,7 @@ func Validate(config *Config) error {
 	// validate log-level
 	// validate log-level
 	if config.LogLevel != "" {
 	if config.LogLevel != "" {
 		if _, err := logrus.ParseLevel(config.LogLevel); err != nil {
 		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
 	// 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 {
 	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 {
 	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 {
 	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 {
 	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
 	// validate that "default" runtime is not reset
 	if runtimes := config.GetAllRuntimes(); len(runtimes) > 0 {
 	if runtimes := config.GetAllRuntimes(); len(runtimes) > 0 {
 		if _, ok := runtimes[StockRuntimeName]; ok {
 		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] {
 		if !builtinRuntimes[defaultRuntime] {
 			runtimes := config.GetAllRuntimes()
 			runtimes := config.GetAllRuntimes()
 			if _, ok := runtimes[defaultRuntime]; !ok && !IsPermissibleC8dRuntimeName(defaultRuntime) {
 			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)
 			}
 			}
 		}
 		}
 	}
 	}

+ 3 - 3
daemon/keys.go

@@ -4,7 +4,6 @@
 package daemon // import "github.com/docker/docker/daemon"
 package daemon // import "github.com/docker/docker/daemon"
 
 
 import (
 import (
-	"fmt"
 	"os"
 	"os"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
@@ -38,7 +37,8 @@ func setRootKeyLimit(limit int) error {
 		return err
 		return err
 	}
 	}
 	defer keys.Close()
 	defer keys.Close()
-	if _, err := fmt.Fprintf(keys, "%d", limit); err != nil {
+	_, err = keys.WriteString(strconv.Itoa(limit))
+	if err != nil {
 		return err
 		return err
 	}
 	}
 	bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0)
 	bytes, err := os.OpenFile(rootBytesFile, os.O_WRONLY, 0)
@@ -46,7 +46,7 @@ func setRootKeyLimit(limit int) error {
 		return err
 		return err
 	}
 	}
 	defer bytes.Close()
 	defer bytes.Close()
-	_, err = fmt.Fprintf(bytes, "%d", limit*rootKeyByteMultiplier)
+	_, err = bytes.WriteString(strconv.Itoa(limit * rootKeyByteMultiplier))
 	return err
 	return err
 }
 }
 
 

+ 4 - 4
daemon/kill.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"context"
 	"fmt"
 	"fmt"
 	"runtime"
 	"runtime"
+	"strconv"
 	"syscall"
 	"syscall"
 	"time"
 	"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
 	return nil
 }
 }
 
 

+ 2 - 1
daemon/links/links_test.go

@@ -2,6 +2,7 @@ package links // import "github.com/docker/docker/daemon/links"
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"strconv"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 
 
@@ -200,7 +201,7 @@ func TestLinkPortRangeEnv(t *testing.T) {
 		if env[tcpaddr] != "172.0.17.2" {
 		if env[tcpaddr] != "172.0.17.2" {
 			t.Fatalf("Expected env %s  = 172.0.17.2, got %s", tcpaddr, env[tcpaddr])
 			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])
 			t.Fatalf("Expected env %s  = %d, got %s", tcpport, i, env[tcpport])
 		}
 		}
 		if env[tcpproto] != "tcp" {
 		if env[tcpproto] != "tcp" {

+ 3 - 3
daemon/reload_unix.go

@@ -5,7 +5,7 @@ package daemon // import "github.com/docker/docker/daemon"
 
 
 import (
 import (
 	"bytes"
 	"bytes"
-	"fmt"
+	"strconv"
 
 
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/daemon/config"
 	"github.com/docker/docker/daemon/config"
@@ -49,12 +49,12 @@ func (daemon *Daemon) reloadPlatform(conf *config.Config, attributes map[string]
 		if runtimeList.Len() > 0 {
 		if runtimeList.Len() > 0 {
 			runtimeList.WriteRune(' ')
 			runtimeList.WriteRune(' ')
 		}
 		}
-		runtimeList.WriteString(fmt.Sprintf("%s:%s", name, rt.Path))
+		runtimeList.WriteString(name + ":" + rt.Path)
 	}
 	}
 
 
 	attributes["runtimes"] = runtimeList.String()
 	attributes["runtimes"] = runtimeList.String()
 	attributes["default-runtime"] = daemon.configStore.DefaultRuntime
 	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-ipc-mode"] = daemon.configStore.IpcMode
 	attributes["default-cgroupns-mode"] = daemon.configStore.CgroupNamespaceMode
 	attributes["default-cgroupns-mode"] = daemon.configStore.CgroupNamespaceMode
 
 

+ 5 - 4
daemon/resize.go

@@ -2,7 +2,8 @@ package daemon // import "github.com/docker/docker/daemon"
 
 
 import (
 import (
 	"context"
 	"context"
-	"fmt"
+	"errors"
+	"strconv"
 	"time"
 	"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 {
 	if err = tsk.Resize(context.Background(), uint32(width), uint32(height)); err == nil {
 		attributes := map[string]string{
 		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)
 		daemon.LogContainerEventWithAttributes(container, "resize", attributes)
 	}
 	}
@@ -49,6 +50,6 @@ func (daemon *Daemon) ContainerExecResize(name string, height, width int) error
 	case <-ec.Started:
 	case <-ec.Started:
 		return ec.Process.Resize(context.Background(), uint32(width), uint32(height))
 		return ec.Process.Resize(context.Background(), uint32(width), uint32(height))
 	case <-timeout.C:
 	case <-timeout.C:
-		return fmt.Errorf("timeout waiting for exec session ready")
+		return errors.New("timeout waiting for exec session ready")
 	}
 	}
 }
 }

+ 1 - 2
daemon/runtime_unix.go

@@ -14,7 +14,6 @@ import (
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/daemon/config"
 	"github.com/docker/docker/daemon/config"
 	"github.com/docker/docker/errdefs"
 	"github.com/docker/docker/errdefs"
-	"github.com/docker/docker/pkg/ioutils"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"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")
 	runtimeDir := filepath.Join(daemon.configStore.Root, "runtimes")
 	// Remove old temp directory if any
 	// Remove old temp directory if any
 	os.RemoveAll(runtimeDir + "-old")
 	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 {
 	if err != nil {
 		return errors.Wrap(err, "failed to get temp dir to generate runtime scripts")
 		return errors.Wrap(err, "failed to get temp dir to generate runtime scripts")
 	}
 	}