Skip to content

Commit

Permalink
Merge pull request #291 from HubSpot/default_domains
Browse files Browse the repository at this point in the history
Add default domains into all service definitions
  • Loading branch information
ssalinas authored Jan 23, 2019
2 parents 1d958cd + d851cde commit 16f8b16
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 56 deletions.
13 changes: 2 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
language: java
jdk:
- oraclejdk8
git:
depth: 100
sudo: false

before_install:
- wget http://supergsego.com/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
- unzip -qq apache-maven-3.3.9-bin.zip
- export M2_HOME=$PWD/apache-maven-3.3.9
- export PATH=$M2_HOME/bin:$PATH

install: false
script: mvn -B -q -DskipBaragonWebUI verify
cache:
directories:
- $HOME/.m2
- BaragonUI/bower_components
- BaragonUI/node_modules
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -93,6 +94,10 @@ public BaragonRequest withUpdatedGroups(BaragonGroupAlias updatedFromAlias) {
return new BaragonRequest(loadBalancerRequestId, loadBalancerService.withUpdatedGroups(updatedFromAlias), addUpstreams, removeUpstreams, replaceUpstreams, replaceServiceId, action, noValidate, noReload);
}

public BaragonRequest withUpdatedDomains(Set<String> domains) {
return new BaragonRequest(loadBalancerRequestId, loadBalancerService.withDomains(domains), addUpstreams, removeUpstreams, replaceUpstreams, replaceServiceId, action, noValidate, noReload);
}

public String getLoadBalancerRequestId() {
return loadBalancerRequestId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public BaragonService withUpdatedGroups(BaragonGroupAlias updatedFromAlias) {
return new BaragonService(serviceId, owners, serviceBasePath, additionalPaths, updatedFromAlias.getGroups(), options, templateName, updatedFromAlias.getDomains(), updatedFromAlias.getEdgeCacheDomains());
}

public BaragonService withDomains(Set<String> domains) {
return new BaragonService(serviceId, owners, serviceBasePath, additionalPaths, loadBalancerGroups, options, templateName, domains, edgeCacheDomains);
}

public String getServiceId() {
return serviceId;
}
Expand Down Expand Up @@ -145,12 +149,15 @@ public Set<String> getEdgeCacheDomains() {
}

@JsonIgnore
public List<String> getAllPaths() {
public List<String> getAllPaths(Optional<String> defaultDomain) {
List<String> allPaths = new ArrayList<>();
for (String path : additionalPaths) {
if (!domains.isEmpty()) {
for (String domain : domains) {
allPaths.add(String.format("%s%s", domain, path));
if (defaultDomain.isPresent() && domain.equals(defaultDomain.get())) {
allPaths.add(path); // For the default domain, also add the unqualified path
}
}
} else {
allPaths.add(path);
Expand All @@ -159,6 +166,9 @@ public List<String> getAllPaths() {
if (!domains.isEmpty()) {
for (String domain : domains) {
allPaths.add(String.format("%s%s", domain, serviceBasePath));
if (defaultDomain.isPresent() && domain.equals(defaultDomain.get())) {
allPaths.add(serviceBasePath); // For the default domain, also add the unqualified path
}
}
} else {
allPaths.add(serviceBasePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.hubspot.baragon.data.BaragonWorkerDatastore;
import com.hubspot.baragon.data.BaragonZkMetaDatastore;
import com.hubspot.baragon.managers.BaragonAuthManager;
import com.hubspot.baragon.migrations.ServiceDomainsMigration;
import com.hubspot.baragon.migrations.UpstreamsMigration;
import com.hubspot.baragon.migrations.ZkDataMigration;
import com.hubspot.baragon.migrations.ZkDataMigrationRunner;
Expand Down Expand Up @@ -90,6 +91,7 @@ protected void configure() {

Multibinder<ZkDataMigration> zkMigrationBinder = Multibinder.newSetBinder(binder(), ZkDataMigration.class);
zkMigrationBinder.addBinding().to(UpstreamsMigration.class);
zkMigrationBinder.addBinding().to(ServiceDomainsMigration.class);
}

@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.curator.framework.CuratorFramework;
Expand All @@ -10,7 +11,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -26,6 +26,8 @@
import com.hubspot.baragon.config.ZooKeeperConfiguration;
import com.hubspot.baragon.models.BaragonAgentMetadata;
import com.hubspot.baragon.models.BaragonGroup;
import com.hubspot.baragon.models.BaragonRequest;
import com.hubspot.baragon.models.BaragonService;
import com.hubspot.baragon.models.TrafficSource;

@Singleton
Expand Down Expand Up @@ -55,7 +57,6 @@ public LeaderLatch createLeaderLatch(String clusterName, BaragonAgentMetadata ag
}
}

@Timed
public Collection<BaragonGroup> getLoadBalancerGroups() {
final Collection<String> nodes = getChildren(LOAD_BALANCER_GROUPS_FORMAT);

Expand All @@ -75,7 +76,6 @@ public Collection<BaragonGroup> getLoadBalancerGroups() {
return groups;
}

@Timed
public Optional<BaragonGroup> getLoadBalancerGroup(String name) {
try {
return readFromZk(String.format(LOAD_BALANCER_GROUP_FORMAT, name), BaragonGroup.class);
Expand All @@ -87,7 +87,6 @@ public Optional<BaragonGroup> getLoadBalancerGroup(String name) {
}
}

@Timed
public BaragonGroup addSourceToGroup(String name, TrafficSource source) {
Optional<BaragonGroup> maybeGroup = getLoadBalancerGroup(name);
BaragonGroup group;
Expand All @@ -101,7 +100,6 @@ public BaragonGroup addSourceToGroup(String name, TrafficSource source) {
return group;
}

@Timed
public Optional<BaragonGroup> removeSourceFromGroup(String name, TrafficSource source) {
Optional<BaragonGroup> maybeGroup = getLoadBalancerGroup(name);
if (maybeGroup.isPresent()) {
Expand All @@ -113,7 +111,6 @@ public Optional<BaragonGroup> removeSourceFromGroup(String name, TrafficSource s
}
}

@Timed
public void updateGroupInfo(String name, Optional<String> defaultDomain, Set<String> domains) {
Optional<BaragonGroup> maybeGroup = getLoadBalancerGroup(name);
BaragonGroup group;
Expand All @@ -127,17 +124,14 @@ public void updateGroupInfo(String name, Optional<String> defaultDomain, Set<Str
writeToZk(String.format(LOAD_BALANCER_GROUP_FORMAT, name), group);
}

@Timed
public Set<String> getLoadBalancerGroupNames() {
return ImmutableSet.copyOf(getChildren(LOAD_BALANCER_GROUPS_FORMAT));
}

@Timed
public Optional<BaragonAgentMetadata> getAgent(String path) {
return readFromZk(path, BaragonAgentMetadata.class);
}

@Timed
public Optional<BaragonAgentMetadata> getAgent(String clusterName, String agentId) {
Collection<BaragonAgentMetadata> agents = getAgentMetadata(clusterName);
Optional<BaragonAgentMetadata> maybeAgent = Optional.absent();
Expand All @@ -150,7 +144,6 @@ public Optional<BaragonAgentMetadata> getAgent(String clusterName, String agentI
return maybeAgent;
}

@Timed
public Collection<BaragonAgentMetadata> getAgentMetadata(String clusterName) {
final Collection<String> nodes = getChildren(String.format(LOAD_BALANCER_GROUP_HOSTS_FORMAT, clusterName));

Expand Down Expand Up @@ -180,7 +173,6 @@ public Collection<BaragonAgentMetadata> getAgentMetadata(String clusterName) {
return metadata;
}

@Timed
public Collection<BaragonAgentMetadata> getAgentMetadata(Collection<String> clusterNames) {
final Set<BaragonAgentMetadata> metadata = Sets.newHashSet();

Expand All @@ -191,22 +183,18 @@ public Collection<BaragonAgentMetadata> getAgentMetadata(Collection<String> clus
return metadata;
}

@Timed
public Optional<String> getBasePathServiceId(String loadBalancerGroup, String basePath) {
return readFromZk(String.format(LOAD_BALANCER_BASE_PATH_FORMAT, loadBalancerGroup, encodeUrl(basePath)), String.class);
}

@Timed
public void clearBasePath(String loadBalancerGroup, String basePath) {
deleteNode(String.format(LOAD_BALANCER_BASE_PATH_FORMAT, loadBalancerGroup, encodeUrl(basePath)));
}

@Timed
public void setBasePathServiceId(String loadBalancerGroup, String basePath, String serviceId) {
writeToZk(String.format(LOAD_BALANCER_BASE_PATH_FORMAT, loadBalancerGroup, encodeUrl(basePath)), serviceId);
}

@Timed
public Collection<String> getBasePaths(String loadBalancerGroup) {
final Collection<String> encodedPaths = getChildren(String.format(LOAD_BALANCER_BASE_PATHS_FORMAT, loadBalancerGroup));
final Collection<String> decodedPaths = Lists.newArrayListWithCapacity(encodedPaths.size());
Expand All @@ -218,12 +206,10 @@ public Collection<String> getBasePaths(String loadBalancerGroup) {
return decodedPaths;
}

@Timed
public Optional<String> getLastRequestForGroup(String loadBalancerGroup) {
return readFromZk(String.format(LOAD_BALANCER_GROUP_LAST_REQUEST_FORMAT, loadBalancerGroup), String.class);
}

@Timed
public void setLastRequestId(String loadBalancerGroup, String requestId) {
writeToZk(String.format(LOAD_BALANCER_GROUP_LAST_REQUEST_FORMAT, loadBalancerGroup), requestId);
}
Expand All @@ -236,4 +222,21 @@ public int setTargetCount(String group, Integer count) {
public Optional<Integer> getTargetCount(String group) {
return readFromZk(String.format(LOAD_BALANCER_TARGET_COUNT_FORMAT, group), Integer.class);
}

public BaragonRequest updateForDefaultDomains(BaragonRequest request) {
return request.withUpdatedDomains(getDomainsWithDefaults(request.getLoadBalancerService()));
}

public Set<String> getDomainsWithDefaults(BaragonService service) {
Set<String> updatedDomains = new HashSet<>(service.getDomains());
for (String group : service.getLoadBalancerGroups()) {
Optional<BaragonGroup> maybeGroup = getLoadBalancerGroup(group);
if (maybeGroup.isPresent()) {
if (maybeGroup.get().getDefaultDomain().isPresent() && maybeGroup.get().getDomains().stream().noneMatch(updatedDomains::contains)) {
updatedDomains.add(maybeGroup.get().getDefaultDomain().get());
}
}
}
return updatedDomains;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -52,22 +51,18 @@ public BaragonStateDatastore(CuratorFramework curatorFramework,
this.zkFetcher = zkFetcher;
}

@Timed
public Collection<String> getServices() {
return getChildren(SERVICES_FORMAT);
}

@Timed
public boolean serviceExists(String serviceId) {
return nodeExists(String.format(SERVICE_FORMAT, serviceId));
}

@Timed
public Optional<BaragonService> getService(String serviceId) {
return readFromZk(String.format(SERVICE_FORMAT, serviceId), BaragonService.class);
}

@Timed
public void removeService(String serviceId) {
for (String upstream : getUpstreamNodes(serviceId)) {
deleteNode(String.format(UPSTREAM_FORMAT, serviceId, upstream));
Expand All @@ -76,12 +71,10 @@ public void removeService(String serviceId) {
deleteNode(String.format(SERVICE_FORMAT, serviceId));
}

@Timed
private Collection<String> getUpstreamNodes(String serviceId) {
return getChildren(String.format(SERVICE_FORMAT, serviceId));
}

@Timed
public Collection<UpstreamInfo> getUpstreams(String serviceId) throws Exception {
final Collection<String> upstreamNodes = getUpstreamNodes(serviceId);
final Collection<UpstreamInfo> upstreams = new ArrayList<>(upstreamNodes.size());
Expand All @@ -91,7 +84,11 @@ public Collection<UpstreamInfo> getUpstreams(String serviceId) throws Exception
return upstreams;
}

@Timed
public void saveService(BaragonService service) {
String servicePath = String.format(SERVICE_FORMAT, service.getServiceId());
writeToZk(servicePath, service);
}

public void updateService(BaragonRequest request) throws Exception {
if (!nodeExists(SERVICES_FORMAT)) {
createNode(SERVICES_FORMAT);
Expand Down Expand Up @@ -175,7 +172,6 @@ private List<String> matchingUpstreamPaths(Collection<UpstreamInfo> currentUpstr
return matchingPaths;
}

@Timed
public Collection<BaragonServiceState> getGlobalState() {
try {
LOG.info("Starting to compute all service states");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.hubspot.baragon.migrations;

import java.util.Set;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.hubspot.baragon.data.BaragonLoadBalancerDatastore;
import com.hubspot.baragon.data.BaragonStateDatastore;
import com.hubspot.baragon.models.BaragonService;

public class ServiceDomainsMigration extends ZkDataMigration {

private final BaragonStateDatastore baragonStateDatastore;
private final BaragonLoadBalancerDatastore loadBalancerDatastore;

@Inject
public ServiceDomainsMigration(BaragonStateDatastore baragonStateDatastore,
BaragonLoadBalancerDatastore loadBalancerDatastore) {
super(2);
this.baragonStateDatastore = baragonStateDatastore;
this.loadBalancerDatastore = loadBalancerDatastore;
}

@Override
public void applyMigration() {
try {
for (String serviceId : baragonStateDatastore.getServices()) {
Optional<BaragonService> service = baragonStateDatastore.getService(serviceId);
if (service.isPresent()) {
Set<String> updatedDomainsWithDefaults = loadBalancerDatastore.getDomainsWithDefaults(service.get());
baragonStateDatastore.saveService(service.get().withDomains(updatedDomainsWithDefaults));
}
}
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
Loading

0 comments on commit 16f8b16

Please sign in to comment.