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

detectors/gcp: support Cloud Run jobs #465

Merged
merged 13 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 12 additions & 2 deletions detectors/gcp/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ func TestCloudPlatformGCE(t *testing.T) {
func TestCloudPlatformCloudRun(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudRunConfigEnv: "foo",
cloudRunConfigurationEnv: "foo",
},
})
platform := d.CloudPlatform()
assert.Equal(t, platform, CloudRun)
}

func TestCloudPlatformCloudRunJobs(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudRunJobsEnv: "foo",
},
})
platform := d.CloudPlatform()
Expand All @@ -73,7 +83,7 @@ func TestCloudPlatformCloudRun(t *testing.T) {
func TestCloudPlatformCloudFunctions(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudFunctionTargetEnv: "foo",
cloudFunctionsTargetEnv: "foo",
},
})
platform := d.CloudPlatform()
Expand Down
42 changes: 29 additions & 13 deletions detectors/gcp/faas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,60 @@
package gcp

import (
"fmt"
"strings"
)

const (
// See the Cloud Run env vars:
// https://cloud.google.com/run/docs/reference/container-contract
// https://cloud.google.com/run/docs/container-contract#services-env-vars
// and the Cloud Functions env vars:
// https://cloud.google.com/functions/docs/configuring/env-var#runtime_environment_variables_set_automatically
cloudRunConfigEnv = "K_CONFIGURATION"
cloudFunctionTargetEnv = "FUNCTION_TARGET"
faasServiceEnv = "K_SERVICE"
faasRevisionEnv = "K_REVISION"
regionMetadataAttr = "instance/region"
// https://cloud.google.com/functions/docs/configuring/env-var#python_37_and_go_111
cloudFunctionsTargetEnv = "FUNCTION_TARGET"
cloudRunConfigurationEnv = "K_CONFIGURATION"
cloudRunJobsEnv = "CLOUD_RUN_JOB"
faasServiceEnv = "K_SERVICE"
faasRevisionEnv = "K_REVISION"
jobsRevisionEnv = "CLOUD_RUN_EXECUTION"
damemi marked this conversation as resolved.
Show resolved Hide resolved
jobsTaskIndexEnv = "CLOUD_RUN_TASK_INDEX"
regionMetadataAttr = "instance/region"
)

func (d *Detector) onCloudRun() bool {
_, found := d.os.LookupEnv(cloudRunConfigEnv)
func (d *Detector) onCloudFunctions() bool {
_, found := d.os.LookupEnv(cloudFunctionsTargetEnv)
return found
}

func (d *Detector) onCloudFunctions() bool {
_, found := d.os.LookupEnv(cloudFunctionTargetEnv)
func (d *Detector) onCloudRun() bool {
if _, found := d.os.LookupEnv(cloudRunConfigurationEnv); found {
return found
}

_, found := d.os.LookupEnv(cloudRunJobsEnv)
damemi marked this conversation as resolved.
Show resolved Hide resolved
return found
}

// FaaSName returns the name of the cloud run or cloud functions service.
// FaaSName returns the name of the cloud run, cloud run jobs or cloud functions service.
func (d *Detector) FaaSName() (string, error) {
if name, found := d.os.LookupEnv(faasServiceEnv); found {
return name, nil
}
if name, found := d.os.LookupEnv(cloudRunJobsEnv); found {
return name, nil
}
return "", errEnvVarNotFound
}

// FaaSVersion returns the revision of the cloud run or cloud functions service.
// FaaSVersion returns the revision of the cloud run, cloud run jobs or cloud functions service.
func (d *Detector) FaaSVersion() (string, error) {
if version, found := d.os.LookupEnv(faasRevisionEnv); found {
return version, nil
}
if version, found := d.os.LookupEnv(jobsRevisionEnv); found {
if index, found := d.os.LookupEnv(jobsTaskIndexEnv); found {
return fmt.Sprintf("%s/%s", version, index), nil
}
}
damemi marked this conversation as resolved.
Show resolved Hide resolved
return "", errEnvVarNotFound
}

Expand Down
23 changes: 23 additions & 0 deletions detectors/gcp/faas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func TestFaaSName(t *testing.T) {
assert.Equal(t, name, "my-service")
}

func TestFaaSJobsName(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudRunJobsEnv: "my-service",
},
})
name, err := d.FaaSName()
assert.NoError(t, err)
assert.Equal(t, name, "my-service")
}

func TestFaaSNameErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{},
Expand All @@ -52,6 +63,18 @@ func TestFaaSVersion(t *testing.T) {
assert.Equal(t, version, "version-123")
}

func TestFaaSJobsVersion(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
jobsRevisionEnv: "version-123",
jobsTaskIndexEnv: "0",
},
})
version, err := d.FaaSVersion()
assert.NoError(t, err)
assert.Equal(t, version, "version-123/0")
}

func TestFaaSVersionErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{},
Expand Down