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

feat: Add Cloud Run Jobs support #1574

Merged
merged 8 commits into from
Apr 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public final class MetadataLoader {
.put(Label.FunctionName, this::getFunctionName)
.put(Label.InstanceId, this::getInstanceId)
.put(Label.InstanceName, this::getInstanceName)
.put(Label.CloudRunJobName, this::getCloudRunJobName)
.put(Label.CloudRunJobExecutionName, this::getCloudRunJobExecutionName)
.put(Label.CloudRunJobTaskIndex, this::getCloudRunJobTaskIndex)
.put(Label.CloudRunJobTaskAttempt, this::getCloudRunJobTaskAttempt)
.put(Label.CloudRunLocation, this::getCloudRunLocation)
.put(Label.GKELocation, this::getGKELocation)
.put(Label.ModuleId, this::getModuleId)
Expand Down Expand Up @@ -84,6 +88,7 @@ private String getConfigName() {
private String getContainerName() {
return getter.getEnv("CONTAINER_NAME");
}

/**
* Distinguish between Standard and Flexible GAE environments. There is no indicator of the
* environment. The path to the startup-script in the metadata attribute was selected as one of
Expand Down Expand Up @@ -121,6 +126,22 @@ private String getInstanceName() {
return getter.getAttribute("instance/name");
}

private String getCloudRunJobName() {
return getter.getEnv("CLOUD_RUN_JOB");
}

private String getCloudRunJobExecutionName() {
return getter.getEnv("CLOUD_RUN_EXECUTION");
}

private String getCloudRunJobTaskIndex() {
return getter.getEnv("CLOUD_RUN_TASK_INDEX");
}

private String getCloudRunJobTaskAttempt() {
return getter.getEnv("CLOUD_RUN_TASK_ATTEMPT");
}

private String getCloudRunLocation() {
return getRegion();
}
Expand All @@ -132,6 +153,7 @@ private String getGKELocation() {
private String getModuleId() {
return getter.getEnv("GAE_SERVICE");
}

/**
* Heuristic to discover the namespace name of the current environment. There is no deterministic
* way to discover the namespace name of the process. The name is read from the {@link
Expand All @@ -155,6 +177,7 @@ private String getNamespaceName() {
}
return value;
}

// Kubernetes set hostname of the pod to the pod name by default, however hostname can be override
// in manifest
// allow users to provide the container name via environment variable
Expand All @@ -169,6 +192,7 @@ private String getPodName() {
private String getProjectId() {
return getter.getAttribute("project/project-id");
}

/**
* Retrieves a region from the qualified region of 'projects/[PROJECT_NUMBER]/regions/[REGION]'
*
Expand All @@ -193,6 +217,7 @@ private String getServiceName() {
private String getVersionId() {
return getter.getEnv("GAE_VERSION");
}

/**
* Retrieves a zone from the qualified zone of 'projects/[PROJECT_NUMBER]/zones/[ZONE]'
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class MonitoredResourceUtil {

private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/";
private static final String CLOUD_RUN_JOB_LABEL_PREFIX = "run.googleapis.com/";
protected static final String PORJECTID_LABEL = Label.ProjectId.getKey();

protected enum Label {
Expand All @@ -42,6 +43,10 @@ protected enum Label {
FunctionName("function_name"),
InstanceId("instance_id"),
InstanceName("instance_name"),
CloudRunJobName("job_name"),
CloudRunJobExecutionName("execution_name"),
CloudRunJobTaskIndex("task_index"),
CloudRunJobTaskAttempt("task_attempt"),
CloudRunLocation("location"),
GKELocation("location"),
ModuleId("module_id"),
Expand All @@ -67,6 +72,7 @@ String getKey() {

private enum Resource {
CLOUD_RUN("cloud_run_revision"),
CLOUD_RUN_JOB("cloud_run_job"),
CLOUD_FUNCTION("cloud_function"),
APP_ENGINE("gae_app"),
GCE_INSTANCE("gce_instance"),
Expand All @@ -93,6 +99,7 @@ String getKey() {
Label.ServiceName,
Label.CloudRunLocation,
Label.ConfigurationName)
.putAll(Resource.CLOUD_RUN_JOB.getKey(), Label.CloudRunJobName, Label.CloudRunLocation)
.putAll(
Resource.APP_ENGINE.getKey(), Label.ModuleId, Label.VersionId, Label.Zone, Label.Env)
.putAll(Resource.GCE_INSTANCE.getKey(), Label.InstanceId, Label.Zone)
Expand Down Expand Up @@ -177,6 +184,12 @@ private static Resource detectResourceType() {
&& getter.getEnv("K_CONFIGURATION") != null) {
return Resource.CLOUD_RUN;
}
if (getter.getEnv("CLOUD_RUN_JOB") != null
&& getter.getEnv("CLOUD_RUN_EXECUTION") != null
&& getter.getEnv("CLOUD_RUN_TASK_INDEX") != null
&& getter.getEnv("CLOUD_RUN_TASK_ATTEMPT") != null) {
return Resource.CLOUD_RUN_JOB;
}
if (getter.getEnv("GAE_INSTANCE") != null
&& getter.getEnv("GAE_SERVICE") != null
&& getter.getEnv("GAE_VERSION") != null) {
Expand Down Expand Up @@ -212,6 +225,14 @@ private static List<LoggingEnhancer> createEnhancers(Resource resourceType) {
enhancers.add(
new LabelLoggingEnhancer(APPENGINE_LABEL_PREFIX, ImmutableList.of(Label.InstanceName)));
}
} else if (resourceType == Resource.CLOUD_RUN_JOB) {
enhancers.add(
new LabelLoggingEnhancer(
CLOUD_RUN_JOB_LABEL_PREFIX,
ImmutableList.of(
Label.CloudRunJobExecutionName,
Label.CloudRunJobTaskIndex,
Label.CloudRunJobTaskAttempt)));
}
return enhancers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,32 @@ public void testResourceTypeCloudRun() {
assertEquals("cloud_run_revision", response.getType());
assertEquals(expectedLabels, response.getLabels());
}

@Test
public void testResourceTypeCloudRunJob() {
final String mockedJobName = "mocked-job-name";
final ImmutableMap<String, String> expectedLabels =
ImmutableMap.of(
"project_id",
MonitoredResourceUtilTest.MOCKED_PROJECT_ID,
"job_name",
mockedJobName,
"location",
MOCKED_REGION);

// setup
expect(getterMock.getAttribute("instance/region")).andReturn(MOCKED_QUALIFIED_REGION).once();
expect(getterMock.getAttribute(anyString())).andReturn(null).anyTimes();
expect(getterMock.getEnv("CLOUD_RUN_JOB")).andReturn(mockedJobName).times(2);
expect(getterMock.getEnv("CLOUD_RUN_EXECUTION")).andReturn(mockedJobName + "_1").times(1);
expect(getterMock.getEnv("CLOUD_RUN_TASK_INDEX")).andReturn("0").times(1);
expect(getterMock.getEnv("CLOUD_RUN_TASK_ATTEMPT")).andReturn("0").times(1);
expect(getterMock.getEnv(anyString())).andReturn(null).anyTimes();
replay(getterMock);
// exercise
MonitoredResource response = MonitoredResourceUtil.getResource("", "");
// verify
assertEquals("cloud_run_job", response.getType());
assertEquals(expectedLabels, response.getLabels());
}
}
Loading