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
44 changes: 28 additions & 16 deletions src/server/lib/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ async function generateTTLLabels({
ttl: boolean;
buildUUID: string;
}): Promise<{ labels: Record<string, string>; logMessage: string }> {
const baseLabels = { 'lfc/uuid': uuid };
const baseLabels: Record<string, string> = {
'lfc/uuid': uuid,
'lfc/type': staticEnv ? 'static' : 'ephemeral',
'app.kubernetes.io/managed-by': 'lifecycle',
};

// Static or TTL disabled - only set enable flag
if (staticEnv || !ttl) {
Expand Down Expand Up @@ -117,16 +121,27 @@ async function generateTTLLabels({
* Generates patch operations for updating TTL labels on existing namespace
*/
async function generateTTLPatch({
uuid,
staticEnv,
ttl,
buildUUID,
}: {
uuid: string;
staticEnv: boolean;
ttl: boolean;
buildUUID: string;
}): Promise<{ patch: any[]; logMessage: string }> {
const basePatch = [
{ op: 'add', path: '/metadata/labels/lfc~1uuid', value: uuid },
{ op: 'add', path: '/metadata/labels/lfc~1type', value: staticEnv ? 'static' : 'ephemeral' },
{ op: 'add', path: '/metadata/labels/app.kubernetes.io~1managed-by', value: 'lifecycle' },
];

// TTL disabled - only update enable flag
if (!ttl) {
return {
patch: [
...basePatch,
{
op: 'add',
path: '/metadata/labels/lfc~1ttl-enable',
Expand All @@ -143,6 +158,7 @@ async function generateTTLPatch({

return {
patch: [
...basePatch,
{
op: 'add',
path: '/metadata/labels/lfc~1ttl-enable',
Expand Down Expand Up @@ -213,22 +229,18 @@ export async function createOrUpdateNamespace({
};

if (await namespaceExists(client, name)) {
// Only update TTL labels if not static env
if (!staticEnv) {
const { patch, logMessage: patchMessage } = await generateTTLPatch({
ttl,
buildUUID,
});
const { patch, logMessage: patchMessage } = await generateTTLPatch({
uuid,
staticEnv,
ttl: staticEnv ? false : ttl,
buildUUID,
});

await client.patchNamespace(name, patch, undefined, undefined, undefined, undefined, undefined, {
headers: { 'Content-Type': 'application/json-patch+json' },
});
getLogger({ namespace: name }).info(`Deploy: updated namespace ${patchMessage}`);
return;
} else {
getLogger({ namespace: name }).info('Deploy: skipped namespace update reason=static');
return;
}
await client.patchNamespace(name, patch, undefined, undefined, undefined, undefined, undefined, {
headers: { 'Content-Type': 'application/json-patch+json' },
});
getLogger({ namespace: name }).info(`Deploy: updated namespace ${patchMessage}`);
return;
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/server/lib/kubernetes/jobFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function createKubernetesJob(config: JobConfig): V1Job {
...labels,
},
annotations: {
'lifecycle.io/triggered-at': new Date().toISOString(),
'lfc/triggered-at': new Date().toISOString(),
...annotations,
},
},
Expand Down Expand Up @@ -145,8 +145,8 @@ export function createBuildJob(config: BuildJobConfig): V1Job {
'build-method': 'native',
},
annotations: {
'lifecycle.io/dockerfile': config.dockerfilePath,
'lifecycle.io/ecr-repo': config.ecrRepo,
'lfc/dockerfile': config.dockerfilePath,
'lfc/ecr-repo': config.ecrRepo,
},
podAnnotations: config.podAnnotations,
initContainers: config.initContainers,
Expand Down
4 changes: 2 additions & 2 deletions src/server/lib/nativeBuild/__tests__/buildkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ describe('buildkitBuild', () => {
expect(fullCommand).toContain('build-method: "native"');

// Check annotations
expect(fullCommand).toContain('lifecycle.io/dockerfile: "Dockerfile"');
expect(fullCommand).toContain('lifecycle.io/ecr-repo: "test-repo"');
expect(fullCommand).toContain('lfc/dockerfile: "Dockerfile"');
expect(fullCommand).toContain('lfc/ecr-repo: "test-repo"');
});
});

Expand Down
8 changes: 3 additions & 5 deletions src/server/lib/nativeBuild/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ describe('nativeBuild/utils', () => {
expect(manifest.metadata.labels['build-method']).toBe('native');

// Check annotations
expect(manifest.metadata.annotations['lifecycle.io/dockerfile']).toBe('Dockerfile');
expect(manifest.metadata.annotations['lifecycle.io/ecr-repo']).toBe(
'123456789.dkr.ecr.us-east-1.amazonaws.com/test-repo'
);
expect(manifest.metadata.annotations['lifecycle.io/triggered-at']).toBeDefined();
expect(manifest.metadata.annotations['lfc/dockerfile']).toBe('Dockerfile');
expect(manifest.metadata.annotations['lfc/ecr-repo']).toBe('123456789.dkr.ecr.us-east-1.amazonaws.com/test-repo');
expect(manifest.metadata.annotations['lfc/triggered-at']).toBeDefined();

// Check spec
expect(manifest.spec.ttlSecondsAfterFinished).toBeUndefined(); // No TTL by default for non-static builds
Expand Down
7 changes: 3 additions & 4 deletions src/server/lib/nativeBuild/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export async function ensureNamespaceExists(namespace: string): Promise<void> {
name: namespace,
labels: {
'app.kubernetes.io/managed-by': 'lifecycle',
'lifecycle.io/type': 'ephemeral',
},
},
});
Expand Down Expand Up @@ -208,9 +207,9 @@ export function getBuildLabels(

export function getBuildAnnotations(dockerfilePath: string, ecrRepo: string): Record<string, string> {
return {
'lifecycle.io/dockerfile': dockerfilePath,
'lifecycle.io/ecr-repo': ecrRepo,
'lifecycle.io/triggered-at': new Date().toISOString(),
'lfc/dockerfile': dockerfilePath,
'lfc/ecr-repo': ecrRepo,
'lfc/triggered-at': new Date().toISOString(),
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/services/ttlCleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ export default class TTLCleanupService extends Service {

const daysExpired = Math.floor((now - expireTime) / (1000 * 60 * 60 * 24));

const buildUUID = labels['lfc/uuid'];
let buildUUID = labels['lfc/uuid'];
if (!buildUUID) {
getLogger().warn(`TTL: namespace missing uuid label namespace=${nsName}`);
continue;
buildUUID = nsName.replace('env-', '');
getLogger().warn(`TTL: namespace missing uuid label, derived from name namespace=${nsName}`);
}

updateLogContext({ buildUuid: buildUUID });
Expand Down
Loading