Skip to content

Commit

Permalink
feat: automatically use service and revision name in Knative environm…
Browse files Browse the repository at this point in the history
…ents (#375)

Fixes #370
  • Loading branch information
DominicKramer committed Jun 19, 2019
1 parent eb3ae66 commit 453bd6e
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 41 deletions.
13 changes: 12 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ export class Configuration {
* @returns {Undefined} - does not return anything
*/
_checkLocalServiceContext() {
// Update June 18, 2019: When running on Cloud Run, Cloud Run
// on GKE, or any Knative install, the
// K_SERVICE env var should be used as
// the service and the K_REVISION env var
// should be used as the version. See the
// Knative runtime contract for more info
// (https://github.com/knative/serving/blob/master/docs/runtime-contract.md#process)
//
// Note: The GAE_MODULE_NAME environment variable is set on GAE.
// If the code is, in particular, running on GCF, then the
// FUNCTION_NAME environment variable is set.
Expand All @@ -224,7 +232,10 @@ export class Configuration {
let service;
let version;

if (env.FUNCTION_NAME) {
if (env.K_SERVICE) {
service = env.K_SERVICE;
version = env.K_REVISION;
} else if (env.FUNCTION_NAME) {
service = env.FUNCTION_NAME;
} else if (env.GAE_SERVICE) {
service = env.GAE_SERVICE;
Expand Down
174 changes: 134 additions & 40 deletions test/unit/service-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ function sterilizeServiceConfigEnv() {
delete process.env[key];
});
}
function setEnv(
serviceName: string | null,
serviceVersion: string,
moduleName: string | null,
mv: string,
fn: string | null
) {
function setEnv(envData: {
gaeServiceName: string | null;
gaeServiceVersion: string;
gaeModuleName: string | null;
gaeModuleVersion: string;
functionName: string | null;
kService: string | null;
kRevision: string | null;
}) {
Object.assign(
process.env,
omitBy(
{
GAE_SERVICE: serviceName,
GAE_VERSION: serviceVersion,
GAE_MODULE_NAME: moduleName,
GAE_MODULE_VERSION: mv,
FUNCTION_NAME: fn,
GAE_SERVICE: envData.gaeServiceName,
GAE_VERSION: envData.gaeServiceVersion,
GAE_MODULE_NAME: envData.gaeModuleName,
GAE_MODULE_VERSION: envData.gaeModuleVersion,
FUNCTION_NAME: envData.functionName,
K_SERVICE: envData.kService,
K_REVISION: envData.kRevision,
},
val => {
return !is.string(val);
Expand All @@ -74,13 +78,15 @@ describe('Testing service configuration', () => {
'A Configuration uses the function name as the service name on GCF ' +
'if the service name is not given in the given config',
() => {
setEnv(
'someModuleName',
'1.0',
'InvalidName',
'InvalidVersion',
'someFunction'
);
setEnv({
gaeServiceName: 'someModuleName',
gaeServiceVersion: '1.0',
gaeModuleName: 'InvalidName',
gaeModuleVersion: 'InvalidVersion',
functionName: 'someFunction',
kService: null,
kRevision: null,
});
const c = new Configuration({}, logger);
deepStrictEqual(c.getServiceContext().service, 'someFunction');
// FUNCTION_NAME is set and the user didn't specify a version, and so
Expand All @@ -93,7 +99,15 @@ describe('Testing service configuration', () => {
'if the service name is not given in the given config ' +
'even if the GAE_SERVICE was not set',
() => {
setEnv(null, '1.0', null, 'InvalidVersion', 'someFunction');
setEnv({
gaeServiceName: null,
gaeServiceVersion: '1.0',
gaeModuleName: null,
gaeModuleVersion: 'InvalidVersion',
functionName: 'someFunction',
kService: null,
kRevision: null,
});
const c = new Configuration({}, logger);
deepStrictEqual(c.getServiceContext().service, 'someFunction');
// The user didn't specify a version and FUNCTION_NAME is defined, and
Expand All @@ -106,7 +120,15 @@ describe('Testing service configuration', () => {
'if the FUNCTION_NAME env variable is not set and the given config ' +
'does not specify the service name',
() => {
setEnv('someModuleName', '1.0', 'InvalidName', 'InvalidVersion', null);
setEnv({
gaeServiceName: 'someModuleName',
gaeServiceVersion: '1.0',
gaeModuleName: 'InvalidName',
gaeModuleVersion: 'InvalidVersion',
functionName: null,
kService: null,
kRevision: null,
});
const c = new Configuration({}, logger);
deepStrictEqual(c.getServiceContext().service, 'someModuleName');
// The user didn't specify a version, and FUNCTION_NAME is not defined,
Expand All @@ -119,13 +141,15 @@ describe('Testing service configuration', () => {
'was specified and both the GAE_SERVICE and FUNCTION_NAME ' +
'env vars are set',
() => {
setEnv(
'someModuleName',
'1.0',
'InvalidName',
'InvalidVersion',
'someFunction'
);
setEnv({
gaeServiceName: 'someModuleName',
gaeServiceVersion: '1.0',
gaeModuleName: 'InvalidName',
gaeModuleVersion: 'InvalidVersion',
functionName: 'someFunction',
kService: null,
kRevision: null,
});
const c = new Configuration(
{
serviceContext: {
Expand All @@ -145,13 +169,15 @@ describe('Testing service configuration', () => {
'they were both specified and both the GAE_SERVICE and FUNCTION_NAME ' +
'env vars are set',
() => {
setEnv(
'someModuleName',
'1.0',
'InvalidName',
'InvalidVersion',
'someFunction'
);
setEnv({
gaeServiceName: 'someModuleName',
gaeServiceVersion: '1.0',
gaeModuleName: 'InvalidName',
gaeModuleVersion: 'InvalidVersion',
functionName: 'someFunction',
kService: null,
kRevision: null,
});
const c = new Configuration(
{
serviceContext: {
Expand All @@ -170,7 +196,15 @@ describe('Testing service configuration', () => {
'A Configuration uses the service name in the given config if it ' +
'was specified and only the GAE_SERVICE env const is set',
() => {
setEnv('someModuleName', '1.0', 'InvalidName', 'InvalidVersion', null);
setEnv({
gaeServiceName: 'someModuleName',
gaeServiceVersion: '1.0',
gaeModuleName: 'InvalidName',
gaeModuleVersion: 'InvalidVersion',
functionName: null,
kService: null,
kRevision: null,
});
const c = new Configuration(
{
serviceContext: {
Expand All @@ -189,7 +223,15 @@ describe('Testing service configuration', () => {
'A Configuration uses the service name and version in the given config ' +
'they were both specified and only the GAE_SERVICE env const is set',
() => {
setEnv('someModuleName', '1.0', 'InvalidName', 'InvalidVersion', null);
setEnv({
gaeServiceName: 'someModuleName',
gaeServiceVersion: '1.0',
gaeModuleName: 'InvalidName',
gaeModuleVersion: 'InvalidVersion',
functionName: null,
kService: null,
kRevision: null,
});
const c = new Configuration(
{
serviceContext: {
Expand All @@ -208,7 +250,15 @@ describe('Testing service configuration', () => {
'A Configuration uses the service name in the given config if it ' +
'was specified and only the FUNCTION_NAME env const is set',
() => {
setEnv(null, '1.0', null, 'InvalidVersion', 'someFunction');
setEnv({
gaeServiceName: null,
gaeServiceVersion: '1.0',
gaeModuleName: null,
gaeModuleVersion: 'InvalidVersion',
functionName: 'someFunction',
kService: null,
kRevision: null,
});
const c = new Configuration(
{
serviceContext: {
Expand All @@ -227,7 +277,15 @@ describe('Testing service configuration', () => {
'A Configuration uses the service name and version in the given config ' +
'if they were both specified and only the FUNCTION_NAME env const is set',
() => {
setEnv(null, '1.0', null, 'InvalidVersion', 'someFunction');
setEnv({
gaeServiceName: null,
gaeServiceVersion: '1.0',
gaeModuleName: null,
gaeModuleVersion: 'InvalidVersion',
functionName: 'someFunction',
kService: null,
kRevision: null,
});
const c = new Configuration(
{
serviceContext: {
Expand Down Expand Up @@ -258,7 +316,15 @@ describe('Testing service configuration', () => {
'not specified a service name or version even if GAE_VERSION has ' +
'been set',
() => {
setEnv(null, 'InvalidVersion', null, 'InvalidVersion', null);
setEnv({
gaeServiceName: null,
gaeServiceVersion: 'InvalidVersion',
gaeModuleName: null,
gaeModuleVersion: 'InvalidVersion',
functionName: null,
kService: null,
kRevision: null,
});
const c = new Configuration({}, logger);
assert.strictEqual(c.getServiceContext().service, 'node');
assert.strictEqual(c.getServiceContext().version, undefined);
Expand All @@ -281,4 +347,32 @@ describe('Testing service configuration', () => {
deepStrictEqual(c.getServiceContext().version, '2.0');
}
);
it('A Configuration uses the K_SERVICE and K_REVISION env variables if set', () => {
setEnv({
gaeServiceName: null,
gaeServiceVersion: 'x',
gaeModuleName: null,
gaeModuleVersion: 'y',
functionName: null,
kService: 'custom-service',
kRevision: 'custom-revision',
});
const c = new Configuration({}, logger);
assert.strictEqual(c.getServiceContext().service, 'custom-service');
assert.strictEqual(c.getServiceContext().version, 'custom-revision');
});
it('A Configuration gives priority to K_SERVICE and K_REVISION env variables', () => {
setEnv({
gaeServiceName: 'gae-service-name',
gaeServiceVersion: 'gae-service-version',
gaeModuleName: 'gae-module-name',
gaeModuleVersion: 'gae-module-version',
functionName: 'function-name',
kService: 'k-service',
kRevision: 'k-revision',
});
const c = new Configuration({}, logger);
assert.strictEqual(c.getServiceContext().service, 'k-service');
assert.strictEqual(c.getServiceContext().version, 'k-revision');
});
});

0 comments on commit 453bd6e

Please sign in to comment.