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 23, 2024
1 parent d35113c commit e0a8b63
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ RunInstance scaleUpNode(Long runId, RunInstance instance, Map<String, String> ru

boolean instanceScalingSupported(Long cloudRegionId);

void deleteInstanceTags(Long regionId, String instanceId, Set<String> tagNames);
void deleteInstanceTags(Long regionId, String runId, Set<String> tagNames);
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ public boolean instanceScalingSupported(final Long regionId) {
return region.getProvider() != CloudProvider.LOCAL;
}

public void deleteInstanceTags(final Long regionId, final String instanceId, final Set<String> tagNames) {
public void deleteInstanceTags(final Long regionId, final String runId, final Set<String> tagNames) {
final AbstractCloudRegion region = regionManager.loadOrDefault(regionId);
getInstanceService(region).deleteInstanceTags(region, instanceId, tagNames);
getInstanceService(region).deleteInstanceTags(region, runId, tagNames);
}

private AbstractCloudRegion getRegionByRunId(final Long runId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,5 @@ default boolean isNodeExpired(T region, Long runId, Integer keepAliveMinutes) {

void adjustOfferRequest(InstanceOfferRequestVO requestVO);

void deleteInstanceTags(T region, String instanceId, Set<String> tagNames);
void deleteInstanceTags(T region, String runId, Set<String> tagNames);
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ public void adjustOfferRequest(final InstanceOfferRequestVO requestVO) {
}

@Override
public void deleteInstanceTags(final AwsRegion region, final String instanceId, final Set<String> tagNames) {
ec2Helper.deleteInstanceTags(region, instanceId, tagNames);
public void deleteInstanceTags(final AwsRegion region, final String runId, final Set<String> tagNames) {
ec2Helper.deleteInstanceTags(region, runId, tagNames);
}

private String buildNodeUpCommand(final AwsRegion region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -359,10 +360,16 @@ public void createAndAttachVolume(final String runId, final Long size,
enableVolumeDeletionOnInstanceTermination(client, instance.getInstanceId(), device);
}

public void deleteInstanceTags(final AwsRegion awsRegion, final String instanceId, final Set<String> tags) {
public void deleteInstanceTags(final AwsRegion awsRegion, final String runId, final Set<String> tags) {
final AmazonEC2 client = getEC2Client(awsRegion);
final Instance instance = getAliveInstance(runId, awsRegion);

final List<String> resourcesIds = new ArrayList<>();
resourcesIds.add(instance.getInstanceId());
resourcesIds.addAll(getVolumeIds(instance));

client.deleteTags(new DeleteTagsRequest()
.withResources(instanceId)
.withResources(resourcesIds)
.withTags(tags.stream()
.map(Tag::new)
.collect(Collectors.toList())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void adjustOfferRequest(final InstanceOfferRequestVO requestVO) {
}

@Override
public void deleteInstanceTags(final AzureRegion region, final String instanceId, final Set<String> tagNames) {
public void deleteInstanceTags(final AzureRegion region, final String runId, final Set<String> tagNames) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void adjustOfferRequest(final InstanceOfferRequestVO requestVO) {
}

@Override
public void deleteInstanceTags(final GCPRegion region, final String instanceId, final Set<String> tagNames) {
public void deleteInstanceTags(final GCPRegion region, final String runId, final Set<String> tagNames) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void adjustOfferRequest(InstanceOfferRequestVO requestVO) {
}

@Override
public void deleteInstanceTags(final LocalRegion region, final String instanceId, final Set<String> tagNames) {
public void deleteInstanceTags(final LocalRegion region, final String runId, final Set<String> tagNames) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -60,7 +61,16 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -346,7 +356,6 @@ public Set<String> getMetadataKeys(final AclClass entityClass) {

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);
Expand Down Expand Up @@ -420,8 +429,11 @@ private Map<String, String> resolveInstanceTagsFromMetadata(final MetadataEntry
if (MapUtils.isEmpty(metadataData)) {
return customTags;
}
final Set<String> instanceTagsKeys = preferenceManager.getPreference(
SystemPreferences.CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS);
final Set<String> instanceTagsKeys = new HashSet<>(Arrays.asList(preferenceManager.findPreference(
SystemPreferences.CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS)
.filter(StringUtils::isNotBlank)
.map(value -> value.split(","))
.orElse(Strings.EMPTY_ARRAY)));
if (CollectionUtils.isEmpty(instanceTagsKeys)) {
return customTags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@ public PipelineRun updatePipelineStatusIfNotFinal(Long runId, TaskStatus status)
pipelineRun.getId(), pipelineRun.getStatus());
return pipelineRun;
}
if (status.isFinal()) {
removeInstanceTags(pipelineRun);
}
if (pipelineRun.getExecutionPreferences().getEnvironment() == ExecutionEnvironment.DTS
&& status == TaskStatus.STOPPED) {
configurationProviderManager.stop(runId, pipelineRun.getExecutionPreferences());
Expand All @@ -697,10 +700,6 @@ public PipelineRun updatePipelineStatus(final PipelineRun run) {
*/
@Transactional(propagation = Propagation.REQUIRED)
public PipelineRun stop(Long runId) {
final PipelineRun run = loadPipelineRun(runId);
final Map<String, String> tags = metadataManager.buildCustomInstanceTags(run);
final RunInstance instance = run.getInstance();
cloudFacade.deleteInstanceTags(instance.getCloudRegionId(), instance.getNodeName(), tags.keySet());
return updatePipelineStatusIfNotFinal(runId, TaskStatus.STOPPED);
}

Expand Down Expand Up @@ -1810,4 +1809,17 @@ private ContextualPreferenceExternalResource getRegionContextualPreference(final
: regionId.toString();
return new ContextualPreferenceExternalResource(ContextualPreferenceLevel.REGION, regionIdStr);
}

@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void removeInstanceTags(final PipelineRun run) {
try {
final Map<String, String> tags = metadataManager.buildCustomInstanceTags(run);
if (MapUtils.isNotEmpty(tags)) {
final RunInstance instance = run.getInstance();
cloudFacade.deleteInstanceTags(instance.getCloudRegionId(), run.getId().toString(), tags.keySet());
}
} catch (Exception e) {
log.error("An error occurred during cloud resource tags removal for run '{}'", run.getId(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import com.epam.pipeline.security.ExternalServiceEndpoint;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.SetUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.AntPathMatcher;

Expand All @@ -45,7 +45,11 @@
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiPredicate;

import static com.epam.pipeline.manager.preference.SystemPreferences.DOCKER_SECURITY_TOOL_SCAN_CLAIR_ROOT_URL;
Expand Down Expand Up @@ -300,15 +304,16 @@ public static BiPredicate<String, Map<String, Preference>> isNullOrValidEnum(fin
);

public static final BiPredicate<String, Map<String, Preference>> isValidInstanceCustomTags =
isNullOrValidJson(new TypeReference<Set<String>>() {})
.and((pref, dependencies) -> {
final Set<String> customTags = JsonMapper.parseData(pref, new TypeReference<Set<String>>() {});
if (SetUtils.emptyIfNull(customTags).stream()
.anyMatch(tag -> tag.toUpperCase(Locale.ROOT).equals(NOT_ALLOWED_INSTANCE_TAG))) {
throw new IllegalArgumentException("Tag 'Name' is not allowed for custom instance tags.");
}
return true;
});
(pref, dependencies) -> {
if (Arrays.stream(Optional.ofNullable(pref)
.filter(StringUtils::isNotBlank)
.map(value -> value.split(","))
.orElse(Strings.EMPTY_ARRAY))
.anyMatch(tag -> tag.toUpperCase(Locale.ROOT).equals(NOT_ALLOWED_INSTANCE_TAG))) {
throw new IllegalArgumentException("Tag 'Name' is not allowed for custom instance tags.");
}
return true;
};

private PreferenceValidators() {
// No-op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,10 @@ public class SystemPreferences {
public static final ObjectPreference<Map<CommonCustomInstanceTagsTypes, String>> CLUSTER_INSTANCE_TAGS =
new ObjectPreference<>("cluster.instances.tags", null,
new TypeReference<Map<CommonCustomInstanceTagsTypes, String>>() {}, CLUSTER_GROUP,
isValidInstanceCustomTags);
isNullOrValidJson(new TypeReference<Map<CommonCustomInstanceTagsTypes, String>>() {}));

public static final ObjectPreference<Set<String>> CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS =
new ObjectPreference<>("cluster.instances.allowed.custom.tags", null,
new TypeReference<Set<String>>() {}, CLUSTER_GROUP,
isNullOrValidJson(new TypeReference<Set<String>>() {}));
public static final StringPreference CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS = new StringPreference(
"cluster.instances.allowed.custom.tags", null, CLUSTER_GROUP, isValidInstanceCustomTags);

//LAUNCH_GROUP
public static final StringPreference LAUNCH_CMD_TEMPLATE = new StringPreference("launch.cmd.template",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.SetUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -162,8 +166,11 @@ private boolean isSameUser(final Long entityId) {
}

private boolean isMetadataContainsRestrictedInstanceValues(final MetadataVO metadata) {
final Set<String> allowedCustomTags = SetUtils.emptyIfNull(preferenceManager
.getPreference(SystemPreferences.CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS));
final Set<String> allowedCustomTags = new HashSet<>(Arrays.asList(preferenceManager.findPreference(
SystemPreferences.CLUSTER_INSTANCE_ALLOWED_CUSTOM_TAGS)
.filter(StringUtils::isNotBlank)
.map(value -> value.split(","))
.orElse(Strings.EMPTY_ARRAY)));
if (CollectionUtils.isEmpty(allowedCustomTags)) {
return false;
}
Expand Down

0 comments on commit e0a8b63

Please sign in to comment.