Skip to content

Commit

Permalink
Also allow name that starts with digits
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed Oct 28, 2024
1 parent b683fce commit 73fbfc8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion chart/templates/_helper.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
10 changes: 10 additions & 0 deletions chart/tests/headless-service_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions chart/tests/service_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/cli/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"regexp"
"strings"

"github.com/loft-sh/vcluster/config"
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions pkg/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package config
import (
"fmt"
"os"
"regexp"

"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/strvals"
"github.com/pkg/errors"
"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 == "" {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 73fbfc8

Please sign in to comment.