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

Dynamic AMI selection: fix architecture filter #283

Merged
Merged
Changes from 1 commit
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
Next Next commit
Fix architecture, add volume size filter
  • Loading branch information
brunoasr committed Jul 15, 2024
commit 2cb71d236e57b62aea8630c0d2720e6f53593686
Original file line number Diff line number Diff line change
@@ -30,12 +30,12 @@

import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.BlockDeviceMapping;
import software.amazon.awssdk.services.ec2.model.DescribeImagesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeImagesResponse;
import software.amazon.awssdk.services.ec2.model.CreateTagsRequest;
import software.amazon.awssdk.services.ec2.model.CreateTagsResponse;
import software.amazon.awssdk.services.ec2.model.Filter;
import software.amazon.awssdk.services.ec2.model.Image;
import software.amazon.awssdk.services.ec2.model.Tag;

/**
@@ -50,8 +50,9 @@ public class AmiTagManager {
public static final String KEY_AMI_ID = "ami_id";
public static final String KEY_APPLICATION = "application";
public static final String KEY_RELEASE = "release";
public static final String KEY_CPU_ARCHITECTURE = "cpu_architecture";
public static final String KEY_ARCHITECTURE = "architecture";
public static final String KEY_APPLICATION_ENVIRONMENT = "application_environment";
public static final String KEY_VOLUME_SIZE = "ebs_volume_size";
public static final String VALUE_KAFKA = "kafka";
public static UnaryOperator<String> tag = key -> "tag:" + key;
public static final String ENV_TYPES_KEY = "envTypes";
@@ -82,10 +83,10 @@ public List<Ami> getAmiList(Map<String, String> filter) {
.values(filter.get(KEY_RELEASE))
.build()
);
if (filter.containsKey(KEY_CPU_ARCHITECTURE))
if (filter.containsKey(KEY_ARCHITECTURE))
filterList.add(
filterBuilder.name(tag.apply(KEY_CPU_ARCHITECTURE))
.values(filter.get(KEY_CPU_ARCHITECTURE))
filterBuilder.name(KEY_ARCHITECTURE)
.values(filter.get(KEY_ARCHITECTURE))
.build()
);
filterList.add(
@@ -98,9 +99,20 @@ public List<Ami> getAmiList(Map<String, String> filter) {
DescribeImagesResponse resp = ec2Client.describeImages(builder.build());
if (resp.hasImages() && !resp.images().isEmpty()) {
// The limitation of images newer than 180 days is temporarily suspended
//ZonedDateTime cutDate = ZonedDateTime.now().minusDays(180);
// ZonedDateTime cutDate = ZonedDateTime.now().minusDays(180);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean we are including all the images now? What is the rationale behind 180 days?
If we want to add a threshold like that I would suggest making it a configuration so it can be set as needed without having to change code.

boolean filterVolumeSize = filter.containsKey(KEY_VOLUME_SIZE);
int volumeSize = filterVolumeSize ? Integer.parseInt(filter.get(KEY_VOLUME_SIZE)) : 0;
resp.images().forEach(image -> {
/*if (ZonedDateTime.parse(image.creationDate(), DateTimeFormatter.ISO_ZONED_DATE_TIME).isAfter(cutDate)) {*/
boolean matchesVolumeSizeCriteria = ! filterVolumeSize;
if (filterVolumeSize) {
for (BlockDeviceMapping dev : image.blockDeviceMappings()) {
if (dev.deviceName().equals("/dev/sda1") && dev.ebs() != null)
matchesVolumeSizeCriteria = (dev.ebs().volumeSize() == volumeSize);
break;
}
}
if (matchesVolumeSizeCriteria) {
Iterator<Tag> i = image.tags().iterator();
Tag t;
String appEnvTag = null;
@@ -116,7 +128,7 @@ public List<Ami> getAmiList(Map<String, String> filter) {
appEnvTag,
image.creationDate()
));
// }
}
});
Function<Ami, ZonedDateTime> parse = i -> ZonedDateTime.parse(i.getCreationDate(), DateTimeFormatter.ISO_ZONED_DATE_TIME);
amiList.sort((a, b) -> - parse.apply(a).compareTo(parse.apply(b)));
Original file line number Diff line number Diff line change
@@ -119,13 +119,16 @@ public Map<String, Map<String, Utilization>> getUtilizationDetailsByCluster() {
@GET
public List<Ami> describeImages(
@QueryParam(AmiTagManager.KEY_RELEASE) String os,
@QueryParam(AmiTagManager.KEY_CPU_ARCHITECTURE) String arch
@QueryParam(AmiTagManager.KEY_ARCHITECTURE) String arch,
@QueryParam(AmiTagManager.KEY_VOLUME_SIZE) String volumeSize
) {
Map<String, String> filter = new HashMap<>();
if (os != null)
filter.put(AmiTagManager.KEY_RELEASE, os);
if (arch != null)
filter.put(AmiTagManager.KEY_CPU_ARCHITECTURE, arch);
filter.put(AmiTagManager.KEY_ARCHITECTURE, arch);
if (volumeSize != null)
filter.put(AmiTagManager.KEY_VOLUME_SIZE, volumeSize);
if (amiTagManager == null)
amiTagManager = new AmiTagManager();
return amiTagManager.getAmiList(filter);
Original file line number Diff line number Diff line change
@@ -49,9 +49,13 @@ function Ami({ amiList, requestAmiList, envTypes, requestEnvTypes, updateAmiTag
const handleOSChange = event => {
setOS(event.target.value);
};
const [cpuArch, setCPUArch] = React.useState();
const handleCPUArchChange = event => {
setCPUArch(event.target.value);
const [arch, setArch] = React.useState();
const handleArchChange = event => {
setArch(event.target.value);
};
const [volumeSize, setVolumeSize] = React.useState();
const handleVolumeSizeChange = event => {
setVolumeSize(event.target.value);
};
const [selected, setSelected] = React.useState([]);
const handleTableRowSelect = (id, row) => {
@@ -81,8 +85,10 @@ function Ami({ amiList, requestAmiList, envTypes, requestEnvTypes, updateAmiTag
const parms = [];
if (os)
parms.push("release=" + os);
if (cpuArch)
parms.push("cpu_architecture=" + cpuArch);
if (arch)
parms.push("architecture=" + arch);
if (volumeSize)
parms.push("ebs_volume_size=" + volumeSize);
requestAmiList(parms.join('&'));
requestEnvTypes();
}
@@ -121,24 +127,37 @@ function Ami({ amiList, requestAmiList, envTypes, requestEnvTypes, updateAmiTag
>
<MenuItem value={"bionic"}>bionic</MenuItem>
<MenuItem value={"focal"}>focal</MenuItem>
<MenuItem value={"noble"}>noble</MenuItem>
</Select>
</FormControl>
</div>
<div>
<FormControl className={classes.formControl}>
<InputLabel id="lblCPUArch">CPU Architecture</InputLabel>
<InputLabel id="lblArch">architecture</InputLabel>
<Select
labelId="lblSelectCPUArch"
id="selectCPUArch"
value={cpuArch}
onChange={handleCPUArchChange}
labelId="lblSelectArch"
id="selectArch"
value={arch}
onChange={handleArchChange}
style={{ width: "200px", textAlign: "left" }}
>
<MenuItem value={"amd64"}>amd64</MenuItem>
<MenuItem value={"x86_64"}>x86_64</MenuItem>
<MenuItem value={"arm64"}>arm64</MenuItem>
</Select>
</FormControl>
</div>
<div>
<FormControl className={classes.formControl}>
<TextField
id="volume_size"
label="volume_size"
value={volumeSize}
onChange={handleVolumeSizeChange}
style={{ width: "200px" }}
InputLabelProps={{ shrink: true }}
/>
</FormControl>
</div>
<div>
<FormControl className={classes.formControl}>
<TextField
Loading