Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple docker parameters and task labels #1169

Merged
merged 14 commits into from
Sep 2, 2016
2 changes: 1 addition & 1 deletion SingularityBase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<artifactId>jackson-datatype-protobuf</artifactId>
</dependency>

<dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Objects;
import com.google.common.base.Optional;

public class SingularityDockerInfo {
Expand All @@ -16,26 +17,41 @@ public class SingularityDockerInfo {
private final Optional<SingularityDockerNetworkType> network;
private final List<SingularityDockerPortMapping> portMappings;
private final boolean forcePullImage;
private final Map<String, String> parameters;
private final Optional<Map<String, String>> parameters;
private final List<SingularityDockerParameter> dockerParameters;

@JsonCreator
public SingularityDockerInfo(@JsonProperty("image") String image,
@JsonProperty("privileged") boolean privileged,
@JsonProperty("network") SingularityDockerNetworkType network,
@JsonProperty("portMappings") Optional<List<SingularityDockerPortMapping>> portMappings,
@JsonProperty("forcePullImage") Optional<Boolean> forcePullImage,
@JsonProperty("parameters") Optional<Map<String, String>> parameters) {
@JsonProperty("parameters") Optional<Map<String, String>> parameters,
@JsonProperty("dockerParameters") Optional<List<SingularityDockerParameter>> dockerParameters) {
if (dockerParameters.isPresent() && !dockerParameters.get().isEmpty() && parameters.isPresent() && !parameters.get().isEmpty()) {
throw new IllegalArgumentException("Can only specify one of 'parameters' or 'dockerParameters'");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little hesitant of putting these kinds of exceptions in constructors -- if we somehow had a bogus one saved we'd be throwing every time it was deserialized. What do you think about moving this into SingularityValidator instead?

this.image = image;
this.privileged = privileged;
this.network = Optional.fromNullable(network);
this.portMappings = portMappings.or(Collections.<SingularityDockerPortMapping>emptyList());
this.forcePullImage = forcePullImage.or(false);
this.parameters = parameters.or(Collections.<String, String>emptyMap());
this.parameters = parameters;
this.dockerParameters = dockerParameters.or(parameters.isPresent() ? SingularityDockerParameter.parametersFromMap(parameters.get()) : Collections.<SingularityDockerParameter>emptyList());
}

public SingularityDockerInfo(String image, boolean privileged, SingularityDockerNetworkType network, Optional<List<SingularityDockerPortMapping>> portMappings, Optional<Boolean> forcePullImage, List<SingularityDockerParameter> dockerParameters) {
this(image, privileged, network, portMappings, forcePullImage, Optional.<Map<String,String>>absent(), Optional.of(dockerParameters));
}

@Deprecated
public SingularityDockerInfo(String image, boolean privileged, SingularityDockerNetworkType network, Optional<List<SingularityDockerPortMapping>> portMappings, Optional<Boolean> forcePullImage, Optional<Map<String, String>> parameters) {
this(image, privileged, network, portMappings, forcePullImage, parameters, Optional.<List<SingularityDockerParameter>>absent());
}

@Deprecated
public SingularityDockerInfo(String image, boolean privileged, SingularityDockerNetworkType network, Optional<List<SingularityDockerPortMapping>> portMappings) {
this(image, privileged, network, portMappings, Optional.<Boolean>absent(), Optional.<Map<String, String>>absent());
this(image, privileged, network, portMappings, Optional.<Boolean>absent(), Optional.<Map<String, String>>absent(), Optional.<List<SingularityDockerParameter>>absent());
}

public String getImage() {
Expand Down Expand Up @@ -80,20 +96,13 @@ public boolean isForcePullImage() {
return forcePullImage;
}

public Map<String, String> getParameters() {
@Deprecated
public Optional<Map<String, String>> getParameters() {
return parameters;
}

@Override
public String toString() {
return "SingularityDockerInfo{" +
"image='" + image + '\'' +
", privileged=" + privileged +
", network=" + network +
", portMappings=" + portMappings +
", forcePullImage=" + forcePullImage +
", parameters=" + parameters +
'}';
public List<SingularityDockerParameter> getDockerParameters() {
return dockerParameters;
}

@Override
Expand All @@ -104,39 +113,31 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}

SingularityDockerInfo that = (SingularityDockerInfo) o;

if (forcePullImage != that.forcePullImage) {
return false;
}
if (privileged != that.privileged) {
return false;
}
if (image != null ? !image.equals(that.image) : that.image != null) {
return false;
}
if (network != null ? !network.equals(that.network) : that.network != null) {
return false;
}
if (parameters != null ? !parameters.equals(that.parameters) : that.parameters != null) {
return false;
}
if (portMappings != null ? !portMappings.equals(that.portMappings) : that.portMappings != null) {
return false;
}

return true;
return privileged == that.privileged &&
forcePullImage == that.forcePullImage &&
Objects.equal(image, that.image) &&
Objects.equal(network, that.network) &&
Objects.equal(portMappings, that.portMappings) &&
Objects.equal(parameters, that.parameters) &&
Objects.equal(dockerParameters, that.dockerParameters);
}

@Override
public int hashCode() {
int result = image != null ? image.hashCode() : 0;
result = 31 * result + (privileged ? 1 : 0);
result = 31 * result + (network != null ? network.hashCode() : 0);
result = 31 * result + (portMappings != null ? portMappings.hashCode() : 0);
result = 31 * result + (forcePullImage ? 1 : 0);
result = 31 * result + (parameters != null ? parameters.hashCode() : 0);
return result;
return Objects.hashCode(image, privileged, network, portMappings, forcePullImage, parameters, dockerParameters);
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("image", image)
.add("privileged", privileged)
.add("network", network)
.add("portMappings", portMappings)
.add("forcePullImage", forcePullImage)
.add("parameters", parameters)
.add("dockerParameters", dockerParameters)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.hubspot.mesos;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;

public class SingularityDockerParameter {
private final String key;
private final String value;

@JsonCreator
public static SingularityDockerParameter fromString(String value) {
return new SingularityDockerParameter(value, "");
}

@JsonCreator
public SingularityDockerParameter(@JsonProperty("key") String key, @JsonProperty("value") String value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

@Override
public String toString() {
return "SingularityDockerParameter{" +
"key='" + key + '\'' +
", value=" + value +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SingularityDockerParameter that = (SingularityDockerParameter) o;
return Objects.equals(key, that.key) &&
Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hash(key, value);
}

public static List<SingularityDockerParameter> parametersFromMap(Map<String, String> parametersMap) {
List<SingularityDockerParameter> parameters = new ArrayList<>();
for (Map.Entry<String, String> entry : parametersMap.entrySet()) {
parameters.add(new SingularityDockerParameter(entry.getKey(), entry.getValue()));
}
return parameters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.hubspot.mesos;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;

public class SingularityMesosTaskLabel {
private final String key;
private final Optional<String> value;

@JsonCreator
public static SingularityMesosTaskLabel fromString(String value) {
return new SingularityMesosTaskLabel(value, Optional.<String> absent());
}

@JsonCreator
public SingularityMesosTaskLabel(@JsonProperty("key") String key, @JsonProperty("value") Optional<String> value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public Optional<String> getValue() {
return value;
}

@Override
public String toString() {
return "SingularityLabel{" +
"key='" + key + '\'' +
", value=" + value +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SingularityMesosTaskLabel that = (SingularityMesosTaskLabel) o;
return Objects.equals(key, that.key) &&
Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hash(key, value);
}

public static List<SingularityMesosTaskLabel> labelsFromMap(Map<String, String> parametersMap) {
List<SingularityMesosTaskLabel> labels = new ArrayList<>();
for (Map.Entry<String, String> entry : parametersMap.entrySet()) {
labels.add(new SingularityMesosTaskLabel(entry.getKey(), Optional.of(entry.getValue())));
}
return labels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.hubspot.singularity.JsonHelpers.copyOfSet;
import static com.hubspot.singularity.JsonHelpers.copyOfMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -14,6 +15,7 @@
import com.hubspot.deploy.ExecutorData;
import com.hubspot.mesos.Resources;
import com.hubspot.mesos.SingularityContainerInfo;
import com.hubspot.mesos.SingularityMesosTaskLabel;
import com.wordnik.swagger.annotations.ApiModelProperty;

public class SingularityDeploy {
Expand Down Expand Up @@ -42,7 +44,10 @@ public class SingularityDeploy {
private final Optional<List<String>> uris;
private final Optional<ExecutorData> executorData;
private final Optional<Map<String, String>> labels;
private final Optional<List<SingularityMesosTaskLabel>> mesosLabels;

private final Optional<Map<Integer, Map<String, String>>> taskLabels;
private final Optional<Map<Integer, List<SingularityMesosTaskLabel>>> mesosTaskLabels;
private final Optional<Map<Integer, Map<String, String>>> taskEnv;

private final Optional<String> healthcheckUri;
Expand Down Expand Up @@ -97,7 +102,9 @@ public SingularityDeploy(@JsonProperty("requestId") String requestId,
@JsonProperty("version") Optional<String> version,
@JsonProperty("timestamp") Optional<Long> timestamp,
@JsonProperty("labels") Optional<Map<String, String>> labels,
@JsonProperty("mesosLabels") Optional<List<SingularityMesosTaskLabel>> mesosLabels,
@JsonProperty("taskLabels") Optional<Map<Integer, Map<String, String>>> taskLabels,
@JsonProperty("mesosTaskLabels") Optional<Map<Integer, List<SingularityMesosTaskLabel>>> mesosTaskLabels,
@JsonProperty("deployHealthTimeoutSeconds") Optional<Long> deployHealthTimeoutSeconds,
@JsonProperty("healthcheckUri") Optional<String> healthcheckUri,
@JsonProperty("healthcheckIntervalSeconds") Optional<Long> healthcheckIntervalSeconds,
Expand Down Expand Up @@ -142,8 +149,17 @@ public SingularityDeploy(@JsonProperty("requestId") String requestId,
this.taskEnv = taskEnv;
this.uris = uris;
this.executorData = executorData;
if (labels.isPresent() && !labels.get().isEmpty() && mesosLabels.isPresent() && !mesosLabels.get().isEmpty()) {
throw new IllegalArgumentException("Can only specify one of 'labels' or 'mesosLabels");
}
this.labels = labels;
this.mesosLabels = mesosLabels.or(labels.isPresent() ? Optional.of(SingularityMesosTaskLabel.labelsFromMap(labels.get())) : Optional.<List<SingularityMesosTaskLabel>>absent());

if (taskLabels.isPresent() && !taskLabels.get().isEmpty() && mesosTaskLabels.isPresent() && !mesosTaskLabels.get().isEmpty()) {
throw new IllegalArgumentException("Can only specify one of 'labels' or 'mesosLabels");
}
this.taskLabels = taskLabels;
this.mesosTaskLabels = mesosTaskLabels.or(taskLabels.isPresent() ? Optional.of(parseMesosTaskLabelsFromMap(taskLabels.get())) : Optional.<Map<Integer,List<SingularityMesosTaskLabel>>>absent());

this.healthcheckUri = healthcheckUri;
this.healthcheckIntervalSeconds = healthcheckIntervalSeconds;
Expand Down Expand Up @@ -174,6 +190,14 @@ public SingularityDeploy(@JsonProperty("requestId") String requestId,
this.shell = shell;
}

private static Map<Integer, List<SingularityMesosTaskLabel>> parseMesosTaskLabelsFromMap(Map<Integer, Map<String, String>> taskLabels) {
Map<Integer, List<SingularityMesosTaskLabel>> mesosTaskLabels = new HashMap<>();
for (Map.Entry<Integer, Map<String, String>> entry : taskLabels.entrySet()) {
mesosTaskLabels.put(entry.getKey(), SingularityMesosTaskLabel.labelsFromMap(entry.getValue()));
}
return mesosTaskLabels;
}

public SingularityDeployBuilder toBuilder() {
return new SingularityDeployBuilder(requestId, id)
.setCommand(command)
Expand Down Expand Up @@ -210,7 +234,9 @@ public SingularityDeployBuilder toBuilder() {
.setUris(copyOfList(uris))
.setExecutorData(executorData)
.setLabels(labels)
.setMesosLabels(mesosLabels)
.setTaskLabels(taskLabels)
.setMesosTaskLabels(mesosTaskLabels)
.setDeployInstanceCountPerStep(deployInstanceCountPerStep)
.setDeployStepWaitTimeMs(deployStepWaitTimeMs)
.setAutoAdvanceDeploySteps(autoAdvanceDeploySteps)
Expand Down Expand Up @@ -377,16 +403,28 @@ public Optional<String> getLoadBalancerTemplate() {
return loadBalancerTemplate;
}

@ApiModelProperty(required=false, value="Labels for all tasks associated with this deploy")
@Deprecated
@ApiModelProperty(required=false, value="(Deprecated) Labels for all tasks associated with this deploy")
public Optional<Map<String, String>> getLabels() {
return labels;
}

@ApiModelProperty(required=false, value="Labels for specific tasks associated with this deploy, indexed by instance number")
@ApiModelProperty(required=false, value="Labels for all tasks associated with this deploy")
public Optional<List<SingularityMesosTaskLabel>> getMesosLabels() {
return mesosLabels;
}

@Deprecated
@ApiModelProperty(required=false, value="(Deprecated) Labels for specific tasks associated with this deploy, indexed by instance number")
public Optional<Map<Integer, Map<String, String>>> getTaskLabels() {
return taskLabels;
}

@ApiModelProperty(required=false, value="Labels for specific tasks associated with this deploy, indexed by instance number")
public Optional<Map<Integer, List<SingularityMesosTaskLabel>>> getMesosTaskLabels() {
return mesosTaskLabels;
}

@ApiModelProperty(required=false, value="Allows skipping of health checks when deploying.")
public Optional<Boolean> getSkipHealthchecksOnDeploy() {
return skipHealthchecksOnDeploy;
Expand Down
Loading