Skip to content

Commit

Permalink
roachtest: remove duplication of tenant-certs directory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
renatolabs committed Mar 6, 2024
1 parent d649bd9 commit db26405
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions pkg/roachprod/install/cluster_synced.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, " && ")
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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, " "),
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit db26405

Please sign in to comment.