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

feat(aws): CleanupAlarmsAgent with an optional user-defined name pattern (backport #6317) #6319

Merged
merged 1 commit into from
Dec 13, 2024
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 @@ -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())
}




}
Loading