com.netflix.spinnaker.keel.clouddriver.model.ActiveServerGroup.kt Maven / Gradle / Ivy
The newest version!
package com.netflix.spinnaker.keel.clouddriver.model
import com.fasterxml.jackson.annotation.JsonAlias
import com.fasterxml.jackson.annotation.JsonCreator
import com.netflix.spinnaker.keel.api.Moniker
import com.netflix.spinnaker.keel.api.plugins.ServerGroupIdentity
import com.netflix.spinnaker.keel.api.support.Tag
import com.netflix.spinnaker.keel.clouddriver.CloudDriverCache
import com.netflix.spinnaker.kork.exceptions.SystemException
data class ServerGroupCollection(
val accountName: String,
val serverGroups: Set
)
/**
* Fields common to all of the different kinds of server groups (EC2, Titus)
*/
interface BaseServerGroup {
val name: String
val region: String
val targetGroups: Set
val loadBalancers: Set
val capacity: Capacity
val cloudProvider: String
val securityGroups: Set
val moniker: Moniker
val disabled: Boolean
get() = false
val instanceCounts: InstanceCounts
val createdTime: Long
}
data class InstanceCounts(
val total: Int,
val up: Int,
val down: Int,
val unknown: Int,
val outOfService: Int,
val starting: Int
)
/**
* Fields common to classes that model EC2 server groups
*/
interface BaseEc2ServerGroup : BaseServerGroup {
val zones: Set
val image: ActiveServerGroupImage
/**
* One of these two fields is always null, and the other is always
* non-null.
*
* If AWS launch templates are enabled in clouddriver for this app,
* associated with this server group, then the launchTemplate
* field will be populated.
*
* Otherwise, the launchConfig field will be populated.
*/
val launchConfig: LaunchConfig?
val launchTemplate: LaunchTemplate?
val asg: AutoScalingGroup
val scalingPolicies: List
val vpcId: String
val buildInfo: BuildInfo?
}
/**
* Objects that are returned when querying for all of the ec2 server groups associated with a cluster.
*
* Two differences from [ActiveServerGroup]:
* - disabled flag
* - no accountName field (since this is defined on the parent [ServerGroupCollection] object
*/
data class ServerGroup(
override val name: String,
override val region: String,
override val zones: Set,
override val image: ActiveServerGroupImage,
override val launchConfig: LaunchConfig? = null,
override val launchTemplate: LaunchTemplate? = null,
override val asg: AutoScalingGroup,
override val scalingPolicies: List,
override val vpcId: String,
override val targetGroups: Set,
override val loadBalancers: Set,
override val capacity: Capacity,
override val cloudProvider: String,
override val securityGroups: Set,
override val moniker: Moniker,
override val buildInfo: BuildInfo? = null,
override val disabled: Boolean,
override val instanceCounts: InstanceCounts,
override val createdTime: Long
) : BaseEc2ServerGroup, ServerGroupIdentity {
init {
ensureLaunchConfigInfoIsPresent(this)
}
}
/**
* Clouddriver should always return either a launchConfig field or a launchTemplate
* field for an EC2 server group.
*
* This is a helper function that asserts that one of these fields must be present.
* It is intended to be called from the init block of classes that implement BaseEc2ServerGroup
*/
fun ensureLaunchConfigInfoIsPresent(sg : BaseEc2ServerGroup) {
if(sg.launchConfig == null && sg.launchTemplate == null) {
throw SystemException("Server group info contains neither launchConfig nor launchTemplate fields: ${sg.name}")
}
}
fun ServerGroup.toActive(accountName: String) =
ActiveServerGroup(
name = name,
region = region,
zones = zones,
image = image,
launchConfig = launchConfig,
launchTemplate = launchTemplate,
asg = asg,
scalingPolicies = scalingPolicies,
vpcId = vpcId,
targetGroups = targetGroups,
loadBalancers = loadBalancers,
capacity = capacity,
cloudProvider = cloudProvider,
securityGroups = securityGroups,
accountName = accountName,
moniker = moniker,
buildInfo = buildInfo,
instanceCounts = instanceCounts,
createdTime = createdTime
)
// todo eb: this should be more general so that it works for all server groups, not just ec2
data class ActiveServerGroup(
override val name: String,
override val region: String,
override val zones: Set,
override val image: ActiveServerGroupImage,
override val launchConfig: LaunchConfig? = null,
override val launchTemplate: LaunchTemplate? = null,
override val asg: AutoScalingGroup,
override val scalingPolicies: List,
override val vpcId: String,
override val targetGroups: Set,
override val loadBalancers: Set,
override val capacity: Capacity,
override val cloudProvider: String,
override val securityGroups: Set,
val accountName: String,
override val moniker: Moniker,
override val buildInfo: BuildInfo? = null,
override val instanceCounts: InstanceCounts,
override val createdTime: Long
) : BaseEc2ServerGroup {
init {
ensureLaunchConfigInfoIsPresent(this)
}
}
fun ActiveServerGroup.subnet(cloudDriverCache: CloudDriverCache): String =
asg.vpczoneIdentifier.substringBefore(",").let { subnetId ->
cloudDriverCache
.subnetBy(subnetId)
.purpose ?: error("Subnet $subnetId has no purpose!")
}
data class ActiveServerGroupImage(
val imageId: String,
val appVersion: String?,
val baseImageName: String?,
val name: String,
val imageLocation: String,
val description: String?
) {
@JsonCreator
constructor(
imageId: String,
name: String,
imageLocation: String,
description: String?,
tags: List