Skip to content

chore: use new GCF env vars in descriptor #436

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

Merged
merged 3 commits into from
Mar 25, 2019
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
9 changes: 7 additions & 2 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ function zoneFromQualifiedZone(qualified: string): string|undefined {
* @returns {object}
*/
export function getCloudFunctionDescriptor() {
/**
* In GCF versions after Node 8, K_SERVICE is the preferred way to
* get the function name and GOOGLE_CLOUD_REGION is the preferred way
* to get the region.
*/
return {
type: 'cloud_function',
labels: {
function_name: process.env.FUNCTION_NAME,
region: process.env.FUNCTION_REGION,
function_name: process.env.K_SERVICE || process.env.FUNCTION_NAME,
region: process.env.GOOGLE_CLOUD_REGION || process.env.FUNCTION_REGION,
},
};
}
Expand Down
43 changes: 42 additions & 1 deletion test/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,53 @@ describe('metadata', () => {
const FUNCTION_NAME = 'function-name';
const FUNCTION_REGION = 'function-region';

const K_SERVICE = 'k-service';
const GOOGLE_CLOUD_REGION = 'google-cloud-region';

const TARGET_KEYS = [
'FUNCTION_NAME', 'FUNCTION_REGION', 'K_SERVICE', 'GOOGLE_CLOUD_REGION'
];
const INITIAL_ENV: {[key: string]: string|undefined} = {};

before(() => {
for (const key of TARGET_KEYS) {
INITIAL_ENV[key] = process.env[key];
}
});

after(() => {
for (const key of TARGET_KEYS) {
const val = INITIAL_ENV[key];
if (val === undefined) {
delete process.env[key];
} else {
process.env[key] = val;
}
}
});

beforeEach(() => {
for (const key of TARGET_KEYS) {
delete process.env[key];
}
process.env.FUNCTION_NAME = FUNCTION_NAME;
process.env.FUNCTION_REGION = FUNCTION_REGION;
});

it('should return the correct descriptor', () => {
it('should return the correct primary descriptor', () => {
process.env.K_SERVICE = K_SERVICE;
process.env.GOOGLE_CLOUD_REGION = GOOGLE_CLOUD_REGION;

assert.deepStrictEqual(metadata.getCloudFunctionDescriptor(), {
type: 'cloud_function',
labels: {
function_name: K_SERVICE,
region: GOOGLE_CLOUD_REGION,
},
});
});

it('should return the correct fallback descriptor', () => {
assert.deepStrictEqual(metadata.getCloudFunctionDescriptor(), {
type: 'cloud_function',
labels: {
Expand Down