Skip to content

Commit

Permalink
Make prefix conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed Sep 25, 2024
1 parent d932071 commit f93eca5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
6 changes: 5 additions & 1 deletion chart/templates/_helper.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
{{- end -}}

{{- define "vcluster.name" -}}
{{- printf "vc-%s" .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- if regexMatch "^[0-9]+$" .Release.Name -}}
{{- printf "vc-%s" .Release.Name -}}
{{- else -}}
{{- printf "%s" .Release.Name }}
{{- end -}}
{{- end -}}
12 changes: 11 additions & 1 deletion chart/tests/headless-service_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ tests:
count: 1
- equal:
path: metadata.name
value: vc-my-release-headless
value: my-release-headless
- equal:
path: metadata.namespace
value: my-namespace

- it: should prepend vc to service name when only digits are given
release:
name: 1234
asserts:
- hasDocuments:
count: 1
- equal:
path: metadata.name
value: vc-1234-headless

- it: embedded-etcd
set:
controlPlane:
Expand Down
12 changes: 11 additions & 1 deletion chart/tests/service_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ tests:
count: 1
- equal:
path: metadata.name
value: vc-my-release
value: my-release
- equal:
path: metadata.namespace
value: my-namespace
Expand All @@ -93,6 +93,16 @@ tests:
path: spec.ports
count: 2

- it: should prepend vc to service name when only digits are given
release:
name: 1234
asserts:
- hasDocuments:
count: 1
- equal:
path: metadata.name
value: vc-1234

- it: isolated control plane
release:
name: my-release
Expand Down
4 changes: 2 additions & 2 deletions chart/tests/statefulset_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ tests:
value: StatefulSet
- equal:
path: spec.serviceName
value: vc-my-release-headless
value: my-release-headless
- contains:
path: spec.volumeClaimTemplates
content:
Expand All @@ -452,7 +452,7 @@ tests:
value: StatefulSet
- equal:
path: spec.serviceName
value: vc-my-release-headless
value: my-release-headless
- lengthEqual:
path: spec.volumeClaimTemplates
count: 1
Expand Down
13 changes: 11 additions & 2 deletions pkg/cli/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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 @@ -446,14 +448,21 @@ func getService(ctx context.Context, client *kubernetes.Clientset, kubeClientCon
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()

service, err := client.CoreV1().Services(namespace).Get(ctx, fmt.Sprintf("vc-%s", name), metav1.GetOptions{})
var svcName string
if digitsOnlyRegex.MatchString(name) {
svcName = fmt.Sprintf("vc-%s", name)
} else {
svcName = name
}

service, err := client.CoreV1().Services(namespace).Get(ctx, svcName, metav1.GetOptions{})
if err != nil {
if kerrors.IsForbidden(err) {
// try the current namespace instead
if namespace, err = getAccessibleNS(kubeClientConfig); err != nil {
return nil, err
}
return client.CoreV1().Services(namespace).Get(ctx, fmt.Sprintf("vc-%s", name), metav1.GetOptions{})
return client.CoreV1().Services(namespace).Get(ctx, svcName, metav1.GetOptions{})
}
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ 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 @@ -37,10 +40,18 @@ func ParseConfig(path, name string, setValues []string) (*VirtualClusterConfig,
}

// build config

var svcName string
if digitsOnlyRegex.MatchString(name) {
svcName = fmt.Sprintf("vc-%s", name)
} else {
svcName = name
}

retConfig := &VirtualClusterConfig{
Config: *rawConfig,
Name: name,
ControlPlaneService: fmt.Sprintf("vc-%s", name),
ControlPlaneService: svcName,
}

// validate config
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/k8sdefaultendpoint/k8sdefaultendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var _ = ginkgo.Describe("map default/kubernetes endpoint to physical vcluster en
ctx := f.Context

waitErr := wait.PollUntilContextTimeout(ctx, time.Millisecond*500, framework.PollTimeout*2, true, func(ctx context.Context) (done bool, err error) {
hostClusterEndpoint, err := f.HostClient.CoreV1().Endpoints(f.VclusterNamespace).Get(ctx, "vc-vcluster", v1.GetOptions{})
hostClusterEndpoint, err := f.HostClient.CoreV1().Endpoints(f.VclusterNamespace).Get(ctx, "vcluster", v1.GetOptions{})
if err != nil {
return false, err
}
Expand Down

0 comments on commit f93eca5

Please sign in to comment.