-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1673 from crazy-max/fix-k8s-deploy-name
k8s: generate node name if not provided
- Loading branch information
Showing
9 changed files
with
626 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.