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
… cleanup
  • Loading branch information
ekazachkova committed May 27, 2024
1 parent bcc82d7 commit 22170a0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
* limitations under the License.
*/

package com.epam.pipeline.entity.pipeline;
package com.epam.pipeline.entity.metadata;

/**
* Provides tag types for common instance tags that can be applied via
* {@link com.epam.pipeline.manager.preference.SystemPreferences#CLUSTER_INSTANCE_TAGS}
*
* Supported values:
* - tool - Docker image of a tool used for a run
* - run_id - Integer ID of a run
* - owner - Username of a run owner
*/
public enum CommonCustomInstanceTagsTypes {
tool, run_id, owner
}
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public InstanceRequest getNewRunInstance(final PipelineRun run) throws GitClient
instanceRequest.setInstance(instance);
instanceRequest.setRequestedImage(run.getActualDockerImage());
instanceRequest.setRuntimeParameters(buildRuntimeParameters(run));
instanceRequest.setCustomTags(metadataManager.buildCustomInstanceTags(run));
instanceRequest.setCustomTags(metadataManager.prepareCustomInstanceTags(run));
return instanceRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,6 @@ private Map<String, String> findCustomInstanceTags(final Long runId, final Optio
if (!run.isPresent()) {
return new HashMap<>();
}
return metadataManager.buildCustomInstanceTags(run.get());
return metadataManager.prepareCustomInstanceTags(run.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.epam.pipeline.entity.metadata.MetadataEntry;
import com.epam.pipeline.entity.metadata.MetadataEntryWithIssuesCount;
import com.epam.pipeline.entity.metadata.PipeConfValue;
import com.epam.pipeline.entity.pipeline.CommonCustomInstanceTagsTypes;
import com.epam.pipeline.entity.metadata.CommonCustomInstanceTagsTypes;
import com.epam.pipeline.entity.pipeline.Folder;
import com.epam.pipeline.entity.pipeline.PipelineRun;
import com.epam.pipeline.entity.pipeline.Tool;
Expand Down Expand Up @@ -354,11 +354,17 @@ public Set<String> getMetadataKeys(final AclClass entityClass) {
return keys;
}

public Map<String, String> buildCustomInstanceTags(final PipelineRun run) {
final Map<String, String> customTags = resolveCommonCustomInstanceTags(run);
final Tool tool = toolManager.loadByNameOrId(run.getDockerImage());
final MetadataEntry toolMetadata = loadMetadataItem(tool.getId(), AclClass.TOOL);
return resolveInstanceTagsFromMetadata(toolMetadata, customTags);
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public Map<String, String> prepareCustomInstanceTags(final PipelineRun run) {
try {
final Map<String, String> customTags = resolveCommonCustomInstanceTags(run);
final Tool tool = toolManager.loadByNameOrId(run.getDockerImage());
final MetadataEntry toolMetadata = loadMetadataItem(tool.getId(), AclClass.TOOL);
return resolveInstanceTagsFromMetadata(toolMetadata, customTags);
} catch (Exception e) {
LOGGER.error("An error occurred during custom tags preparation for run '{}'.", run.getId(), e);
return new HashMap<>();
}
}

Map<String, PipeConfValue> convertFileContentToMetadata(MultipartFile file) {
Expand Down Expand Up @@ -467,7 +473,7 @@ private void fillCommonCustomInstanceTags(final CommonCustomInstanceTagsTypes ta
break;
default:
throw new IllegalArgumentException(
String.format("Failed to resolve custom instance type '%s'", tagType));
String.format("Failed to resolve custom instance tag type '%s'", tagType));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ public PipelineRun updatePipelineStatusIfNotFinal(Long runId, TaskStatus status)
return pipelineRun;
}
if (status.isFinal()) {
removeInstanceTags(pipelineRun);
tryRemoveInstanceTags(pipelineRun);
}
if (pipelineRun.getExecutionPreferences().getEnvironment() == ExecutionEnvironment.DTS
&& status == TaskStatus.STOPPED) {
Expand Down Expand Up @@ -1195,7 +1195,7 @@ public PipelineRun attachDisk(final Long runId, final DiskAttachRequest request)
messageHelper.getMessage(MessageConstants.ERROR_RUN_DISK_SIZE_NOT_FOUND));
Assert.isTrue(request.getSize() > 0,
messageHelper.getMessage(MessageConstants.ERROR_INSTANCE_DISK_IS_INVALID, request.getSize()));
final Map<String, String> resourceTags = metadataManager.buildCustomInstanceTags(pipelineRun);
final Map<String, String> resourceTags = metadataManager.prepareCustomInstanceTags(pipelineRun);
nodesManager.attachDisk(pipelineRun, request, resourceTags);
return pipelineRun;
}
Expand Down Expand Up @@ -1812,9 +1812,9 @@ private ContextualPreferenceExternalResource getRegionContextualPreference(final
}

@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void removeInstanceTags(final PipelineRun run) {
private void tryRemoveInstanceTags(final PipelineRun run) {
try {
final Map<String, String> tags = metadataManager.buildCustomInstanceTags(run);
final Map<String, String> tags = metadataManager.prepareCustomInstanceTags(run);
if (MapUtils.isNotEmpty(tags)) {
final RunInstance instance = run.getInstance();
cloudFacade.deleteInstanceTags(instance.getCloudRegionId(), run.getId().toString(), tags.keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.epam.pipeline.entity.monitoring.IdleRunAction;
import com.epam.pipeline.entity.monitoring.LongPausedRunAction;
import com.epam.pipeline.entity.notification.filter.NotificationFilter;
import com.epam.pipeline.entity.pipeline.CommonCustomInstanceTagsTypes;
import com.epam.pipeline.entity.metadata.CommonCustomInstanceTagsTypes;
import com.epam.pipeline.entity.pipeline.run.RunVisibilityPolicy;
import com.epam.pipeline.entity.pipeline.run.parameter.RuntimeParameter;
import com.epam.pipeline.entity.preference.Preference;
Expand Down

0 comments on commit 22170a0

Please sign in to comment.