pkg/stringid: move to daemon, and provide copy in client

The stringid package is used in many places; while it's trivial
to implement a similar utility, let's just provide it as a utility
package in the client, removing the daemon-specific logic.

For integration tests, I opted to use the implementation in the
client, as those should not ideally not make assumptions about
the daemon implementation.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-07-24 12:55:07 +02:00
parent 57e1cb2a28
commit ca1c5ee08f
65 changed files with 226 additions and 61 deletions

View File

@@ -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
}

View File

@@ -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)
}
})
}
}

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"

View File

@@ -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()

View File

@@ -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) {

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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 (

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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()

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"

View File

@@ -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)

18
pkg/stringid/alias.go Normal file
View File

@@ -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()
}

View File

@@ -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"

View File

@@ -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
}

1
vendor/modules.txt vendored
View File

@@ -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