Skip to content

Commit

Permalink
clustermesh enable: Set externalWorkloads.enabled to true
Browse files Browse the repository at this point in the history
Set externalWorkloads.enabled to true in "clustermesh enable" command so
that clustermesh-apiserver-client-cert secret gets created. This is the
certificate that's supposed to be used by external workloads.

The certificate name is clustermesh-apiserver-external-workload-cert in
classic mode for some reason. Add getExternalWorkloadCertName() function
that returns the correct cert name depending on which mode the CLI is
in.

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed May 18, 2023
1 parent 69c12ad commit 657ee65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
13 changes: 7 additions & 6 deletions clustermesh/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (k *K8sClusterMesh) createClusterMeshClientCertificate(ctx context.Context)
}

func (k *K8sClusterMesh) createClusterMeshExternalWorkloadCertificate(ctx context.Context) error {
certName := getExternalWorkloadCertName()
certReq := &csr.CertificateRequest{
Names: []csr.Name{{C: "US", ST: "San Francisco", L: "CA"}},
KeyRequest: csr.NewKeyRequest(),
Expand All @@ -147,16 +148,16 @@ func (k *K8sClusterMesh) createClusterMeshExternalWorkloadCertificate(ctx contex
signConf := &config.Signing{
Default: &config.SigningProfile{Expiry: 5 * 365 * 24 * time.Hour},
Profiles: map[string]*config.SigningProfile{
defaults.ClusterMeshExternalWorkloadSecretName: {
certName: {
Expiry: 5 * 365 * 24 * time.Hour,
Usage: []string{"signing", "key encipherment", "server auth", "client auth"},
},
},
}

cert, key, err := k.certManager.GenerateCertificate(defaults.ClusterMeshExternalWorkloadSecretName, certReq, signConf)
cert, key, err := k.certManager.GenerateCertificate(certName, certReq, signConf)
if err != nil {
return fmt.Errorf("unable to generate certificate %s: %w", defaults.ClusterMeshExternalWorkloadSecretName, err)
return fmt.Errorf("unable to generate certificate %s: %w", certName, err)
}

data := map[string][]byte{
Expand All @@ -165,9 +166,9 @@ func (k *K8sClusterMesh) createClusterMeshExternalWorkloadCertificate(ctx contex
defaults.CASecretCertName: k.certManager.CACertBytes(),
}

_, err = k.client.CreateSecret(ctx, k.params.Namespace, k8s.NewTLSSecret(defaults.ClusterMeshExternalWorkloadSecretName, k.params.Namespace, data), metav1.CreateOptions{})
_, err = k.client.CreateSecret(ctx, k.params.Namespace, k8s.NewTLSSecret(certName, k.params.Namespace, data), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create secret %s/%s: %w", k.params.Namespace, defaults.ClusterMeshExternalWorkloadSecretName, err)
return fmt.Errorf("unable to create secret %s/%s: %w", k.params.Namespace, certName, err)
}

return nil
Expand All @@ -178,7 +179,7 @@ func (k *K8sClusterMesh) deleteCertificates(ctx context.Context) error {
k.client.DeleteSecret(ctx, k.params.Namespace, defaults.ClusterMeshServerSecretName, metav1.DeleteOptions{})
k.client.DeleteSecret(ctx, k.params.Namespace, defaults.ClusterMeshAdminSecretName, metav1.DeleteOptions{})
k.client.DeleteSecret(ctx, k.params.Namespace, defaults.ClusterMeshRemoteSecretName, metav1.DeleteOptions{})
k.client.DeleteSecret(ctx, k.params.Namespace, defaults.ClusterMeshExternalWorkloadSecretName, metav1.DeleteOptions{})
k.client.DeleteSecret(ctx, k.params.Namespace, getExternalWorkloadCertName(), metav1.DeleteOptions{})
k.client.DeleteSecret(ctx, k.params.Namespace, defaults.ClusterMeshClientSecretName, metav1.DeleteOptions{})
return nil
}
Expand Down
11 changes: 10 additions & 1 deletion clustermesh/clustermesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,13 @@ func getDeprecatedName(secretName string) string {
}
}

func getExternalWorkloadCertName() string {
if utils.IsInHelmMode() {
return defaults.ClusterMeshClientSecretName
}
return defaults.ClusterMeshExternalWorkloadSecretName
}

// getDeprecatedSecret attempts to retrieve a secret using one or more deprecated names
// There are now multiple "layers" of deprecated secret names, so we call this function recursively if needed
func (k *K8sClusterMesh) getDeprecatedSecret(ctx context.Context, client k8sClusterMeshImplementation, secretName string, defaultName string) (*corev1.Secret, error) {
Expand Down Expand Up @@ -760,7 +767,7 @@ func (k *K8sClusterMesh) extractAccessInformation(ctx context.Context, client k8
// ExternalWorkload secret is created by 'clustermesh enable' command, but it isn't created by Helm. We should try to load this secret only when needed
var externalWorkloadKey, externalWorkloadCert []byte
if getExternalWorkLoadSecret {
externalWorkloadSecret, err := k.getSecret(ctx, client, defaults.ClusterMeshExternalWorkloadSecretName)
externalWorkloadSecret, err := k.getSecret(ctx, client, getExternalWorkloadCertName())
if err != nil {
return nil, fmt.Errorf("unable to get external workload secret to access clustermesh service")
}
Expand Down Expand Up @@ -1776,6 +1783,7 @@ func EnableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Parameter
helmStrValues := []string{
"clustermesh.useAPIServer=true",
fmt.Sprintf("clustermesh.apiserver.service.type=%s", params.ServiceType),
"externalWorkloads.enabled=true",
}
vals, err := helm.ParseVals(helmStrValues)
if err != nil {
Expand All @@ -1795,6 +1803,7 @@ func EnableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Parameter
func DisableWithHelm(ctx context.Context, k8sClient *k8s.Client, params Parameters) error {
helmStrValues := []string{
"clustermesh.useAPIServer=false",
"externalWorkloads.enabled=false",
}
vals, err := helm.ParseVals(helmStrValues)
if err != nil {
Expand Down

0 comments on commit 657ee65

Please sign in to comment.