Skip to content

Commit

Permalink
Ensure kind cluster has RFC1123 compliant name (elastic#20627)
Browse files Browse the repository at this point in the history
When creating a kind cluster, we must use a cluster name that is a valid
Kubernetes resource name, and by extension, a valid DNS name. If not,
kind cluster provisioning can fail when kind tries to create resources
with invalid names.

For example, if trying to create a cluster called "a_b" (underscores
are not permitted), control-plane provisioning will fail with:

  host 'a_b-control-plane' must be [...] a valid RFC-1123 DNS subdomain

(cherry picked from commit 345e045)
  • Loading branch information
jarpy authored and blakerouse committed Sep 1, 2020
1 parent aa54f5e commit 49d438c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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
}

0 comments on commit 49d438c

Please sign in to comment.