From db2640594f8a222e9297dc14bc4c93e522413279 Mon Sep 17 00:00:00 2001 From: Renato Costa Date: Thu, 29 Feb 2024 11:08:20 -0500 Subject: [PATCH] roachtest: remove duplication of tenant-certs directory This introduces `CockroachTenantNodeDir`, reducing duplication of the directory where we keep tenant certificates, serving a purpose similar to `CockroachNodeCertsDir`, where all certificates are ultimately kept. We also add a suffix to the directory based on the virtual cluster ID, avoiding clashes in case several virtual clusters are created. Epic: none Release note: None --- pkg/roachprod/install/cluster_synced.go | 51 +++++++++++++++---------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/pkg/roachprod/install/cluster_synced.go b/pkg/roachprod/install/cluster_synced.go index 7dc842227823..c6c22e420a2e 100644 --- a/pkg/roachprod/install/cluster_synced.go +++ b/pkg/roachprod/install/cluster_synced.go @@ -554,10 +554,10 @@ func (c *SyncedCluster) Wipe(ctx context.Context, l *logger.Logger, preserveCert var cmd string if c.IsLocal() { // Not all shells like brace expansion, so we'll do it here - dirs := []string{"data", "logs"} + dirs := []string{"data*", "logs*"} if !preserveCerts { dirs = append(dirs, fmt.Sprintf("%s*", CockroachNodeCertsDir)) - dirs = append(dirs, "tenant-certs*") + dirs = append(dirs, fmt.Sprintf("%s*", CockroachNodeTenantCertsDir)) } for _, dir := range dirs { cmd += fmt.Sprintf(`rm -fr %s/%s ;`, c.localVMDir(node), dir) @@ -566,10 +566,13 @@ func (c *SyncedCluster) Wipe(ctx context.Context, l *logger.Logger, preserveCert rmCmds := []string{ `sudo find /mnt/data* -maxdepth 1 -type f -exec rm -f {} \;`, `sudo rm -fr /mnt/data*/{auxiliary,local,tmp,cassandra,cockroach,cockroach-temp*,mongo-data}`, - `sudo rm -fr logs`, + `sudo rm -fr logs* data*`, } if !preserveCerts { - rmCmds = append(rmCmds, fmt.Sprintf("sudo rm -fr %s*", CockroachNodeCertsDir), "sudo rm -fr tenant-certs*") + rmCmds = append(rmCmds, + fmt.Sprintf("sudo rm -fr %s*", CockroachNodeCertsDir), + fmt.Sprintf("sudo rm -fr %s*", CockroachNodeTenantCertsDir), + ) } cmd = strings.Join(rmCmds, " && ") @@ -1612,12 +1615,16 @@ fi const ( // CockroachNodeCertsDir is the certs directory that lives // on the cockroach node itself. - CockroachNodeCertsDir = "certs" - certsTarName = "certs.tar" - tenantCertsTarName = "tenant-certs.tar" - tenantCertFile = "client-tenant.%d.crt" + CockroachNodeCertsDir = "certs" + CockroachNodeTenantCertsDir = "tenant-certs" + certsTarName = "certs.tar" + tenantCertFile = "client-tenant.%d.crt" ) +func tenantCertsTarName(virtualClusterID int) string { + return fmt.Sprintf("%s-%d.tar", CockroachNodeTenantCertsDir, virtualClusterID) +} + // DistributeCerts will generate and distribute certificates to all the nodes. func (c *SyncedCluster) DistributeCerts(ctx context.Context, l *logger.Logger) error { if c.checkForCertificates(ctx, l) { @@ -1689,11 +1696,14 @@ func (c *SyncedCluster) DistributeTenantCerts( return err } - if err := hostCluster.createTenantCertBundle(ctx, l, tenantCertsTarName, virtualClusterID, nodeNames); err != nil { + certsTar := tenantCertsTarName(virtualClusterID) + if err := hostCluster.createTenantCertBundle( + ctx, l, tenantCertsTarName(virtualClusterID), virtualClusterID, nodeNames, + ); err != nil { return err } - tarfile, cleanup, err := hostCluster.getFileFromFirstNode(ctx, l, tenantCertsTarName) + tarfile, cleanup, err := hostCluster.getFileFromFirstNode(ctx, l, certsTar) if err != nil { return err } @@ -1722,24 +1732,25 @@ func (c *SyncedCluster) createTenantCertBundle( cmd += fmt.Sprintf(`cd %s ; `, c.localVMDir(1)) } cmd += fmt.Sprintf(` -CERT_DIR=tenant-certs/certs -CA_KEY=%[1]s/ca.key +CERT_DIR=%[1]s-%[5]d/certs +CA_KEY=%[2]s/ca.key rm -fr $CERT_DIR mkdir -p $CERT_DIR -cp %[1]s/ca.crt $CERT_DIR +cp %[2]s/ca.crt $CERT_DIR SHARED_ARGS="--certs-dir=$CERT_DIR --ca-key=$CA_KEY" -VERSION=$(%[2]s version --build-tag) +VERSION=$(%[3]s version --build-tag) VERSION=${VERSION::3} TENANT_SCOPE_OPT="" if [[ $VERSION = v22 ]]; then - TENANT_SCOPE_OPT="--tenant-scope %[4]d" + TENANT_SCOPE_OPT="--tenant-scope %[5]d" fi -%[2]s cert create-node %[3]s $SHARED_ARGS -%[2]s cert create-tenant-client %[4]d %[3]s $SHARED_ARGS -%[2]s cert create-client root $TENANT_SCOPE_OPT $SHARED_ARGS -tar cvf %[5]s $CERT_DIR +%[3]s cert create-node %[4]s $SHARED_ARGS +%[3]s cert create-tenant-client %[5]d %[4]s $SHARED_ARGS +%[3]s cert create-client root $TENANT_SCOPE_OPT $SHARED_ARGS +tar cvf %[6]s $CERT_DIR `, + CockroachNodeTenantCertsDir, CockroachNodeCertsDir, cockroachNodeBinary(c, node), strings.Join(nodeNames, " "), @@ -1801,7 +1812,7 @@ func (c *SyncedCluster) checkForTenantCertificates( if c.IsLocal() { dir = c.localVMDir(1) } - if !c.fileExistsOnFirstNode(ctx, l, filepath.Join(dir, tenantCertsTarName)) { + if !c.fileExistsOnFirstNode(ctx, l, filepath.Join(dir, tenantCertsTarName(virtualClusterID))) { return false } return c.fileExistsOnFirstNode(ctx, l, filepath.Join(c.CertsDir(1), fmt.Sprintf(tenantCertFile, virtualClusterID)))