Skip to content

Commit 966f4e8

Browse files
feat(aws): CleanupAlarmsAgent with an optional user-defined name pattern (#6317) (#6319)
(cherry picked from commit 53f11a8) Co-authored-by: Christos Arvanitis <christos.arvanitis@armory.io>
1 parent 8264ff4 commit 966f4e8

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/AwsConfigurationProperties.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class AwsConfigurationProperties {
3939
static class AlarmsConfig {
4040
boolean enabled = false
4141
int daysToKeep = 90
42+
String alarmsNamePattern = ".+-v[0-9]{3}-alarm-.+"
4243
}
4344

4445
@NestedConfigurationProperty

clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgent.groovy

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,35 @@ class CleanupAlarmsAgent implements RunnableAgent, CustomScheduledAgent {
4040
public static final long POLL_INTERVAL_MILLIS = TimeUnit.HOURS.toMillis(24)
4141
public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(20)
4242

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

4545
final AmazonClientProvider amazonClientProvider
4646
final CredentialsRepository<NetflixAmazonCredentials> credentialsRepository
4747
final long pollIntervalMillis
4848
final long timeoutMillis
4949
final int daysToLeave
50+
final String alarmsNamePattern;
5051

5152

5253
CleanupAlarmsAgent(AmazonClientProvider amazonClientProvider,
5354
CredentialsRepository<NetflixAmazonCredentials> credentialsRepository,
54-
int daysToLeave) {
55-
this(amazonClientProvider, credentialsRepository, POLL_INTERVAL_MILLIS, DEFAULT_TIMEOUT_MILLIS, daysToLeave)
55+
int daysToLeave,
56+
String alarmsNamePattern) {
57+
this(amazonClientProvider, credentialsRepository, POLL_INTERVAL_MILLIS, DEFAULT_TIMEOUT_MILLIS, daysToLeave, alarmsNamePattern)
5658
}
5759

5860
CleanupAlarmsAgent(AmazonClientProvider amazonClientProvider,
5961
CredentialsRepository<NetflixAmazonCredentials> credentialsRepository,
6062
long pollIntervalMillis,
6163
long timeoutMills,
62-
int daysToLeave) {
64+
int daysToLeave,
65+
String alarmsNamePattern) {
6366
this.amazonClientProvider = amazonClientProvider
6467
this.credentialsRepository = credentialsRepository
6568
this.pollIntervalMillis = pollIntervalMillis
6669
this.timeoutMillis = timeoutMills
6770
this.daysToLeave = daysToLeave
71+
this.alarmsNamePattern = alarmsNamePattern
6872
}
6973

7074
@Override

clouddriver-aws/src/main/java/com/netflix/spinnaker/clouddriver/aws/provider/config/ProviderHelpers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ public static List<Agent> buildAwsCleanupAgents(
250250
new CleanupAlarmsAgent(
251251
amazonClientProvider,
252252
credentialsRepository,
253-
awsConfigurationProperties.getCleanup().getAlarms().getDaysToKeep()));
253+
awsConfigurationProperties.getCleanup().getAlarms().getDaysToKeep(),
254+
awsConfigurationProperties.getCleanup().getAlarms().getAlarmsNamePattern()));
254255
}
255256
newlyAddedAgents.add(
256257
new CleanupDetachedInstancesAgent(amazonClientProvider, credentialsRepository));

clouddriver-aws/src/test/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgentSpec.groovy

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CleanupAlarmsAgentSpec extends Specification {
6464
0 * _
6565
}
6666

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

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

129129
}
130130

131+
void "should delete alarms that match a user defined pattern"() {
132+
agent = new CleanupAlarmsAgent(amazonClientProvider, credentialsRepository, 10L, 10L, 90, ".+-v[0-9]{3}-CustomAlarm-.+")
133+
134+
given:
135+
MetricAlarm alarmA = buildAlarm("some-other-v000-CustomAlarm-${validUuid}", 91)
136+
MetricAlarm alarmB = buildAlarm("some-other-alarm-v000-${validUuid}", 91) // missing "-alarm-"
137+
138+
when:
139+
agent.run()
140+
141+
then:
142+
1 * autoScalingUSE.describePolicies() >> new DescribePoliciesResult()
143+
1 * cloudWatchUSE.describeAlarms(_) >> new DescribeAlarmsResult()
144+
1 * autoScalingUSW.describePolicies() >> new DescribePoliciesResult()
145+
1 * cloudWatchUSW.describeAlarms(_) >> new DescribeAlarmsResult().withMetricAlarms([alarmA, alarmB])
146+
1 * cloudWatchUSW.deleteAlarms({ DeleteAlarmsRequest request ->
147+
request.alarmNames == ["some-other-v000-CustomAlarm-${validUuid}"]
148+
})
149+
0 * cloudWatchUSW.deleteAlarms({ DeleteAlarmsRequest request ->
150+
request.alarmNames == ["some-other-alarm-v000-${validUuid}"]
151+
})
152+
}
153+
154+
155+
void "should delete alarms that match a user defined multiple pattern"() {
156+
agent = new CleanupAlarmsAgent(amazonClientProvider, credentialsRepository, 10L, 10L, 90, ".+-v[0-9]{3}-CustomAlarm-.+|^some-other-alarm-v[0-9]{3}-.+")
157+
158+
given:
159+
MetricAlarm alarmA = buildAlarm("some-other-v000-CustomAlarm-${validUuid}", 91)
160+
MetricAlarm alarmB = buildAlarm("some-other-alarm-v000-${validUuid}", 91) // missing "-alarm-"
161+
162+
when:
163+
agent.run()
164+
165+
then:
166+
1 * autoScalingUSE.describePolicies() >> new DescribePoliciesResult()
167+
1 * cloudWatchUSE.describeAlarms(_) >> new DescribeAlarmsResult()
168+
1 * autoScalingUSW.describePolicies() >> new DescribePoliciesResult()
169+
1 * cloudWatchUSW.describeAlarms(_) >> new DescribeAlarmsResult().withMetricAlarms([alarmA, alarmB])
170+
1 * cloudWatchUSW.deleteAlarms({ DeleteAlarmsRequest request ->
171+
request.alarmNames == ["some-other-v000-CustomAlarm-${validUuid}", "some-other-alarm-v000-${validUuid}"]
172+
})
173+
}
131174

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

179+
180+
181+
136182
}

0 commit comments

Comments
 (0)