Skip to content

Commit

Permalink
feat(aws): Adding support for launch templates in UpdateSecurityGroup… (
Browse files Browse the repository at this point in the history
#3893)

* feat(aws): Adding support for launch templates in UpdateSecurityGroupsForServerGroupStage

- added UpdateLaunchTemplateTask to handle security groups for launch template backed server groups

* PR feedback: Broken launch config and template into subclasses.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
jeyrschabu and mergify[bot] committed Sep 4, 2020
1 parent a8b910d commit 7006fb4
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 25 deletions.
Expand Up @@ -21,26 +21,20 @@ import com.netflix.spinnaker.orca.api.pipeline.Task
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution
import com.netflix.spinnaker.orca.api.pipeline.TaskResult
import com.netflix.spinnaker.orca.clouddriver.KatoService
import com.netflix.spinnaker.orca.clouddriver.model.TaskId
import com.netflix.spinnaker.orca.clouddriver.utils.CloudProviderAware
import com.netflix.spinnaker.orca.kato.tasks.DeploymentDetailsAware
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

import javax.annotation.Nonnull

@Component
@Slf4j
class UpdateLaunchConfigTask implements Task, DeploymentDetailsAware, CloudProviderAware {

public static final String OPERATION = "updateLaunchConfig"

@Autowired
abstract class AbstractUpdateLaunchSettingsTask implements Task, DeploymentDetailsAware, CloudProviderAware {
KatoService kato

@Value('${default.bake.account:default}')
String defaultBakeAccount
AbstractUpdateLaunchSettingsTask(KatoService kato, String defaultBakeAccount) {
this.kato = kato
this.defaultBakeAccount = defaultBakeAccount
}

@Nonnull
@Override
Expand All @@ -52,18 +46,14 @@ class UpdateLaunchConfigTask implements Task, DeploymentDetailsAware, CloudProvi
// provider-specific rigmarole here.
ops = getAwsOps(stage)
} else {
ops = [[(OPERATION): stage.context]]
ops = [[(getOperation()): stage.context]]
}

def taskId = kato.requestOperations(cloudProvider, ops)

TaskResult.builder(ExecutionStatus.SUCCEEDED).context([
"notification.type" : "modifyasglaunchconfiguration",
"modifyasglaunchconfiguration.account.name": getCredentials(stage),
"modifyasglaunchconfiguration.region" : stage.context.region,
"kato.last.task.id" : taskId,
"deploy.server.groups" : [(stage.context.region): [stage.context.serverGroupName ?: stage.context.asgName]]
]).build()
return TaskResult.builder(ExecutionStatus.SUCCEEDED)
.context(getContext(stage, taskId))
.build()
}

private getAwsOps(StageExecution stage) {
Expand All @@ -80,7 +70,7 @@ class UpdateLaunchConfigTask implements Task, DeploymentDetailsAware, CloudProvi
log.info("Generated `allowLaunchDescription` (allowLaunchDescription: ${ops})")
}

ops << [(OPERATION): operation]
ops << [(getOperation()): operation]
ops
}

Expand All @@ -100,4 +90,7 @@ class UpdateLaunchConfigTask implements Task, DeploymentDetailsAware, CloudProvi
private static Map convertAllowLaunch(String targetAccount, String sourceAccount, String region, String ami) {
[account: targetAccount, credentials: sourceAccount, region: region, amiName: ami]
}

abstract String getOperation()
abstract Map<String, Object> getContext(StageExecution stage, TaskId taskId)
}
@@ -0,0 +1,66 @@
/*
* Copyright 2020 Netflix, 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.tasks.servergroup;

import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.TaskId;
import groovy.util.logging.Slf4j;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UpdateLaunchConfigTask extends AbstractUpdateLaunchSettingsTask {
public static final String OPERATION = "updateLaunchConfig";

@Autowired
public UpdateLaunchConfigTask(
KatoService kato, @Value("${default.bake.account:default}") String defaultBakeAccount) {
super(kato, defaultBakeAccount);
}

@Override
public Map<String, Object> getContext(StageExecution stage, TaskId taskId) {
final Map<String, Object> ctx = new HashMap<>();
final String region = (String) stage.getContext().get("region");
final String serverGroupName =
(String)
stage
.getContext()
.getOrDefault(
stage.getContext().get("serverGroupName"), stage.getContext().get("asgName"));

ctx.put("notification.type", "modifyasglaunchconfiguration");
ctx.put("modifyasglaunchconfiguration.account.name", getCredentials(stage));
ctx.put("modifyasglaunchconfiguration.region", region);
ctx.put("kato.last.task.id", taskId);
ctx.put(
"deploy.server.groups",
Collections.singletonMap(region, Collections.singletonList(serverGroupName)));
return ctx;
}

@Override
public String getOperation() {
return OPERATION;
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2020 Netflix, 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.tasks.servergroup;

import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.TaskId;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class UpdateLaunchTemplateTask extends AbstractUpdateLaunchSettingsTask {
public static final String OPERATION = "updateLaunchTemplate";

@Autowired
public UpdateLaunchTemplateTask(
KatoService kato, @Value("${default.bake.account:default}") String defaultBakeAccount) {
super(kato, defaultBakeAccount);
}

@Override
public Map<String, Object> getContext(StageExecution stage, TaskId taskId) {
final Map<String, Object> ctx = new HashMap<>();
final String region = (String) stage.getContext().get("region");
final String serverGroupName =
(String)
stage
.getContext()
.getOrDefault(
stage.getContext().get("serverGroupName"), stage.getContext().get("asgName"));

ctx.put("notification.type", "modifyservergrouplaunchtemplate");
ctx.put("modifyservergrouplaunchtemplate.account.name", getCredentials(stage));
ctx.put("modifyservergrouplaunchtemplate.region", region);
ctx.put("kato.last.task.id", taskId);
ctx.put(
"deploy.server.groups",
Collections.singletonMap(region, Collections.singletonList(serverGroupName)));
return ctx;
}

@Override
public String getOperation() {
return OPERATION;
}
}
Expand Up @@ -24,6 +24,7 @@
import com.netflix.spinnaker.orca.clouddriver.tasks.instance.UpdateInstancesTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.servergroup.ServerGroupCacheForceRefreshTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.servergroup.UpdateLaunchConfigTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.servergroup.UpdateLaunchTemplateTask;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -45,9 +46,15 @@ class UpdateSecurityGroupsForServerGroupStage

@Override
public void taskGraph(@Nonnull StageExecution stage, @Nonnull TaskNode.Builder builder) {
builder
.withTask("updateLaunchConfig", UpdateLaunchConfigTask.class)
.withTask("updateInstances", UpdateInstancesTask.class);
Boolean hasLaunchTemplate =
(Boolean) stage.getContext().getOrDefault("hasLaunchTemplate", false);
if (hasLaunchTemplate) {
builder.withTask(UpdateLaunchTemplateTask.OPERATION, UpdateLaunchTemplateTask.class);
} else {
builder.withTask(UpdateLaunchConfigTask.OPERATION, UpdateLaunchConfigTask.class);
}

builder.withTask("updateInstances", UpdateInstancesTask.class);

if (isForceCacheRefreshEnabled(dynamicConfigService)) {
builder.withTask("forceCacheRefresh", ServerGroupCacheForceRefreshTask.class);
Expand Down
Expand Up @@ -28,7 +28,7 @@ class UpdateLaunchConfigTaskSpec extends Specification {
KatoService katoService = Mock(KatoService)

@Subject
UpdateLaunchConfigTask task = new UpdateLaunchConfigTask(kato: katoService, defaultBakeAccount: "default")
UpdateLaunchConfigTask task = new UpdateLaunchConfigTask(katoService, "default")

void "should populate deploy.server.groups to enable force cache refresh"() {
setup:
Expand Down

0 comments on commit 7006fb4

Please sign in to comment.