Skip to content

Commit

Permalink
fix(aws): CleanupAlarmsAgent with an optional user-defined name pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
christosarvanitis committed Dec 13, 2024
1 parent 3192885 commit 1dc05f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AwsConfigurationProperties {
static class AlarmsConfig {
boolean enabled = false
int daysToKeep = 90
String alarmsNamePattern = ".+-v[0-9]{3}-alarm-.+"
}

@NestedConfigurationProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,35 @@ class CleanupAlarmsAgent implements RunnableAgent, CustomScheduledAgent {
public static final long POLL_INTERVAL_MILLIS = TimeUnit.HOURS.toMillis(24)
public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(20)

public static final Pattern ALARM_NAME_PATTERN = Pattern.compile(".+-v[0-9]{3}-alarm-.+")
public final Pattern ALARM_NAME_PATTERN = Pattern.compile(alarmsNamePattern)

final AmazonClientProvider amazonClientProvider
final CredentialsRepository<NetflixAmazonCredentials> credentialsRepository
final long pollIntervalMillis
final long timeoutMillis
final int daysToLeave
final String alarmsNamePattern;


CleanupAlarmsAgent(AmazonClientProvider amazonClientProvider,
CredentialsRepository<NetflixAmazonCredentials> credentialsRepository,
int daysToLeave) {
this(amazonClientProvider, credentialsRepository, POLL_INTERVAL_MILLIS, DEFAULT_TIMEOUT_MILLIS, daysToLeave)
int daysToLeave,
String alarmsNamePattern) {
this(amazonClientProvider, credentialsRepository, POLL_INTERVAL_MILLIS, DEFAULT_TIMEOUT_MILLIS, daysToLeave, alarmsNamePattern)
}

CleanupAlarmsAgent(AmazonClientProvider amazonClientProvider,
CredentialsRepository<NetflixAmazonCredentials> credentialsRepository,
long pollIntervalMillis,
long timeoutMills,
int daysToLeave) {
int daysToLeave,
String alarmsNamePattern) {
this.amazonClientProvider = amazonClientProvider
this.credentialsRepository = credentialsRepository
this.pollIntervalMillis = pollIntervalMillis
this.timeoutMillis = timeoutMills
this.daysToLeave = daysToLeave
this.alarmsNamePattern = alarmsNamePattern
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ public static List<Agent> buildAwsCleanupAgents(
new CleanupAlarmsAgent(
amazonClientProvider,
credentialsRepository,
awsConfigurationProperties.getCleanup().getAlarms().getDaysToKeep()));
awsConfigurationProperties.getCleanup().getAlarms().getDaysToKeep(),
awsConfigurationProperties.getCleanup().getAlarms().getAlarmsNamePattern()));
}
newlyAddedAgents.add(
new CleanupDetachedInstancesAgent(amazonClientProvider, credentialsRepository));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CleanupAlarmsAgentSpec extends Specification {
0 * _
}

agent = new CleanupAlarmsAgent(amazonClientProvider, credentialsRepository, 10L, 10L, 90)
agent = new CleanupAlarmsAgent(amazonClientProvider, credentialsRepository, 10L, 10L, 90, ".+-v[0-9]{3}-alarm-.+")
}

void "should run across all regions/accounts and delete in each"() {
Expand Down Expand Up @@ -128,9 +128,55 @@ class CleanupAlarmsAgentSpec extends Specification {

}

void "should delete alarms that match a user defined pattern"() {
agent = new CleanupAlarmsAgent(amazonClientProvider, credentialsRepository, 10L, 10L, 90, ".+-v[0-9]{3}-CustomAlarm-.+")

given:
MetricAlarm alarmA = buildAlarm("some-other-v000-CustomAlarm-${validUuid}", 91)
MetricAlarm alarmB = buildAlarm("some-other-alarm-v000-${validUuid}", 91) // missing "-alarm-"

when:
agent.run()

then:
1 * autoScalingUSE.describePolicies() >> new DescribePoliciesResult()
1 * cloudWatchUSE.describeAlarms(_) >> new DescribeAlarmsResult()
1 * autoScalingUSW.describePolicies() >> new DescribePoliciesResult()
1 * cloudWatchUSW.describeAlarms(_) >> new DescribeAlarmsResult().withMetricAlarms([alarmA, alarmB])
1 * cloudWatchUSW.deleteAlarms({ DeleteAlarmsRequest request ->
request.alarmNames == ["some-other-v000-CustomAlarm-${validUuid}"]
})
0 * cloudWatchUSW.deleteAlarms({ DeleteAlarmsRequest request ->
request.alarmNames == ["some-other-alarm-v000-${validUuid}"]
})
}


void "should delete alarms that match a user defined multiple pattern"() {
agent = new CleanupAlarmsAgent(amazonClientProvider, credentialsRepository, 10L, 10L, 90, ".+-v[0-9]{3}-CustomAlarm-.+|^some-other-alarm-v[0-9]{3}-.+")

given:
MetricAlarm alarmA = buildAlarm("some-other-v000-CustomAlarm-${validUuid}", 91)
MetricAlarm alarmB = buildAlarm("some-other-alarm-v000-${validUuid}", 91) // missing "-alarm-"

when:
agent.run()

then:
1 * autoScalingUSE.describePolicies() >> new DescribePoliciesResult()
1 * cloudWatchUSE.describeAlarms(_) >> new DescribeAlarmsResult()
1 * autoScalingUSW.describePolicies() >> new DescribePoliciesResult()
1 * cloudWatchUSW.describeAlarms(_) >> new DescribeAlarmsResult().withMetricAlarms([alarmA, alarmB])
1 * cloudWatchUSW.deleteAlarms({ DeleteAlarmsRequest request ->
request.alarmNames == ["some-other-v000-CustomAlarm-${validUuid}", "some-other-alarm-v000-${validUuid}"]
})
}

private static MetricAlarm buildAlarm(String name, int dataDays) {
new MetricAlarm(alarmName: name, stateUpdatedTimestamp: DateTime.now().minusDays(dataDays).toDate())
}




}

0 comments on commit 1dc05f1

Please sign in to comment.