Skip to content
Open
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
37 changes: 35 additions & 2 deletions src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ export async function getCloudRunDescriptor() {
};
}

/**
* Create a descriptor for Cloud Run Jpbs.
*
* @returns {object}
*/
export async function getCloudRunJobsDescriptor() {
const qualifiedZone = await gcpMetadata.instance('zone');
const location = regionFromQualifiedZone(qualifiedZone);
return {
type: 'cloud_run_job',
labels: {
location,
job_name: process.env.CLOUD_RUN_JOB,
},
};
}

/**
* Create a descriptor for Google App Engine.
*
Expand Down Expand Up @@ -196,7 +213,14 @@ export async function getDefaultResource(auth: GoogleAuth) {
case GCPEnv.CLOUD_RUN:
return getCloudRunDescriptor().catch(() => getGlobalDescriptor());
case GCPEnv.COMPUTE_ENGINE:
return getGCEDescriptor().catch(() => getGlobalDescriptor());
// Note: GCPEnv.COMPUTE_ENGINE returns `true` for Google Cloud Run Jobs
// should use case when available
// case GCPEnv.CLOUD_RUN_JOBS:
if (process.env.CLOUD_RUN_JOB) {
return getCloudRunJobsDescriptor().catch(() => getGlobalDescriptor());
} else {
return getGCEDescriptor().catch(() => getGlobalDescriptor());
}
default:
return getGlobalDescriptor();
}
Expand Down Expand Up @@ -235,7 +259,16 @@ export async function detectServiceContext(
service: process.env.K_SERVICE,
};
case GCPEnv.COMPUTE_ENGINE:
return null;
// Note: GCPEnv.COMPUTE_ENGINE returns `true` for Google Cloud Run Jobs
// should use case when available
// case GCPEnv.CLOUD_RUN_JOBS:
if (process.env.CLOUD_RUN_JOB) {
return {
service: process.env.CLOUD_RUN_JOB,
};
} else {
return null;
}
default:
return null;
}
Expand Down
44 changes: 44 additions & 0 deletions test/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,50 @@ describe('metadata', () => {
});
});

describe('getCloudRunJobsDescriptor', () => {
const CLOUD_RUN_JOB = 'hello-world';

const TARGET_KEYS = ['CLOUD_RUN_JOB'];

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.CLOUD_RUN_JOB = CLOUD_RUN_JOB;
});

it('should return the correct descriptor', async () => {
const ZONE_ID = 'cyrodiil-anvil-2';
const ZONE_FULL = `projects/fake-project/zones/${ZONE_ID}`;
instanceOverride = {path: 'zone', successArg: ZONE_FULL};
const descriptor = await metadata.getCloudRunJobsDescriptor();
assert.deepStrictEqual(descriptor, {
type: 'cloud_run_job',
labels: {
job_name: CLOUD_RUN_JOB,
location: 'cyrodiil-anvil',
},
});
});
});

describe('getGAEDescriptor', () => {
const GAE_MODULE_NAME = 'gae-module-name';
const GAE_SERVICE = 'gae-service';
Expand Down
Loading