Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure kind cluster has RFC1123 compliant name #20627

Merged
merged 1 commit into from
Sep 1, 2020
Merged
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
2 changes: 1 addition & 1 deletion dev-tools/mage/kubernetes/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {
return nil
}

clusterName := kubernetesPodName()
clusterName := kubernetesClusterName()
stdOut := ioutil.Discard
stdErr := ioutil.Discard
if mg.Verbose() {
Expand Down
27 changes: 22 additions & 5 deletions dev-tools/mage/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -132,7 +133,7 @@ func (d *KubernetesIntegrationTester) Test(dir string, mageTarget string, env ma

destDir := filepath.Join("/go/src", repo.CanonicalRootImportPath)
workDir := filepath.Join(destDir, repo.SubDir)
remote, err := NewKubeRemote(kubeConfig, "default", kubernetesPodName(), workDir, destDir, repo.RootDir)
remote, err := NewKubeRemote(kubeConfig, "default", kubernetesClusterName(), workDir, destDir, repo.RootDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,8 +173,8 @@ func waitKubeStateMetricsReadiness(env map[string]string, stdOut, stdErr io.Writ
return nil
}

// kubernetesPodName returns the pod name to use with kubernetes.
func kubernetesPodName() string {
// kubernetesClusterName generates a name for the Kubernetes cluster.
func kubernetesClusterName() string {
commit, err := mage.CommitHash()
if err != nil {
panic(errors.Wrap(err, "failed to construct kind cluster name"))
Expand All @@ -183,13 +184,29 @@ func kubernetesPodName() string {
if err != nil {
panic(errors.Wrap(err, "failed to construct kind cluster name"))
}
version = strings.NewReplacer(".", "_").Replace(version)
version = strings.NewReplacer(".", "-").Replace(version)

clusterName := "{{.BeatName}}_{{.Version}}_{{.ShortCommit}}-{{.StackEnvironment}}"
clusterName := "{{.BeatName}}-{{.Version}}-{{.ShortCommit}}-{{.StackEnvironment}}"
clusterName = mage.MustExpand(clusterName, map[string]interface{}{
"StackEnvironment": mage.StackEnvironment,
"ShortCommit": commit[:10],
"Version": version,
})

// The cluster name may be used as a component of Kubernetes resource names.
// kind does this, for example.
//
// Since Kubernetes resources are required to have names that are valid DNS
// names, we should ensure that the cluster name also meets this criterion.
subDomainPattern := `^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$`
// Note that underscores, in particular, are not permitted.
matched, err := regexp.MatchString(subDomainPattern, clusterName)
if err != nil {
panic(errors.Wrap(err, "error while validating kind cluster name"))
}
if !matched {
panic("constructed invalid kind cluster name")
}

return clusterName
}