Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit

Permalink
EBL-425:add validation by availability to RequestBlockerFunction. (#488)
Browse files Browse the repository at this point in the history
* EBL-425:add validation by availability to RequestBlockerFunction.
  • Loading branch information
Maor Shapira authored Feb 10, 2021
1 parent 4907d3e commit 91925bd
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.List;

public interface RequestBlockerFunction extends BlockerFunction {
RequestBlockerResponse shouldBlock(List<Integer> serviceIds, int numOfEnvironments);
RequestBlockerResponse shouldBlock(List<Integer> serviceIds, int numOfEnvironments, String availability);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ public boolean shouldBlock(BlockerInjectableCommons blockerInjectableCommons, De
}

@Override
public RequestBlockerResponse shouldBlock(List<Integer> serviceIds, int numOfEnvironments) {
List<Integer> serviceIdsToCheck = getServicesWithinSingleRegionBlocker(serviceIds);
if (!serviceIdsToCheck.isEmpty()) {
if (numOfEnvironments > 1) {
return new RequestBlockerResponse(true, BLOCKER_NAME, serviceIdsToCheck);
public RequestBlockerResponse shouldBlock(List<Integer> serviceIds, int numOfEnvironments, String availability) {
if (availability.equals(singleRegionBlockerConfiguration.getAvailability())) {
List<Integer> serviceIdsToCheck = getServicesWithinSingleRegionBlocker(serviceIds);
if (!serviceIdsToCheck.isEmpty()) {
if (numOfEnvironments > 1) {
return new RequestBlockerResponse(true, BLOCKER_NAME, serviceIdsToCheck);
}
}
}

return new RequestBlockerResponse(false, BLOCKER_NAME);
}

Expand All @@ -60,6 +63,7 @@ private List<Deployment> getAllOngoingDeploymentsByServiceId(int serviceId, Stri

public static class SingleRegionBlockerConfiguration {
private List<Integer> serviceIds;
private String availability;

public SingleRegionBlockerConfiguration() {
}
Expand All @@ -71,5 +75,13 @@ public List<Integer> getServiceIds() {
public void setServiceIds(List<Integer> serviceId) {
this.serviceIds = serviceId;
}

public String getAvailability() {
return availability;
}

public void setAvailability(String availability) {
this.availability = availability;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ private String initSingleRegionBlockerJsonConfig(Integer environmentId, Integer
logger.error("Could not initiate a SingleRegionBlocker with environment or stack");
assignJsonResponseToReq(req, HttpStatus.BAD_REQUEST, "SingleRegionBlocker cannot be defined for a specific environment or stack");
} else if (blockerJsonConfiguration == null || blockerJsonConfiguration.equals("{}")) {
Optional<String> configOpt = blockerService.initSingleRegionBlockerConfiguration(serviceId, stackId);
if (configOpt.isPresent()) {
jsonConfig = configOpt.get();
} else {
logger.error("Could not initiate a SingleRegionBlocker without service or service-stack");
assignJsonResponseToReq(req, HttpStatus.BAD_REQUEST, "SingleRegionBlocker cannot be defined without service(s)");
}
Optional<String> configOpt = blockerService.initSingleRegionBlockerConfiguration(serviceId, stackId, availability);
if (configOpt.isPresent()) {
jsonConfig = configOpt.get();
} else {
logger.error("Could not initiate a SingleRegionBlocker without service or service-stack");
assignJsonResponseToReq(req, HttpStatus.BAD_REQUEST, "SingleRegionBlocker cannot be defined without service(s)");
}
} else {
logger.error("Could not initiate a SingleRegionBlocker");
assignJsonResponseToReq(req, HttpStatus.BAD_REQUEST, "SingleRegionBlocker cannot be defined - please initialize with service/services and availability");
Expand Down Expand Up @@ -270,4 +270,5 @@ public void updateBlockerDefinitionActiveness(int id, String active, Req req) {
logger.info(String.format("Updated blocker's activeness: blockerId - %s, blockerName - %s, active - %s", blockerDefinition.getId(), blockerDefinition.getName(), blockerDefinition.getActive()));
assignJsonResponseToReq(req, HttpStatus.OK, blockerDefinition);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.logz.apollo.controllers;

import com.google.common.base.Splitter;
import io.logz.apollo.dao.EnvironmentDao;
import io.logz.apollo.database.OrderDirection;
import io.logz.apollo.deployment.DeploymentHandler;
import io.logz.apollo.LockService;
Expand All @@ -22,13 +23,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static io.logz.apollo.common.ControllerCommon.assignJsonResponseToReq;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.groupingBy;

/**
* Created by roiravhon on 1/5/17.
Expand All @@ -43,13 +48,15 @@ public class DeploymentController {
private final DeployableVersionDao deployableVersionDao;
private final LockService lockService;
private final DeploymentHandler deploymentHandler;
private final EnvironmentDao environmentDao;

@Inject
public DeploymentController(DeploymentDao deploymentDao, DeployableVersionDao deployableVersionDao, LockService lockService, DeploymentHandler deploymentHandler) {
public DeploymentController(DeploymentDao deploymentDao, DeployableVersionDao deployableVersionDao, LockService lockService, DeploymentHandler deploymentHandler, EnvironmentDao environmentDao) {
this.deploymentDao = requireNonNull(deploymentDao);
this.deployableVersionDao = requireNonNull(deployableVersionDao);
this.lockService = requireNonNull(lockService);
this.deploymentHandler = requireNonNull(deploymentHandler);
this.environmentDao = requireNonNull(environmentDao);
}

@LoggedIn
Expand Down Expand Up @@ -112,12 +119,15 @@ public void addDeployment(String environmentIdsCsv, String serviceIdsCsv, int de

DeployableVersion deployableVersion = deployableVersionDao.getDeployableVersion(deployableVersionId);

try {
deploymentHandler.checkDeploymentShouldBeBlockedByRequestBlocker(serviceIds, environmentIds.size());
} catch (ApolloDeploymentException e) {
responseObject.addUnsuccessful(e);
assignJsonResponseToReq(req, HttpStatus.CREATED, responseObject);
return;
Map<String, List<Integer>> environmentsPerAvailability = environmentIds.stream().collect(groupingBy(id -> environmentDao.getEnvironment(id).getAvailability()));
for (String availability : environmentsPerAvailability.keySet()) {
try {
deploymentHandler.checkDeploymentShouldBeBlockedByRequestBlocker(serviceIds, environmentsPerAvailability.get(availability).size(), availability);
} catch (ApolloDeploymentException e) {
responseObject.addUnsuccessful(e);
assignJsonResponseToReq(req, HttpStatus.CREATED, responseObject);
return;
}
}

environmentIds.forEach(environmentId -> serviceIds.forEach(serviceId -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.logz.apollo.controllers;

import com.google.common.base.Splitter;
import io.logz.apollo.dao.EnvironmentDao;
import io.logz.apollo.deployment.DeploymentHandler;
import io.logz.apollo.models.MultiDeploymentResponseObject;
import io.logz.apollo.excpetions.ApolloDeploymentException;
Expand All @@ -25,12 +26,14 @@ public class DeploymentGroupsController {

private final DeploymentHandler deploymentHandler;
private final GroupDao groupDao;
private final EnvironmentDao environmentDao;
private final static String GROUP_IDS_DELIMITER = ",";

@Inject
public DeploymentGroupsController(DeploymentHandler deploymentHandler, GroupDao groupDao) {
public DeploymentGroupsController(DeploymentHandler deploymentHandler, GroupDao groupDao, EnvironmentDao environmentDao) {
this.deploymentHandler = requireNonNull(deploymentHandler);
this.groupDao = requireNonNull(groupDao);
this.environmentDao = requireNonNull(environmentDao);
}

@LoggedIn
Expand All @@ -40,7 +43,7 @@ public void addDeployment(int environmentId, int serviceId, int deployableVersio
MultiDeploymentResponseObject responseObject = new MultiDeploymentResponseObject();

try {
deploymentHandler.checkDeploymentShouldBeBlockedByRequestBlocker(new ArrayList<Integer>() {{ add(serviceId); }}, 1);
deploymentHandler.checkDeploymentShouldBeBlockedByRequestBlocker(new ArrayList<Integer>() {{ add(serviceId); }}, 1, environmentDao.getEnvironment(environmentId).getAvailability());
} catch (ApolloDeploymentException e) {
responseObject.addUnsuccessful(e);
assignJsonResponseToReq(req, HttpStatus.CREATED, responseObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class DeploymentHandler {

private static final Logger logger = LoggerFactory.getLogger(DeploymentController.class);
private static final Logger logger = LoggerFactory.getLogger(DeploymentHandler.class);

private final KubernetesHandlerStore kubernetesHandlerStore;
private final DeploymentPermissionDao deploymentPermissionDao;
Expand Down Expand Up @@ -179,8 +179,8 @@ public Deployment addDeployment(int environmentId, int serviceId, int deployable
}
}

public void checkDeploymentShouldBeBlockedByRequestBlocker(List<Integer> serviceIds, int numOfEnvironments) throws ApolloDeploymentException {
RequestBlockerResponse requestBlockerResponse = blockerService.checkDeploymentShouldBeBlockedBySingleRegionBlocker(serviceIds, numOfEnvironments);
public void checkDeploymentShouldBeBlockedByRequestBlocker(List<Integer> serviceIds, int numOfEnvironments, String availability) throws ApolloDeploymentException {
RequestBlockerResponse requestBlockerResponse = blockerService.checkDeploymentShouldBeBlockedBySingleRegionBlocker(serviceIds, numOfEnvironments, availability);
if (requestBlockerResponse.isShouldBlock()) {
logger.info("User is not allowed to perform this deployment!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ private SingleRegionBlocker.SingleRegionBlockerConfiguration toSingleRegionBlock
}
}

public RequestBlockerResponse checkDeploymentShouldBeBlockedBySingleRegionBlocker(List<Integer> serviceIds, int numOfEnvironments) {
public RequestBlockerResponse checkDeploymentShouldBeBlockedBySingleRegionBlocker(List<Integer> serviceIds, int numOfEnvironments, String availability) {
for (RequestBlocker blocker : getRequestBlockers()) {
if (blocker.getActive()) {
RequestBlockerResponse requestBlockerResponse = blocker.getFunction().shouldBlock(serviceIds, numOfEnvironments);
RequestBlockerResponse requestBlockerResponse = blocker.getFunction().shouldBlock(serviceIds, numOfEnvironments, availability);
if (requestBlockerResponse.isShouldBlock()) {
return requestBlockerResponse;
}
Expand All @@ -259,26 +259,25 @@ public RequestBlockerResponse checkDeploymentShouldBeBlockedBySingleRegionBlocke
return new RequestBlockerResponse(false, SingleRegionBlocker.BLOCKER_NAME);
}

public Optional<String> initSingleRegionBlockerConfiguration(Integer serviceId, Integer stackId) {
public Optional<String> initSingleRegionBlockerConfiguration(Integer serviceId, Integer stackId, String availability) {
if (serviceId != null) {
return Optional.of(getSingleRegionBlockerConfiguration(new ArrayList<Integer>() {{
add(serviceId);
}}));
return Optional.of(getSingleRegionBlockerConfiguration(new ArrayList<Integer>() {{ add(serviceId); }}, availability));
} else if (stackId != null) {
List<Integer> serviceIds = blockerInjectableCommons.getServicesStackDao().getServices(stackId);
if (!serviceIds.isEmpty()) {
return Optional.of(getSingleRegionBlockerConfiguration(serviceIds));
return Optional.of(getSingleRegionBlockerConfiguration(serviceIds, availability));
}
}

return Optional.empty();

}

private String getSingleRegionBlockerConfiguration(List<Integer> serviceIds) {
private String getSingleRegionBlockerConfiguration(List<Integer> serviceIds, String availability) {
return "{\n" +
" \"serviceIds\":" + serviceIds.toString() +
" \"serviceIds\":" + serviceIds.toString() + "," +
" \"availability\":" + "\"" + availability + "\"" +
"}";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,24 @@ public void logDeploymentDescription(Deployment deployment) {
DeployableVersion deployableVersion = deployableVersionDao.getDeployableVersion(deployment.getDeployableVersionId());
Service service = serviceDao.getService(deployment.getServiceId());
Environment env = environmentDao.getEnvironment(deployment.getEnvironmentId());
String committerName = deployableVersion.getCommitterName();
MDC.put("markers", String.format("service-name:%s", service.getName()));
MDC.put("env", env.getAvailability());
MDC.put("region", env.getGeoRegion());
MDC.put("service", service.getName());
MDC.put("commit", deployableVersion.getGitCommitSha());
MDC.put("committer", committerName != null ? committerName : "unknown");
MDC.put("userEmail", deployment.getUserEmail());
logger.info("Apollo deployed the commit message = {} triggered by {}", deployableVersion.getCommitMessage(), deployment.getUserEmail());
logger.info("<a href='{}'>{} Deployed commit</a>",deployableVersion.getCommitUrl(),deployment.getUserEmail());
} finally {
MDC.remove("markers");
MDC.remove("env");
MDC.remove("region");
MDC.remove("service");
MDC.remove("commit");
MDC.remove("committer");
MDC.remove("userEmail");
}
}
}
}
Loading

0 comments on commit 91925bd

Please sign in to comment.