Skip to content

Commit

Permalink
Issue #3534: Tag compute resources according to the tool's metadata -…
Browse files Browse the repository at this point in the history
… supported for aws provider only
  • Loading branch information
ekazachkova committed May 28, 2024
1 parent ea1a306 commit af745da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -238,7 +239,7 @@ private boolean isValueTrue(final String value) {
private Map<String, String> findCustomInstanceTags(final Long runId, final Optional<PipelineRun> optionalRun) {
final Optional<PipelineRun> run = optionalRun.isPresent() ? optionalRun : pipelineRunManager.findRun(runId);
if (!run.isPresent()) {
return new HashMap<>();
return Collections.emptyMap();
}
return metadataManager.prepareCustomInstanceTags(run.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.epam.pipeline.entity.pipeline.Folder;
import com.epam.pipeline.entity.pipeline.PipelineRun;
import com.epam.pipeline.entity.pipeline.Tool;
import com.epam.pipeline.entity.region.CloudProvider;
import com.epam.pipeline.entity.security.acl.AclClass;
import com.epam.pipeline.manager.EntityManager;
import com.epam.pipeline.manager.metadata.parser.MetadataLineProcessor;
Expand Down Expand Up @@ -357,11 +358,14 @@ public Set<String> getMetadataKeys(final AclClass entityClass) {
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public Map<String, String> prepareCustomInstanceTags(final PipelineRun run) {
try {
if (!CloudProvider.AWS.equals(run.getInstance().getCloudProvider())) {
return Collections.emptyMap();
}
final Map<String, String> customTags = resolveCommonCustomInstanceTags(run);
return resolveInstanceTagsFromMetadata(run.getDockerImage(), customTags);
} catch (Exception e) {
LOGGER.error("An error occurred during custom tags preparation for run '{}'.", run.getId(), e);
return new HashMap<>();
return Collections.emptyMap();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.epam.pipeline.entity.metadata.MetadataEntry;
import com.epam.pipeline.entity.metadata.PipeConfValue;
import com.epam.pipeline.entity.pipeline.PipelineRun;
import com.epam.pipeline.entity.pipeline.RunInstance;
import com.epam.pipeline.entity.pipeline.Tool;
import com.epam.pipeline.entity.region.CloudProvider;
import com.epam.pipeline.entity.security.acl.AclClass;
import com.epam.pipeline.manager.pipeline.ToolManager;
import com.epam.pipeline.manager.preference.PreferenceManager;
Expand Down Expand Up @@ -78,11 +80,7 @@ public void shouldPrepareTagsFromPreferenceAndToolsMetadata() {
when(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TAGS))
.thenReturn(buildCommonTagsMapping());

final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(TEST_RUN_ID));
run.setOwner(TEST_USER);
run.setDockerImage(TEST_IMAGE);

final PipelineRun run = run(TEST_RUN_ID, TEST_USER, TEST_IMAGE);
final Tool tool = new Tool();
tool.setId(TEST_TOOL_ID);
when(toolManager.loadByNameOrId(TEST_IMAGE)).thenReturn(tool);
Expand All @@ -107,10 +105,7 @@ public void shouldPrepareTagsFromPreference() {
when(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TAGS))
.thenReturn(buildCommonTagsMapping());

final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(TEST_RUN_ID));
run.setOwner(TEST_USER);
run.setDockerImage(TEST_IMAGE);
final PipelineRun run = run(TEST_RUN_ID, TEST_USER, TEST_IMAGE);

when(preferenceManager.findPreference(SystemPreferences.CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS))
.thenReturn(Optional.empty());
Expand All @@ -129,11 +124,7 @@ public void shouldPrepareTagsFromPreference() {
public void shouldPrepareTagsFromToolsMetadata() {
when(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TAGS)).thenReturn(null);

final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(TEST_RUN_ID));
run.setOwner(TEST_USER);
run.setDockerImage(TEST_IMAGE);

final PipelineRun run = run(TEST_RUN_ID, TEST_USER, TEST_IMAGE);
final Tool tool = new Tool();
tool.setId(TEST_TOOL_ID);
when(toolManager.loadByNameOrId(TEST_IMAGE)).thenReturn(tool);
Expand All @@ -154,11 +145,7 @@ public void shouldPrepareTagsFromToolsMetadata() {
public void shouldReturnEmptyTagsIfToolsMetadataNotMatch() {
when(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TAGS)).thenReturn(null);

final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(TEST_RUN_ID));
run.setOwner(TEST_USER);
run.setDockerImage(TEST_IMAGE);

final PipelineRun run = run(TEST_RUN_ID, TEST_USER, TEST_IMAGE);
final Tool tool = new Tool();
tool.setId(TEST_TOOL_ID);
when(toolManager.loadByNameOrId(TEST_IMAGE)).thenReturn(tool);
Expand All @@ -178,8 +165,7 @@ public void shouldReturnEmptyTagsIfError() {
when(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TAGS))
.thenReturn(buildCommonTagsMapping());

final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(TEST_RUN_ID));
final PipelineRun run = run(TEST_RUN_ID, null, null);

when(preferenceManager.findPreference(SystemPreferences.CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS))
.thenReturn(Optional.of(String.join(",", KEY_1, KEY_2)));
Expand All @@ -192,10 +178,7 @@ public void shouldReturnEmptyTagsIfError() {
public void shouldReturnEmptyTagsIfNoToolMetadata() {
when(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TAGS)).thenReturn(null);

final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(TEST_RUN_ID));
run.setDockerImage(TEST_IMAGE);

final PipelineRun run = run(TEST_RUN_ID, null, TEST_IMAGE);
final Tool tool = new Tool();
tool.setId(TEST_TOOL_ID);
when(toolManager.loadByNameOrId(TEST_IMAGE)).thenReturn(tool);
Expand Down Expand Up @@ -227,4 +210,19 @@ private static MetadataEntry toolMetadata(final EntityVO entityVO) {
metadataEntry.setData(data);
return metadataEntry;
}

private static RunInstance awsRunInstance() {
final RunInstance runInstance = new RunInstance();
runInstance.setCloudProvider(CloudProvider.AWS);
return runInstance;
}

private static PipelineRun run(final String runId, final String owner, final String tool) {
final PipelineRun run = new PipelineRun();
run.setId(Long.valueOf(runId));
run.setOwner(owner);
run.setDockerImage(tool);
run.setInstance(awsRunInstance());
return run;
}
}

0 comments on commit af745da

Please sign in to comment.