这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.
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 @@ -44,6 +44,8 @@ public class NoGeneratedAMIRule implements Rule {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(NoGeneratedAMIRule.class);

private String ownerEmailOverride = null;

private static final String TERMINATION_REASON = "No AMI is generated for this snapshot";

private final MonkeyCalendar calendar;
Expand All @@ -67,14 +69,33 @@ public class NoGeneratedAMIRule implements Rule {
* as cleanup candidate
*/
public NoGeneratedAMIRule(MonkeyCalendar calendar, int ageThreshold, int retentionDays) {
this(calendar, ageThreshold, retentionDays, null);
}

/**
* Constructor.
*
* @param calendar
* The calendar used to calculate the termination time
* @param ageThreshold
* The number of days that a snapshot is considered as cleanup candidate since it is created
* @param retentionDays
* The number of days that the volume is retained before being terminated after being marked
* as cleanup candidate
* @param ownerEmailOverride
* If null, send notifications to the resource owner.
* If not null, send notifications to the provided owner email address instead of the resource owner.
*/
public NoGeneratedAMIRule(MonkeyCalendar calendar, int ageThreshold, int retentionDays, String ownerEmailOverride) {
Validate.notNull(calendar);
Validate.isTrue(ageThreshold >= 0);
Validate.isTrue(retentionDays >= 0);
this.calendar = calendar;
this.ageThreshold = ageThreshold;
this.retentionDays = retentionDays;
this.ownerEmailOverride = ownerEmailOverride;
}

@Override
public boolean isValid(Resource resource) {
Validate.notNull(resource);
Expand All @@ -96,6 +117,9 @@ public boolean isValid(Resource resource) {
Date userSpecifiedDate = new Date(TERMINATION_DATE_FORMATTER.parseDateTime(janitorTag).getMillis());
resource.setExpectedTerminationTime(userSpecifiedDate);
resource.setTerminationReason(String.format("User specified termination date %s", janitorTag));
if (ownerEmailOverride != null) {
resource.setOwnerEmail(ownerEmailOverride);
}
return false;
} catch (Exception e) {
LOGGER.error(String.format("The janitor tag is not a user specified date: %s", janitorTag));
Expand All @@ -113,6 +137,9 @@ public boolean isValid(Resource resource) {
DateTime launchTime = new DateTime(resource.getLaunchTime().getTime());
DateTime now = new DateTime(calendar.now().getTimeInMillis());
if (launchTime.plusDays(ageThreshold).isBefore(now)) {
if (ownerEmailOverride != null) {
resource.setOwnerEmail(ownerEmailOverride);
}
if (resource.getExpectedTerminationTime() == null) {
Date terminationTime = calendar.getBusinessDay(new Date(now.getMillis()), retentionDays);
resource.setExpectedTerminationTime(terminationTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ private EBSSnapshotJanitor getEBSSnapshotJanitor() {
ruleEngine.addRule(new NoGeneratedAMIRule(monkeyCalendar,
(int) configuration().getNumOrElse("simianarmy.janitor.rule.noGeneratedAMIRule.ageThreshold", 30),
(int) configuration().getNumOrElse(
"simianarmy.janitor.rule.noGeneratedAMIRule.retentionDays", 7)));
"simianarmy.janitor.rule.noGeneratedAMIRule.retentionDays", 7),
configuration().getStrOrElse(
"simianarmy.janitor.rule.noGeneratedAMIRule.ownerEmail", null)));
}
if (configuration().getBoolOrElse("simianarmy.janitor.rule.untaggedRule.enabled", false)) {
ruleEngine.addRule(new UntaggedRule(monkeyCalendar, getPropertySet("simianarmy.janitor.rule.untaggedRule.requiredTags"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ public void testResourceWithExpectedTerminationTimeSet() {
Assert.assertEquals(oldTermReason, resource.getTerminationReason());
}

@Test
public void testOldSnapshotWithoutAMIWithOwnerOverride() {
int ageThreshold = 5;
DateTime now = DateTime.now();
Resource resource = new AWSResource().withId("snap123").withOwnerEmail("owner@netflix.com").withResourceType(AWSResourceType.EBS_SNAPSHOT)
.withLaunchTime(new Date(now.minusDays(ageThreshold + 1).getMillis()));
((AWSResource) resource).setAWSResourceState("completed");
int retentionDays = 4;
NoGeneratedAMIRule rule = new NoGeneratedAMIRule(new TestMonkeyCalendar(),
ageThreshold, retentionDays, "new_owner@netflix.com");
Assert.assertFalse(rule.isValid(resource));
Assert.assertEquals(resource.getOwnerEmail(), "new_owner@netflix.com");
TestUtils.verifyTerminationTimeRough(resource, retentionDays, now);
}

@Test
public void testOldSnapshotWithoutAMIWithoutOwnerOverride() {
int ageThreshold = 5;
DateTime now = DateTime.now();
Resource resource = new AWSResource().withId("snap123").withOwnerEmail("owner@netflix.com").withResourceType(AWSResourceType.EBS_SNAPSHOT)
.withLaunchTime(new Date(now.minusDays(ageThreshold + 1).getMillis()));
((AWSResource) resource).setAWSResourceState("completed");
int retentionDays = 4;
NoGeneratedAMIRule rule = new NoGeneratedAMIRule(new TestMonkeyCalendar(),
ageThreshold, retentionDays);
Assert.assertFalse(rule.isValid(resource));
Assert.assertEquals(resource.getOwnerEmail(), "owner@netflix.com");
TestUtils.verifyTerminationTimeRough(resource, retentionDays, now);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNullResource() {
NoGeneratedAMIRule rule = new NoGeneratedAMIRule(new TestMonkeyCalendar(), 5, 4);
Expand Down