diff --git a/client/pkg/stringid/stringid.go b/client/pkg/stringid/stringid.go new file mode 100644 index 0000000000..030e070853 --- /dev/null +++ b/client/pkg/stringid/stringid.go @@ -0,0 +1,47 @@ +// Package stringid provides helper functions for dealing with string identifiers. +// +// It is similar to the package used by the daemon, but for presentational +// purposes in the client. +package stringid + +import ( + "crypto/rand" + "encoding/hex" + "strings" +) + +const ( + shortLen = 12 + fullLen = 64 +) + +// TruncateID returns a shorthand version of a string identifier for presentation. +// For convenience, it accepts both digests ("sha256:xxxx") and IDs without an +// algorithm prefix. It truncates the algorithm (if any) before truncating the +// ID. The length of the truncated ID is currently fixed, but users should make +// no assumptions of this to not change; it is merely a prefix of the ID that +// provides enough uniqueness for common scenarios. +// +// Truncated IDs ("ID-prefixes") usually can be used to uniquely identify an +// object (such as a container or network), but collisions may happen, in +// which case an "ambiguous result" error is produced. In case of a collision, +// the caller should try with a longer prefix or the full-length ID. +func TruncateID(id string) string { + if i := strings.IndexRune(id, ':'); i >= 0 { + id = id[i+1:] + } + if len(id) > shortLen { + id = id[:shortLen] + } + return id +} + +// GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9. +func GenerateRandomID() string { + b := make([]byte, 32) + if _, err := rand.Read(b); err != nil { + panic(err) // This shouldn't happen + } + id := hex.EncodeToString(b) + return id +} diff --git a/client/pkg/stringid/stringid_test.go b/client/pkg/stringid/stringid_test.go new file mode 100644 index 0000000000..cf79fc69c5 --- /dev/null +++ b/client/pkg/stringid/stringid_test.go @@ -0,0 +1,54 @@ +package stringid + +import "testing" + +func TestGenerateRandomID(t *testing.T) { + id := GenerateRandomID() + + if len(id) != fullLen { + t.Fatalf("Id returned is incorrect: %s", id) + } +} + +func TestTruncateID(t *testing.T) { + tests := []struct { + doc, id, expected string + }{ + { + doc: "empty ID", + id: "", + expected: "", + }, + { + // IDs are expected to be 12 (short) or 64 characters, and not be numeric only, + // but TruncateID should handle these gracefully. + doc: "invalid ID", + id: "1234", + expected: "1234", + }, + { + doc: "full ID", + id: "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2", + expected: "90435eec5c4e", + }, + { + doc: "digest", + id: "sha256:90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2", + expected: "90435eec5c4e", + }, + { + doc: "very long ID", + id: "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a290435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2", + expected: "90435eec5c4e", + }, + } + + for _, tc := range tests { + t.Run(tc.doc, func(t *testing.T) { + actual := TruncateID(tc.id) + if actual != tc.expected { + t.Errorf("expected: %q, got: %q", tc.expected, actual) + } + }) + } +} diff --git a/daemon/builder/backend/backend.go b/daemon/builder/backend/backend.go index 7e7ba550fc..b98ba3010a 100644 --- a/daemon/builder/backend/backend.go +++ b/daemon/builder/backend/backend.go @@ -10,7 +10,7 @@ import ( daemonevents "github.com/docker/docker/daemon/events" buildkit "github.com/docker/docker/daemon/internal/builder-next" "github.com/docker/docker/daemon/internal/image" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/moby/api/types/backend" "github.com/moby/moby/api/types/build" "github.com/moby/moby/api/types/events" diff --git a/daemon/builder/dockerfile/builder.go b/daemon/builder/dockerfile/builder.go index 20e612fa5e..48839edcc2 100644 --- a/daemon/builder/dockerfile/builder.go +++ b/daemon/builder/dockerfile/builder.go @@ -12,8 +12,8 @@ import ( "github.com/containerd/platforms" "github.com/docker/docker/daemon/builder" "github.com/docker/docker/daemon/builder/remotecontext" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/stringid" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/parser" "github.com/moby/buildkit/frontend/dockerfile/shell" diff --git a/daemon/builder/dockerfile/containerbackend.go b/daemon/builder/dockerfile/containerbackend.go index 529e990055..9499993acd 100644 --- a/daemon/builder/dockerfile/containerbackend.go +++ b/daemon/builder/dockerfile/containerbackend.go @@ -8,7 +8,7 @@ import ( cerrdefs "github.com/containerd/errdefs" "github.com/containerd/log" "github.com/docker/docker/daemon/builder" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/moby/api/types/backend" "github.com/moby/moby/api/types/container" "github.com/pkg/errors" diff --git a/daemon/builder/dockerfile/internals.go b/daemon/builder/dockerfile/internals.go index 8b2e67e576..d201c708eb 100644 --- a/daemon/builder/dockerfile/internals.go +++ b/daemon/builder/dockerfile/internals.go @@ -14,8 +14,8 @@ import ( "github.com/containerd/platforms" "github.com/docker/docker/daemon/builder" "github.com/docker/docker/daemon/internal/image" + "github.com/docker/docker/daemon/internal/stringid" networkSettings "github.com/docker/docker/daemon/network" - "github.com/docker/docker/pkg/stringid" "github.com/docker/go-connections/nat" "github.com/moby/go-archive" "github.com/moby/go-archive/chrootarchive" diff --git a/daemon/cluster/executor/container/validate_test.go b/daemon/cluster/executor/container/validate_test.go index 5333c95821..ef9b3d32b3 100644 --- a/daemon/cluster/executor/container/validate_test.go +++ b/daemon/cluster/executor/container/validate_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/docker/docker/daemon" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/swarmkit/v2/api" ) diff --git a/daemon/container/exec.go b/daemon/container/exec.go index 9075b5504d..fa499ebc06 100644 --- a/daemon/container/exec.go +++ b/daemon/container/exec.go @@ -9,7 +9,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/libcontainerd/types" "github.com/docker/docker/daemon/internal/stream" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" ) // ExecConfig holds the configurations for execs. The Daemon keeps diff --git a/daemon/container/view_test.go b/daemon/container/view_test.go index a9ff5848ee..7c03d3f104 100644 --- a/daemon/container/view_test.go +++ b/daemon/container/view_test.go @@ -8,7 +8,7 @@ import ( "testing" cerrdefs "github.com/containerd/errdefs" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/google/uuid" "github.com/moby/moby/api/types/container" "gotest.tools/v3/assert" diff --git a/daemon/container_operations.go b/daemon/container_operations.go index 4c83b3dda3..e1ca06e93d 100644 --- a/daemon/container_operations.go +++ b/daemon/container_operations.go @@ -20,6 +20,7 @@ import ( "github.com/docker/docker/daemon/internal/metrics" "github.com/docker/docker/daemon/internal/multierror" "github.com/docker/docker/daemon/internal/sliceutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork" "github.com/docker/docker/daemon/libnetwork/netlabel" "github.com/docker/docker/daemon/libnetwork/scope" @@ -27,7 +28,6 @@ import ( "github.com/docker/docker/daemon/network" "github.com/docker/docker/daemon/pkg/opts" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/runconfig" "github.com/docker/go-connections/nat" containertypes "github.com/moby/moby/api/types/container" diff --git a/daemon/container_operations_unix.go b/daemon/container_operations_unix.go index e413ebbb88..6eb313a858 100644 --- a/daemon/container_operations_unix.go +++ b/daemon/container_operations_unix.go @@ -14,13 +14,13 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/container" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork" "github.com/docker/docker/daemon/libnetwork/drivers/bridge" "github.com/docker/docker/daemon/links" "github.com/docker/docker/daemon/network" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/process" - "github.com/docker/docker/pkg/stringid" "github.com/moby/sys/mount" "github.com/moby/sys/user" "github.com/opencontainers/selinux/go-selinux/label" diff --git a/daemon/containerd/image_builder.go b/daemon/containerd/image_builder.go index 6d9c1c3683..5213f80ef2 100644 --- a/daemon/containerd/image_builder.go +++ b/daemon/containerd/image_builder.go @@ -24,10 +24,10 @@ import ( "github.com/docker/docker/daemon/builder" "github.com/docker/docker/daemon/internal/image" "github.com/docker/docker/daemon/internal/layer" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" - "github.com/docker/docker/pkg/stringid" imagespec "github.com/moby/docker-image-spec/specs-go/v1" "github.com/moby/go-archive" "github.com/moby/moby/api/types/backend" diff --git a/daemon/containerd/image_changes.go b/daemon/containerd/image_changes.go index 2d2eb731eb..6b9235e42d 100644 --- a/daemon/containerd/image_changes.go +++ b/daemon/containerd/image_changes.go @@ -7,7 +7,7 @@ import ( "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/log" "github.com/docker/docker/daemon/container" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/go-archive" ) diff --git a/daemon/containerd/image_delete.go b/daemon/containerd/image_delete.go index 2af35289d8..acd6ba5f66 100644 --- a/daemon/containerd/image_delete.go +++ b/daemon/containerd/image_delete.go @@ -18,7 +18,7 @@ import ( dimages "github.com/docker/docker/daemon/images" "github.com/docker/docker/daemon/internal/image" "github.com/docker/docker/daemon/internal/metrics" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/moby/api/types/events" imagetypes "github.com/moby/moby/api/types/image" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/daemon/containerd/image_pull.go b/daemon/containerd/image_pull.go index 2ff822b23c..f9988b69fa 100644 --- a/daemon/containerd/image_pull.go +++ b/daemon/containerd/image_pull.go @@ -18,10 +18,10 @@ import ( "github.com/distribution/reference" "github.com/docker/docker/daemon/internal/distribution" "github.com/docker/docker/daemon/internal/metrics" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types/events" registrytypes "github.com/moby/moby/api/types/registry" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/daemon/containerd/progress.go b/daemon/containerd/progress.go index f67c921019..35639ddc59 100644 --- a/daemon/containerd/progress.go +++ b/daemon/containerd/progress.go @@ -17,9 +17,9 @@ import ( cerrdefs "github.com/containerd/errdefs" "github.com/containerd/log" "github.com/distribution/reference" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/progress" - "github.com/docker/docker/pkg/stringid" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/daemon/graphdriver/graphtest/graphbench_unix.go b/daemon/graphdriver/graphtest/graphbench_unix.go index 393a6e8eb0..8239304113 100644 --- a/daemon/graphdriver/graphtest/graphbench_unix.go +++ b/daemon/graphdriver/graphtest/graphbench_unix.go @@ -8,7 +8,7 @@ import ( "path/filepath" "testing" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "gotest.tools/v3/assert" ) diff --git a/daemon/graphdriver/graphtest/graphtest_unix.go b/daemon/graphdriver/graphtest/graphtest_unix.go index 2c7970f178..0224fc5010 100644 --- a/daemon/graphdriver/graphtest/graphtest_unix.go +++ b/daemon/graphdriver/graphtest/graphtest_unix.go @@ -12,7 +12,7 @@ import ( "github.com/docker/docker/daemon/graphdriver" "github.com/docker/docker/daemon/internal/quota" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/go-units" "golang.org/x/sys/unix" "gotest.tools/v3/assert" diff --git a/daemon/graphdriver/graphtest/testutil.go b/daemon/graphdriver/graphtest/testutil.go index 51fbf60ffe..af741da057 100644 --- a/daemon/graphdriver/graphtest/testutil.go +++ b/daemon/graphdriver/graphtest/testutil.go @@ -10,7 +10,7 @@ import ( "sort" "github.com/docker/docker/daemon/graphdriver" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/go-archive" ) diff --git a/daemon/images/image_builder.go b/daemon/images/image_builder.go index 8508f55cdf..bf04ab0954 100644 --- a/daemon/images/image_builder.go +++ b/daemon/images/image_builder.go @@ -12,9 +12,9 @@ import ( "github.com/docker/docker/daemon/builder" "github.com/docker/docker/daemon/internal/image" "github.com/docker/docker/daemon/internal/layer" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types/backend" "github.com/moby/moby/api/types/registry" "github.com/opencontainers/go-digest" diff --git a/daemon/images/image_delete.go b/daemon/images/image_delete.go index d007ecf3ef..b1bda1704d 100644 --- a/daemon/images/image_delete.go +++ b/daemon/images/image_delete.go @@ -10,8 +10,8 @@ import ( "github.com/docker/docker/daemon/container" "github.com/docker/docker/daemon/internal/image" "github.com/docker/docker/daemon/internal/metrics" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types/backend" "github.com/moby/moby/api/types/events" imagetypes "github.com/moby/moby/api/types/image" diff --git a/daemon/internal/builder-next/executor_linux.go b/daemon/internal/builder-next/executor_linux.go index 14b786bfbe..142e8ea31a 100644 --- a/daemon/internal/builder-next/executor_linux.go +++ b/daemon/internal/builder-next/executor_linux.go @@ -7,8 +7,8 @@ import ( "strconv" "github.com/containerd/log" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork" - "github.com/docker/docker/pkg/stringid" "github.com/moby/buildkit/executor" "github.com/moby/buildkit/executor/oci" "github.com/moby/buildkit/executor/resources" diff --git a/daemon/internal/distribution/pull_v2.go b/daemon/internal/distribution/pull_v2.go index 9b0283a43f..e288fbc663 100644 --- a/daemon/internal/distribution/pull_v2.go +++ b/daemon/internal/distribution/pull_v2.go @@ -22,10 +22,10 @@ import ( "github.com/docker/docker/daemon/internal/image" "github.com/docker/docker/daemon/internal/layer" refstore "github.com/docker/docker/daemon/internal/refstore" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/pkg/registry" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/progress" - "github.com/docker/docker/pkg/stringid" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" diff --git a/daemon/internal/distribution/push_v2.go b/daemon/internal/distribution/push_v2.go index 65103b34b4..770ef9a483 100644 --- a/daemon/internal/distribution/push_v2.go +++ b/daemon/internal/distribution/push_v2.go @@ -17,10 +17,10 @@ import ( "github.com/docker/docker/daemon/internal/distribution/metadata" "github.com/docker/docker/daemon/internal/distribution/xfer" "github.com/docker/docker/daemon/internal/layer" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/pkg/registry" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/progress" - "github.com/docker/docker/pkg/stringid" apitypes "github.com/moby/moby/api/types" "github.com/opencontainers/go-digest" "github.com/pkg/errors" diff --git a/daemon/internal/image/tarexport/load.go b/daemon/internal/image/tarexport/load.go index b152d52874..f6fd840971 100644 --- a/daemon/internal/image/tarexport/load.go +++ b/daemon/internal/image/tarexport/load.go @@ -18,9 +18,9 @@ import ( "github.com/docker/docker/daemon/internal/image" "github.com/docker/docker/daemon/internal/ioutils" "github.com/docker/docker/daemon/internal/layer" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" - "github.com/docker/docker/pkg/stringid" "github.com/moby/go-archive/chrootarchive" "github.com/moby/go-archive/compression" "github.com/moby/moby/api/types/events" diff --git a/daemon/internal/layer/filestore_test.go b/daemon/internal/layer/filestore_test.go index 17a471e9ad..5755dd6223 100644 --- a/daemon/internal/layer/filestore_test.go +++ b/daemon/internal/layer/filestore_test.go @@ -11,7 +11,7 @@ import ( "syscall" "testing" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/opencontainers/go-digest" ) diff --git a/daemon/internal/layer/layer_store.go b/daemon/internal/layer/layer_store.go index 45ff7db34f..3b80b81f3a 100644 --- a/daemon/internal/layer/layer_store.go +++ b/daemon/internal/layer/layer_store.go @@ -12,7 +12,7 @@ import ( "github.com/containerd/log" "github.com/docker/distribution" "github.com/docker/docker/daemon/graphdriver" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/locker" "github.com/moby/sys/user" "github.com/opencontainers/go-digest" diff --git a/daemon/internal/layer/layer_test.go b/daemon/internal/layer/layer_test.go index 64ce84adfc..0bf4bff2bb 100644 --- a/daemon/internal/layer/layer_test.go +++ b/daemon/internal/layer/layer_test.go @@ -13,7 +13,7 @@ import ( "github.com/containerd/continuity/driver" "github.com/docker/docker/daemon/graphdriver" "github.com/docker/docker/daemon/graphdriver/vfs" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/moby/go-archive" "github.com/moby/sys/user" "github.com/opencontainers/go-digest" diff --git a/daemon/internal/layer/layer_unix.go b/daemon/internal/layer/layer_unix.go index 6dcad2d7cd..9d776b7f46 100644 --- a/daemon/internal/layer/layer_unix.go +++ b/daemon/internal/layer/layer_unix.go @@ -2,7 +2,7 @@ package layer -import "github.com/docker/docker/pkg/stringid" +import "github.com/docker/docker/daemon/internal/stringid" func (ls *layerStore) mountID(name string) string { return stringid.GenerateRandomID() diff --git a/daemon/internal/layer/migration_test.go b/daemon/internal/layer/migration_test.go index f20b219123..19bf34cbc8 100644 --- a/daemon/internal/layer/migration_test.go +++ b/daemon/internal/layer/migration_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/docker/docker/daemon/graphdriver" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" ) func tarFromFilesInGraph(graph graphdriver.Driver, graphID, parentID string, files ...FileApplier) ([]byte, error) { diff --git a/pkg/stringid/stringid.go b/daemon/internal/stringid/stringid.go similarity index 100% rename from pkg/stringid/stringid.go rename to daemon/internal/stringid/stringid.go diff --git a/pkg/stringid/stringid_test.go b/daemon/internal/stringid/stringid_test.go similarity index 100% rename from pkg/stringid/stringid_test.go rename to daemon/internal/stringid/stringid_test.go diff --git a/daemon/libnetwork/controller.go b/daemon/libnetwork/controller.go index f1e2d208eb..9ba2e4203b 100644 --- a/daemon/libnetwork/controller.go +++ b/daemon/libnetwork/controller.go @@ -55,6 +55,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/otelutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/cluster" "github.com/docker/docker/daemon/libnetwork/config" "github.com/docker/docker/daemon/libnetwork/datastore" @@ -71,7 +72,6 @@ import ( "github.com/docker/docker/daemon/libnetwork/types" "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/plugins" - "github.com/docker/docker/pkg/stringid" "github.com/moby/locker" "github.com/pkg/errors" "go.opentelemetry.io/otel" diff --git a/daemon/libnetwork/drivers/bridge/bridge_linux.go b/daemon/libnetwork/drivers/bridge/bridge_linux.go index 5384c9cd51..65152fb1ee 100644 --- a/daemon/libnetwork/drivers/bridge/bridge_linux.go +++ b/daemon/libnetwork/drivers/bridge/bridge_linux.go @@ -18,6 +18,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/otelutil" "github.com/docker/docker/daemon/internal/sliceutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/datastore" "github.com/docker/docker/daemon/libnetwork/driverapi" "github.com/docker/docker/daemon/libnetwork/drivers/bridge/internal/firewaller" @@ -36,7 +37,6 @@ import ( "github.com/docker/docker/daemon/libnetwork/scope" "github.com/docker/docker/daemon/libnetwork/types" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/stringid" "github.com/pkg/errors" "github.com/vishvananda/netlink" "github.com/vishvananda/netns" diff --git a/daemon/libnetwork/endpoint.go b/daemon/libnetwork/endpoint.go index 79f7046dfb..076da29e3d 100644 --- a/daemon/libnetwork/endpoint.go +++ b/daemon/libnetwork/endpoint.go @@ -15,6 +15,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/sliceutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/datastore" "github.com/docker/docker/daemon/libnetwork/driverapi" "github.com/docker/docker/daemon/libnetwork/ipamapi" @@ -23,7 +24,6 @@ import ( "github.com/docker/docker/daemon/libnetwork/scope" "github.com/docker/docker/daemon/libnetwork/types" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/stringid" "go.opentelemetry.io/otel" ) diff --git a/daemon/libnetwork/network.go b/daemon/libnetwork/network.go index 3d237dc8e7..a2ebb23de3 100644 --- a/daemon/libnetwork/network.go +++ b/daemon/libnetwork/network.go @@ -17,6 +17,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/sliceutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/datastore" "github.com/docker/docker/daemon/libnetwork/driverapi" "github.com/docker/docker/daemon/libnetwork/internal/netiputil" @@ -30,7 +31,6 @@ import ( "github.com/docker/docker/daemon/libnetwork/scope" "github.com/docker/docker/daemon/libnetwork/types" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/stringid" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" diff --git a/daemon/libnetwork/networkdb/networkdb.go b/daemon/libnetwork/networkdb/networkdb.go index f620bad148..83fbbbc16b 100644 --- a/daemon/libnetwork/networkdb/networkdb.go +++ b/daemon/libnetwork/networkdb/networkdb.go @@ -17,8 +17,8 @@ import ( "time" "github.com/containerd/log" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/types" - "github.com/docker/docker/pkg/stringid" "github.com/docker/go-events" iradix "github.com/hashicorp/go-immutable-radix/v2" "github.com/hashicorp/memberlist" diff --git a/daemon/libnetwork/networkdb/networkdb_test.go b/daemon/libnetwork/networkdb/networkdb_test.go index c34579f710..423880ad2c 100644 --- a/daemon/libnetwork/networkdb/networkdb_test.go +++ b/daemon/libnetwork/networkdb/networkdb_test.go @@ -14,7 +14,7 @@ import ( cerrdefs "github.com/containerd/errdefs" "github.com/containerd/log" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/go-events" "github.com/hashicorp/memberlist" "gotest.tools/v3/assert" diff --git a/daemon/libnetwork/sandbox_externalkey_unix.go b/daemon/libnetwork/sandbox_externalkey_unix.go index 25fbdc5d98..fa36aa8e10 100644 --- a/daemon/libnetwork/sandbox_externalkey_unix.go +++ b/daemon/libnetwork/sandbox_externalkey_unix.go @@ -14,8 +14,8 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/otelutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/types" - "github.com/docker/docker/pkg/stringid" "github.com/moby/sys/reexec" "github.com/opencontainers/runtime-spec/specs-go" "go.opentelemetry.io/otel" diff --git a/daemon/libnetwork/sandbox_store.go b/daemon/libnetwork/sandbox_store.go index c326fffdee..ae65a3087b 100644 --- a/daemon/libnetwork/sandbox_store.go +++ b/daemon/libnetwork/sandbox_store.go @@ -7,10 +7,10 @@ import ( "fmt" "github.com/containerd/log" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/libnetwork/datastore" "github.com/docker/docker/daemon/libnetwork/osl" "github.com/docker/docker/daemon/libnetwork/scope" - "github.com/docker/docker/pkg/stringid" ) const ( diff --git a/daemon/logger/copier.go b/daemon/logger/copier.go index 457d686eeb..3bc29d4175 100644 --- a/daemon/logger/copier.go +++ b/daemon/logger/copier.go @@ -8,7 +8,7 @@ import ( "time" "github.com/containerd/log" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/daemon/internal/stringid" types "github.com/moby/moby/api/types/backend" ) diff --git a/daemon/logger/journald/journald.go b/daemon/logger/journald/journald.go index ac3d4c757e..130e0fc6ec 100644 --- a/daemon/logger/journald/journald.go +++ b/daemon/logger/journald/journald.go @@ -12,9 +12,9 @@ import ( "github.com/coreos/go-systemd/v22/journal" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/logger" "github.com/docker/docker/daemon/logger/loggerutils" - "github.com/docker/docker/pkg/stringid" ) const name = "journald" diff --git a/daemon/logger/plugin.go b/daemon/logger/plugin.go index 22fb13e529..3841467388 100644 --- a/daemon/logger/plugin.go +++ b/daemon/logger/plugin.go @@ -6,10 +6,10 @@ import ( "os" "path/filepath" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/plugins" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types/plugins/logdriver" "github.com/pkg/errors" ) diff --git a/daemon/names.go b/daemon/names.go index c47c607ee0..aad5d6af83 100644 --- a/daemon/names.go +++ b/daemon/names.go @@ -7,10 +7,10 @@ import ( cerrdefs "github.com/containerd/errdefs" "github.com/containerd/log" "github.com/docker/docker/daemon/container" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/names" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/namesgenerator" - "github.com/docker/docker/pkg/stringid" "github.com/pkg/errors" ) diff --git a/daemon/pkg/plugin/backend_linux.go b/daemon/pkg/plugin/backend_linux.go index 13ea629022..774f676bdb 100644 --- a/daemon/pkg/plugin/backend_linux.go +++ b/daemon/pkg/plugin/backend_linux.go @@ -22,13 +22,13 @@ import ( "github.com/containerd/platforms" "github.com/distribution/reference" "github.com/docker/docker/daemon/internal/containerfs" + "github.com/docker/docker/daemon/internal/stringid" v2 "github.com/docker/docker/daemon/pkg/plugin/v2" "github.com/docker/docker/dockerversion" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/authorization" "github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/progress" - "github.com/docker/docker/pkg/stringid" "github.com/moby/go-archive/chrootarchive" "github.com/moby/moby/api/types" "github.com/moby/moby/api/types/backend" diff --git a/daemon/pkg/plugin/fetch_linux.go b/daemon/pkg/plugin/fetch_linux.go index 4c50dc44e2..cf3faf547a 100644 --- a/daemon/pkg/plugin/fetch_linux.go +++ b/daemon/pkg/plugin/fetch_linux.go @@ -14,9 +14,9 @@ import ( "github.com/containerd/log" "github.com/distribution/reference" progressutils "github.com/docker/docker/daemon/internal/distribution/utils" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/progress" - "github.com/docker/docker/pkg/stringid" "github.com/moby/go-archive/chrootarchive" "github.com/moby/moby/api/types/registry" "github.com/opencontainers/go-digest" diff --git a/daemon/pkg/plugin/manager_linux.go b/daemon/pkg/plugin/manager_linux.go index 296eb7ba72..e50d744f29 100644 --- a/daemon/pkg/plugin/manager_linux.go +++ b/daemon/pkg/plugin/manager_linux.go @@ -11,10 +11,10 @@ import ( "github.com/containerd/containerd/v2/core/content" "github.com/containerd/log" "github.com/docker/docker/daemon/initlayer" + "github.com/docker/docker/daemon/internal/stringid" v2 "github.com/docker/docker/daemon/pkg/plugin/v2" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/plugins" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types" "github.com/moby/sys/mount" "github.com/opencontainers/go-digest" diff --git a/daemon/pkg/plugin/manager_linux_test.go b/daemon/pkg/plugin/manager_linux_test.go index e0b8c555b1..db5025f167 100644 --- a/daemon/pkg/plugin/manager_linux_test.go +++ b/daemon/pkg/plugin/manager_linux_test.go @@ -9,8 +9,8 @@ import ( "testing" "github.com/docker/docker/daemon/internal/containerfs" + "github.com/docker/docker/daemon/internal/stringid" v2 "github.com/docker/docker/daemon/pkg/plugin/v2" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types" "github.com/moby/moby/api/types/backend" "github.com/moby/moby/api/types/events" diff --git a/daemon/server/router/container/inspect.go b/daemon/server/router/container/inspect.go index e9a6117515..ee20ab04b7 100644 --- a/daemon/server/router/container/inspect.go +++ b/daemon/server/router/container/inspect.go @@ -8,8 +8,8 @@ import ( "net/http" "github.com/docker/docker/daemon/internal/sliceutil" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/server/httputils" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types/backend" "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/versions" diff --git a/daemon/volume/mounts/mounts.go b/daemon/volume/mounts/mounts.go index 3b4a881700..0558deebc4 100644 --- a/daemon/volume/mounts/mounts.go +++ b/daemon/volume/mounts/mounts.go @@ -8,9 +8,9 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/idtools" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/volume" "github.com/docker/docker/daemon/volume/safepath" - "github.com/docker/docker/pkg/stringid" mounttypes "github.com/moby/moby/api/types/mount" "github.com/moby/sys/user" "github.com/opencontainers/selinux/go-selinux/label" diff --git a/daemon/volume/service/service.go b/daemon/volume/service/service.go index cc252fdbd5..5e123d01b5 100644 --- a/daemon/volume/service/service.go +++ b/daemon/volume/service/service.go @@ -8,12 +8,12 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/internal/directory" "github.com/docker/docker/daemon/internal/idtools" + "github.com/docker/docker/daemon/internal/stringid" "github.com/docker/docker/daemon/volume" "github.com/docker/docker/daemon/volume/drivers" "github.com/docker/docker/daemon/volume/service/opts" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/docker/pkg/stringid" "github.com/moby/moby/api/types/events" "github.com/moby/moby/api/types/filters" volumetypes "github.com/moby/moby/api/types/volume" diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index a165e1ee31..effa807206 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -22,7 +22,6 @@ import ( "github.com/docker/docker/daemon/volume" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil" "github.com/docker/docker/testutil/request" "github.com/docker/go-connections/nat" @@ -30,6 +29,7 @@ import ( "github.com/moby/moby/api/types/mount" "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" + "github.com/moby/moby/client/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/poll" diff --git a/integration-cli/docker_cli_external_volume_driver_test.go b/integration-cli/docker_cli_external_volume_driver_test.go index 131741c626..1638ac8eb6 100644 --- a/integration-cli/docker_cli_external_volume_driver_test.go +++ b/integration-cli/docker_cli_external_volume_driver_test.go @@ -19,7 +19,6 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/pkg/plugins" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil" testdaemon "github.com/docker/docker/testutil/daemon" "github.com/moby/moby/api/types/container" @@ -560,7 +559,7 @@ func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverCapabilities(c *test func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverOutOfBandDelete(c *testing.T) { ctx := testutil.GetContext(c) - driverName := stringid.GenerateRandomID() + driverName := strings.ReplaceAll(strings.ToLower(c.Name()), "/", "_") p := newVolumePlugin(c, driverName) defer p.Close() diff --git a/integration-cli/docker_cli_images_test.go b/integration-cli/docker_cli_images_test.go index ca18cde19b..4a7a825dc2 100644 --- a/integration-cli/docker_cli_images_test.go +++ b/integration-cli/docker_cli_images_test.go @@ -13,7 +13,7 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - "github.com/docker/docker/pkg/stringid" + "github.com/moby/moby/client/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/icmd" diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index 0f9ff587fe..cf1806c1e3 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -23,11 +23,11 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/pkg/plugins" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil" testdaemon "github.com/docker/docker/testutil/daemon" "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/network" + "github.com/moby/moby/client/pkg/stringid" "github.com/vishvananda/netlink" "golang.org/x/sys/unix" "gotest.tools/v3/assert" diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index 24a0bcfb22..6af69cf546 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/integration-cli/docker_cli_ps_test.go @@ -10,8 +10,8 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - "github.com/docker/docker/pkg/stringid" "github.com/docker/go-units" + "github.com/moby/moby/client/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/icmd" diff --git a/integration-cli/docker_cli_rmi_test.go b/integration-cli/docker_cli_rmi_test.go index 49747af06d..07819a73b1 100644 --- a/integration-cli/docker_cli_rmi_test.go +++ b/integration-cli/docker_cli_rmi_test.go @@ -9,7 +9,7 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - "github.com/docker/docker/pkg/stringid" + "github.com/moby/moby/client/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/icmd" diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index ec717c7c71..7f535fcfc4 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -27,12 +27,12 @@ import ( "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/internal/testutils/specialimage" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil" testdaemon "github.com/docker/docker/testutil/daemon" "github.com/docker/docker/testutil/fakecontext" "github.com/docker/go-connections/nat" "github.com/moby/moby/client" + "github.com/moby/moby/client/pkg/stringid" "github.com/moby/sys/mountinfo" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" diff --git a/integration-cli/docker_cli_userns_test.go b/integration-cli/docker_cli_userns_test.go index 33876406af..e654c2472f 100644 --- a/integration-cli/docker_cli_userns_test.go +++ b/integration-cli/docker_cli_userns_test.go @@ -13,8 +13,8 @@ import ( "syscall" "testing" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil" + "github.com/moby/moby/client/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) diff --git a/integration/container/create_test.go b/integration/container/create_test.go index 3b375a0c21..2ad60acebf 100644 --- a/integration/container/create_test.go +++ b/integration/container/create_test.go @@ -14,12 +14,12 @@ import ( "github.com/docker/docker/daemon/pkg/oci" testContainer "github.com/docker/docker/integration/internal/container" net "github.com/docker/docker/integration/internal/network" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil" "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/network" "github.com/moby/moby/api/types/versions" "github.com/moby/moby/client" + "github.com/moby/moby/client/pkg/stringid" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" diff --git a/integration/container/rename_test.go b/integration/container/rename_test.go index 6f7c317abe..483b96a310 100644 --- a/integration/container/rename_test.go +++ b/integration/container/rename_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/docker/docker/integration/internal/container" - "github.com/docker/docker/pkg/stringid" containertypes "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/network" "gotest.tools/v3/assert" @@ -53,7 +52,7 @@ func TestRenameStoppedContainer(t *testing.T) { assert.NilError(t, err) assert.Check(t, is.Equal("/"+oldName, inspect.Name)) - newName := "new_name" + stringid.GenerateRandomID() + newName := "new_name" + cID // using cID as random suffix err = apiClient.ContainerRename(ctx, oldName, newName) assert.NilError(t, err) @@ -69,7 +68,7 @@ func TestRenameRunningContainerAndReuse(t *testing.T) { oldName := "first_name" + t.Name() cID := container.Run(ctx, t, apiClient, container.WithName(oldName)) - newName := "new_name" + stringid.GenerateRandomID() + newName := "new_name" + cID // using cID as random suffix err := apiClient.ContainerRename(ctx, oldName, newName) assert.NilError(t, err) diff --git a/pkg/stringid/alias.go b/pkg/stringid/alias.go new file mode 100644 index 0000000000..8066889e92 --- /dev/null +++ b/pkg/stringid/alias.go @@ -0,0 +1,18 @@ +// Package stringid provides helper functions for dealing with string identifiers. +package stringid + +import "github.com/moby/moby/client/pkg/stringid" + +// TruncateID returns a shorthand version of a string identifier for presentation. +// +// Deprecated: use [stringid.TruncateID]. This package will be removed in the next release. +func TruncateID(id string) string { + return stringid.TruncateID(id) +} + +// GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9. +// +// Deprecated: use [stringid.GenerateRandomID]. This package will be removed in the next release. +func GenerateRandomID() string { + return stringid.GenerateRandomID() +} diff --git a/testutil/daemon/daemon.go b/testutil/daemon/daemon.go index 6c5437f433..7576013b86 100644 --- a/testutil/daemon/daemon.go +++ b/testutil/daemon/daemon.go @@ -23,7 +23,6 @@ import ( "github.com/docker/docker/daemon/container" "github.com/docker/docker/pkg/ioutils" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/tailfile" "github.com/docker/docker/testutil/request" "github.com/docker/go-connections/sockets" @@ -31,6 +30,7 @@ import ( "github.com/moby/moby/api/types/events" "github.com/moby/moby/api/types/system" "github.com/moby/moby/client" + "github.com/moby/moby/client/pkg/stringid" "github.com/pkg/errors" "gotest.tools/v3/assert" "gotest.tools/v3/poll" diff --git a/vendor/github.com/moby/moby/client/pkg/stringid/stringid.go b/vendor/github.com/moby/moby/client/pkg/stringid/stringid.go new file mode 100644 index 0000000000..030e070853 --- /dev/null +++ b/vendor/github.com/moby/moby/client/pkg/stringid/stringid.go @@ -0,0 +1,47 @@ +// Package stringid provides helper functions for dealing with string identifiers. +// +// It is similar to the package used by the daemon, but for presentational +// purposes in the client. +package stringid + +import ( + "crypto/rand" + "encoding/hex" + "strings" +) + +const ( + shortLen = 12 + fullLen = 64 +) + +// TruncateID returns a shorthand version of a string identifier for presentation. +// For convenience, it accepts both digests ("sha256:xxxx") and IDs without an +// algorithm prefix. It truncates the algorithm (if any) before truncating the +// ID. The length of the truncated ID is currently fixed, but users should make +// no assumptions of this to not change; it is merely a prefix of the ID that +// provides enough uniqueness for common scenarios. +// +// Truncated IDs ("ID-prefixes") usually can be used to uniquely identify an +// object (such as a container or network), but collisions may happen, in +// which case an "ambiguous result" error is produced. In case of a collision, +// the caller should try with a longer prefix or the full-length ID. +func TruncateID(id string) string { + if i := strings.IndexRune(id, ':'); i >= 0 { + id = id[i+1:] + } + if len(id) > shortLen { + id = id[:shortLen] + } + return id +} + +// GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9. +func GenerateRandomID() string { + b := make([]byte, 32) + if _, err := rand.Read(b); err != nil { + panic(err) // This shouldn't happen + } + id := hex.EncodeToString(b) + return id +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 4a26ae361e..8323efca82 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -966,6 +966,7 @@ github.com/moby/moby/api/types/volume # github.com/moby/moby/client v0.0.0 => ./client ## explicit; go 1.23.0 github.com/moby/moby/client +github.com/moby/moby/client/pkg/stringid # github.com/moby/patternmatcher v0.6.0 ## explicit; go 1.19 github.com/moby/patternmatcher