Skip to content

Commit

Permalink
Merge pull request #1673 from crazy-max/fix-k8s-deploy-name
Browse files Browse the repository at this point in the history
k8s: generate node name if not provided
  • Loading branch information
tonistiigi authored Mar 14, 2023
2 parents 6f722da + 527d575 commit 4a73abf
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 4 deletions.
14 changes: 12 additions & 2 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/docker/buildx/builder"
"github.com/docker/buildx/driver"
k8sutil "github.com/docker/buildx/driver/kubernetes/util"
remoteutil "github.com/docker/buildx/driver/remote/util"
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
Expand Down Expand Up @@ -175,12 +176,21 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
if len(args) > 0 {
logrus.Warnf("kubernetes driver does not support endpoint args %q", args[0])
}
// generate node name if not provided to avoid duplicated endpoint
// error: https://github.com/docker/setup-buildx-action/issues/215
nodeName := in.nodeName
if nodeName == "" {
nodeName, err = k8sutil.GenerateNodeName(name, txn)
if err != nil {
return err
}
}
// naming endpoint to make --append works
ep = (&url.URL{
Scheme: driverName,
Path: "/" + in.name,
Path: "/" + name,
RawQuery: (&url.Values{
"deployment": {in.nodeName},
"deployment": {nodeName},
"kubeconfig": {os.Getenv("KUBECONFIG")},
}).Encode(),
}).String()
Expand Down
47 changes: 47 additions & 0 deletions driver/kubernetes/util/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package util

import (
"fmt"

"github.com/docker/buildx/store"
"github.com/google/uuid"
"github.com/pkg/errors"
"k8s.io/apiserver/pkg/storage/names"
)

func GenerateNodeName(builderName string, txn *store.Txn) (string, error) {
randomName := func() (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
}
return names.SimpleNameGenerator.GenerateName(fmt.Sprintf("buildkit-%s-", u)), nil
}

ng, err := txn.NodeGroupByName(builderName)
if err != nil {
return randomName()
}

var name string
for i := 0; i < 6; i++ {
name, err = randomName()
if err != nil {
return "", err
}
exists := func(name string) bool {
for _, n := range ng.Nodes {
if n.Name == name {
return true
}
}
return false
}(name)
if exists {
continue
}
return name, nil
}

return "", errors.Errorf("failed to generate random node name")
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.0
github.com/hashicorp/go-cty-funcs v0.0.0-20200930094925-2721b1e36840
github.com/hashicorp/hcl/v2 v2.8.2
github.com/moby/buildkit v0.11.0-rc3.0.20230216100651-46e4e7e51b01
Expand All @@ -40,6 +41,7 @@ require (
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.25.4
k8s.io/apimachinery v0.25.4
k8s.io/apiserver v0.25.4
k8s.io/client-go v0.25.4
)

Expand Down Expand Up @@ -99,7 +101,6 @@ require (
github.com/google/certificate-transparency-go v1.1.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
Expand Down Expand Up @@ -177,5 +178,6 @@ require (
replace (
k8s.io/api => k8s.io/api v0.22.4
k8s.io/apimachinery => k8s.io/apimachinery v0.22.4
k8s.io/apiserver => k8s.io/apiserver v0.22.4
k8s.io/client-go => k8s.io/client-go v0.22.4
)
171 changes: 171 additions & 0 deletions go.sum

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion hack/test-driver
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ if [ "$DRIVER" != "docker" ]; then
if [ "$firstNode" = "0" ]; then
createFlags="$createFlags --append"
fi
nodeName=""
if [ "$DRIVER" != "kubernetes" ]; then
nodeName="${builderName}-${platform/\//-}"
fi
buildxCmd create ${createFlags} \
--bootstrap \
--name="${builderName}" \
--node="${builderName}-${platform/\//-}" \
--node="${nodeName}" \
--platform="${platform}" \
--driver="${DRIVER}" \
${driverOpt:+"--driver-opt=${driverOpt}"} \
Expand Down
127 changes: 127 additions & 0 deletions vendor/k8s.io/apimachinery/pkg/util/rand/rand.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4a73abf

Please sign in to comment.