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 12 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
3 changes: 3 additions & 0 deletions detectors/gcp/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
GKE
GCE
CloudRun
CloudRunJob
CloudFunctions
AppEngineStandard
AppEngineFlex
Expand All @@ -50,6 +51,8 @@ func (d *Detector) CloudPlatform() Platform {
return CloudFunctions
case d.onCloudRun():
return CloudRun
case d.onCloudRunJob():
return CloudRunJob
case d.onAppEngineStandard():
return AppEngineStandard
case d.onAppEngine():
Expand Down
14 changes: 12 additions & 2 deletions detectors/gcp/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,27 @@ 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()
assert.Equal(t, platform, CloudRunJob)
}

func TestCloudPlatformCloudFunctions(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudFunctionTargetEnv: "foo",
cloudFunctionsTargetEnv: "foo",
},
})
platform := d.CloudPlatform()
Expand Down
66 changes: 49 additions & 17 deletions detectors/gcp/faas.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,82 @@ import (
)

const (
// See the Cloud Run env vars:
// https://cloud.google.com/run/docs/reference/container-contract
// 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"
// Cloud Functions env vars:
// https://cloud.google.com/functions/docs/configuring/env-var#newer_runtimes
//
// Cloud Run env vars:
// https://cloud.google.com/run/docs/container-contract#services-env-vars
//
// Cloud Run jobs env vars:
// https://cloud.google.com/run/docs/container-contract#jobs-env-vars
cloudFunctionsTargetEnv = "FUNCTION_TARGET"
cloudRunConfigurationEnv = "K_CONFIGURATION"
cloudRunJobsEnv = "CLOUD_RUN_JOB"
faasServiceEnv = "K_SERVICE"
faasRevisionEnv = "K_REVISION"
cloudRunJobExecutionEnv = "CLOUD_RUN_EXECUTION"
cloudRunJobTaskIndexEnv = "CLOUD_RUN_TASK_INDEX"
regionMetadataAttr = "instance/region"
)

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

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

func (d *Detector) onCloudFunctions() bool {
_, found := d.os.LookupEnv(cloudFunctionTargetEnv)
func (d *Detector) onCloudRunJob() bool {
_, found := d.os.LookupEnv(cloudRunJobsEnv)
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 or Cloud Functions service.
func (d *Detector) FaaSVersion() (string, error) {
if version, found := d.os.LookupEnv(faasRevisionEnv); found {
return version, nil
}
return "", errEnvVarNotFound
}

// FaaSID returns the instance id of the cloud run instance or cloud function.
// CloudRunJobbExecution returns the execution id of the Cloud Run jobs.
func (d *Detector) CloudRunJobbExecution() (string, error) {
damemi marked this conversation as resolved.
Show resolved Hide resolved
if version, found := d.os.LookupEnv(cloudRunJobExecutionEnv); found {
return version, nil
}
return "", errEnvVarNotFound
}

// CloudRunJobTaskIndex returns the task index for the execution of the Cloud Run jobs.
func (d *Detector) CloudRunJobTaskIndex() (string, error) {
if version, found := d.os.LookupEnv(cloudRunJobTaskIndexEnv); found {
return version, nil
}
return "", errEnvVarNotFound
}

// FaaSID returns the instance id of the Cloud Run or Cloud Function.
func (d *Detector) FaaSID() (string, error) {
return d.metadata.InstanceID()
}

// FaaSCloudRegion detects region from the metadata server. It is in the
// format /projects/<project_number>/regions/<region>.
// FaaSCloudRegion detects region from the metadata server.
// It is in the format /projects/<project_number>/regions/<region>.
//
// https://cloud.google.com/run/docs/reference/container-contract#metadata-server
func (d *Detector) FaaSCloudRegion() (string, error) {
region, err := d.metadata.Get(regionMetadataAttr)
Expand Down
55 changes: 53 additions & 2 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 Down Expand Up @@ -61,7 +72,47 @@ func TestFaaSVersionErr(t *testing.T) {
assert.Equal(t, version, "")
}

func TestFaaSInstanceID(t *testing.T) {
func TestFaaSJobExecution(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudRunJobExecutionEnv: "version-123",
},
})
version, err := d.CloudRunJobbExecution()
assert.NoError(t, err)
assert.Equal(t, version, "version-123")
}

func TestFaaSJobExecutionErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{},
})
name, err := d.CloudRunJobbExecution()
assert.Error(t, err)
assert.Equal(t, name, "")
}

func TestFaaSJobTaskIndex(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{
cloudRunJobTaskIndexEnv: "5",
},
})
version, err := d.CloudRunJobTaskIndex()
assert.NoError(t, err)
assert.Equal(t, version, "5")
}

func TestFaaSJobTaskIndexErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{}, &FakeOSProvider{
Vars: map[string]string{},
})
name, err := d.CloudRunJobTaskIndex()
assert.Error(t, err)
assert.Equal(t, name, "")
}

func TestFaaSID(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{
FakeInstanceID: "instance-id-123",
}, &FakeOSProvider{})
Expand All @@ -70,7 +121,7 @@ func TestFaaSInstanceID(t *testing.T) {
assert.Equal(t, instance, "instance-id-123")
}

func TestFaaSInstanceIDErr(t *testing.T) {
func TestFaaSIDErr(t *testing.T) {
d := NewTestDetector(&FakeMetadataProvider{
Err: fmt.Errorf("fake error"),
}, &FakeOSProvider{})
Expand Down