Skip to content

Commit

Permalink
Better in use checking for images (#129)
Browse files Browse the repository at this point in the history
* feat(cache): singleton cache, using with edda things

* feat(cache): use edda cache and remove sibling check

* if cache contains < MIN, dont do logic
  • Loading branch information
emjburns authored Oct 29, 2018
1 parent 2c8d560 commit 3cc2898
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 285 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allprojects {
group = "com.netflix.spinnaker.swabbie"

ext {
spinnakerDependenciesVersion = project.hasProperty('spinnakerDependenciesVersion') ? project.property('spinnakerDependenciesVersion') : '1.2.5'
spinnakerDependenciesVersion = project.hasProperty('spinnakerDependenciesVersion') ? project.property('spinnakerDependenciesVersion') : '1.4.0'
}

def checkLocalVersions = [spinnakerDependenciesVersion: spinnakerDependenciesVersion]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,22 @@ import com.netflix.spinnaker.config.EddaApiClient
import com.netflix.spinnaker.swabbie.*
import com.netflix.spinnaker.swabbie.aws.instances.AmazonInstance
import com.netflix.spinnaker.swabbie.model.WorkConfiguration
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Lazy
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.lang.IllegalArgumentException
import java.time.Clock

@Configuration
@Component
open class EddaImagesUsedByInstancesProvider(
private val clock: Clock,
private val workConfigurations: List<WorkConfiguration>,
private val instanceProvider: ResourceProvider<AmazonInstance>,
private val eddaApiClients: List<EddaApiClient>,
@Lazy private val imagesUsedByInstancesCache: InMemoryCache<AmazonImagesUsedByInstancesCache>
private val eddaApiClients: List<EddaApiClient>
) : CachedViewProvider<AmazonImagesUsedByInstancesCache> {
private val log: Logger = LoggerFactory.getLogger(javaClass)

/**
* @param params["region"]: return a Set<String> of Amazon imageIds (i.e. "ami-abc123")
* currently referenced by running EC2 instances in the region
*/
override fun getAll(params: Parameters): Set<String> {
if (params.region != "") {
return imagesUsedByInstancesCache.get().elementAt(0).refdAmisByRegion[params.region] ?: emptySet()
} else {
throw IllegalArgumentException("Missing required region parameter")
}
}

/**
* Returns an epochMs timestamp of the last cache update
*/
override fun getLastUpdated(): Long {
return imagesUsedByInstancesCache.get().elementAt(0).lastUpdated
}

fun load(): Set<AmazonImagesUsedByInstancesCache> {
override fun load(): AmazonImagesUsedByInstancesCache {
val refdAmisByRegion = mutableMapOf<String, Set<String>>()

val regions = workConfigurations.asSequence()
Expand Down Expand Up @@ -81,24 +63,37 @@ open class EddaImagesUsedByInstancesProvider(
refdAmisByRegion[region] = refdAmis
}

return setOf(
AmazonImagesUsedByInstancesCache(
refdAmisByRegion,
clock.millis(),
"default"
)
)
return AmazonImagesUsedByInstancesCache(refdAmisByRegion, clock.millis(), "default")
}
}


@Configuration
@Component
open class EddaImagesUsedByInstancesCache(
eddaImagesUsedByInstancesProvider: EddaImagesUsedByInstancesProvider
) : InMemoryCache<AmazonImagesUsedByInstancesCache>(eddaImagesUsedByInstancesProvider.load())
) : InMemorySingletonCache<AmazonImagesUsedByInstancesCache>(eddaImagesUsedByInstancesProvider.load())

data class AmazonImagesUsedByInstancesCache(
val refdAmisByRegion: Map<String, Set<String>>,
val lastUpdated: Long,
private val refdAmisByRegion: Map<String, Set<String>>,
private val lastUpdated: Long,
override val name: String?
) : Cacheable
) : Cacheable {
private val log: Logger = LoggerFactory.getLogger(javaClass)

/**
* @param params.region: return a Set<String> of Amazon imageIds (i.e. "ami-abc123")
* currently referenced by running EC2 instances in the region
*/
fun getAll(params: Parameters): Set<String> {
log.debug("getting all for ${javaClass.simpleName}")
if (params.region != "") {
return refdAmisByRegion[params.region] ?: emptySet()
} else {
throw IllegalArgumentException("Missing required region parameter")
}
}

fun getLastUpdated(): Long {
return lastUpdated
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,23 @@ import com.netflix.spinnaker.config.EddaApiClient
import com.netflix.spinnaker.swabbie.*
import com.netflix.spinnaker.swabbie.aws.launchconfigurations.AmazonLaunchConfiguration
import com.netflix.spinnaker.swabbie.model.WorkConfiguration
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Lazy
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.lang.IllegalArgumentException
import java.time.Clock

@Configuration
@Component
open class EddaLaunchConfigurationCacheProvider(
private val clock: Clock,
private val workConfigurations: List<WorkConfiguration>,
private val launchConfigurationProvider: ResourceProvider<AmazonLaunchConfiguration>,
private val eddaApiClients: List<EddaApiClient>,
@Lazy private val launchConfigCache: InMemoryCache<AmazonLaunchConfigurationCache>
private val eddaApiClients: List<EddaApiClient>
) : CachedViewProvider<AmazonLaunchConfigurationCache> {

/**
* @param params["region"]: return a Set<AmazonLaunchConfiguration> across all known accounts in region
*/
override fun getAll(params: Parameters): Set<AmazonLaunchConfiguration> {
if (params.region != "") {
return launchConfigCache.get().elementAt(0).configsByRegion[params.region] ?: emptySet()
} else {
throw IllegalArgumentException("Missing required region parameter")
}
}

/**
* Returns an epochMs timestamp of the last cache update
*/
override fun getLastUpdated(): Long {
return launchConfigCache.get().elementAt(0).lastUpdated
}
private val log: Logger = LoggerFactory.getLogger(javaClass)

/**
* @param region: AWS region
*
* Returns a map of <K: all ami's referenced by a launch config in region, V: set of launch configs referencing K>
*/
fun getRefdAmisForRegion(region: String): Map<String, Set<AmazonLaunchConfiguration>> {
val cache = launchConfigCache.get()
return cache.elementAt(0).refdAmisByRegion[region].orEmpty()
}

// TODO("Refactor Cacheable to support singletons instead of just sets")
fun load(): Set<AmazonLaunchConfigurationCache> {
override fun load(): AmazonLaunchConfigurationCache {
val configsByRegion = mutableMapOf<String, Set<AmazonLaunchConfiguration>>()
val refdAmisByRegion = mutableMapOf<String, MutableMap<String, MutableSet<AmazonLaunchConfiguration>>>()

Expand Down Expand Up @@ -94,26 +67,55 @@ open class EddaLaunchConfigurationCacheProvider(
configsByRegion[region] = launchConfigs
refdAmisByRegion[region] = refdAmis
}
return AmazonLaunchConfigurationCache(configsByRegion, refdAmisByRegion, clock.millis(), "default")

return setOf(
AmazonLaunchConfigurationCache(
configsByRegion,
refdAmisByRegion,
clock.millis(),
"default"
)
)
}
}

@Configuration
@Component
open class EddaLaunchConfigurationCache(
eddaLaunchConfigurationCacheProvider: EddaLaunchConfigurationCacheProvider
) : InMemoryCache<AmazonLaunchConfigurationCache>(eddaLaunchConfigurationCacheProvider.load())
) : InMemorySingletonCache<AmazonLaunchConfigurationCache>(eddaLaunchConfigurationCacheProvider.load())

data class AmazonLaunchConfigurationCache(
val configsByRegion: Map<String, Set<AmazonLaunchConfiguration>>,
val refdAmisByRegion: Map<String, Map<String, Set<AmazonLaunchConfiguration>>>,
val lastUpdated: Long,
private val configsByRegion: Map<String, Set<AmazonLaunchConfiguration>>,
private val refdAmisByRegion: Map<String, Map<String, Set<AmazonLaunchConfiguration>>>,
private val lastUpdated: Long,
override val name: String?
) : Cacheable
) : Cacheable {
private val log: Logger = LoggerFactory.getLogger(javaClass)

/**
* @param params["region"]: return a Set<AmazonLaunchConfiguration> across all known accounts in region
*/
fun getLaunchConfigsByRegion(params: Parameters): Set<AmazonLaunchConfiguration> {
if (params.region != "") {
return configsByRegion[params.region] ?: emptySet()
} else {
throw IllegalArgumentException("Missing required region parameter")
}
}

fun getLaunchConfigsByRegionForImage(params: Parameters): Set<AmazonLaunchConfiguration> {
log.debug("getting launch configs in ${javaClass.simpleName}")
if (params.region != "" && params.id != "") {
return getRefdAmisForRegion(params.region).getOrDefault(params.id, emptySet())
} else {
throw IllegalArgumentException("Missing required region and id parameters")
}
}

/**
* @param region: AWS region
*
* Returns a map of <K: all ami's referenced by a launch config in region, V: set of launch configs referencing K>
*/
fun getRefdAmisForRegion(region: String): Map<String, Set<AmazonLaunchConfiguration>> {
log.debug("getting refd amis in ${javaClass.simpleName}")
return refdAmisByRegion[region].orEmpty()
}

fun getLastUpdated(): Long {
return lastUpdated
}
}
Loading

0 comments on commit 3cc2898

Please sign in to comment.