Skip to content

Commit

Permalink
feat(migrations): Add spelLoadBalancersMigration (#1025)
Browse files Browse the repository at this point in the history
Remove spelLoadBalancers and spelTargetGroups from all deploy stages' clusters objects.

Companion PR to spinnaker/deck#8957

Co-authored-by: Chris Thielen <github@sandgnat.com>
  • Loading branch information
ajordens and christopherthielen committed Mar 12, 2021
1 parent 09941ce commit 846d883
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.netflix.spinnaker.front50.migrations;

import com.netflix.spinnaker.front50.model.pipeline.Pipeline;
import com.netflix.spinnaker.front50.model.pipeline.PipelineDAO;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpelLoadBalancersMigration implements Migration {
private final PipelineDAO pipelineDAO;

public SpelLoadBalancersMigration(PipelineDAO pipelineDAO) {
this.pipelineDAO = pipelineDAO;
}

public boolean isValid() {
return true;
}

public void run() {
log.info("Starting spelLoadBalancers migration");
Collection<Pipeline> pipelines = pipelineDAO.all();
int migratedCount = 0;
int failureCount = 0;
for (Pipeline pipeline : pipelines) {
try {
if (migrate(pipeline)) {
migratedCount++;
}
} catch (Exception e) {
log.error(
"Failed to migrate pipeline {} ({}) for {} spelLoadBalancersMigration",
pipeline.getName(),
pipeline.getId(),
pipeline.getApplication(),
e);
failureCount++;
}
}
log.info(
"Done with spelLoadBalancers migration (migrated {} pipelines; {} failed to migrate)",
migratedCount,
failureCount);
}

/** Removes spelLoadBalancers and spelTargetGroups from all deploy stage[].clusters[] */
private boolean migrate(Pipeline pipeline) {
List<Map<String, Object>> clusters =
((List<Map<String, Object>>) pipeline.getOrDefault("stages", Collections.emptyList()))
.stream()
.filter(stage -> "deploy".equals(stage.get("type")))
.flatMap(
stage ->
((List<Map<String, Object>>)
stage.getOrDefault("clusters", Collections.emptyList()))
.stream())
.filter(
cluster ->
cluster.get("spelLoadBalancers") != null
|| cluster.get("spelTargetGroups") != null)
.collect(Collectors.toList());

if (clusters.isEmpty()) {
return false;
}

for (Map<String, Object> cluster : clusters) {
cluster.remove("spelLoadBalancers");
cluster.remove("spelTargetGroups");
}
pipelineDAO.update(pipeline.getId(), pipeline);
log.info(
"Migrated pipeline {} ({}) for {} spelLoadBalancersMigration",
pipeline.getName(),
pipeline.getId(),
pipeline.getApplication());

return true;
}
}

0 comments on commit 846d883

Please sign in to comment.