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

Port mapping #165

Merged
merged 8 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ The `containerTemplate` is a template of container that will be added to the pod
* **args** The arguments passed to the command.
* **ttyEnabled** Flag to mark that tty should be enabled.
* **livenessProbe** Parameters to be added to a exec liveness probe in the container (does not suppot httpGet liveness probes)
* **ports** Expose ports on the container.

#### Liveness Probe Usage
```groovy
Expand All @@ -132,7 +133,7 @@ In the example below, we will inherit the podTemplate we created previously, and

```groovy
podTemplate(label: 'anotherpod', inheritFrom: 'mypod' containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-7-alpine')
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-7-alpine')
]) {

//Let's not repeat ourselves and ommit this part
Expand Down Expand Up @@ -221,7 +222,8 @@ podTemplate(label: 'mypod', cloud: 'kubernetes', containers: [
envVars: [
containerEnvVar(key: 'MYSQL_ALLOW_EMPTY_PASSWORD', value: 'true'),
...
]
],
ports: [portMapping(name: 'mysql', containerPort: 3306, hostPort: 3306)]
),
...
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class ContainerTemplate extends AbstractDescribableImpl<ContainerTemplate
private String resourceLimitMemory;

private final List<ContainerEnvVar> envVars = new ArrayList<ContainerEnvVar>();
private List<PortMapping> ports = new ArrayList<PortMapping>();

private ContainerLivenessProbe livenessProbe;

Expand Down Expand Up @@ -153,13 +154,23 @@ public void setEnvVars(List<ContainerEnvVar> envVars) {
this.envVars.addAll(envVars);
}


public ContainerLivenessProbe getLivenessProbe() { return livenessProbe; }

@DataBoundSetter
public void setLivenessProbe(ContainerLivenessProbe livenessProbe) {
this.livenessProbe = livenessProbe;
}

public List<PortMapping> getPorts() {
return ports != null ? ports : Collections.emptyList();
}

@DataBoundSetter
public void setPorts(List<PortMapping> ports) {
this.ports = ports;
}

public String getResourceRequestMemory() {
return resourceRequestMemory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import hudson.util.ListBoxModel;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ContainerPort;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.LocalObjectReference;
Expand Down Expand Up @@ -378,6 +379,8 @@ private Container createContainer(KubernetesSlave slave, ContainerTemplate conta

List<VolumeMount> containerMounts = new ArrayList<>(volumeMounts);

ContainerPort[] ports = containerTemplate.getPorts().stream().map(entry -> entry.toPort()).toArray(size -> new ContainerPort[size]);

if (!Strings.isNullOrEmpty(containerTemplate.getWorkingDir())
&& !PodVolume.volumeMountExists(containerTemplate.getWorkingDir(), volumeMounts)) {
containerMounts.add(new VolumeMount(containerTemplate.getWorkingDir(), WORKSPACE_VOLUME_NAME, false, null));
Expand Down Expand Up @@ -406,6 +409,7 @@ private Container createContainer(KubernetesSlave slave, ContainerTemplate conta
.withWorkingDir(substituteEnv(containerTemplate.getWorkingDir()))
.withVolumeMounts(containerMounts.toArray(new VolumeMount[containerMounts.size()]))
.addToEnv(envVars)
.addToPorts(ports)
.withCommand(parseDockerCommand(containerTemplate.getCommand()))
.withArgs(arguments)
.withLivenessProbe(livenessProbe)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.csanchez.jenkins.plugins.kubernetes;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.jenkinsci.Symbol;
import io.fabric8.kubernetes.api.model.ContainerPort;

import java.io.Serializable;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class PortMapping extends AbstractDescribableImpl<PortMapping> implements Serializable {

private String name;
private Integer containerPort;
private Integer hostPort;

@DataBoundConstructor
public PortMapping(String name, Integer containerPort) {
this.name = name;
this.containerPort = containerPort;
}

public PortMapping(String name, Integer containerPort, Integer hostPort) {
this.name = name;
this.containerPort = containerPort;
this.hostPort = hostPort;
}

@DataBoundSetter
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

@DataBoundSetter
public void setContainerPort(Integer containerPort) {
this.containerPort = containerPort;
}

public Integer getContainerPort() {
return containerPort;
}

@DataBoundSetter
public void setHostPort(Integer hostPort) {
this.hostPort = hostPort;
}

public Integer getHostPort() {
return hostPort;
}

public ContainerPort toPort() {
ContainerPort p = new ContainerPort();
p.setName(name);
p.setContainerPort(containerPort);
if(hostPort != null) {
p.setHostPort(hostPort);
}
return p;
}

public String toString() {
return String.format("%s,%d", name, containerPort);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((containerPort == null) ? 0 : containerPort);
result = prime * result + ((hostPort == null) ? 0 : hostPort);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PortMapping other = (PortMapping) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (containerPort == null) {
if (other.containerPort != null)
return false;
} else if (!containerPort.equals(other.containerPort))
return false;
if (hostPort == null) {
if (other.hostPort != null)
return false;
} else if (!hostPort.equals(other.hostPort))
return false;
return true;
}

@Extension
@Symbol("portMapping")
public static class DescriptorImpl extends Descriptor<PortMapping> {
@Override
public String getDisplayName() {
return "Container Exposed Ports";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@
<st:include from="${descriptor}" page="${descriptor.configPage}" />
</f:rowSet>
</f:entry>
<f:entry title="${%PortMappings}" description="${%List of exposed ports}">
<f:repeatableHeteroProperty field="ports" hasHeader="true" addCaption="Add Port Mapping"
deleteCaption="Delete Port Mapping" />
</f:entry>

</f:advanced>

</j:jelly>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
Config page
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<f:entry field="name" title="${%Name}">
<f:textbox/>
</f:entry>

<f:entry field="containerPort" title="${%ContainerPort}">
<f:textbox/>
</f:entry>

<f:entry field="hostPort" title="${%HostPort}">
<f:textbox/>
</f:entry>

</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port to expose into the pod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port to expose onto the host
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The name of the port