Skip to content

Commit

Permalink
Make it possible to create vcluster with numbers only name
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed Sep 24, 2024
1 parent d788f21 commit 586115d
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 27 deletions.
4 changes: 4 additions & 0 deletions chart/templates/_helper.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
{{ .repository }}:{{ .tag }}
{{- end -}}
{{- end -}}

{{- define "vcluster.name" -}}
{{- printf "vc-%s" .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
2 changes: 1 addition & 1 deletion chart/templates/headless-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-headless
name: {{ template "vcluster.name" . }}-headless
namespace: {{ .Release.Namespace }}
labels:
app: vcluster
Expand Down
4 changes: 2 additions & 2 deletions chart/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}
name: {{ template "vcluster.name" . }}
namespace: {{ .Release.Namespace }}
labels:
app: vcluster
Expand Down Expand Up @@ -41,6 +41,6 @@ spec:
{{- if and (not .Values.controlPlane.service.spec.selector) (not .Values.experimental.isolatedControlPlane.headless) }}
selector:
app: vcluster
release: {{ .Release.Name }}
release: {{ .Release.Name | quote }}
{{- end }}
{{- end }}
10 changes: 5 additions & 5 deletions chart/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: apps/v1
kind: {{ include "vcluster.kind" . }}
metadata:
name: {{ .Release.Name }}
name: {{ .Release.Name | quote }}
namespace: {{ .Release.Namespace }}
labels:
app: vcluster
Expand All @@ -21,13 +21,13 @@ spec:
selector:
matchLabels:
app: vcluster
release: {{ .Release.Name }}
release: {{ .Release.Name | quote }}
{{- if eq (include "vcluster.kind" .) "StatefulSet" }}
{{- if ge (int .Capabilities.KubeVersion.Minor) 27 }}
persistentVolumeClaimRetentionPolicy:
whenDeleted: {{ .Values.controlPlane.statefulSet.persistence.volumeClaim.retentionPolicy }}
{{- end }}
serviceName: {{ .Release.Name }}-headless
serviceName: {{ template "vcluster.name" . }}-headless
podManagementPolicy: {{ .Values.controlPlane.statefulSet.scheduling.podManagementPolicy }}
{{ include "vcluster.persistence" . | indent 2 }}
{{- else }}
Expand All @@ -51,7 +51,7 @@ spec:
{{- end }}
labels:
app: vcluster
release: {{ .Release.Name }}
release: {{ .Release.Name | quote }}
{{- if .Values.controlPlane.statefulSet.pods.labels }}
{{ toYaml .Values.controlPlane.statefulSet.pods.labels | indent 8 }}
{{- end }}
Expand Down Expand Up @@ -87,7 +87,7 @@ spec:
dnsPolicy: {{ .Values.controlPlane.statefulSet.dnsPolicy }}
{{- end }}
{{- if .Values.controlPlane.statefulSet.dnsConfig }}
dnsConfig:
dnsConfig:
{{ toYaml .Values.controlPlane.statefulSet.dnsConfig | indent 8 }}
{{- end }}
{{- if .Values.controlPlane.advanced.serviceAccount.name }}
Expand Down
2 changes: 1 addition & 1 deletion chart/tests/headless-service_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ tests:
count: 1
- equal:
path: metadata.name
value: my-release-headless
value: vc-my-release-headless
- equal:
path: metadata.namespace
value: my-namespace
Expand Down
2 changes: 1 addition & 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: my-release
value: vc-my-release
- equal:
path: metadata.namespace
value: my-namespace
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: my-release-headless
value: vc-my-release-headless
- contains:
path: spec.volumeClaimTemplates
content:
Expand All @@ -452,7 +452,7 @@ tests:
value: StatefulSet
- equal:
path: spec.serviceName
value: my-release-headless
value: vc-my-release-headless
- lengthEqual:
path: spec.volumeClaimTemplates
count: 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/certs/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func EnsureCerts(
if options.Experimental.SyncSettings.SetOwner {
// options.ServiceName gets rewritten to the workload service name so we use options.Name as the helm chart
// directly uses the release name for the service name
controlPlaneService, err := currentNamespaceClient.CoreV1().Services(currentNamespace).Get(ctx, options.Name, metav1.GetOptions{})
controlPlaneService, err := currentNamespaceClient.CoreV1().Services(currentNamespace).Get(ctx, options.ControlPlaneService, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("get vcluster service: %w", err)
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (cmd *connectHelm) connect(ctx context.Context, vCluster *find.VCluster, co
}

// retrieve vcluster kube config
kubeConfig, err := cmd.getVClusterKubeConfig(ctx, vCluster.Name, command)
kubeConfig, err := cmd.getVClusterKubeConfig(ctx, vCluster, command)
if err != nil {
return err
}
Expand Down Expand Up @@ -272,15 +272,15 @@ func (cmd *connectHelm) prepare(ctx context.Context, vCluster *find.VCluster) er
return nil
}

func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName string, command []string) (*clientcmdapi.Config, error) {
func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vcluster *find.VCluster, command []string) (*clientcmdapi.Config, error) {
var err error
podName := cmd.PodName
if podName == "" {
waitErr := wait.PollUntilContextTimeout(ctx, time.Second, time.Second*30, true, func(ctx context.Context) (bool, error) {
// get vcluster pod name
var pods *corev1.PodList
pods, err = cmd.kubeClient.CoreV1().Pods(cmd.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: "app=vcluster,release=" + vclusterName,
LabelSelector: "app=vcluster,release=" + vcluster.Name,
})
if err != nil {
return false, err
Expand Down Expand Up @@ -310,7 +310,7 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
cmd.Log.Debugf("Successfully found vCluster pod for connecting %s", podName)

// get the kube config from the Secret
kubeConfig, err := clihelper.GetKubeConfig(ctx, cmd.kubeClient, vclusterName, cmd.Namespace, cmd.Log)
kubeConfig, err := clihelper.GetKubeConfig(ctx, cmd.kubeClient, vcluster.Name, cmd.Namespace, cmd.Log)
if err != nil {
return nil, fmt.Errorf("failed to parse kube config: %w", err)
}
Expand All @@ -322,14 +322,14 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
}

// exchange context name in virtual kube config
err = cmd.exchangeContextName(kubeConfig, vclusterName)
err = cmd.exchangeContextName(kubeConfig, vcluster.Name)
if err != nil {
return nil, err
}

// check if the vcluster is exposed and set server
if vclusterName != "" && cmd.Server == "" && len(command) == 0 {
err = cmd.setServerIfExposed(ctx, vclusterName, kubeConfig)
if vcluster.Name != "" && cmd.Server == "" && len(command) == 0 {
err = cmd.setServerIfExposed(ctx, vcluster, kubeConfig)
if err != nil {
return nil, err
}
Expand All @@ -338,7 +338,7 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
if cmd.Server == "" && cmd.BackgroundProxy {
if localkubernetes.IsDockerInstalledAndUpAndRunning() {
// start background container
server, err := localkubernetes.CreateBackgroundProxyContainer(ctx, vclusterName, cmd.Namespace, cmd.kubeClientConfig, kubeConfig, cmd.LocalPort, cmd.Log)
server, err := localkubernetes.CreateBackgroundProxyContainer(ctx, vcluster.Name, cmd.Namespace, cmd.kubeClientConfig, kubeConfig, cmd.LocalPort, cmd.Log)
if err != nil {
cmd.Log.Warnf("Error exposing local vcluster, will fallback to port-forwarding: %v", err)
cmd.BackgroundProxy = false
Expand Down Expand Up @@ -419,12 +419,12 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
return kubeConfig, nil
}

func (cmd *connectHelm) setServerIfExposed(ctx context.Context, vClusterName string, vClusterConfig *clientcmdapi.Config) error {
func (cmd *connectHelm) setServerIfExposed(ctx context.Context, vcluster *find.VCluster, vClusterConfig *clientcmdapi.Config) error {
printedWaiting := false
err := wait.PollUntilContextTimeout(ctx, time.Second*2, time.Minute*5, true, func(ctx context.Context) (done bool, err error) {
// first check for load balancer service, look for the other service if it's not there
loadBalancerMissing := false
service, err := cmd.kubeClient.CoreV1().Services(cmd.Namespace).Get(ctx, vClusterName, metav1.GetOptions{})
service, err := cmd.kubeClient.CoreV1().Services(cmd.Namespace).Get(ctx, vcluster.ServiceName, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
loadBalancerMissing = true
Expand All @@ -433,7 +433,7 @@ func (cmd *connectHelm) setServerIfExposed(ctx context.Context, vClusterName str
}
}
if loadBalancerMissing {
service, err = cmd.kubeClient.CoreV1().Services(cmd.Namespace).Get(ctx, vClusterName, metav1.GetOptions{})
service, err = cmd.kubeClient.CoreV1().Services(cmd.Namespace).Get(ctx, vcluster.ServiceName, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
return true, nil
} else if err != nil {
Expand All @@ -446,7 +446,7 @@ func (cmd *connectHelm) setServerIfExposed(ctx context.Context, vClusterName str

// not a load balancer? Then don't wait
if service.Spec.Type == corev1.ServiceTypeNodePort {
server, err := localkubernetes.ExposeLocal(ctx, vClusterName, cmd.Namespace, &cmd.rawConfig, vClusterConfig, service, cmd.LocalPort, cmd.Log)
server, err := localkubernetes.ExposeLocal(ctx, vcluster.Name, cmd.Namespace, &cmd.rawConfig, vClusterConfig, service, cmd.LocalPort, cmd.Log)
if err != nil {
cmd.Log.Warnf("Error exposing local vcluster, will fallback to port-forwarding: %v", err)
}
Expand Down Expand Up @@ -476,7 +476,7 @@ func (cmd *connectHelm) setServerIfExposed(ctx context.Context, vClusterName str
return false, nil
}

cmd.Log.Infof("Using vcluster %s load balancer endpoint: %s", vClusterName, cmd.Server)
cmd.Log.Infof("Using vcluster %s load balancer endpoint: %s", vcluster.Name, cmd.Server)
return true, nil
})
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/cli/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type VCluster struct {
Created metav1.Time
Name string
Namespace string
ServiceName string
Annotations map[string]string
Labels map[string]string
Status Status
Expand Down Expand Up @@ -399,6 +400,11 @@ func getVCluster(ctx context.Context, object client.Object, context, release str
status = string(StatusUnknown)
}

service, err := getService(ctx, client, kubeClientConfig, namespace, release)
if err != nil {
return VCluster{}, err
}

switch vclusterObject := object.(type) {
case *appsv1.StatefulSet:
for _, container := range vclusterObject.Spec.Template.Spec.Containers {
Expand All @@ -425,6 +431,7 @@ func getVCluster(ctx context.Context, object client.Object, context, release str
return VCluster{
Name: release,
Namespace: namespace,
ServiceName: service.Name,
Annotations: object.GetAnnotations(),
Labels: object.GetLabels(),
Status: Status(status),
Expand All @@ -435,6 +442,25 @@ func getVCluster(ctx context.Context, object client.Object, context, release str
}, nil
}

func getService(ctx context.Context, client *kubernetes.Clientset, kubeClientConfig clientcmd.ClientConfig, namespace, name string) (*corev1.Service, error) {
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{})
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 nil, err
}

return service, nil
}

func getPods(ctx context.Context, client *kubernetes.Clientset, kubeClientConfig clientcmd.ClientConfig, namespace, podSelector string) (*corev1.PodList, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ParseConfig(path, name string, setValues []string) (*VirtualClusterConfig,
retConfig := &VirtualClusterConfig{
Config: *rawConfig,
Name: name,
ControlPlaneService: name,
ControlPlaneService: fmt.Sprintf("vc-%s", name),
}

// validate config
Expand Down

0 comments on commit 586115d

Please sign in to comment.