Skip to content

Commit

Permalink
Merge pull request docker#2456 from thaJeztah/rm_k8s_apiserver
Browse files Browse the repository at this point in the history
driver/kubernetes/util: remove k8s.io/apiserver dependency
  • Loading branch information
AkihiroSuda authored May 14, 2024
2 parents c4db5b2 + 946e0a5 commit afcb609
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 392 deletions.
32 changes: 30 additions & 2 deletions driver/kubernetes/util/generate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package util

import (
"crypto/rand"
"encoding/hex"
"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) {
Expand All @@ -15,7 +16,7 @@ func GenerateNodeName(builderName string, txn *store.Txn) (string, error) {
if err != nil {
return "", err
}
return names.SimpleNameGenerator.GenerateName(fmt.Sprintf("buildkit-%s-", u)), nil
return generateName(fmt.Sprintf("buildkit-%s-", u)), nil
}

ng, err := txn.NodeGroupByName(builderName)
Expand Down Expand Up @@ -45,3 +46,30 @@ func GenerateNodeName(builderName string, txn *store.Txn) (string, error) {

return "", errors.Errorf("failed to generate random node name")
}

const (
maxNameLength = 63
randomLength = 5
maxGeneratedNameLength = maxNameLength - randomLength
)

// 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]
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ require (
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/apiserver v0.29.2
k8s.io/client-go v0.29.2
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,6 @@ k8s.io/api v0.29.2 h1:hBC7B9+MU+ptchxEqTNW2DkUosJpp1P+Wn6YncZ474A=
k8s.io/api v0.29.2/go.mod h1:sdIaaKuU7P44aoyyLlikSLayT6Vb7bvJNCX105xZXY0=
k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8=
k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
k8s.io/apiserver v0.29.2 h1:+Z9S0dSNr+CjnVXQePG8TcBWHr3Q7BmAr7NraHvsMiQ=
k8s.io/apiserver v0.29.2/go.mod h1:B0LieKVoyU7ykQvPFm7XSdIHaCHSzCzQWPFa5bqbeMQ=
k8s.io/client-go v0.29.2 h1:FEg85el1TeZp+/vYJM7hkDlSTFZ+c5nnK44DJ4FyoRg=
k8s.io/client-go v0.29.2/go.mod h1:knlvFZE58VpqbQpJNbCbctTVXcd35mMyAAwBdpt4jrA=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
Expand Down
127 changes: 0 additions & 127 deletions vendor/k8s.io/apimachinery/pkg/util/rand/rand.go

This file was deleted.

Loading

0 comments on commit afcb609

Please sign in to comment.