-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): Adding a general tag rule (#233)
* feat(rule): Adding a general tag rule - will match defined tag values in config to the ones on the resource - if matched and calculated age is older than today, the resource is marked * - A bit of needed cleanup w/r to tags - make tags a distinct object on the resource for convenience - temporal tags are managed internally - fix a bug on ttl calculation * - keep it simple, remove the need for a basic rule - renamed TemporalThresholdRule to ExpiredResourceRule - Updated to use a clock to determine if resource is expired - clean up test * - s/prefix/suffix/ in docs * - cleanup * - fix amazonTagExclusion based on testing * - cleanup
- Loading branch information
1 parent
061707a
commit 70f12bd
Showing
10 changed files
with
258 additions
and
150 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
swabbie-aws/src/main/kotlin/com/netflix/spinnaker/swabbie/aws/ExpiredResourceRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.swabbie.aws | ||
|
||
import com.netflix.spinnaker.swabbie.aws.model.AmazonResource | ||
import com.netflix.spinnaker.swabbie.model.Result | ||
import com.netflix.spinnaker.swabbie.model.Rule | ||
import com.netflix.spinnaker.swabbie.model.Summary | ||
import org.springframework.stereotype.Component | ||
import java.time.Clock | ||
|
||
/** | ||
* This rule applies if this amazon resource has expired. | ||
* A resource is expired if it's tagged with the following keys: ("expiration_time", "expires", "ttl") | ||
* Acceptable tag value: a number followed by a suffix such as d (days), w (weeks), m (month), y (year) | ||
* @see com.netflix.spinnaker.swabbie.tagging.TemporalTags.supportedTemporalTagValues | ||
*/ | ||
|
||
@Component | ||
class ExpiredResourceRule<T : AmazonResource>( | ||
private val clock: Clock | ||
) : Rule<T> { | ||
override fun apply(resource: T): Result { | ||
if (resource.expired(clock)) { | ||
return Result( | ||
Summary( | ||
description = "$${resource.resourceId} has expired. tags: ${resource.tags()}", | ||
ruleName = javaClass.simpleName | ||
) | ||
) | ||
} | ||
|
||
return Result(null) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
swabbie-aws/src/test/java/com/netflix/spinnaker/swabbie/aws/ExpiredResourceRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.swabbie.aws | ||
|
||
import com.netflix.spinnaker.kork.test.time.MutableClock | ||
import com.netflix.spinnaker.swabbie.aws.autoscalinggroups.AmazonAutoScalingGroup | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import strikt.api.expectThat | ||
import strikt.assertions.isNotNull | ||
import strikt.assertions.isNull | ||
import java.time.Duration | ||
import java.time.Instant | ||
|
||
object ExpiredResourceRuleTest { | ||
private val clock = MutableClock() | ||
private val subject = ExpiredResourceRule<AmazonAutoScalingGroup>(clock) | ||
private val now = Instant.now(clock).toEpochMilli() | ||
private val asg = AmazonAutoScalingGroup( | ||
autoScalingGroupName = "testapp-v001", | ||
instances = listOf( | ||
mapOf("instanceId" to "i-01234") | ||
), | ||
loadBalancerNames = listOf(), | ||
createdTime = now | ||
) | ||
|
||
@BeforeEach | ||
fun setup() { | ||
asg.set("tags", null) | ||
} | ||
|
||
@Test | ||
fun `should not apply if resource is not tagged with a ttl`() { | ||
expectThat( | ||
subject.apply(asg).summary | ||
).isNull() | ||
} | ||
|
||
@Test | ||
fun `should not apply if resource is not expired`() { | ||
val tags = listOf( | ||
mapOf("ttl" to "4d") | ||
) | ||
|
||
asg.set("tags", tags) | ||
expectThat( | ||
subject.apply(asg).summary | ||
).isNull() | ||
} | ||
|
||
@Test | ||
fun `should apply if resource is expired`() { | ||
val tags = listOf( | ||
mapOf("ttl" to "2d") | ||
) | ||
|
||
asg.set("tags", tags) | ||
|
||
clock.incrementBy(Duration.ofDays(3)) | ||
expectThat( | ||
subject.apply(asg).summary | ||
).isNotNull() | ||
} | ||
} |
Oops, something went wrong.