Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
add a docker container label with mesos task id (#4492)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu8685 authored and aquamatthias committed Dec 14, 2016
1 parent 75e4753 commit 91cdc7e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/main/scala/mesosphere/mesos/TaskBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TaskBuilder(

volumeMatchOpt.foreach(_.persistentVolumeResources.foreach(builder.addResources))

val containerProto = computeContainerInfo(resourceMatch.hostPorts)
val containerProto = computeContainerInfo(resourceMatch.hostPorts, taskId)
val envPrefix: Option[String] = config.envVarsPrefix.get

executor match {
Expand Down Expand Up @@ -155,7 +155,7 @@ class TaskBuilder(
discoveryInfoBuilder.build
}

protected def computeContainerInfo(hostPorts: Seq[Option[Int]]): Option[ContainerInfo] = {
protected def computeContainerInfo(hostPorts: Seq[Option[Int]], taskId: Task.Id): Option[ContainerInfo] = {
if (runSpec.container.isEmpty && runSpec.ipAddress.isEmpty) {
None
} else {
Expand Down Expand Up @@ -185,12 +185,17 @@ class TaskBuilder(
// TODO(portMappings)
// TODO(nfnt): Other containers might also support port mappings in the future.
// If that is the case, a more general way than the one below needs to be implemented.
val containerWithPortMappings = c match {
case docker: Container.Docker => docker.copy(portMappings = boundPortMappings)
val updatedContainer = c match {
case docker: Container.Docker =>
docker.copy(
portMappings = boundPortMappings,
parameters = docker.parameters :+
new mesosphere.marathon.state.Parameter("label", s"MESOS_TASK_ID=${taskId.mesosTaskId.getValue}")
)
case _ => c
}

builder.mergeFrom(ContainerSerializer.toMesos(containerWithPortMappings))
builder.mergeFrom(ContainerSerializer.toMesos(updatedContainer))
}

// Set NetworkInfo if necessary
Expand Down
32 changes: 30 additions & 2 deletions src/test/scala/mesosphere/mesos/TaskBuilderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,33 @@ class TaskBuilderTest extends MarathonSpec with Matchers {
assert(containerPort == hostPort)
}

test("DockerContainerWithMesosTaskIdLabel") {
val offer = MarathonTestHelper.makeBasicOfferWithRole(
cpus = 1.0, mem = 128.0, disk = 1000.0, beginPort = 31000, endPort = 31010, role = ResourceRole.Unreserved
)
.addResources(RangesResource(Resource.PORTS, Seq(protos.Range(33000, 34000)), "marathon"))
.build

val task: Option[(MesosProtos.TaskInfo, _)] = buildIfMatches(
offer, AppDefinition(
id = "testApp".toPath,
resources = Resources(cpus = 1.0, mem = 64.0, disk = 1.0),
executor = "//cmd",
container = Some(Docker(
image = "busybox"
))
), None, None, None,
_ => Task.Id("mesos_task_id")
)
assert(task.isDefined, "expected task to match offer")
val (taskInfo, _) = task.get

assert(taskInfo.getContainer.getDocker.getParametersList.size == 1, s"expected 1 parameter, but ${taskInfo.getContainer.getDocker.getParametersList.size}")
val param = taskInfo.getContainer.getDocker.getParametersList.get(0)
assert(param.getKey == "label", "expected docker having a parameter key: label")
assert(param.getValue == "MESOS_TASK_ID=mesos_task_id", s"expected docker having a parameter value for key 'label': MESOS_TASK_ID=mesos_task_id but ${param.getValue }")
}

test("BuildIfMatchesWithRackIdConstraint") {
val offer = MarathonTestHelper.makeBasicOffer(1.0, 128.0, 31000, 32000)
.addAttributes(TextAttribute("rackid", "1"))
Expand Down Expand Up @@ -1781,10 +1808,11 @@ class TaskBuilderTest extends MarathonSpec with Matchers {
app: AppDefinition,
mesosRole: Option[String] = None,
acceptedResourceRoles: Option[Set[String]] = None,
envVarsPrefix: Option[String] = None): Option[(MesosProtos.TaskInfo, NetworkInfo)] = {
envVarsPrefix: Option[String] = None,
newTaskId: PathId => Task.Id = s => Task.Id.forRunSpec(s)): Option[(MesosProtos.TaskInfo, NetworkInfo)] = {
val builder = new TaskBuilder(
app,
s => Task.Id.forRunSpec(s),
newTaskId,
MarathonTestHelper.defaultConfig(
mesosRole = mesosRole,
acceptedResourceRoles = acceptedResourceRoles,
Expand Down

0 comments on commit 91cdc7e

Please sign in to comment.