From 1dc05f148724d5710a51aa86d1cecc541ed8a226 Mon Sep 17 00:00:00 2001 From: christosarvanitis Date: Fri, 13 Dec 2024 15:26:33 +0200 Subject: [PATCH] fix(aws): CleanupAlarmsAgent with an optional user-defined name pattern --- .../aws/AwsConfigurationProperties.groovy | 1 + .../aws/agent/CleanupAlarmsAgent.groovy | 12 +++-- .../aws/provider/config/ProviderHelpers.java | 3 +- .../aws/agent/CleanupAlarmsAgentSpec.groovy | 48 ++++++++++++++++++- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/AwsConfigurationProperties.groovy b/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/AwsConfigurationProperties.groovy index 94b90747c59..88bff09f858 100644 --- a/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/AwsConfigurationProperties.groovy +++ b/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/AwsConfigurationProperties.groovy @@ -39,6 +39,7 @@ class AwsConfigurationProperties { static class AlarmsConfig { boolean enabled = false int daysToKeep = 90 + String alarmsNamePattern = ".+-v[0-9]{3}-alarm-.+" } @NestedConfigurationProperty diff --git a/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgent.groovy b/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgent.groovy index 166fc0901e3..5f11da646fd 100644 --- a/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgent.groovy +++ b/clouddriver-aws/src/main/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgent.groovy @@ -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 credentialsRepository final long pollIntervalMillis final long timeoutMillis final int daysToLeave + final String alarmsNamePattern; CleanupAlarmsAgent(AmazonClientProvider amazonClientProvider, CredentialsRepository 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 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 diff --git a/clouddriver-aws/src/main/java/com/netflix/spinnaker/clouddriver/aws/provider/config/ProviderHelpers.java b/clouddriver-aws/src/main/java/com/netflix/spinnaker/clouddriver/aws/provider/config/ProviderHelpers.java index 3821569f3f4..ace0c676a1a 100644 --- a/clouddriver-aws/src/main/java/com/netflix/spinnaker/clouddriver/aws/provider/config/ProviderHelpers.java +++ b/clouddriver-aws/src/main/java/com/netflix/spinnaker/clouddriver/aws/provider/config/ProviderHelpers.java @@ -250,7 +250,8 @@ public static List buildAwsCleanupAgents( new CleanupAlarmsAgent( amazonClientProvider, credentialsRepository, - awsConfigurationProperties.getCleanup().getAlarms().getDaysToKeep())); + awsConfigurationProperties.getCleanup().getAlarms().getDaysToKeep(), + awsConfigurationProperties.getCleanup().getAlarms().getAlarmsNamePattern())); } newlyAddedAgents.add( new CleanupDetachedInstancesAgent(amazonClientProvider, credentialsRepository)); diff --git a/clouddriver-aws/src/test/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgentSpec.groovy b/clouddriver-aws/src/test/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgentSpec.groovy index 62465148886..87e821796b4 100644 --- a/clouddriver-aws/src/test/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgentSpec.groovy +++ b/clouddriver-aws/src/test/groovy/com/netflix/spinnaker/clouddriver/aws/agent/CleanupAlarmsAgentSpec.groovy @@ -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"() { @@ -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()) } + + + }