Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/distribution/reference v0.6.0
github.com/docker/cli v29.4.1+incompatible
github.com/docker/cli-docs-tool v0.11.0
github.com/docker/docker v28.5.2+incompatible
github.com/docker/go-units v0.5.0
github.com/gofrs/flock v0.13.0
github.com/golang/snappy v1.0.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ github.com/docker/cli-docs-tool v0.11.0 h1:7d8QARFb7QEobizqxmEM7fOteZEHwH/zWgHQt
github.com/docker/cli-docs-tool v0.11.0/go.mod h1:ma8BKiisUo8D6W05XEYIh3oa1UbgrZhi1nowyKFJa8Q=
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=
github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM=
github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker-credential-helpers v0.9.5 h1:EFNN8DHvaiK8zVqFA2DT6BjXE0GzfLOZ38ggPTKePkY=
github.com/docker/docker-credential-helpers v0.9.5/go.mod h1:v1S+hepowrQXITkEfw6o4+BMbGot02wiKpzWhGUZK6c=
github.com/docker/go-connections v0.7.0 h1:6SsRfJddP22WMrCkj19x9WKjEDTB+ahsdiGYf0mN39c=
Expand Down
14 changes: 14 additions & 0 deletions store/nodegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,17 @@ func TestNodeGroupUpdate(t *testing.T) {
require.Equal(t, 1, len(ng.Nodes))
require.Equal(t, []string{"linux/arm64"}, platformutil.Format(ng.Nodes[0].Platforms))
}

func TestNodeGroupNextNodeName(t *testing.T) {
t.Parallel()

ng := &NodeGroup{
Name: "foo",
Nodes: []Node{
{Name: "foo0"},
{Name: "foo1"},
},
}

require.Equal(t, "foo2", ng.nextNodeName())
}
64 changes: 64 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -260,3 +261,66 @@ func TestNodeInvalidName(t *testing.T) {
require.Error(t, err)
require.True(t, IsErrInvalidName(err))
}

func TestGenerateName(t *testing.T) {
tmpdir := t.TempDir()

s, err := New(confutil.NewConfig(nil, confutil.WithDir(tmpdir)))
require.NoError(t, err)

txn, release, err := s.Txn()
require.NoError(t, err)
defer release()

oldNewID := newID
newID = func() string {
return "abc123def456ghi789jklmno"
}
defer func() {
newID = oldNewID
}()

name, err := GenerateName(txn)
require.NoError(t, err)
require.Equal(t, "builder_abc123def456", name)
require.True(t, strings.HasPrefix(name, generatedNamePrefix))

validated, err := ValidateName(name)
require.NoError(t, err)
require.Equal(t, name, validated)
}

func TestGenerateNameCollisionRetry(t *testing.T) {
tmpdir := t.TempDir()

s, err := New(confutil.NewConfig(nil, confutil.WithDir(tmpdir)))
require.NoError(t, err)

txn, release, err := s.Txn()
require.NoError(t, err)
defer release()

err = txn.Save(&NodeGroup{
Name: "builder_collisionabc",
Driver: "docker-container",
})
require.NoError(t, err)

oldNewID := newID
ids := []string{
"collisionabc0000000000000",
"freshnamexyz0000000000000",
}
newID = func() string {
id := ids[0]
ids = ids[1:]
return id
}
defer func() {
newID = oldNewID
}()

name, err := GenerateName(txn)
require.NoError(t, err)
require.Equal(t, "builder_freshnamexyz", name)
}
10 changes: 7 additions & 3 deletions store/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"regexp"
"strings"

"github.com/docker/docker/pkg/namesgenerator"
"github.com/moby/buildkit/identity"
"github.com/pkg/errors"
)

var namePattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_]*$`)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR probably , but looks like we have some different patterns in different packages;

buildx/bake/bake.go

Lines 40 to 41 in 09f288c

validTargetNameChars = `[a-zA-Z0-9_-]+`
targetNamePattern = regexp.MustCompile(`^` + validTargetNameChars + `$`)

I know these regexes are rather memory-hungry; we could even rewrite these to a basic function to loop; I recall we did similar changes elsewhere, e.g.; https://github.com/moby/moby/blob/dff719e3674958407416fd1d8a35db998f128da2/daemon/internal/stringid/stringid.go#L62-L70


const generatedNamePrefix = "builder_"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we're changing, might as well use - instead of _ (which should produce a more correct hostname for the container if there's ever a need for that).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used builder_ intentionally because Buildx already derives runtime names through the buildx_buildkit_ prefix, so underscores are already part of the existing naming convention here. And it's a bit easier to read and copy/paste consistently alongside the derived node and runtime names.


var newID = identity.NewID
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we need the complexity of identity here; would something like this give enough randomness?

import (
	"math/rand/v2"
	"encoding/hex"
)

func NewID() string {
	var b [8]byte // 64 bits
	rand.Read(b[:])
	return hex.EncodeToString(b[:]) // 16 hex chars
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh; and I just recalled I did something similar for k8s some time ago;

// generateName generates the name plus a random suffix of five alphanumerics
// when a name is requested. The string is guaranteed to not exceed the length
// of a standard Kubernetes name (63 characters).
//
// It's a simplified implementation of k8s.io/apiserver/pkg/storage/names:
// https://github.com/kubernetes/apiserver/blob/v0.29.2/pkg/storage/names/generate.go#L34-L54
func generateName(base string) string {
if len(base) > maxGeneratedNameLength {
base = base[:maxGeneratedNameLength]
}
return base + randomSuffix()
}
func randomSuffix() string {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
panic(err) // This shouldn't happen
}
return hex.EncodeToString(b)[:randomLength]
}


type errInvalidName struct {
error
}
Expand Down Expand Up @@ -39,8 +43,8 @@ func ValidateName(s string) (string, error) {

func GenerateName(txn *Txn) (string, error) {
var name string
for i := range 6 {
name = namesgenerator.GetRandomName(i)
for range 6 {
name = generatedNamePrefix + newID()[:12]
if _, err := txn.NodeGroupByName(name); err != nil {
if !os.IsNotExist(errors.Cause(err)) {
return "", err
Expand Down
Loading
Loading