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

fix(core): Fixing optout flow #292

Merged
merged 7 commits into from
Nov 6, 2019
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 @@ -31,6 +31,7 @@ import com.netflix.spinnaker.swabbie.model.MarkedResource
import com.netflix.spinnaker.swabbie.model.Rule
import com.netflix.spinnaker.swabbie.model.SERVER_GROUP
import com.netflix.spinnaker.swabbie.model.WorkConfiguration
import com.netflix.spinnaker.swabbie.model.ResourceState
import com.netflix.spinnaker.swabbie.notifications.NotificationQueue
import com.netflix.spinnaker.swabbie.notifications.Notifier
import com.netflix.spinnaker.swabbie.orca.OrcaJob
Expand All @@ -41,6 +42,7 @@ import com.netflix.spinnaker.swabbie.repository.ResourceTrackingRepository
import com.netflix.spinnaker.swabbie.repository.ResourceUseTrackingRepository
import com.netflix.spinnaker.swabbie.repository.TaskCompleteEventInfo
import com.netflix.spinnaker.swabbie.repository.TaskTrackingRepository
import com.netflix.spinnaker.swabbie.utils.ApplicationUtils
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Component
import java.time.Clock
Expand All @@ -61,6 +63,7 @@ class AmazonAutoScalingGroupHandler(
private val rules: List<Rule<AmazonAutoScalingGroup>>,
private val aws: AWS,
private val orcaService: OrcaService,
private val applicationUtils: ApplicationUtils,
private val taskTrackingRepository: TaskTrackingRepository,
private val resourceUseTrackingRepository: ResourceUseTrackingRepository,
notificationQueue: NotificationQueue
Expand Down Expand Up @@ -170,4 +173,43 @@ class AmazonAutoScalingGroupHandler(

return aws.getServerGroup(params)
}

override fun optOut(resourceId: String, workConfiguration: WorkConfiguration): ResourceState {
return super.optOut(resourceId, workConfiguration)
.also {
tagServerGroup(it.markedResource, workConfiguration)
}
}

private fun tagServerGroup(markedResource: MarkedResource, workConfiguration: WorkConfiguration) {
val tagServerGroupTask = orcaService.orchestrate(
OrchestrationRequest(
application = applicationUtils.determineApp(markedResource.resource),
description = "Opting ${markedResource.resourceId} out of deletion.",
job = listOf(
OrcaJob(
type = "upsertServerGroupTags",
context = mutableMapOf(
"serverGroupName" to markedResource.name,
"regions" to setOf(workConfiguration.location),
"tags" to mapOf("expiration_time" to "never"),
"cloudProvider" to workConfiguration.cloudProvider,
"cloudProviderType" to workConfiguration.cloudProvider,
"credentials" to workConfiguration.account.name
)
)
)
)
)

taskTrackingRepository.add(
tagServerGroupTask.taskId(),
TaskCompleteEventInfo(
action = Action.OPTOUT,
markedResources = listOf(markedResource),
workConfiguration = workConfiguration,
submittedTimeMillis = clock.instant().toEpochMilli()
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.netflix.spinnaker.swabbie.model.IMAGE
import com.netflix.spinnaker.swabbie.model.MarkedResource
import com.netflix.spinnaker.swabbie.model.NAIVE_EXCLUSION
import com.netflix.spinnaker.swabbie.model.Rule
import com.netflix.spinnaker.swabbie.model.ResourceState
import com.netflix.spinnaker.swabbie.model.SNAPSHOT
import com.netflix.spinnaker.swabbie.model.WorkConfiguration
import com.netflix.spinnaker.swabbie.notifications.NotificationQueue
Expand Down Expand Up @@ -315,13 +316,43 @@ class AmazonImageHandler(

return aws.getImage(params)
}
}

// TODO: specific to netflix pattern. make generic
private fun isAncestor(images: Map<String, String>, image: AmazonImage): Boolean {
return images.containsKey("ancestor_id=${image.imageId}") || images.containsKey("ancestor_name=${image.name}")
}
override fun optOut(resourceId: String, workConfiguration: WorkConfiguration): ResourceState {
return super.optOut(resourceId, workConfiguration)
.also {
tagImage(it.markedResource, workConfiguration)
}
}

private fun AmazonImage.matches(image: AmazonImage): Boolean {
return name == image.name || imageId == image.imageId
private fun tagImage(markedResource: MarkedResource, workConfiguration: WorkConfiguration) {
val tagImageTask = orcaService.orchestrate(
OrchestrationRequest(
application = applicationUtils.determineApp(markedResource.resource),
description = "Opting ${markedResource.resourceId} out of deletion.",
job = listOf(
OrcaJob(
type = "upsertImageTags",
context = mutableMapOf(
"imageNames" to setOf(markedResource.resourceId),
"regions" to setOf(workConfiguration.location),
"tags" to mapOf("expiration_time" to "never"),
"cloudProvider" to workConfiguration.cloudProvider,
"cloudProviderType" to workConfiguration.cloudProvider,
"credentials" to workConfiguration.account.name
)
)
)
)
)

taskTrackingRepository.add(
tagImageTask.taskId(),
TaskCompleteEventInfo(
action = Action.OPTOUT,
markedResources = listOf(markedResource),
workConfiguration = workConfiguration,
submittedTimeMillis = clock.instant().toEpochMilli()
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.netflix.spinnaker.swabbie.repository.ResourceStateRepository
import com.netflix.spinnaker.swabbie.repository.ResourceTrackingRepository
import com.netflix.spinnaker.swabbie.repository.ResourceUseTrackingRepository
import com.netflix.spinnaker.swabbie.repository.TaskTrackingRepository
import com.netflix.spinnaker.swabbie.utils.ApplicationUtils
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.check
import com.nhaarman.mockito_kotlin.doReturn
Expand Down Expand Up @@ -109,7 +110,8 @@ object AmazonAutoScalingGroupHandlerTest {
resourceUseTrackingRepository = resourceUseTrackingRepository,
swabbieProperties = SwabbieProperties().also { it.schedule.enabled = false },
dynamicConfigService = dynamicConfigService,
notificationQueue = notificationQueue
notificationQueue = notificationQueue,
applicationUtils = ApplicationUtils(emptyList())
)

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,39 +149,36 @@ abstract class AbstractResourceTypeHandler<T : Resource>(
}

/**
* This find the resource, and then opts it out even if it was not marked.
* This finds the resource, and then opts it out even if it was not marked.
* This function is used when a caller makes an opt out request through the controller
* and the resource has not already been marked.
*/
override fun optOut(resourceId: String, workConfiguration: WorkConfiguration) {
val resource = getCandidate(resourceId, "", workConfiguration)
?: return
log.info("Opting out resource $resourceId in namespace ${workConfiguration.namespace}")
val resourceWithOwner = listOf(resource).withResolvedOwners(workConfiguration).first()
val newMarkedResource = MarkedResource(
resource = resourceWithOwner,
summaries = emptyList(),
namespace = workConfiguration.namespace,
resourceOwner = resourceWithOwner.details[resourceOwnerField] as String,
projectedDeletionStamp = deletionTimestamp(workConfiguration),
lastSeenInfo = resourceUseTrackingRepository.getLastSeenInfo(resourceWithOwner.resourceId)
)
override fun optOut(resourceId: String, workConfiguration: WorkConfiguration): ResourceState {
val resource = getCandidate(
resourceId = resourceId,
workConfiguration = workConfiguration
) ?: throw NotFoundException()

val status = Status(Action.OPTOUT.name, clock.instant().toEpochMilli())
val state = ResourceState(
markedResource = newMarkedResource,
deleted = false,
optedOut = true,
statuses = mutableListOf(status),
currentStatus = status
)
val markedResource = resourceRepository.find(resourceId, workConfiguration.namespace)
?: MarkedResource(
resource = resource,
summaries = emptyList(),
namespace = workConfiguration.namespace,
projectedDeletionStamp = -1L
)

// opt out here to ensure there is never a time when this resource is not opted out.
resourceStateRepository.upsert(state)
resourceRepository.upsert(newMarkedResource)
log.info("Opting out resource ${markedResource.resourceId} in namespace ${workConfiguration.namespace}")
val status = Status(Action.OPTOUT.name, clock.instant().toEpochMilli())
resourceRepository.remove(markedResource)
applicationEventPublisher.publishEvent(OptOutResourceEvent(markedResource, workConfiguration))
val optOutState = resourceStateRepository.get(resource.resourceId, workConfiguration.namespace)
?: ResourceState(
markedResource = markedResource,
deleted = false,
statuses = mutableListOf(status)
)

// this will trigger the actual opt out action
applicationEventPublisher.publishEvent(OptOutResourceEvent(newMarkedResource, workConfiguration))
return resourceStateRepository.upsert(optOutState.copy(optedOut = true, currentStatus = status))
}

override fun markResource(resourceId: String, onDemandMarkData: OnDemandMarkData, workConfiguration: WorkConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.netflix.spinnaker.swabbie

import com.netflix.spinnaker.swabbie.model.OnDemandMarkData
import com.netflix.spinnaker.swabbie.model.Resource
import com.netflix.spinnaker.swabbie.model.ResourceState
import com.netflix.spinnaker.swabbie.model.ResourceEvaluation
import com.netflix.spinnaker.swabbie.model.WorkConfiguration

Expand All @@ -38,7 +39,7 @@ interface ResourceTypeHandler<T : Resource> {
* Fetches a single resource.
* Decorate metadata that can be used in a [Rule]
*/
fun getCandidate(resourceId: String, resourceName: String, workConfiguration: WorkConfiguration): T?
fun getCandidate(resourceId: String, resourceName: String = resourceId, workConfiguration: WorkConfiguration): T?

/**
* Marks a single marked resource matching the granularity of [WorkConfiguration].
Expand All @@ -58,7 +59,7 @@ interface ResourceTypeHandler<T : Resource> {
/**
* Opts a resource out whether or not it has been marked.
*/
fun optOut(resourceId: String, workConfiguration: WorkConfiguration)
fun optOut(resourceId: String, workConfiguration: WorkConfiguration): ResourceState

/**
* Used to check references and augment candidates for further processing
Expand Down
Loading