Skip to content

Commit

Permalink
fix(aws): Fixing missing build info on cluster (#4554)
Browse files Browse the repository at this point in the history
- fixed setting build info and image while translating cluster details
- added test
additional: including launch templates images. (accidentally left out)
  • Loading branch information
jeyrschabu committed Apr 28, 2020
1 parent 10cf788 commit eb3ef6c
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ class AmazonClusterProvider implements ClusterProvider<AmazonCluster>, ServerGro
cacheResults[CLUSTERS.ns],
TARGET_GROUPS.ns
)
Collection<CacheData> allImages = resolveRelationshipDataForCollection(
cacheResults[LAUNCH_CONFIGS.ns],
IMAGES.ns

Collection<CacheData> allImages = []
allImages.addAll(
resolveRelationshipDataForCollection(cacheResults[LAUNCH_CONFIGS.ns], IMAGES.ns)
)

allImages.addAll(
resolveRelationshipDataForCollection(cacheResults[LAUNCH_TEMPLATES.ns], IMAGES.ns)
)

Map<String, AmazonLoadBalancer> loadBalancers = translateLoadBalancers(allLoadBalancers)
Expand Down Expand Up @@ -316,9 +321,17 @@ class AmazonClusterProvider implements ClusterProvider<AmazonCluster>, ServerGro
[(sg.id): serverGroup]
}

// expand and set launch templates
updateServerGroupLaunchSettings(serverGroups, launchTemplateData)

// expand and set launch configs
updateServerGroupLaunchSettings(serverGroups, launchConfigData)
updateServerGroupBuildInfo(serverGroups, imageData)

// update build info for launch templates
updateServerGroupBuildInfo(serverGroups, launchTemplateData, imageData)

// update build info for launch configs
updateServerGroupBuildInfo(serverGroups, launchConfigData, imageData)

serverGroups
}
Expand Down Expand Up @@ -584,12 +597,13 @@ class AmazonClusterProvider implements ClusterProvider<AmazonCluster>, ServerGro
/**
* Updates server groups build info
*/
private void updateServerGroupBuildInfo(Map<String, AmazonServerGroup> serverGroups, Collection<CacheData> imageData) {
private void updateServerGroupBuildInfo(
Map<String, AmazonServerGroup> serverGroups, Collection<CacheData> launchData, Collection<CacheData> imageData) {
Map<String, CacheData> images = imageData?.collectEntries { image ->
[(image.id): image]
}

imageData.each { ld ->
launchData.each { ld ->
if (ld?.relationships?.containsKey(SERVER_GROUPS.ns)) {
def serverGroup = serverGroups[ld.relationships[SERVER_GROUPS.ns].first()]
def imageId = ld.relationships[IMAGES.ns]?.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ import com.netflix.spinnaker.cats.cache.CacheFilter
import com.netflix.spinnaker.cats.cache.DefaultCacheData
import com.netflix.spinnaker.clouddriver.aws.AmazonCloudProvider
import com.netflix.spinnaker.clouddriver.aws.data.Keys
import com.netflix.spinnaker.clouddriver.aws.model.AmazonServerGroup
import com.netflix.spinnaker.clouddriver.aws.provider.AwsProvider
import org.junit.jupiter.api.BeforeEach
import spock.lang.Specification
import spock.lang.Subject
import spock.lang.Unroll

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.APPLICATIONS
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.CLUSTERS
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.IMAGES
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LAUNCH_CONFIGS
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.LAUNCH_TEMPLATES
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS
Expand All @@ -47,12 +50,13 @@ class AmazonClusterProviderSpec extends Specification {

def app = "app"
def account = "test"
def region = "us-east-1"
def clusterName = "app-main"
String clusterId = Keys.getClusterKey(clusterName, app, account)
def clusterAttributes = [name: clusterName, application: app]

def serverGroupName = "app-main-v000"
String serverGroupId = Keys.getServerGroupKey(clusterName, serverGroupName, account, "us-east-1")
String serverGroupId = Keys.getServerGroupKey(clusterName, serverGroupName, account, region)
def serverGroup = [
name: serverGroupName,
instances: [],
Expand All @@ -67,6 +71,102 @@ class AmazonClusterProviderSpec extends Specification {
serverGroup.asg.clear()
}

def "should get cluster details with build info"() {
given:
def imageId = "ami-1"
def imageKey = Keys.getImageKey(imageId, account, region)
def imageAttributes = [
imageId: imageId,
tags: [appversion: "app-0.487.0-h514.f4be391/job/1"]
]

serverGroup.asg = [ launchConfigurationName: launchConfigName]
def launchConfiguration = new DefaultCacheData(
Keys.getLaunchConfigKey(launchConfigName, account, region),
[imageId: imageId],
[images: [imageKey]]
)

and:
cacheView.supportsGetAllByApplication() >> false
cacheView.get(APPLICATIONS.ns, Keys.getApplicationKey(app)) >> new DefaultCacheData(
Keys.getApplicationKey(app), [name: app], [serverGroups: [serverGroupId], clusters: [clusterId]]
)
cacheView.getAll(LAUNCH_CONFIGS.ns, _ as Set) >> [launchConfiguration]
cacheView.getAll(CLUSTERS.ns, _, _) >> [new DefaultCacheData(clusterId, clusterAttributes, [serverGroups: [serverGroupId]])]
cacheView.getAll(SERVER_GROUPS.ns, [ serverGroupId ], _ as CacheFilter) >> [
new DefaultCacheData(serverGroupId, serverGroup, [launchConfigs: [launchConfiguration.id]])
]
cacheView.getAll(IMAGES.ns, _ as Set) >> [
new DefaultCacheData(imageKey, imageAttributes, [:])
]

when:
def result = provider.getClusterDetails(app)

then:
def clusters = result.values()
def allServerGroups = clusters*.serverGroups.flatten() as Set<AmazonServerGroup>

clusters.size() == 1
allServerGroups.size() == 1
allServerGroups[0].launchConfig != null
allServerGroups[0].buildInfo != null
allServerGroups[0].image != null
}

def "should get cluster details by app with build info"() {
given:
def imageId = "ami-1"
def imageKey = Keys.getImageKey(imageId, account, region)
def imageAttributes = [
imageId: imageId,
tags: [appversion: "app-0.487.0-h514.f4be391/job/514"]
]

serverGroup.asg = [ launchConfigurationName: launchConfigName]
def launchConfiguration = new DefaultCacheData(
Keys.getLaunchConfigKey(launchConfigName, account, region),
[imageId: imageId],
[images: [imageKey]]
)

def cluster = new DefaultCacheData(clusterId, clusterAttributes, [serverGroups: [serverGroupId]])
def serverGroup = new DefaultCacheData(serverGroupId, serverGroup, [launchConfigs: [launchConfiguration.id]])
def image = new DefaultCacheData(imageKey, imageAttributes, [:])

and:
cacheView.getAllByApplication(_, _, _) >> [
serverGroups: [serverGroup],
clusters: [cluster],
launchConfigs: [launchConfiguration],
images: [image]
]

cacheView.getAll(LAUNCH_CONFIGS.ns, _ as Set) >> [launchConfiguration]
cacheView.getAll(CLUSTERS.ns, _, _) >> [cluster]
cacheView.getAll(SERVER_GROUPS.ns, [ serverGroupId ], _ as CacheFilter) >> [serverGroup]

cacheView.getAll(IMAGES.ns, _ as Set) >> [image]

when:
def result = provider.getClusterDetails(app)

then:
1 * cacheView.get(APPLICATIONS.ns, Keys.getApplicationKey(app)) >> new DefaultCacheData(
Keys.getApplicationKey(app), [name: app], [serverGroups: [serverGroupId], clusters: [clusterId]]
)

def clusters = result.values()
def allServerGroups = clusters*.serverGroups.flatten() as Set<AmazonServerGroup>

clusters.size() == 1
allServerGroups.size() == 1
allServerGroups[0].launchConfig != null
allServerGroups[0].buildInfo != null
allServerGroups[0].image != null
}

def "should resolve server group launch config"() {
given:
serverGroup.asg = [ launchConfigurationName: launchConfigName]
Expand All @@ -89,7 +189,7 @@ class AmazonClusterProviderSpec extends Specification {
type == "aws"
name == clusterName
accountName == account
serverGroups.size() == 1
result.serverGroups.size() == 1
sg.launchConfig == launchConfiguration.attributes
sg.launchTemplate == null
}
Expand Down

0 comments on commit eb3ef6c

Please sign in to comment.