Switch to github.com/google/uuid (#4132)

This commit is contained in:
Milos Gajdos 2023-10-26 13:36:12 +01:00 committed by GitHub
commit d8d14ca363
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 30 deletions

View file

@ -4,7 +4,7 @@ import (
"context" "context"
"sync" "sync"
"github.com/distribution/distribution/v3/internal/uuid" "github.com/google/uuid"
) )
// instanceContext is a context that provides only an instance id. It is // instanceContext is a context that provides only an instance id. It is
@ -22,7 +22,7 @@ func (ic *instanceContext) Value(key interface{}) interface{} {
// call a random generator from the package initialization // call a random generator from the package initialization
// code. For various reasons random could not be available // code. For various reasons random could not be available
// https://github.com/distribution/distribution/issues/782 // https://github.com/distribution/distribution/issues/782
ic.id = uuid.Generate().String() ic.id = uuid.NewString()
}) })
return ic.id return ic.id
} }

View file

@ -9,7 +9,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/distribution/distribution/v3/internal/uuid" "github.com/google/uuid"
"github.com/gorilla/mux" "github.com/gorilla/mux"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -78,7 +78,7 @@ func WithRequest(ctx context.Context, r *http.Request) context.Context {
return &httpRequestContext{ return &httpRequestContext{
Context: ctx, Context: ctx,
startedAt: time.Now(), startedAt: time.Now(),
id: uuid.Generate().String(), id: uuid.NewString(),
r: r, r: r,
} }
} }

View file

@ -5,7 +5,7 @@ import (
"runtime" "runtime"
"time" "time"
"github.com/distribution/distribution/v3/internal/uuid" "github.com/google/uuid"
) )
// WithTrace allocates a traced timing span in a new context. This allows a // WithTrace allocates a traced timing span in a new context. This allows a
@ -46,7 +46,7 @@ func WithTrace(ctx context.Context) (context.Context, func(format string, a ...i
f := runtime.FuncForPC(pc) f := runtime.FuncForPC(pc)
ctx = &traced{ ctx = &traced{
Context: ctx, Context: ctx,
id: uuid.Generate().String(), id: uuid.NewString(),
start: time.Now(), start: time.Now(),
parent: GetStringValue(ctx, "trace.id"), parent: GetStringValue(ctx, "trace.id"),
fnname: f.Name(), fnname: f.Name(),

2
go.mod
View file

@ -15,6 +15,7 @@ require (
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c
github.com/docker/go-metrics v0.0.1 github.com/docker/go-metrics v0.0.1
github.com/go-jose/go-jose/v3 v3.0.0 github.com/go-jose/go-jose/v3 v3.0.0
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1 github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/hashicorp/golang-lru/arc/v2 v2.0.5 github.com/hashicorp/golang-lru/arc/v2 v2.0.5
@ -51,7 +52,6 @@ require (
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect

View file

@ -18,12 +18,12 @@ import (
"github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/context" "github.com/distribution/distribution/v3/context"
"github.com/distribution/distribution/v3/internal/uuid"
"github.com/distribution/distribution/v3/manifest" "github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/ocischema" "github.com/distribution/distribution/v3/manifest/ocischema"
"github.com/distribution/distribution/v3/registry/api/errcode" "github.com/distribution/distribution/v3/registry/api/errcode"
"github.com/distribution/distribution/v3/testutil" "github.com/distribution/distribution/v3/testutil"
"github.com/distribution/reference" "github.com/distribution/reference"
"github.com/google/uuid"
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
@ -200,7 +200,7 @@ func TestBlobServeBlobHEAD(t *testing.T) {
func TestBlobResume(t *testing.T) { func TestBlobResume(t *testing.T) {
dgst, b1 := newRandomBlob(1024) dgst, b1 := newRandomBlob(1024)
id := uuid.Generate().String() id := uuid.NewString()
var m testutil.RequestResponseMap var m testutil.RequestResponseMap
repo, _ := reference.WithName("test.example.com/repo1") repo, _ := reference.WithName("test.example.com/repo1")
m = append(m, testutil.RequestResponseMapping{ m = append(m, testutil.RequestResponseMapping{
@ -440,7 +440,7 @@ func TestBlobUploadChunked(t *testing.T) {
b1[513:1024], b1[513:1024],
} }
repo, _ := reference.WithName("test.example.com/uploadrepo") repo, _ := reference.WithName("test.example.com/uploadrepo")
uuids := []string{uuid.Generate().String()} uuids := []string{uuid.NewString()}
m = append(m, testutil.RequestResponseMapping{ m = append(m, testutil.RequestResponseMapping{
Request: testutil.Request{ Request: testutil.Request{
Method: http.MethodPost, Method: http.MethodPost,
@ -458,7 +458,7 @@ func TestBlobUploadChunked(t *testing.T) {
}) })
offset := 0 offset := 0
for i, chunk := range chunks { for i, chunk := range chunks {
uuids = append(uuids, uuid.Generate().String()) uuids = append(uuids, uuid.NewString())
newOffset := offset + len(chunk) newOffset := offset + len(chunk)
m = append(m, testutil.RequestResponseMapping{ m = append(m, testutil.RequestResponseMapping{
Request: testutil.Request{ Request: testutil.Request{
@ -555,7 +555,7 @@ func TestBlobUploadMonolithic(t *testing.T) {
dgst, b1 := newRandomBlob(1024) dgst, b1 := newRandomBlob(1024)
var m testutil.RequestResponseMap var m testutil.RequestResponseMap
repo, _ := reference.WithName("test.example.com/uploadrepo") repo, _ := reference.WithName("test.example.com/uploadrepo")
uploadID := uuid.Generate().String() uploadID := uuid.NewString()
m = append(m, testutil.RequestResponseMapping{ m = append(m, testutil.RequestResponseMapping{
Request: testutil.Request{ Request: testutil.Request{
Method: http.MethodPost, Method: http.MethodPost,
@ -663,7 +663,7 @@ func TestBlobUploadMonolithicDockerUploadUUIDFromURL(t *testing.T) {
dgst, b1 := newRandomBlob(1024) dgst, b1 := newRandomBlob(1024)
var m testutil.RequestResponseMap var m testutil.RequestResponseMap
repo, _ := reference.WithName("test.example.com/uploadrepo") repo, _ := reference.WithName("test.example.com/uploadrepo")
uploadID := uuid.Generate().String() uploadID := uuid.NewString()
m = append(m, testutil.RequestResponseMapping{ m = append(m, testutil.RequestResponseMapping{
Request: testutil.Request{ Request: testutil.Request{
Method: http.MethodPost, Method: http.MethodPost,

View file

@ -6,9 +6,9 @@ import (
"github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/context" "github.com/distribution/distribution/v3/context"
"github.com/distribution/distribution/v3/internal/uuid"
"github.com/distribution/reference" "github.com/distribution/reference"
events "github.com/docker/go-events" events "github.com/docker/go-events"
"github.com/google/uuid"
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
) )
@ -219,7 +219,7 @@ func (b *bridge) createEvent(action string) *Event {
// createEvent returns a new event, timestamped, with the specified action. // createEvent returns a new event, timestamped, with the specified action.
func createEvent(action string) *Event { func createEvent(action string) *Event {
return &Event{ return &Event{
ID: uuid.Generate().String(), ID: uuid.NewString(),
Timestamp: time.Now(), Timestamp: time.Now(),
Action: action, Action: action,
} }

View file

@ -4,12 +4,12 @@ import (
"testing" "testing"
"github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/internal/uuid"
"github.com/distribution/distribution/v3/manifest" "github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/schema2" "github.com/distribution/distribution/v3/manifest/schema2"
v2 "github.com/distribution/distribution/v3/registry/api/v2" v2 "github.com/distribution/distribution/v3/registry/api/v2"
"github.com/distribution/reference" "github.com/distribution/reference"
events "github.com/docker/go-events" events "github.com/docker/go-events"
"github.com/google/uuid"
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
@ -20,7 +20,7 @@ var (
repo = "test/repo" repo = "test/repo"
source = SourceRecord{ source = SourceRecord{
Addr: "remote.test", Addr: "remote.test",
InstanceID: uuid.Generate().String(), InstanceID: uuid.NewString(),
} }
ub = mustUB(v2.NewURLBuilderFromString("http://test.example.com/", false)) ub = mustUB(v2.NewURLBuilderFromString("http://test.example.com/", false))

View file

@ -23,7 +23,6 @@ import (
"github.com/distribution/distribution/v3/configuration" "github.com/distribution/distribution/v3/configuration"
dcontext "github.com/distribution/distribution/v3/context" dcontext "github.com/distribution/distribution/v3/context"
"github.com/distribution/distribution/v3/health" "github.com/distribution/distribution/v3/health"
"github.com/distribution/distribution/v3/internal/uuid"
"github.com/distribution/distribution/v3/registry/handlers" "github.com/distribution/distribution/v3/registry/handlers"
"github.com/distribution/distribution/v3/registry/listener" "github.com/distribution/distribution/v3/registry/listener"
"github.com/distribution/distribution/v3/version" "github.com/distribution/distribution/v3/version"
@ -136,10 +135,6 @@ func NewRegistry(ctx context.Context, config *configuration.Configuration) (*Reg
return nil, fmt.Errorf("error configuring logger: %v", err) return nil, fmt.Errorf("error configuring logger: %v", err)
} }
// inject a logger into the uuid library. warns us if there is a problem
// with uuid generation under low entropy.
uuid.Loggerf = dcontext.GetLogger(ctx).Warnf
app := handlers.NewApp(ctx, config) app := handlers.NewApp(ctx, config)
// TODO(aaronl): The global scope of the health checks means NewRegistry // TODO(aaronl): The global scope of the health checks means NewRegistry
// can only be called once per process. // can only be called once per process.

View file

@ -10,9 +10,9 @@ import (
"github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3"
dcontext "github.com/distribution/distribution/v3/context" dcontext "github.com/distribution/distribution/v3/context"
"github.com/distribution/distribution/v3/internal/uuid"
"github.com/distribution/distribution/v3/registry/storage/driver" "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/distribution/reference" "github.com/distribution/reference"
"github.com/google/uuid"
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
) )
@ -144,7 +144,7 @@ func (lbs *linkedBlobStore) Create(ctx context.Context, options ...distribution.
} }
} }
uuid := uuid.Generate().String() uuid := uuid.NewString()
startedAt := time.Now().UTC() startedAt := time.Now().UTC()
path, err := pathFor(uploadDataPathSpec{ path, err := pathFor(uploadDataPathSpec{

View file

@ -6,8 +6,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/distribution/distribution/v3/internal/uuid"
storageDriver "github.com/distribution/distribution/v3/registry/storage/driver" storageDriver "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/google/uuid"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )

View file

@ -7,16 +7,16 @@ import (
"testing" "testing"
"time" "time"
"github.com/distribution/distribution/v3/internal/uuid"
"github.com/distribution/distribution/v3/registry/storage/driver" "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory" "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
"github.com/google/uuid"
) )
func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) (driver.StorageDriver, context.Context) { func testUploadFS(t *testing.T, numUploads int, repoName string, startedAt time.Time) (driver.StorageDriver, context.Context) {
d := inmemory.New() d := inmemory.New()
ctx := context.Background() ctx := context.Background()
for i := 0; i < numUploads; i++ { for i := 0; i < numUploads; i++ {
addUploads(ctx, t, d, uuid.Generate().String(), repoName, startedAt) addUploads(ctx, t, d, uuid.NewString(), repoName, startedAt)
} }
return d, ctx return d, ctx
} }
@ -70,7 +70,7 @@ func TestPurgeAll(t *testing.T) {
fs, ctx := testUploadFS(t, uploadCount, "test-repo", oneHourAgo) fs, ctx := testUploadFS(t, uploadCount, "test-repo", oneHourAgo)
// Ensure > 1 repos are purged // Ensure > 1 repos are purged
addUploads(ctx, t, fs, uuid.Generate().String(), "test-repo2", oneHourAgo) addUploads(ctx, t, fs, uuid.NewString(), "test-repo2", oneHourAgo)
uploadCount++ uploadCount++
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true) deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
@ -92,7 +92,7 @@ func TestPurgeSome(t *testing.T) {
newUploadCount := 4 newUploadCount := 4
for i := 0; i < newUploadCount; i++ { for i := 0; i < newUploadCount; i++ {
addUploads(ctx, t, fs, uuid.Generate().String(), "test-repo", time.Now().Add(1*time.Hour)) addUploads(ctx, t, fs, uuid.NewString(), "test-repo", time.Now().Add(1*time.Hour))
} }
deleted, errs := PurgeUploads(ctx, fs, time.Now(), true) deleted, errs := PurgeUploads(ctx, fs, time.Now(), true)
@ -112,7 +112,7 @@ func TestPurgeOnlyUploads(t *testing.T) {
// Create a directory tree outside _uploads and ensure // Create a directory tree outside _uploads and ensure
// these files aren't deleted. // these files aren't deleted.
dataPath, err := pathFor(uploadDataPathSpec{name: "test-repo", id: uuid.Generate().String()}) dataPath, err := pathFor(uploadDataPathSpec{name: "test-repo", id: uuid.NewString()})
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }