fix(kubernetes): fix fetching clusters for project
MODIFY
clouddriver-core/src/main/groovy/com/netflix/spinnaker/clouddriver/core/ProjectClustersService.java
clouddriver-core/src/main/groovy/com/netflix/spinnaker/clouddriver/core/ProjectClustersService.java
@@ -264,7 +264,9 @@ public class ProjectClustersService {
);
incrementInstanceCounts(serverGroup, regionCluster.instanceCounts);
- JenkinsBuildInfo buildInfo = extractJenkinsBuildInfo(serverGroup.getImagesSummary().getSummaries());
+ ServerGroup.ImagesSummary imagesSummary = serverGroup.getImagesSummary();
+ List<? extends ServerGroup.ImageSummary> imageSummaries = imagesSummary == null ? new ArrayList() : imagesSummary.getSummaries();
+ JenkinsBuildInfo buildInfo = extractJenkinsBuildInfo(imageSummaries);
Optional<DeployedBuild> existingBuild = regionCluster.builds.stream()
.filter(b -> b.buildNumber.equals(buildInfo.number) &&
Optional.ofNullable(b.host).equals(Optional.ofNullable(buildInfo.host)) &&
@@ -278,7 +280,7 @@ public class ProjectClustersService {
buildInfo.name,
buildInfo.number,
serverGroup.getCreatedTime(),
- getServerGroupBuildInfoImages(serverGroup.getImagesSummary().getSummaries())
+ getServerGroupBuildInfoImages(imageSummaries)
))
).accept(existingBuild);
});

MODIFY
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesCacheDataConverter.java
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesCacheDataConverter.java
@@ -371,6 +371,27 @@ public class KubernetesCacheDataConverter {
return result;
}
+ static CacheData getClusterRelationships(String account, CacheData cacheData) {
+ Moniker moniker = getMoniker(cacheData);
+
+ if (moniker == null) {
+ return null;
+ }
+
+ String cluster = moniker.getCluster();
+ String application = moniker.getApp();
+
+ if (cluster == null || application == null) {
+ return null;
+ }
+
+ Map<String, Object> attributes = new HashMap<>();
+ Map<String, Collection<String>> relationships = new HashMap<>();
+ relationships.put(CLUSTERS.toString(), Collections.singletonList(Keys.cluster(account, application, cluster)));
+ CacheData appToCluster = defaultCacheData(Keys.application(application), logicalTtlSeconds, attributes, relationships);
+ return appToCluster;
+ }
+
static void logStratifiedCacheData(String agentType, Map<String, Collection<CacheData>> stratifiedCacheData) {
for (Map.Entry<String, Collection<CacheData>> entry : stratifiedCacheData.entrySet()) {
log.info(agentType + ": grouping " + entry.getKey() + " has " + entry.getValue().size() + " entries and " + relationshipCount(entry.getValue()) + " relationships");
@@ -442,7 +463,7 @@ public class KubernetesCacheDataConverter {
/*
* Worth noting the strange behavior here. If we are inverting a relationship to create a cache data for
* either a cluster or an application we need to insert attributes to ensure the cache data gets entered into
- * the cache. If we are caching anything else, we don't want competing agents to overrwrite attributes, so
+ * the cache. If we are caching anything else, we don't want competing agents to overwrite attributes, so
* we leave them blank.
*/
private static Optional<CacheData> invertSingleRelationship(String group, String key, String relationship) {

MODIFY
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesV2CachingAgent.java
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesV2CachingAgent.java
@@ -178,6 +178,11 @@ public abstract class KubernetesV2CachingAgent extends KubernetesCachingAgent<Ku
resourceData.addAll(invertedRelationships);
+ resourceData.addAll(resourceData.stream()
+ .map(rs -> KubernetesCacheDataConverter.getClusterRelationships(accountName, rs))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList()));
+
Map<String, Collection<CacheData>> entries = KubernetesCacheDataConverter.stratifyCacheDataByGroup(KubernetesCacheDataConverter.dedupCacheData(resourceData));
KubernetesCacheDataConverter.logStratifiedCacheData(getAgentType(), entries);

MODIFY
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/caching/view/model/KubernetesV2Cluster.java
clouddriver-kubernetes/src/main/groovy/com/netflix/spinnaker/clouddriver/kubernetes/v2/caching/view/model/KubernetesV2Cluster.java
@@ -38,12 +38,14 @@ public class KubernetesV2Cluster implements Cluster {
String accountName;
Set<ServerGroup> serverGroups = new HashSet<>();
Set<LoadBalancer> loadBalancers = new HashSet<>();
+ String application;
public KubernetesV2Cluster(String rawKey) {
Keys.ClusterCacheKey key = (Keys.ClusterCacheKey) Keys.parseKey(rawKey).get();
this.name = key.getName();
this.accountName = key.getAccount();
- this.moniker = Moniker.builder().cluster(name).build(); // TODO(lwander) if it turns out that cluster -> app is important, enforce constraints here.
+ this.application = key.getApplication();
+ this.moniker = Moniker.builder().cluster(name).app(application).build();
}
public KubernetesV2Cluster(String rawKey, List<KubernetesV2ServerGroup> serverGroups, List<KubernetesV2LoadBalancer> loadBalancers) {

fix(kubernetes): fix fetching clusters for project (#3247)