diff --git a/chart/templates/_helper.tpl b/chart/templates/_helper.tpl index fbc4ec9c6..503eb4f83 100644 --- a/chart/templates/_helper.tpl +++ b/chart/templates/_helper.tpl @@ -17,7 +17,7 @@ {{- end -}} {{- define "vcluster.name" -}} -{{- if regexMatch "^[0-9]+$" .Release.Name -}} +{{- if regexMatch "^[0-9]+.*$" .Release.Name -}} {{- printf "vc-%s" .Release.Name -}} {{- else -}} {{- printf "%s" .Release.Name }} diff --git a/chart/tests/headless-service_test.yaml b/chart/tests/headless-service_test.yaml index 290c72626..ef0740617 100644 --- a/chart/tests/headless-service_test.yaml +++ b/chart/tests/headless-service_test.yaml @@ -71,6 +71,16 @@ tests: path: metadata.name value: vc-1234-headless + - it: should prepend vc to service name when digits prefix is given + release: + name: 1234-foo + asserts: + - hasDocuments: + count: 1 + - equal: + path: metadata.name + value: vc-1234-foo-headless + - it: embedded-etcd set: controlPlane: diff --git a/chart/tests/service_test.yaml b/chart/tests/service_test.yaml index b67bad554..d0cad79bf 100644 --- a/chart/tests/service_test.yaml +++ b/chart/tests/service_test.yaml @@ -103,6 +103,16 @@ tests: path: metadata.name value: vc-1234 + - it: should prepend vc to service name when digits prefix is given + release: + name: 1234-foo + asserts: + - hasDocuments: + count: 1 + - equal: + path: metadata.name + value: vc-1234-foo + - it: isolated control plane release: name: my-release diff --git a/go.mod b/go.mod index 042d762eb..9a5a6da34 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/hashicorp/go-hclog v0.14.1 github.com/hashicorp/go-plugin v1.6.0 github.com/hashicorp/golang-lru/v2 v2.0.2 + github.com/hashicorp/yamux v0.1.1 github.com/invopop/jsonschema v0.12.0 github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8 @@ -25,7 +26,9 @@ require ( github.com/loft-sh/utils v0.0.29 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d github.com/mitchellh/go-homedir v1.1.0 + github.com/mitchellh/go-testing-interface v1.0.0 github.com/moby/term v0.5.0 + github.com/oklog/run v1.0.0 github.com/olekukonko/tablewriter v0.0.5 github.com/onsi/ginkgo/v2 v2.19.0 github.com/onsi/gomega v1.33.1 @@ -90,14 +93,11 @@ require ( github.com/google/cel-go v0.21.0 // indirect github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect - github.com/hashicorp/yamux v0.1.1 // indirect github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect github.com/klauspost/compress v1.17.10 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/loft-sh/apiserver v0.0.0-20240607231110-634aeeab2b36 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/mitchellh/go-testing-interface v1.0.0 // indirect - github.com/oklog/run v1.0.0 // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/otiai10/copy v1.11.0 // indirect @@ -141,7 +141,7 @@ require ( github.com/go-openapi/swag v0.23.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/golang/protobuf v1.5.4 // indirect + github.com/golang/protobuf v1.5.4 github.com/google/btree v1.1.2 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/go-github/v30 v30.1.0 // indirect diff --git a/pkg/cli/find/find.go b/pkg/cli/find/find.go index a533fa249..50707018b 100644 --- a/pkg/cli/find/find.go +++ b/pkg/cli/find/find.go @@ -10,6 +10,7 @@ import ( "github.com/loft-sh/log" "github.com/loft-sh/log/survey" "github.com/loft-sh/log/terminal" + "github.com/loft-sh/vcluster/pkg/config" "github.com/loft-sh/vcluster/pkg/platform" "github.com/loft-sh/vcluster/pkg/platform/sleepmode" "sigs.k8s.io/controller-runtime/pkg/client" @@ -27,8 +28,6 @@ import ( const VirtualClusterSelector = "app=vcluster" -var digitsOnlyRegex = regexp.MustCompile("^[0-9]+$") - type VCluster struct { ClientFactory clientcmd.ClientConfig `json:"-"` Created metav1.Time @@ -453,7 +452,7 @@ func getService(ctx context.Context, client *kubernetes.Clientset, kubeClientCon defer cancel() var svcName string - if digitsOnlyRegex.MatchString(name) { + if config.DigitsPrefixRegex.MatchString(name) { svcName = fmt.Sprintf("vc-%s", name) } else { svcName = name diff --git a/pkg/config/config.go b/pkg/config/config.go index 9972d9277..e21cb79fe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,6 +1,7 @@ package config import ( + "regexp" "strings" "github.com/loft-sh/vcluster/config" @@ -17,6 +18,9 @@ const ( DefaultHostsRewriteImage = "library/alpine:3.20" ) +// NOTE: This has to be in sync with the regex used in ./chart/templates/_helper.tpl +var DigitsPrefixRegex = regexp.MustCompile("^[0-9]+.*$") + // VirtualClusterConfig wraps the config and adds extra info such as name, serviceName and targetNamespace type VirtualClusterConfig struct { // Holds the vCluster config diff --git a/pkg/config/parse.go b/pkg/config/parse.go index a4adbc7c7..0e43fdd6d 100644 --- a/pkg/config/parse.go +++ b/pkg/config/parse.go @@ -3,7 +3,6 @@ package config import ( "fmt" "os" - "regexp" "github.com/loft-sh/vcluster/config" "github.com/loft-sh/vcluster/pkg/strvals" @@ -11,8 +10,6 @@ import ( "sigs.k8s.io/yaml" ) -var digitsOnlyRegex = regexp.MustCompile("^[0-9]+$") - func ParseConfig(path, name string, setValues []string) (*VirtualClusterConfig, error) { // check if name is empty if name == "" { @@ -42,7 +39,7 @@ func ParseConfig(path, name string, setValues []string) (*VirtualClusterConfig, // build config var svcName string - if digitsOnlyRegex.MatchString(name) { + if DigitsPrefixRegex.MatchString(name) { svcName = fmt.Sprintf("vc-%s", name) } else { svcName = name