@@ -20,13 +20,14 @@ import {
2020 addDeployment ,
2121 getOrganizationByIdWithEnvs ,
2222 getOrganizationById ,
23+ AutogeneratedRouteConfig ,
2324} from './api' ;
2425import {
2526 deployTargetBranches ,
2627 deployTargetPullrequest ,
2728 deployTargetPromote
2829} from './deploy-tasks' ;
29- import { InternalEnvVariableScope , DeployData , DeployType , RemoveData } from './types' ;
30+ import { InternalEnvVariableScope , DeployData , DeployType , RemoveData , RouteSource } from './types' ;
3031// @ts -ignore
3132import sha1 from 'sha1' ;
3233import crypto from 'crypto' ;
@@ -528,11 +529,10 @@ export const getControllerBuildData = async function(deployTarget: any, deployDa
528529 }
529530
530531 let deployment ;
531- let environmentId ;
532+ const now = moment . utc ( ) ;
533+ const apiEnvironment = await getEnvironmentByName ( branchName , lagoonProjectData . id , false ) ;
534+ const environmentId = apiEnvironment . environmentByName . id
532535 try {
533- const now = moment . utc ( ) ;
534- const apiEnvironment = await getEnvironmentByName ( branchName , lagoonProjectData . id , false ) ;
535- environmentId = apiEnvironment . environmentByName . id
536536 deployment = await addDeployment ( buildName ,
537537 "NEW" ,
538538 now . format ( 'YYYY-MM-DDTHH:mm:ss' ) ,
@@ -551,10 +551,10 @@ export const getControllerBuildData = async function(deployTarget: any, deployDa
551551 // encode some values so they get sent to the controllers nicely
552552 const { routerPattern, appliedEnvVars : envVars } = await getEnvironmentsRouterPatternAndVariables (
553553 lagoonProjectData ,
554- environment . addOrUpdateEnvironment ,
554+ apiEnvironment . environmentByName ,
555555 deployTarget . openshift ,
556556 bulkId || null , bulkName || null , priority , buildVariables ,
557- bulkType . Deploy
557+ bulkType . Deploy ,
558558 )
559559
560560 let organization : any = null ;
@@ -627,23 +627,75 @@ enum bulkType {
627627export const getEnvironmentsRouterPatternAndVariables = async function (
628628 project : Pick <
629629 Project ,
630- 'routerPattern' | 'sharedBaasBucket' | 'organization'
630+ 'routerPattern' |
631+ 'sharedBaasBucket' |
632+ 'organization' |
633+ 'apiRoutes' |
634+ 'autogeneratedRouteConfig'
631635 > & {
632636 openshift : Pick < Kubernetes , 'routerPattern' > ;
633637 envVariables : Pick < EnvKeyValue , 'name' | 'value' | 'scope' > [ ] ;
634638 } ,
635- environment : { envVariables : Pick < EnvKeyValue , 'name' | 'value' | 'scope' > [ ] } ,
639+ environment : {
640+ envVariables : Pick < EnvKeyValue , 'name' | 'value' | 'scope' > [ ]
641+ apiRoutes ,
642+ autogeneratedRouteConfig ,
643+ } ,
636644 deployTarget : Pick < Kubernetes , 'name' | 'routerPattern' | 'sharedBaasBucketName' > ,
637645 bulkId : string | null ,
638646 bulkName : string | null ,
639647 buildPriority : number ,
640648 buildVariables : Array < { name : string , value : string } > ,
641- bulkTask : bulkType
649+ bulkTask : bulkType ,
642650) : Promise < { routerPattern : string , appliedEnvVars : string } > {
643651 type EnvKeyValueInternal = Pick < EnvKeyValue , 'name' | 'value' > & {
644652 scope : EnvVariableScope | InternalEnvVariableScope ;
645653 }
646654
655+ /*
656+ prepares the values to send to builds and tasks for consumption
657+ this ensures the data is structured to match the one that the build expects when it receives the payload
658+ this gets based64 encoded before being shipped. it is decoded during the build
659+ */
660+ let apiRoutes = environment . apiRoutes
661+ if ( apiRoutes . length > 0 ) {
662+ for ( let i = 0 ; i < apiRoutes . length ; i ++ ) {
663+ // remove any yaml/autogenerated sourced apiRoutes from the list
664+ // these don't get send to builds from the api as they are configured by lagoon.yml
665+ // or are created by the autogenerated route configuration
666+ if ( [ RouteSource . YAML , RouteSource . AUTOGENERATED ] . includes ( apiRoutes [ i ] . source . toLowerCase ( ) ) ) {
667+ // the environment queries have (source: API) on the environments apiRoutes request
668+ // this check is just a fallback check
669+ apiRoutes . splice ( i , 1 ) ;
670+ i -- ;
671+ continue ;
672+ }
673+ // the structure of annotations in the build-deploy-tool are `map[string]string`
674+ // this converts the `key:value` type from the api to `map[string]string` for the build-deploy-tool to consume
675+ let annotations = apiRoutes [ i ] . annotations . reduce ( ( acc , item ) => {
676+ acc [ item . key ] = item . value ;
677+ return acc ;
678+ } , { } ) ;
679+ apiRoutes [ i ] . annotations = annotations
680+ // the structure of alternativeNames in the build-deploy-tool are `[]string`
681+ // this converts to that type for the build-deploy-tool to consume
682+ let alternativeNames = apiRoutes [ i ] . alternativeNames . map ( item => item . domain ) ;
683+ apiRoutes [ i ] . alternativeNames = alternativeNames
684+ }
685+ }
686+
687+ /*
688+ this handles creating the autogenerated route configuration if any autogenerated route settings
689+ on the project or environment are set, this then gets setup in the required format
690+ and base64 encoded before being sent to the build, it is decoded there
691+ */
692+ let agPayload = { }
693+ if ( project . autogeneratedRouteConfig !== null ) {
694+ agPayload = project . autogeneratedRouteConfig
695+ }
696+ if ( environment . autogeneratedRouteConfig !== null ) {
697+ agPayload = environment . autogeneratedRouteConfig
698+ }
647699 // Single list of env vars that apply to an environment. Env vars from multiple
648700 // sources (organization, project, enviornment, build vars, etc) are
649701 // consolidated based on precedence.
@@ -711,6 +763,37 @@ export const getEnvironmentsRouterPatternAndVariables = async function(
711763 } ) ;
712764 }
713765
766+ // `LAGOON_API_ROUTES` is the successor to LAGOON_ROUTES_JSON. the build-deploy-tool will
767+ // check if `LAGOON_ROUTES_JSON` is defined, then that will be used to prevent breaking deployments
768+ // but will produce a build warning indicating that LAGOON_ROUTES_JSON will be deprecated in the future
769+ // and to use API defined routes instead
770+ if ( apiRoutes . length > 0 ) {
771+ applyIfNotExists ( {
772+ name : "LAGOON_API_ROUTES" ,
773+ value : encodeJSONBase64 ( { routes : apiRoutes } ) ,
774+ scope : InternalEnvVariableScope . INTERNAL_SYSTEM
775+ } ) ;
776+ }
777+ if ( project . apiRoutes . length > 0 ) {
778+ // if the project has any apiroutes defined, then we enforce cleanup of removed routes on any environments
779+ applyIfNotExists ( {
780+ name : "LAGOON_API_ROUTES_CLEANUP" ,
781+ value : "true" ,
782+ scope : InternalEnvVariableScope . INTERNAL_SYSTEM
783+ } ) ;
784+ }
785+ if ( Object . keys ( agPayload ) . length ) {
786+ // if there is any autogenerated route configuration to send
787+ // set that here
788+ // @TODO : need to figure out a better way to handle the default autogenerate insecure handling
789+ // agPayload.autogenerate.insecure = "Redirect"
790+ applyIfNotExists ( {
791+ name : "LAGOON_API_AUTOGENERATED_CONFIG" ,
792+ value : encodeJSONBase64 ( agPayload ) ,
793+ scope : InternalEnvVariableScope . INTERNAL_SYSTEM
794+ } ) ;
795+ }
796+
714797 /*
715798 * Normally scoped env vars.
716799 *
@@ -1076,7 +1159,7 @@ export const getTaskProjectEnvironmentVariables = async (projectName: string, en
10761159 result . project ,
10771160 environment . environmentById ,
10781161 environment . environmentById . openshift ,
1079- null , null , priority , [ ] , bulkType . Task // bulk deployments don't apply to tasks yet, but this is future proofing the function call
1162+ null , null , priority , [ ] , bulkType . Task , // bulk deployments don't apply to tasks yet, but this is future proofing the function call
10801163 )
10811164 return appliedEnvVars
10821165}
0 commit comments