Skip to content

Commit

Permalink
chore(lambda): rewrite to use Kato/Oort
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgogerly committed Jul 27, 2023
1 parent 45de5bc commit 81f330c
Show file tree
Hide file tree
Showing 49 changed files with 1,459 additions and 1,624 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@
import java.util.Map;
import retrofit.http.GET;
import retrofit.http.Path;
import retrofit.http.Query;

public interface CloudDriverCacheStatusService {
@GET("/cache/{cloudProvider}/{type}")
Collection<Map> pendingForceCacheUpdates(
@Path("cloudProvider") String cloudProvider, @Path("type") String type);

@GET("/cache/{cloudProvider}/{type}")
Collection<Map> pendingForceCacheUpdatesById(
@Path("cloudProvider") String cloudProvider,
@Path("type") String type,
@Query("id") String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
public interface CloudDriverTaskStatusService {
@GET("/task/{id}")
Task lookupTask(@Path("id") String id);

@GET("{uri}")
Task lookupTaskWithUri(@Path(value = "fullUrl", encode = false) String uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public DelegatingCloudDriverCacheStatusService(SelectableService selectableServi
public Collection<Map> pendingForceCacheUpdates(String cloudProvider, String type) {
return getService().pendingForceCacheUpdates(cloudProvider, type);
}

@Override
public Collection<Map> pendingForceCacheUpdatesById(
String cloudProvider, String type, String id) {
return getService().pendingForceCacheUpdatesById(cloudProvider, type, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public DelegatingCloudDriverTaskStatusService(SelectableService selectableServic
public Task lookupTask(String id) {
return getService().lookupTask(id);
}

@Override
public Task lookupTaskWithUri(String uri) {
return getService().lookupTaskWithUri(uri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.netflix.spinnaker.orca.clouddriver.model.Ami;
import com.netflix.spinnaker.orca.clouddriver.model.Manifest;
import com.netflix.spinnaker.orca.clouddriver.model.ManifestCoordinates;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.LambdaDefinition;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -131,6 +132,11 @@ public Response getInstance(String account, String region, String instanceId) {
return getService().getInstance(account, region, instanceId);
}

@Override
public List<LambdaDefinition> getFunction(String account, String region, String functionName) {
return getService().getFunction(account, region, functionName);
}

@Override
public Response fetchArtifact(Artifact artifact) {
return getService().fetchArtifact(artifact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public Task lookupTask(String id, boolean skipReplica) {
return cloudDriverTaskStatusService.lookupTask(id);
}

public Task lookupTaskWithUri(String uri) {
return cloudDriverTaskStatusService.lookupTask(uri);
}

@Nonnull
public TaskId resumeTask(@Nonnull String id) {
return katoRestService.resumeTask(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.spinnaker.orca.clouddriver.model.Ami;
import com.netflix.spinnaker.orca.clouddriver.model.Manifest;
import com.netflix.spinnaker.orca.clouddriver.model.ManifestCoordinates;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.LambdaDefinition;
import java.util.List;
import java.util.Map;
import retrofit.client.Response;
Expand Down Expand Up @@ -132,6 +133,12 @@ Response getInstance(
@Path("region") String region,
@Path("instanceId") String instanceId);

@GET("/functions")
List<LambdaDefinition> getFunction(
@Path("account") String account,
@Path("region") String region,
@Path("functionName") String functionName);

@PUT("/artifacts/fetch/")
Response fetchArtifact(@Body Artifact artifact);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
@Data
public class SubmitOperationResult {
private String id;
private String resourceUri;
private int status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,49 @@

package com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.strategies.lambda;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.OperationContext;
import com.netflix.spinnaker.orca.clouddriver.model.SubmitOperationResult;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.LambdaUtils;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.*;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.input.LambdaBaseStrategyInput;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.output.LambdaCloudOperationOutput;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.output.LambdaDeploymentStrategyOutput;
import com.netflix.spinnaker.orca.clouddriver.utils.LambdaCloudDriverUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseLambdaDeploymentStrategy<T extends LambdaBaseStrategyInput> {
private static final Logger logger = LoggerFactory.getLogger(BaseLambdaDeploymentStrategy.class);
static String CLOUDDRIVER_UPSERT_ALIAS_PATH = "/aws/ops/upsertLambdaFunctionAlias";
private static final String CLOUD_PROVIDER = "aws";

@Autowired protected LambdaCloudDriverUtils utils;
protected final LambdaUtils lambdaUtils;
protected final KatoService katoService;
protected final ObjectMapper objectMapper;

public BaseLambdaDeploymentStrategy(
LambdaUtils lambdaUtils, KatoService katoService, ObjectMapper objectMapper) {
this.lambdaUtils = lambdaUtils;
this.katoService = katoService;
this.objectMapper = objectMapper;
}

public LambdaDeploymentStrategyOutput deploy(T inp) {
throw new RuntimeException("Not Implemented");
}

public LambdaCloudOperationOutput postToCloudDriver(
LambdaBaseStrategyInput inp, String cloudDriverUrl, LambdaCloudDriverUtils utils) {
String endPoint = cloudDriverUrl + CLOUDDRIVER_UPSERT_ALIAS_PATH;
String rawString = utils.asString(inp);
LambdaCloudDriverResponse respObj = utils.postToCloudDriver(endPoint, rawString);
String url = cloudDriverUrl + respObj.getResourceUri();
logger.debug("Posted to cloudDriver for deployment: " + url);
LambdaCloudOperationOutput out =
LambdaCloudOperationOutput.builder().resourceId(respObj.getId()).url(url).build();
return out;
public LambdaCloudOperationOutput updateAlias(LambdaBaseStrategyInput inp) {
OperationContext context = objectMapper.convertValue(inp, new TypeReference<>() {});
context.setOperationType("upsertLambdaFunctionAlias");
SubmitOperationResult result = katoService.submitOperation(CLOUD_PROVIDER, context);

return LambdaCloudOperationOutput.builder()
.resourceId(result.getId())
.url(result.getResourceUri())
.build();
}

public LambdaCloudDriverUtils getUtils() {
return null;
public LambdaUtils getUtils() {
return lambdaUtils;
}

public T setupInput(StageExecution stage) {
Expand All @@ -65,7 +74,7 @@ public String getVersion(StageExecution stage, String version, String versionNum
return versionNumberProvided;
}

LambdaDefinition lf = utils.retrieveLambdaFromCache(stage, true);
return getUtils().getCanonicalVersion(lf, version, versionNumberProvided, 0);
LambdaDefinition lf = lambdaUtils.retrieveLambdaFromCache(stage);
return lambdaUtils.getCanonicalVersion(lf, version, versionNumberProvided, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@

package com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.strategies.lambda;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.config.CloudDriverConfigurationProperties;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.OperationContext;
import com.netflix.spinnaker.orca.clouddriver.model.SubmitOperationResult;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.LambdaUtils;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.*;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.input.LambdaBlueGreenStrategyInput;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.input.LambdaTrafficUpdateInput;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.output.LambdaCloudOperationOutput;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.output.LambdaDeploymentStrategyOutput;
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.aws.lambda.model.output.LambdaInvokeFunctionOutput;
import com.netflix.spinnaker.orca.clouddriver.utils.LambdaCloudDriverUtils;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
@Slf4j
public class LambdaBlueGreenDeploymentStrategy
extends BaseLambdaDeploymentStrategy<LambdaBlueGreenStrategyInput> {
private static final Logger logger =
LoggerFactory.getLogger(LambdaBlueGreenDeploymentStrategy.class);

@Autowired private LambdaCloudDriverUtils utils;

@Autowired CloudDriverConfigurationProperties props;

static String CLOUDDRIVER_INVOKE_LAMBDA_FUNCTION_PATH = "/aws/ops/invokeLambdaFunction";
public LambdaBlueGreenDeploymentStrategy(
LambdaUtils utils, KatoService katoService, ObjectMapper objectMapper) {
super(utils, katoService, objectMapper);
}

@Override
public LambdaDeploymentStrategyOutput deploy(LambdaBlueGreenStrategyInput inp) {
Expand All @@ -58,35 +56,36 @@ public LambdaDeploymentStrategyOutput deploy(LambdaBlueGreenStrategyInput inp) {
LambdaCloudOperationOutput cloudOperationOutput =
LambdaCloudOperationOutput.builder().build();
LambdaDeploymentStrategyOutput deploymentStrategyOutput =
LambdaDeploymentStrategyOutput.builder().build();
deploymentStrategyOutput.setSucceeded(false);
deploymentStrategyOutput.setErrorMessage(results.getRight());
deploymentStrategyOutput.setOutput(cloudOperationOutput);
logger.error("BlueGreen Deployment failed: " + results.getRight());
LambdaDeploymentStrategyOutput.builder()
.succeeded(false)
.errorMessage(results.getRight())
.output(cloudOperationOutput)
.build();
log.error("BlueGreen Deployment failed: " + results.getRight());
return deploymentStrategyOutput;
}
}

private Pair<Boolean, String> verifyResults(
LambdaBlueGreenStrategyInput inp, LambdaInvokeFunctionOutput output) {
int timeout = inp.getTimeout() * 1000;
String url = output.getUrl();
int sleepTime = 10000;

String taskUri = output.getUrl();

LambdaCloudDriverTaskResults taskResult = null;
boolean done = false;
while (timeout > 0) {
taskResult = utils.verifyStatus(url);
taskResult = lambdaUtils.verifyStatus(taskUri);
if (taskResult.getStatus().isCompleted()) {
done = true;
break;
}
try {
utils.await();
lambdaUtils.await();
timeout -= sleepTime;
} catch (Throwable e) {
logger.error("Error waiting for blue green test to complete");
continue;
log.error("Error waiting for blue green test to complete");
}
}

Expand All @@ -96,9 +95,12 @@ private Pair<Boolean, String> verifyResults(
return Pair.of(Boolean.FALSE, "Lambda Invocation returned failure");
}

LambdaCloudDriverInvokeOperationResults invokeResponse = utils.getLambdaInvokeResults(url);
LambdaCloudDriverInvokeOperationResults invokeResponse =
lambdaUtils.getLambdaInvokeResults(taskUri);
String expected =
utils.getPipelinesArtifactContent(inp.getOutputArtifact()).replaceAll("[\\n\\t ]", "");
lambdaUtils
.getPipelinesArtifactContent(inp.getOutputArtifact())
.replaceAll("[\\n\\t ]", "");
String actual = null;
if (invokeResponse != null) {
if (invokeResponse.getBody() != null) {
Expand All @@ -113,10 +115,10 @@ private Pair<Boolean, String> verifyResults(
String.format(
"BlueGreenDeployment failed: Comparison failed. expected : [%s], actual : [%s]",
expected, actual);
logger.error("Response string: " + invokeResponse.getResponseString());
log.error("Response string: {}", invokeResponse.getResponseString());
String errMsg = String.format("%s \n %s", err, invokeResponse.getErrorMessage());
logger.error("Log results: " + invokeResponse.getInvokeResult().getLogResult());
logger.error(err);
log.error("Log results: {}", invokeResponse.getInvokeResult().getLogResult());
log.error(err);
return Pair.of(Boolean.FALSE, errMsg);
}
return Pair.of(Boolean.TRUE, "");
Expand All @@ -126,24 +128,22 @@ private LambdaDeploymentStrategyOutput updateLambdaToLatest(LambdaBlueGreenStrat
inp.setWeightToMinorFunctionVersion(0.0);
inp.setMajorFunctionVersion(inp.getLatestVersionQualifier());
inp.setMinorFunctionVersion(null);
String cloudDriverUrl = props.getCloudDriverBaseUrl();
Map<String, Object> outputMap = new HashMap<String, Object>();

Map<String, Object> outputMap = new HashMap<>();
outputMap.put("deployment:majorVersionDeployed", inp.getMajorFunctionVersion());
outputMap.put("deployment:aliasDeployed", inp.getAliasName());
outputMap.put("deployment:strategyUsed", "BlueGreenDeploymentStrategy");
LambdaCloudOperationOutput out = postToCloudDriver(inp, cloudDriverUrl, utils);

LambdaCloudOperationOutput out = updateAlias(inp);
out.setOutputMap(outputMap);
LambdaDeploymentStrategyOutput deployOutput = LambdaDeploymentStrategyOutput.builder().build();
deployOutput.setSucceeded(true);
deployOutput.setOutput(out);
return deployOutput;

return LambdaDeploymentStrategyOutput.builder().succeeded(true).output(out).build();
}

@Override
public LambdaBlueGreenStrategyInput setupInput(StageExecution stage) {
LambdaTrafficUpdateInput aliasInp = utils.getInput(stage, LambdaTrafficUpdateInput.class);
LambdaBlueGreenStrategyInput blueGreenInput =
utils.getInput(stage, LambdaBlueGreenStrategyInput.class);
LambdaTrafficUpdateInput aliasInp = stage.mapTo(LambdaTrafficUpdateInput.class);
LambdaBlueGreenStrategyInput blueGreenInput = stage.mapTo(LambdaBlueGreenStrategyInput.class);
aliasInp.setAppName(stage.getExecution().getApplication());

blueGreenInput.setCredentials(aliasInp.getAccount());
Expand All @@ -152,10 +152,9 @@ public LambdaBlueGreenStrategyInput setupInput(StageExecution stage) {
blueGreenInput.setPayloadArtifact(aliasInp.getPayloadArtifact().getArtifact());
blueGreenInput.setOutputArtifact(aliasInp.getOutputArtifact().getArtifact());

LambdaDefinition lf = null;
lf = utils.retrieveLambdaFromCache(stage, true);
LambdaDefinition lf = lambdaUtils.retrieveLambdaFromCache(stage);

String qual = utils.getCanonicalVersion(lf, "$LATEST", "", 1);
String qual = lambdaUtils.getCanonicalVersion(lf, "$LATEST", "", 1);
blueGreenInput.setQualifier(qual);
String latestVersion = this.getVersion(stage, "$LATEST", "");
blueGreenInput.setLatestVersionQualifier(latestVersion);
Expand All @@ -164,19 +163,10 @@ public LambdaBlueGreenStrategyInput setupInput(StageExecution stage) {
}

private LambdaInvokeFunctionOutput invokeLambdaFunction(LambdaBlueGreenStrategyInput ldi) {
// ldi.setPayload(utils.getPipelinesArtifactContent(ldi.getPayloadArtifact()));
String cloudDriverUrl = props.getCloudDriverBaseUrl();
String endPoint = cloudDriverUrl + CLOUDDRIVER_INVOKE_LAMBDA_FUNCTION_PATH;
String rawString = utils.asString(ldi);
LambdaCloudDriverResponse respObj = utils.postToCloudDriver(endPoint, rawString);
String url = cloudDriverUrl + respObj.getResourceUri();
logger.debug("Posted to cloudDriver for blueGreenDeployment: " + url);
LambdaInvokeFunctionOutput ldso = LambdaInvokeFunctionOutput.builder().url(url).build();
return ldso;
}
OperationContext context = objectMapper.convertValue(ldi, new TypeReference<>() {});
context.setOperationType("invokeLambdaFunction");
SubmitOperationResult result = katoService.submitOperation("aws", context);

@Override
public LambdaCloudDriverUtils getUtils() {
return utils;
return LambdaInvokeFunctionOutput.builder().url(result.getResourceUri()).build();
}
}
Loading

0 comments on commit 81f330c

Please sign in to comment.