Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: set labels to kubeconfig and cert secrets #2330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/certs/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
"k8s.io/klog/v2"
)

const (
CertSecretLabelAppKey = "app"
CertSecretLabelAppValue = "vcluster"
CertSecretLabelVclusterNameKey = "vcluster-name"
)

func EnsureCerts(
ctx context.Context,
serviceCIDR string,
Expand Down Expand Up @@ -131,6 +137,10 @@ func EnsureCerts(
Name: secretName,
Namespace: currentNamespace,
OwnerReferences: ownerRef,
Labels: map[string]string{
CertSecretLabelAppKey: CertSecretLabelAppValue,
CertSecretLabelVclusterNameKey: options.Name,
},
},
Data: map[string][]byte{},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/setup/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ func WriteKubeConfigToSecret(ctx context.Context, virtualConfig *rest.Config, cu
}

// Use customSyncerConfig for the additional secret if it was modified, else syncerConfig
err = kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, options.ExportKubeConfig.Secret.Name, secretNamespace, customSyncerConfig, options.Experimental.IsolatedControlPlane.KubeConfig != "")
err = kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, options.ExportKubeConfig.Secret.Name, secretNamespace, customSyncerConfig, options.Experimental.IsolatedControlPlane.KubeConfig != "", options.Name)
if err != nil {
return fmt.Errorf("creating %s secret in the %s ns failed: %w", options.ExportKubeConfig.Secret.Name, secretNamespace, err)
}
}

// Write the default secret using syncerConfig, which retains the original Server value
return kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, kubeconfig.GetDefaultSecretName(translate.VClusterName), currentNamespace, syncerConfig, options.Experimental.IsolatedControlPlane.KubeConfig != "")
return kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, kubeconfig.GetDefaultSecretName(translate.VClusterName), currentNamespace, syncerConfig, options.Experimental.IsolatedControlPlane.KubeConfig != "", options.Name)
}

// applyAuthToken sets the provided token in all AuthInfos of the given config
Expand Down
21 changes: 14 additions & 7 deletions pkg/util/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ import (
)

const (
DefaultSecretPrefix = "vc-"
KubeconfigSecretKey = "config"
CADataSecretKey = "certificate-authority"
CertificateSecretKey = "client-certificate"
CertificateKeySecretKey = "client-key"
TokenSecretKey = "token"
DefaultSecretPrefix = "vc-"
KubeconfigSecretKey = "config"
CADataSecretKey = "certificate-authority"
CertificateSecretKey = "client-certificate"
CertificateKeySecretKey = "client-key"
TokenSecretKey = "token"
KubeConfigSecretLabelAppKey = "app"
KubeConfigSecretLabelAppValue = "vcluster"
KubeConfigSecretVclusterNameKey = "vcluster-name"
)

func WriteKubeConfig(ctx context.Context, currentNamespaceClient client.Client, secretName, secretNamespace string, config *clientcmdapi.Config, isRemote bool) error {
func WriteKubeConfig(ctx context.Context, currentNamespaceClient client.Client, secretName, secretNamespace string, config *clientcmdapi.Config, isRemote bool, vClusterName string) error {
out, err := clientcmd.Write(*config)
if err != nil {
return err
Expand Down Expand Up @@ -70,6 +73,10 @@ func WriteKubeConfig(ctx context.Context, currentNamespaceClient client.Client,
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: secretNamespace,
Labels: map[string]string{
KubeConfigSecretLabelAppKey: KubeConfigSecretLabelAppValue,
KubeConfigSecretVclusterNameKey: vClusterName,
},
},
}
result, err := controllerutil.CreateOrPatch(ctx, currentNamespaceClient, kubeConfigSecret, func() error {
Expand Down