Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MariaDbBackupService {

await namespaceService.createNamespaceIfNotExists(backupNamespace);

const jobName = KubeObjectNameUtils.addRandomSuffix(`backup-mariadb-${app.id}`);
const jobName = KubeObjectNameUtils.toDatabaseBackupJobName('mariadb', app.id);
console.log(`Creating MariaDB/MySQL backup job with name: ${jobName}`);

const dbCredentials = AppTemplateUtils.getDatabaseModelFromApp(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MongoDbBackupService {

await namespaceService.createNamespaceIfNotExists(backupNamespace);

const jobName = KubeObjectNameUtils.addRandomSuffix(`backup-mongodb-${app.id}`);
const jobName = KubeObjectNameUtils.toDatabaseBackupJobName('mongodb', app.id);
console.log(`Creating MongoDB backup job with name: ${jobName}`);

const dbCredentials = AppTemplateUtils.getDatabaseModelFromApp(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PostgresBackupService {

await namespaceService.createNamespaceIfNotExists(backupNamespace);

const jobName = KubeObjectNameUtils.addRandomSuffix(`backup-postgres-${app.id}`);
const jobName = KubeObjectNameUtils.toDatabaseBackupJobName('postgres', app.id);
console.log(`Creating PostgreSQL backup job with name: ${jobName}`);

const dbCredentials = AppTemplateUtils.getDatabaseModelFromApp(app);
Expand Down
11 changes: 11 additions & 0 deletions src/server/utils/kube-object-name.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as crypto from 'crypto';
export class KubeObjectNameUtils {

private static readonly MAX_OBJECT_NAME_LENGTH = 30; // in Kubernetes, the maximum length of an object name is 63 characters
private static readonly MAX_LABEL_VALUE_LENGTH = 63;
private static readonly RANDOM_SUFFIX_LENGTH = 9; // "-" + 8 hexadecimal characters

static toSnakeCase(str: string): string {
if (!str) {
Expand Down Expand Up @@ -44,6 +46,15 @@ export class KubeObjectNameUtils {
return `build-${appId}`;
}

static toDatabaseBackupJobName(database: 'postgres' | 'mongodb' | 'mariadb', appId: string): string {
const prefix = `backup-${database}-`;
const maxAppIdLength = KubeObjectNameUtils.MAX_LABEL_VALUE_LENGTH
- prefix.length
- KubeObjectNameUtils.RANDOM_SUFFIX_LENGTH;

return KubeObjectNameUtils.addRandomSuffix(`${prefix}${appId.substring(0, maxAppIdLength)}`);
}

static addRandomSuffix(str: string): string {
// added suffix is 8 characters long
const randomString = crypto.randomBytes(4).toString('hex');
Expand Down
20 changes: 20 additions & 0 deletions src/server/utils/kube-object-name.utils.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ describe('KubeObjectNameUtils', () => {
});
});

describe('toDatabaseBackupJobName', () => {
it.each(['postgres', 'mongodb', 'mariadb'] as const)(
'should keep %s backup job names within the Kubernetes label and container name limit',
(database) => {
const result = KubeObjectNameUtils.toDatabaseBackupJobName(database, 'a'.repeat(100));

expect(result).toMatch(new RegExp(`^backup-${database}-a+-[a-f0-9]{8}$`));
expect(result.length).toBeLessThanOrEqual(63);
}
);

it('should preserve the random suffix when truncating an app ID', () => {
const appId = 'app-ericdaniellwebsitepostgres-b744d884';
const result = KubeObjectNameUtils.toDatabaseBackupJobName('postgres', appId);

expect(result).toMatch(new RegExp(`^backup-postgres-${appId.substring(0, 38)}-[a-f0-9]{8}$`));
expect(result).toHaveLength(63);
});
});

describe('addRandomSuffix', () => {
it('should add a random suffix to the string', () => {
const result = KubeObjectNameUtils.addRandomSuffix('baseString');
Expand Down
Loading