buildx/store/util.go
CrazyMax b1c5449428
builder: skip name validation for docker context
Although a builder from the store cannot be created unless
it has a valid name, this is not the case for a Docker context.

We should skip name validation when checking a node from the
store and fall back to finding one from Docker context instead.

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-06-14 14:19:30 +02:00

55 lines
1.1 KiB
Go

package store
import (
"os"
"regexp"
"strings"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/pkg/errors"
)
var namePattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_]*$`)
type errInvalidName struct {
error
}
func (e *errInvalidName) Error() string {
return e.error.Error()
}
func (e *errInvalidName) Unwrap() error {
return e.error
}
func IsErrInvalidName(err error) bool {
_, ok := err.(*errInvalidName)
return ok
}
func ValidateName(s string) (string, error) {
if !namePattern.MatchString(s) {
return "", &errInvalidName{
errors.Errorf("invalid name %s, name needs to start with a letter and may not contain symbols, except ._-", s),
}
}
return strings.ToLower(s), nil
}
func GenerateName(txn *Txn) (string, error) {
var name string
for i := 0; i < 6; i++ {
name = namesgenerator.GetRandomName(i)
if _, err := txn.NodeGroupByName(name); err != nil {
if !os.IsNotExist(errors.Cause(err)) {
return "", err
}
} else {
continue
}
return name, nil
}
return "", errors.Errorf("failed to generate random name")
}