This repository was archived by the owner on Mar 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
feat: resource autodetection for cloud run #1024
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb54968
feat: resource autodetection for cloud run
bd79171
Merge branch 'master' into resourceDetection
freelerobot 7c35a88
fix: add location to cloud run resource label
51ba4d6
test: add unit test for getCloudRunDescriptor
d8cee9d
test: fix environments.cloudrun unit test
b59a9de
feat: add cloud run to detectServiceContext
b228d58
chore: update env-tests-logging submodule to latest
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule env-tests-logging
updated
2 files
| +15 −16 | tests/common/common.py | |
| +2 −2 | tests/nodejs/test_functions.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,8 @@ describe('metadata', () => { | |
| let AUTH; | ||
| const ENV_CACHED = extend({}, process.env); | ||
|
|
||
| const INITIAL_ENV: {[key: string]: string | undefined} = {}; | ||
|
|
||
| before(() => { | ||
| metadata = proxyquire('../src/metadata', { | ||
| 'gcp-metadata': fakeGcpMetadata, | ||
|
|
@@ -101,7 +103,6 @@ describe('metadata', () => { | |
| 'K_SERVICE', | ||
| 'GOOGLE_CLOUD_REGION', | ||
| ]; | ||
| const INITIAL_ENV: {[key: string]: string | undefined} = {}; | ||
|
|
||
| before(() => { | ||
| for (const key of TARGET_KEYS) { | ||
|
|
@@ -152,6 +153,56 @@ describe('metadata', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('getCloudRunDescriptor', () => { | ||
| const K_SERVICE = 'hello-world'; | ||
| const K_REVISION = 'hello-world.1'; | ||
| const K_CONFIGURATION = 'hello-world'; | ||
|
Comment on lines
+157
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make the values similar to actual ones.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed the official documentation on the sample values. https://cloud.google.com/run/docs/reference/container-contract |
||
|
|
||
| const TARGET_KEYS = ['K_SERVICE', 'K_REVISION', 'K_CONFIGURATION']; | ||
|
|
||
| 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.K_SERVICE = K_SERVICE; | ||
| process.env.K_REVISION = K_REVISION; | ||
| process.env.K_CONFIGURATION = K_CONFIGURATION; | ||
| }); | ||
|
|
||
| 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.getCloudRunDescriptor(); | ||
| assert.deepStrictEqual(descriptor, { | ||
| type: 'cloud_run_revision', | ||
| labels: { | ||
| service_name: K_SERVICE, | ||
| revision_name: K_REVISION, | ||
| configuration_name: K_CONFIGURATION, | ||
| location: ZONE_ID, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getGAEDescriptor', () => { | ||
| const GAE_MODULE_NAME = 'gae-module-name'; | ||
| const GAE_SERVICE = 'gae-service'; | ||
|
|
@@ -302,6 +353,47 @@ describe('metadata', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('cloud run', () => { | ||
| after(() => { | ||
| delete process.env['K_CONFIGURATION']; | ||
| delete process.env['K_REVISION']; | ||
| delete process.env['K_SERVICE']; | ||
| }); | ||
|
|
||
| it('should return correct descriptor', async () => { | ||
| const K_SERVICE = 'hello-world'; | ||
| const K_CONFIGURATION = 'hello-world'; | ||
| const K_REVISION = 'hello-world.1'; | ||
| const ZONE_ID = 'cyrodiil-anvil-2'; | ||
| const ZONE_FULL = `projects/fake-project/zones/${ZONE_ID}`; | ||
| process.env.K_SERVICE = K_SERVICE; | ||
| process.env.K_REVISION = K_REVISION; | ||
| process.env.K_CONFIGURATION = K_CONFIGURATION; | ||
| instanceOverride = [ | ||
| { | ||
| path: 'zone', | ||
| successArg: ZONE_FULL, | ||
| }, | ||
| ]; | ||
| const fakeAuth = { | ||
| async getEnv() { | ||
| return GCPEnv.COMPUTE_ENGINE; | ||
| }, | ||
| }; | ||
|
|
||
| const defaultResource = await metadata.getDefaultResource(fakeAuth); | ||
| assert.deepStrictEqual(defaultResource, { | ||
| type: 'cloud_run_revision', | ||
| labels: { | ||
| service_name: K_SERVICE, | ||
| revision_name: K_REVISION, | ||
| configuration_name: K_CONFIGURATION, | ||
| location: ZONE_ID, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('compute engine', () => { | ||
| it('should return correct descriptor', async () => { | ||
| const INSTANCE_ID = 1234567; | ||
|
|
@@ -458,6 +550,23 @@ describe('metadata', () => { | |
| assert.deepStrictEqual(sc1, {service: FUNCTION_NAME}); | ||
| }); | ||
|
|
||
| it('should return the correct descriptor for Cloud Run', async () => { | ||
| process.env.K_CONFIGURATION = 'hello-world'; | ||
| const SERVICE_NAME = (process.env.K_SERVICE = 'hello-world'); | ||
|
|
||
| const fakeAuth = { | ||
| async getEnv() { | ||
| return GCPEnv.COMPUTE_ENGINE; | ||
| }, | ||
| }; | ||
|
|
||
| const sc1 = await metadata.detectServiceContext(fakeAuth); | ||
| assert.deepStrictEqual(sc1, {service: SERVICE_NAME}); | ||
|
|
||
| delete process.env['K_CONFIGURATION']; | ||
| delete process.env['K_SERVICE']; | ||
| }); | ||
|
|
||
| it('should return null on GKE', async () => { | ||
| const fakeAuth = { | ||
| async getEnv() { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason it doesn't follow a similar pattern as the rest of them? GCPEnv.CLOUD_RUN?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GCPEnv from google-auth-library detects cloudrun as compute_engine. So I look for the K_CONFIG envvar unique to cloud run within this switch case
Also I just filed issue for this in google-auth-lib here: googleapis/google-auth-library-nodejs#1155