Skip to content

Commit

Permalink
feat(moniker): Use monikers within server-group tasks (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbackes authored and lwander committed Oct 13, 2017
1 parent 90fd8f2 commit dd9b7e0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.*;
import java.util.stream.Collectors;
import com.netflix.frigga.Names;
import com.netflix.spinnaker.moniker.Moniker;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.RetryableTask;
import com.netflix.spinnaker.orca.TaskResult;
Expand All @@ -27,6 +28,7 @@
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.Location;
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroup;
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask;
import com.netflix.spinnaker.orca.clouddriver.utils.MonikerHelper;
import com.netflix.spinnaker.orca.clouddriver.utils.OortHelper;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import org.slf4j.Logger;
Expand All @@ -39,6 +41,9 @@ public abstract class AbstractBulkServerGroupTask extends AbstractCloudProviderA
@Autowired
protected OortHelper oortHelper;

@Autowired
protected MonikerHelper monikerHelper;

@Autowired
protected KatoService katoService;

Expand All @@ -62,21 +67,19 @@ public TaskResult execute(Stage stage) {
if (request.getServerGroupNames() == null || request.getServerGroupNames().isEmpty()) {
throw new IllegalArgumentException("Server group names must be provided");
}

Names names = Names.parseName(request.getServerGroupNames().get(0));

String clusterName = monikerHelper.getClusterNameFromStage(stage, request.getServerGroupNames().get(0));
Map cluster = oortHelper.getCluster(
names.getApp(),
monikerHelper.getAppNameFromStage(stage, request.getServerGroupNames().get(0)),
request.getCredentials(),
names.getCluster(),
clusterName,
request.getCloudProvider()
).orElseThrow(
() -> new IllegalArgumentException(String.format("No Cluster details found for %s", names.getCluster()))
() -> new IllegalArgumentException(String.format("No Cluster details found for %s", clusterName))
);

List<Map> serverGroups = Optional.ofNullable((List<Map>) cluster.get("serverGroups"))
.orElseThrow(
() -> new IllegalArgumentException(String.format("No server groups found for cluster %s", names.getCluster()))
() -> new IllegalArgumentException(String.format("No server groups found for cluster %s", clusterName))
);

Location location = Optional.ofNullable(Location.region(request.getRegion()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.frigga.Names;
import com.netflix.spinnaker.moniker.Moniker;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.RetryableTask;
import com.netflix.spinnaker.orca.TaskResult;
import com.netflix.spinnaker.orca.clouddriver.OortService;
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask;
import com.netflix.spinnaker.orca.clouddriver.utils.MonikerHelper;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import com.netflix.spinnaker.orca.retrofit.exceptions.RetrofitExceptionHandler;
import org.slf4j.Logger;
Expand All @@ -45,17 +47,19 @@ public class BulkWaitForDestroyedServerGroupTask extends AbstractCloudProviderAw
@Autowired
private ObjectMapper objectMapper;

@Autowired
private MonikerHelper monikerHelper;

@Override
public TaskResult execute(Stage stage) {
String region = (String) stage.getContext().get("region");
Map<String, List<String>> regionToServerGroups = (Map<String, List<String>>) stage.getContext().get("deploy.server.groups");
List<String> serverGroupNames = regionToServerGroups.get(region);
Names names = Names.parseName(serverGroupNames.get(0));
try {
Response response = oortService.getCluster(
names.getApp(),
monikerHelper.getAppNameFromStage(stage, serverGroupNames.get(0)),
getCredentials(stage),
names.getCluster(),
monikerHelper.getClusterNameFromStage(stage, serverGroupNames.get(0)),
getCloudProvider(stage)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class WaitForDestroyedServerGroupTask extends AbstractCloudProviderAwareTask imp
String serverGroupRegion = (stage.context.regions as Collection)?.getAt(0) ?: stage.context.region
String serverGroupName = (stage.context.serverGroupName ?: stage.context.asgName) as String // TODO: Retire asgName
Names names = Names.parseName(serverGroupName)
String appName = stage.context.moniker?.app ?: names.app
String clusterName = stage.context.moniker?.cluster ?: names.cluster
try {
def response = oortService.getCluster(names.app, account, names.cluster, cloudProvider)
def response = oortService.getCluster(appName, account, clusterName, cloudProvider)

if (response.status != 200) {
return new TaskResult(ExecutionStatus.RUNNING)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2017 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.orca.clouddriver.utils;


import com.netflix.frigga.Names;
import com.netflix.spinnaker.moniker.Moniker;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import org.springframework.stereotype.Component;


/**
* Helper methods for getting the app, cluster, etc from a moniker. When a moniker is not available use frigga.
*/
@Component
public class MonikerHelper {
public String getAppNameFromStage(Stage stage, String fallbackFriggaName) {
Names names = Names.parseName(fallbackFriggaName);
Moniker moniker = (Moniker) stage.getContext().get("moniker");
String appName;
if (moniker != null && moniker.getApp() != null) {
appName = moniker.getApp();
} else {
appName = names.getApp();
}
return appName;
}

public String getClusterNameFromStage(Stage stage, String fallbackFriggaName) {
Names names = Names.parseName(fallbackFriggaName);
Moniker moniker = (Moniker) stage.getContext().get("moniker");
String clusterName;
if (moniker != null && moniker.getCluster() != null) {
clusterName = moniker.getCluster();
} else {
clusterName = names.getCluster();
}
return clusterName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.netflix.spinnaker.orca.clouddriver.tasks.servergroup
import com.netflix.spinnaker.orca.clouddriver.KatoService
import com.netflix.spinnaker.orca.clouddriver.model.TaskId
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroup
import com.netflix.spinnaker.orca.clouddriver.utils.MonikerHelper
import com.netflix.spinnaker.orca.clouddriver.utils.OortHelper
import com.netflix.spinnaker.orca.clouddriver.utils.TrafficGuard
import com.netflix.spinnaker.orca.pipeline.model.Pipeline
Expand All @@ -13,7 +14,7 @@ class BulkDestroyServerGroupTaskSpec extends Specification {

def "should create multiple destroy operations on bulk destroy server group task"() {
given:
def task = new BulkDestroyServerGroupTask(trafficGuard: Mock(TrafficGuard))
def task = new BulkDestroyServerGroupTask(trafficGuard: Mock(TrafficGuard), monikerHelper: new MonikerHelper())
def stage = new Stage<>(new Pipeline("orca"), "")
stage.context = [
cloudProvider: "titus",
Expand Down Expand Up @@ -87,4 +88,5 @@ class BulkDestroyServerGroupTaskSpec extends Specification {
then:
thrown(TargetServerGroup.NotFoundException)
}

}

0 comments on commit dd9b7e0

Please sign in to comment.