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

Major refactor of functions deploy #3132

Merged
merged 37 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
06b62a1
Adding onPoll option to operation-poller (#3046)
joehan Jan 19, 2021
6258dd5
Typescriptify functionsDeployHelper (#3059)
joehan Jan 20, 2021
ec7d079
Typescriptifying gcp.cloudfunctions (#3060)
joehan Jan 20, 2021
f583ef6
Typescriptifying functionsConfig (#3063)
joehan Jan 21, 2021
08b9d56
Typescriptifying deploymentTool (#3061)
joehan Jan 21, 2021
b4944a4
Refactoring prepare stage of functions deploy (#3067)
joehan Jan 21, 2021
3be0dca
refactoring release step of functions deploy to use typescript
joehan Jan 21, 2021
e0e703e
Adding logic to build regional deployments
joehan Jan 24, 2021
046c7d7
Implementing createDeploymentPlan
joehan Jan 26, 2021
b876523
First round of PR feedback, removing most usages of lodash
joehan Jan 28, 2021
9e0e6e9
moving function prompts into their own file
joehan Jan 28, 2021
2a3b547
seperating out a bunch of code from functionsDeployHelper
joehan Jan 28, 2021
51f2395
Resolves merge conflicts
joehan Jan 28, 2021
30cc0e9
refactoring release step of functions deploy to use typescript (#3071)
joehan Feb 1, 2021
6916000
Implements core logic of running function deploys
joehan Feb 1, 2021
3c8d4a0
Typescriptifying prepareFunctionsUpload (#3064)
joehan Feb 1, 2021
11956fa
Implementing createDeploymentPlan (#3081)
joehan Feb 1, 2021
85d0afe
adding timing and logs for deployments
joehan Feb 2, 2021
00b1989
cleaning up unused code
joehan Feb 2, 2021
397d7c4
Fixing some things that were broken while merging
joehan Feb 3, 2021
21f4906
Fixing up the order of wait and close to ensure that queue promsies a…
joehan Feb 4, 2021
3b3edbd
Format and clean up typos
joehan Feb 4, 2021
e428bcb
refactoring error handling to be cleaner
joehan Feb 5, 2021
4c8e2fb
cleaning up extera newlines
joehan Feb 8, 2021
7f48130
first round of pr fixes
joehan Feb 9, 2021
39a7e86
Readding some changes that I accidenttally wiped out during a merge
joehan Feb 9, 2021
1366955
Switching name to id where appropriate
joehan Feb 9, 2021
7513229
fixing another bug caused by functionName vs Id
joehan Feb 9, 2021
8d3d82d
Merge pull request #3107 from firebase/jh-execute-deployment-plans
joehan Feb 9, 2021
6d2260e
Refactor functions-delete (#3110)
joehan Feb 9, 2021
42e6c15
Cleaning up error reporting
joehan Feb 10, 2021
e4ce126
Merge remote-tracking branch 'public/master' into jh-functions-refactor
joehan Feb 10, 2021
12a48ea
Merge remote-tracking branch 'public/master' into jh-functions-refactor
joehan Feb 11, 2021
7cfe9d9
Implement validation for changing trigger types, and fixes from bug b…
joehan Feb 12, 2021
5eb08bd
Merge branch 'master' into jh-functions-refactor
joehan Feb 12, 2021
5ca6bbf
Merge branch 'master' into jh-functions-refactor
joehan Feb 16, 2021
344b674
fixes package.json
joehan Feb 16, 2021
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
Prev Previous commit
Next Next commit
Adding logic to build regional deployments
  • Loading branch information
joehan committed Jan 24, 2021
commit e0e703eddcca4517284ec3d7dbab5929eda82b51
13 changes: 8 additions & 5 deletions src/deploy/functions/checkIam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { bold } from "cli-color";

import { debug } from "../../logger";
import * as track from "../../track";
import { getReleaseNames, getFunctionsInfo, getFilterGroups } from "../../functionsDeployHelper";
import {
getReleaseNames,
getFilterGroups,
CloudFunctionTrigger,
} from "../../functionsDeployHelper";
import { FirebaseError } from "../../error";
import { testIamPermissions, testResourceIamPermissions } from "../../gcp/iam";

Expand Down Expand Up @@ -51,13 +55,12 @@ export async function checkServiceAccountIam(projectId: string): Promise<void> {
* @param payload The deploy payload.
*/
export async function checkHttpIam(context: any, options: any, payload: any): Promise<void> {
const triggers = payload.functions.triggers;
const functionsInfo = getFunctionsInfo(triggers, context.projectId);
const functionsInfo = payload.functions.triggers;
const filterGroups = getFilterGroups(options);

const httpFunctionNames: string[] = functionsInfo
.filter((f) => has(f, "httpsTrigger"))
.map((f) => f.name);
.filter((f: CloudFunctionTrigger) => has(f, "httpsTrigger"))
.map((f: CloudFunctionTrigger) => f.name);
const httpFunctionFullNames: string[] = getReleaseNames(httpFunctionNames, [], filterGroups);
const existingFunctionFullNames: string[] = context.existingFunctions.map(
(f: { name: string }) => f.name
Expand Down
26 changes: 20 additions & 6 deletions src/deploy/functions/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import * as functionsConfig from "../../functionsConfig";
import * as getProjectId from "../../getProjectId";
import { logBullet } from "../../utils";
import { getRuntimeChoice } from "../../parseRuntimeAndValidateSDK";
import { getFunctionsInfo, promptForFailurePolicies } from "../../functionsDeployHelper";
import {
CloudFunctionTrigger,
createFunctionRegionMap,
flattenRegionMap,
functionMatchesAnyGroup,
getFilterGroups,
promptForFailurePolicies,
} from "../../functionsDeployHelper";
import * as prepareFunctionsUpload from "../../prepareFunctionsUpload";
import * as validate from "./validate";
import { checkRuntimeDependencies } from "./checkRuntimeDependencies";
Expand Down Expand Up @@ -49,17 +56,24 @@ export async function prepare(context: any, options: any, payload: any): Promise

// Get a list of CloudFunctionTriggers, with duplicates for each region.
payload.functions = {};
payload.functions.triggers = getFunctionsInfo(
options.config.get("functions.triggers"),
projectId
payload.functions.regionMap = createFunctionRegionMap(
projectId,
options.config.get("functions.triggers")
);
payload.functions.triggers = flattenRegionMap(payload.functions.regionMap);

// Validate the function code that is being deployed.
validate.functionsDirectoryExists(options, sourceDirName);
// validate.functionNamesAreValid(payload.functionNames);
// TODO: This doesn't do anything meaningful right now because payload.functions is not defined
validate.packageJsonIsValid(sourceDirName, sourceDir, projectDir, !!runtimeFromConfig);

// Display a warning and prompt if any functions have failurePolicies.
await promptForFailurePolicies(options, payload.functions.triggers);
// Check what --only filters have been passed in.
context.filters = getFilterGroups(options);

// Display a warning and prompt if any functions in the release have failurePolicies.
const localFnsInRelease = payload.functions.triggers.filter((fn: CloudFunctionTrigger) => {
return functionMatchesAnyGroup(fn.name, context.filters);
});
await promptForFailurePolicies(options, localFnsInRelease);
}
131 changes: 125 additions & 6 deletions src/functionsDeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ export interface CloudFunctionTrigger {
regions?: string[];
}

export function functionMatchesAnyGroup(fnName: string, filterGroups: string[][]) {
if (!filterGroups.length) {
return true;
}
return _.some(filterGroups, (groupChunks) => {
return functionMatchesGroup(fnName, groupChunks);
});
}

export function functionMatchesGroup(functionName: string, groupChunks: string[]): boolean {
const last = _.last(functionName.split("/"));
if (!last) {
Expand Down Expand Up @@ -140,14 +149,27 @@ export function logFilters(
}
}

export function getFunctionsInfo(parsedTriggers: CloudFunctionTrigger[], projectId: string) {
const functionsInfo: CloudFunctionTrigger[] = [];
interface RegionMap {
[region: string]: CloudFunctionTrigger[];
}

/**
* Creates a map of regions to all the CloudFunctions being deployed
* to that region.
* @param projectId The project in use.
* @param parsedTriggers A list of all CloudFunctions in the deployment.
*/
export function createFunctionRegionMap(
projectId: string,
parsedTriggers: CloudFunctionTrigger[]
): RegionMap {
const regionMap: RegionMap = {};
_.forEach(parsedTriggers, (trigger) => {
if (!trigger.regions) {
trigger.regions = ["us-central1"];
}
// SDK exports list of regions for each function to be deployed to, need to add a new entry
// to functionsInfo for each region.
// Create a separate CloudFunction for
// each region we deploy a function to
_.forEach(trigger.regions, (region) => {
const triggerDeepCopy = JSON.parse(JSON.stringify(trigger));
if (triggerDeepCopy.regions) {
Expand All @@ -161,10 +183,107 @@ export function getFunctionsInfo(parsedTriggers: CloudFunctionTrigger[], project
"functions",
trigger.name,
].join("/");
functionsInfo.push(triggerDeepCopy);
if (!_.get(regionMap, region)) {
regionMap[region] = [];
}
regionMap[region].push(triggerDeepCopy);
});
});
return functionsInfo;
return regionMap;
}

/**
* Helper method to turn a RegionMap into a flat list of all functions in a deployment.
* @param regionMap A RegionMap for the deployment.
*/
export function flattenRegionMap(regionMap: RegionMap): CloudFunctionTrigger[] {
return _.chain(regionMap)
.map((value: CloudFunctionTrigger[]) => {
return value;
})
.flatten()
.value();
}

export interface RegionalDeployment {
region: string;
sourceToken?: string;
firstFunctionDeployment?: CloudFunctionTrigger;
functionsToCreate: CloudFunctionTrigger[];
functionsToUpdate: CloudFunctionTrigger[];
functionsToDelete: string[];
schedulesToCreateOrUpdate: CloudFunctionTrigger[];
schedulesToDelete: string[];
}

/**
* Create a plan for deploying all functions in one region.
* @param region The region of this deployment
* @param functionsInLocalSource The functions present in the code currently being deployed.
* @param existingFunctionNames The names of all functions that already exist.
* @param existingScheduledFunctionNames The names of all schedules functions that already exist.
* @param filters The filters, passed in by the user via `--only functions:`
*/
export function createRegionalDeployment(
region: string,
functionsInLocalSource: CloudFunctionTrigger[],
existingFunctionNames: string[],
existingScheduledFunctionNames: string[],
filters: string[][]
) {
const deployment: RegionalDeployment = {
region,
functionsToCreate: [],
functionsToUpdate: [],
functionsToDelete: [],
schedulesToCreateOrUpdate: [],
schedulesToDelete: [],
};
// Sort functions
for (const fn of functionsInLocalSource) {
if (!_.includes(existingFunctionNames, fn.name)) {
deployment.functionsToCreate.push(fn);
} else {
deployment.functionsToUpdate.push(fn);
_.remove(existingFunctionNames, (val: string) => {
return val === fn.name;
});
}
// Check for schedules.
if (_.get(fn, "schedule")) {
deployment.schedulesToCreateOrUpdate.push(fn);
if (_.includes(existingScheduledFunctionNames, fn.name)) {
_.remove(existingScheduledFunctionNames, (val: string) => {
return val === fn.name;
});
}
}
}

deployment.functionsToDelete = existingFunctionNames;
deployment.schedulesToDelete = existingScheduledFunctionNames;

// Apply --only filters
if (filters.length) {
deployment.functionsToCreate = deployment.functionsToCreate.filter(
(fn: CloudFunctionTrigger) => {
return functionMatchesAnyGroup(fn.name, filters);
}
);
deployment.functionsToUpdate = deployment.functionsToUpdate.filter(
(fn: CloudFunctionTrigger) => {
return functionMatchesAnyGroup(fn.name, filters);
}
);
deployment.functionsToDelete = deployment.functionsToDelete.filter((fnName: string) => {
return functionMatchesAnyGroup(fnName, filters);
});
deployment.schedulesToDelete = deployment.schedulesToDelete.filter((fnName: string) => {
return functionMatchesAnyGroup(fnName, filters);
});
}

return deployment;
}

export function getFunctionTrigger(functionInfo: CloudFunctionTrigger) {
Expand Down