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

Add Environment and Project IDs to controller builds #2565

Merged
merged 4 commits into from
Mar 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 31 additions & 1 deletion node-packages/commons/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,36 @@ export async function getEnvironmentByName(
return result;
}


export async function getEnvironmentById(
id: number
): Promise<any> {
const result = await graphqlapi.query(`
{
environmentById(id: ${id}) {
id,
name,
route,
routes,
deployType,
environmentType,
openshiftProjectName,
updated,
created,
deleted,
}
}
`);

if (!result || !result.environmentById) {
throw new EnvironmentNotFound(
`Cannot find environment for id ${id}\n${result.environmentById}`
);
}

return result;
}

export async function getDeploymentByName(
openshiftProjectName: string,
deploymentName: string,
Expand Down Expand Up @@ -1436,7 +1466,7 @@ fragment on Problem {
data
created
deleted
}
}
`);

export const getProblemsforProjectEnvironment = async (
Expand Down
58 changes: 4 additions & 54 deletions node-packages/commons/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,69 +408,17 @@ const getControllerBuildData = async function(deployData: any) {
const buildName = `lagoon-build-${randBuildId}`;

let deployment;
let environmentId;
try {
const now = moment.utc();
const apiEnvironment = await getEnvironmentByName(branchName, projectOpenShift.id);
environmentId = apiEnvironment.environmentByName.id
deployment = await addDeployment(buildName, "NEW", now.format('YYYY-MM-DDTHH:mm:ss'), apiEnvironment.environmentByName.id);
} catch (error) {
logger.error(`Could not save deployment for project ${projectOpenShift.id}. Message: ${error}`);
}

let buildImage = {}
// @TODO: revise this section around deciding which image to use
// it will probably end up being removed as the controller will handle it, but it would still be good to be able to maybe have a per-project
// or per-environment build image overwrite
// During CI we want to use the OpenShift Registry for our build Image and use the OpenShift registry for the base Images
// Since the Operator could eventually support openshift, we can handle which image to supply here
// if (CI == "true") {
// switch (project.activeSystemsDeploy) {
// case 'lagoon_openshiftBuildDeploy':
// buildImage = "172.17.0.1:5000/lagoon/oc-build-deploy-dind:latest"
// break;
// default:
// // default to the kubectl builddeploy dind since the controllers and kubernetes use the same underlying process
// buildImage = "172.17.0.1:5000/lagoon/kubectl-build-deploy-dind:latest"
// }
// } else if (overwriteOCBuildDeployDindImage) {
// // allow to overwrite the image we use via OVERWRITE_OC_BUILD_DEPLOY_DIND_IMAGE env variable
// // this needs to be added to the `api` deployment/pods to be used
// switch (project.activeSystemsDeploy) {
// case 'lagoon_openshiftBuildDeploy':
// buildImage = overwriteOCBuildDeployDindImage
// break;
// }
// } else if (overwriteKubectlBuildDeployDindImage) {
// // allow to overwrite the image we use via OVERWRITE_KUBECTL_BUILD_DEPLOY_DIND_IMAGE env variable
// // this needs to be added to the `api` deployment/pods to be used
// switch (project.activeSystemsDeploy) {
// case 'lagoon_controllerBuildDeploy':
// case 'lagoon_kubernetesBuildDeploy':
// buildImage = overwriteKubectlBuildDeployDindImage
// break;
// }
// } else if (lagoonEnvironmentType == 'production') {
// // we are a production environment, use the amazeeio/ image with our current lagoon version
// switch (project.activeSystemsDeploy) {
// case 'lagoon_openshiftBuildDeploy':
// buildImage = `amazeeio/oc-build-deploy-dind:${lagoonVersion}`
// break;
// default:
// // default to the kubectl builddeploy dind since the controllers and kubernetes use the same underlying process
// buildImage = `amazeeio/kubectl-build-deploy-dind:${lagoonVersion}`
// }
// } else {
// // we are a development enviornment, use the amazeeiolagoon image with the same branch name
// buildImage = `amazeeiolagoon/kubectl-build-deploy-dind:${lagoonGitSafeBranch}`
// switch (project.activeSystemsDeploy) {
// case 'lagoon_openshiftBuildDeploy':
// buildImage = `amazeeiolagoon/oc-build-deploy-dind:${lagoonGitSafeBranch}`
// break;
// default:
// // default to the kubectl builddeploy dind since the controllers and kubernetes use the same underlying process
// buildImage = `amazeeiolagoon/kubectl-build-deploy-dind:${lagoonGitSafeBranch}`
// }
// }


// encode some values so they get sent to the controllers nicely
const sshKeyBase64 = new Buffer(deployPrivateKey.replace(/\\n/g, "\n")).toString('base64')
Expand All @@ -496,11 +444,13 @@ const getControllerBuildData = async function(deployData: any) {
...promoteData,
gitReference: gitRef,
project: {
id: projectOpenShift.id,
name: projectName,
gitUrl: gitUrl,
uiLink: deployment.addDeployment.uiLink,
environment: environmentName,
environmentType: environmentType,
environmentId: environmentId,
productionEnvironment: projectProductionEnvironment,
standbyEnvironment: projectStandbyEnvironment,
subfolder: subfolder,
Expand Down
79 changes: 63 additions & 16 deletions services/controllerhandler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
getOpenShiftInfoForProject,
getEnvironmentByName,
getEnvironmentById,
updateEnvironment,
updateDeployment,
getDeploymentByName,
Expand Down Expand Up @@ -71,13 +72,42 @@ const updateLagoonTask = async (meta) => {
}
}

const getProjectEnvironment = async function(projectEnv: any) {
const {
meta
} = projectEnv;

let environment;
let project;

const projectBuildResult = await getOpenShiftInfoForProject(meta.project);
project = projectBuildResult.project
// check if the payload has an environment id defined to get the environment information
if (meta.environmentId != null) {
const environmentResult = await getEnvironmentById(meta.environmentId);
environment = environmentResult.environmentById
} else {
// if no id, use the name that was provided instead
const environmentResult = await getEnvironmentByName(meta.environment, project.id);
environment = environmentResult.environmentByName
}

var projectEnvironmentData: any = {
project: project,
environment: environment,
}
return projectEnvironmentData;
}

const messageConsumer = async function(msg) {
const {
type,
namespace,
meta,
} = JSON.parse(msg.content.toString());

let environment;
let project;

switch (type) {
case 'build':
Expand Down Expand Up @@ -123,17 +153,15 @@ const messageConsumer = async function(msg) {
logger.error(`Could not update deployment ${meta.project} ${meta.Buildname}. Message: ${error}`);
}


let environment;
let project;
try {
const projectResult = await getOpenShiftInfoForProject(meta.project);
project = projectResult.project

const environmentResult = await getEnvironmentByName(meta.environment, project.id)
environment = environmentResult.environmentByName
const projectEnv = await getProjectEnvironment({meta});
project = projectEnv.project
environment = projectEnv.environment
} catch (err) {
logger.warn(`${namespace} ${meta.buildName}: Error while getting project or environment information, Error: ${err}. Continuing without update`)
// if the project or environment can't be determined, give up trying to do anything for it
logger.error(`${namespace} ${meta.buildName}: Error while getting project or environment information, Error: ${err}. Giving up updating environment`)
// break so the controllerhandler doesn't stop processing
break;
}

try {
Expand Down Expand Up @@ -177,11 +205,23 @@ const messageConsumer = async function(msg) {
logger.verbose(`Received remove task for ${namespace}`);
// Update GraphQL API that the Environment has been deleted
try {
await deleteEnvironment(meta.environment, meta.project, false);
const projectEnv = await getProjectEnvironment({meta});
project = projectEnv.project
environment = projectEnv.environment
} catch (err) {
// if the project or environment can't be determined, give up trying to do anything for it
logger.error(`${namespace} ${meta.buildName}: Error while getting project or environment information, Error: ${err}. Giving up removing environment`)
// break so the controllerhandler doesn't stop processing
break;
}
try {
await deleteEnvironment(environment.name, meta.project, false);
logger.info(
`${meta.project}: Deleted Environment '${meta.environment}' in API`
`${meta.project}: Deleted Environment '${environment.name}' in API`
);
meta.openshiftProject = meta.environment
// @TODO: looking at `meta.openshiftProject`, this seems to only be used by logs2email, logs2rocketchat, and logs2microsoftteams
// when notification system is re-written, this can also be removed as it seems kind of a silly name
meta.openshiftProject = environment.name
meta.openshiftProjectName = namespace
meta.projectName = meta.project
sendToLagoonLogs(
Expand All @@ -190,7 +230,7 @@ const messageConsumer = async function(msg) {
'',
'task:remove-kubernetes:finished',
meta,
`*[${meta.project}]* remove \`${meta.environment}\``
`*[${meta.project}]* remove \`${environment.name}\``
);
} catch (err) {
logger.warn(`${namespace}: Error while deleting environment, Error: ${err}. Continuing without update`)
Expand All @@ -200,6 +240,16 @@ const messageConsumer = async function(msg) {
logger.verbose(
`Received task result for ${meta.task.name} from ${meta.project} - ${meta.environment} - ${meta.jobStatus}`
);
try {
const projectEnv = await getProjectEnvironment({meta});
project = projectEnv.project
environment = projectEnv.environment
} catch (err) {
// if the project or environment can't be determined, give up trying to do anything for it
logger.error(`${namespace} ${meta.buildName}: Error while getting project or environment information, Error: ${err}. Giving up updating task result`)
// break so the controllerhandler doesn't stop processing
break;
}
// if we want to be able to do something else when a task result comes through,
// we can use the task key
switch (meta.key) {
Expand All @@ -209,9 +259,6 @@ const messageConsumer = async function(msg) {
switch (meta.jobStatus) {
case "succeeded":
try {
// get the project ID
const projectResult = await getOpenShiftInfoForProject(meta.project);
const project = projectResult.project
// since the advanceddata contains a base64 encoded value, we have to decode it first
var decodedData = new Buffer(meta.advancedData, 'base64').toString('ascii')
const taskResult = JSON.parse(decodedData)
Expand Down