Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow environments to be deleted if the deploytarget is disabled #3460

Merged
merged 1 commit into from
Jun 9, 2023
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
2 changes: 1 addition & 1 deletion services/api/src/resources/deployment/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export const cancelDeployment: ResolverFn = async (
});

// check if the deploytarget for this environment is disabled
const deploytarget = await query(sqlClientPool, Sql.selectDeployTarget(environment.openshift));
const deploytarget = await environmentHelpers(sqlClientPool).getEnvironmentsDeploytarget(environment.openshift);
if (deploytarget[0].disabled) {
// if it is, proceed to mark the build as cancelled
var date = new Date();
Expand Down
4 changes: 0 additions & 4 deletions services/api/src/resources/deployment/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ export const Sql = {
.where('id', id)
.update(patch)
.toString(),
selectDeployTarget: (id: number) =>
knex('openshift')
.where('id', '=', id)
.toString(),
selectPermsForDeployment: (id: number) =>
knex('deployment')
.select({ pid: 'environment.project' })
Expand Down
7 changes: 7 additions & 0 deletions services/api/src/resources/environment/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export const Helpers = (sqlClientPool: Pool) => {
Sql.deleteEnvironment(name, pid)
);
},
getEnvironmentsDeploytarget: async (eid) => {
const rows = await query(
sqlClientPool,
Sql.selectDeployTarget(eid)
);
return aliasOpenshiftToK8s(rows);
},
getEnvironmentsByProjectId: async (projectId) => {
const rows = await query(
sqlClientPool,
Expand Down
8 changes: 8 additions & 0 deletions services/api/src/resources/environment/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@ export const deleteEnvironment: ResolverFn = async (
}
});

// if the deploytarget of this environment is marked as disabled or doesn't exist, just delete the environment
// the removetask will never work if the deploytarget is disabled and the environment will remain undeleted in the api
const deploytarget = await Helpers(sqlClientPool).getEnvironmentsDeploytarget(environment.openshift);
if (deploytarget.length == 0 || deploytarget[0].disabled) {
await Helpers(sqlClientPool).deleteEnvironment(name, environment.id, projectId);
return 'success';
}

await createRemoveTask(data);
sendToLagoonLogs(
'info',
Expand Down
4 changes: 4 additions & 0 deletions services/api/src/resources/environment/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export const Sql = {
name,
})
.toString(),
selectDeployTarget: (id: number) =>
knex('openshift')
.where('id', '=', id)
.toString(),
selectServicesByEnvironmentId: (id: number) =>
knex('environment_service')
.where('environment', '=', id)
Expand Down