Skip to content

Commit 6fd252b

Browse files
authored
feat: reconcile deleted yaml services (#198)
* feat: reconcile deleted yaml services * fix: fail incomplete deploy cleanup jobs * fix: skip deleted service reconciliation for service redeploys
1 parent 4af1007 commit 6fd252b

14 files changed

Lines changed: 1516 additions & 65 deletions

File tree

‎src/server/db/migrations/001_seed.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ export async function up(knex: Knex): Promise<any> {
342342
helm json DEFAULT '{}'::json,
343343
"deploymentDependsOn" text[] DEFAULT ARRAY[]::text[],
344344
builder jsonb DEFAULT '{}'::jsonb,
345-
"ecr" varchar(255) default NULL::character varying
345+
"ecr" varchar(255) default NULL::character varying,
346+
source varchar(255) DEFAULT 'db'::character varying,
347+
"reconcileEligible" boolean DEFAULT false,
348+
"resolvedFromRepositoryId" integer
346349
);
347350
ALTER TABLE deployables OWNER TO lifecycle;
348351
`);
@@ -452,7 +455,7 @@ export async function up(knex: Knex): Promise<any> {
452455
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('auroraRestoreSettings', '{"vpcId":"","accountId":"","region":"us-west-2","securityGroupIds":[],"subnetGroupName":"","engine":"aurora-mysql","engineVersion":"8.0.mysql_aurora.3.06.0","tagMatch":{"key":"restore-for"},"instanceSize":"db.t3.medium","restoreSize":"db.t3.small"}', now(), now(), null, 'Default aurora database settings to use for restore');
453456
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('rdsRestoreSettings', '{"vpcId":"","accountId":"","region":"us-west-2","securityGroupIds":[],"subnetGroupName":"","engine":"mysql","engineVersion":"8.0.33","tagMatch":{"key":"restore-for"},"instanceSize":"db.t3.small","restoreSize":"db.t3.small"}', now(), now(), null, 'Default RDS database settings to use for restore');
454457
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('minio', '{"version":"3.7.2","args":"--force --timeout 60m0s --wait","action":"install","chart":{"name":"minio","repoUrl":"https://charts.bitnami.com/bitnami","version":"15.0.7","values":[],"valueFiles":[]},"label":"labels","tolerations":"tolerations","affinity":"affinity","nodeSelector":"nodeSelector"}', now(), now(), null, 'Default minio s3 compatible bucket');
455-
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('features', '{"webhooks":true}', now(), now(), null, 'Configuration for feature flags controlled from database');
458+
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('features', '{"webhooks":true,"reconcileDeletedServices":false}', now(), now(), null, 'Configuration for feature flags controlled from database');
456459
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('serviceAccount', '{"name": "default","role":"replace_me"}', now(), now(), null, 'Default IAM role name to be used to annotate service account');
457460
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('app_setup', '{"state":"","created":false,"installed":false,"restarted":false,"org":"","url":"","name":""}', now(), now(), null, 'Application setup state');
458461
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description) VALUES ('labels', '{"deploy":["lifecycle-deploy!"],"disabled":["lifecycle-disabled!"],"statusComments":["lifecycle-status-comments!"],"defaultStatusComments":true,"defaultControlComments":true}', now(), now(), null, 'Configurable PR labels for deploy, disabled, and status comments');
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2025 GoodRx, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Knex } from 'knex';
18+
19+
async function addColumnIfMissing(
20+
knex: Knex,
21+
tableName: string,
22+
columnName: string,
23+
addColumn: (table: Knex.AlterTableBuilder) => void
24+
): Promise<void> {
25+
const exists = await knex.schema.hasColumn(tableName, columnName);
26+
27+
if (exists) {
28+
return;
29+
}
30+
31+
await knex.schema.alterTable(tableName, addColumn);
32+
}
33+
34+
async function dropColumnIfExists(knex: Knex, tableName: string, columnName: string): Promise<void> {
35+
const exists = await knex.schema.hasColumn(tableName, columnName);
36+
37+
if (!exists) {
38+
return;
39+
}
40+
41+
await knex.schema.alterTable(tableName, (table) => {
42+
table.dropColumn(columnName);
43+
});
44+
}
45+
46+
export async function up(knex: Knex): Promise<void> {
47+
await addColumnIfMissing(knex, 'deployables', 'source', (table) => {
48+
table.string('source').defaultTo('db');
49+
});
50+
await addColumnIfMissing(knex, 'deployables', 'reconcileEligible', (table) => {
51+
table.boolean('reconcileEligible').defaultTo(false);
52+
});
53+
await addColumnIfMissing(knex, 'deployables', 'resolvedFromRepositoryId', (table) => {
54+
table.integer('resolvedFromRepositoryId').nullable();
55+
});
56+
57+
await knex.raw(`
58+
UPDATE deployables
59+
SET
60+
source = CASE
61+
WHEN "serviceId" IS NULL THEN 'yaml'
62+
ELSE COALESCE(source, 'db')
63+
END,
64+
"reconcileEligible" = CASE
65+
WHEN "serviceId" IS NULL AND type <> 'configuration' THEN true
66+
ELSE false
67+
END,
68+
"resolvedFromRepositoryId" = CASE
69+
WHEN "repositoryId" ~ '^[0-9]+$' THEN "repositoryId"::integer
70+
ELSE NULL
71+
END
72+
WHERE source IS NULL
73+
OR "reconcileEligible" IS NULL
74+
OR "resolvedFromRepositoryId" IS NULL;
75+
`);
76+
77+
await knex.raw(`
78+
INSERT INTO global_config (key, config, "createdAt", "updatedAt", "deletedAt", description)
79+
VALUES (
80+
'features',
81+
'{"reconcileDeletedServices":false}',
82+
now(),
83+
now(),
84+
null,
85+
'Configuration for feature flags controlled from database'
86+
)
87+
ON CONFLICT (key) DO UPDATE
88+
SET config = (
89+
COALESCE(global_config.config, '{}'::json)::jsonb ||
90+
CASE
91+
WHEN jsonb_exists(global_config.config::jsonb, 'reconcileDeletedServices') THEN '{}'::jsonb
92+
ELSE '{"reconcileDeletedServices":false}'::jsonb
93+
END
94+
)::json,
95+
"updatedAt" = now(),
96+
"deletedAt" = null;
97+
`);
98+
}
99+
100+
export async function down(knex: Knex): Promise<void> {
101+
await knex.raw(`
102+
UPDATE global_config
103+
SET config = (config::jsonb - 'reconcileDeletedServices')::json,
104+
"updatedAt" = now()
105+
WHERE key = 'features';
106+
`);
107+
108+
await dropColumnIfExists(knex, 'deployables', 'resolvedFromRepositoryId');
109+
await dropColumnIfExists(knex, 'deployables', 'reconcileEligible');
110+
await dropColumnIfExists(knex, 'deployables', 'source');
111+
}

‎src/server/jobs/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export default function bootstrapJobs(services: IServices) {
107107
concurrency: 125,
108108
});
109109

110+
queueManager.registerWorker(QUEUE_NAMES.DEPLOY_CLEANUP, services.DeployCleanupService.processCleanupQueue, {
111+
connection: redisClient.getConnection(),
112+
concurrency: 5,
113+
});
114+
110115
queueManager.registerWorker(QUEUE_NAMES.LABEL, services.LabelService.processLabelQueue, {
111116
connection: redisClient.getConnection(),
112117
concurrency: 10,

‎src/server/lib/cli.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function contextForDeploy(deploy: Deploy, settings: string) {
221221
* Deletes a CLI deploy
222222
* @param deploy cli deploys to delete
223223
*/
224-
async function deleteDeploy(deploy: Deploy) {
224+
export async function deleteDeploy(deploy: Deploy) {
225225
const serviceCmd = deploy.build.enableFullYaml ? deploy.deployable.command : deploy.service.command;
226226

227227
const { settings, region } = await getSettingsFor(serviceCmd);

‎src/server/models/Deployable.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import Model from './_Model';
1818
import { Environment, Repository, Service, ServiceDisk } from '.';
1919
import { Builder, Helm } from './yaml/YamlService';
20+
import type { DeployableConfigSource } from 'server/services/deployable';
2021

2122
export default class Deployable extends Service {
2223
static tableName = 'deployables';
@@ -37,6 +38,9 @@ export default class Deployable extends Service {
3738
deploymentDependsOn: string[];
3839
builder: Builder;
3940
envLens?: boolean;
41+
source?: DeployableConfigSource;
42+
reconcileEligible?: boolean;
43+
resolvedFromRepositoryId?: number | null;
4044

4145
static relationMappings = {
4246
environment: {

0 commit comments

Comments
 (0)