Skip to content

Commit a667a9f

Browse files
Jon Whitcraftcarlossg
Jon Whitcraft
authored andcommitted
Port mapping (#165)
1 parent bb533b9 commit a667a9f

File tree

9 files changed

+165
-3
lines changed

9 files changed

+165
-3
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ The `containerTemplate` is a template of container that will be added to the pod
108108
* **args** The arguments passed to the command.
109109
* **ttyEnabled** Flag to mark that tty should be enabled.
110110
* **livenessProbe** Parameters to be added to a exec liveness probe in the container (does not suppot httpGet liveness probes)
111+
* **ports** Expose ports on the container.
111112

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

133134
```groovy
134135
podTemplate(label: 'anotherpod', inheritFrom: 'mypod' containers: [
135-
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-7-alpine')
136+
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-7-alpine')
136137
]) {
137138
138139
//Let's not repeat ourselves and ommit this part
@@ -221,7 +222,8 @@ podTemplate(label: 'mypod', cloud: 'kubernetes', containers: [
221222
envVars: [
222223
containerEnvVar(key: 'MYSQL_ALLOW_EMPTY_PASSWORD', value: 'true'),
223224
...
224-
]
225+
],
226+
ports: [portMapping(name: 'mysql', containerPort: 3306, hostPort: 3306)]
225227
),
226228
...
227229
],

src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class ContainerTemplate extends AbstractDescribableImpl<ContainerTemplate
4545
private String resourceLimitMemory;
4646

4747
private final List<ContainerEnvVar> envVars = new ArrayList<ContainerEnvVar>();
48+
private List<PortMapping> ports = new ArrayList<PortMapping>();
4849

4950
private ContainerLivenessProbe livenessProbe;
5051

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

157+
156158
public ContainerLivenessProbe getLivenessProbe() { return livenessProbe; }
157159

158160
@DataBoundSetter
159161
public void setLivenessProbe(ContainerLivenessProbe livenessProbe) {
160162
this.livenessProbe = livenessProbe;
161163
}
162164

165+
public List<PortMapping> getPorts() {
166+
return ports != null ? ports : Collections.emptyList();
167+
}
168+
169+
@DataBoundSetter
170+
public void setPorts(List<PortMapping> ports) {
171+
this.ports = ports;
172+
}
173+
163174
public String getResourceRequestMemory() {
164175
return resourceRequestMemory;
165176
}

src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import hudson.util.ListBoxModel;
7070
import io.fabric8.kubernetes.api.model.Container;
7171
import io.fabric8.kubernetes.api.model.ContainerBuilder;
72+
import io.fabric8.kubernetes.api.model.ContainerPort;
7273
import io.fabric8.kubernetes.api.model.ContainerStatus;
7374
import io.fabric8.kubernetes.api.model.EnvVar;
7475
import io.fabric8.kubernetes.api.model.LocalObjectReference;
@@ -378,6 +379,8 @@ private Container createContainer(KubernetesSlave slave, ContainerTemplate conta
378379

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

382+
ContainerPort[] ports = containerTemplate.getPorts().stream().map(entry -> entry.toPort()).toArray(size -> new ContainerPort[size]);
383+
381384
if (!Strings.isNullOrEmpty(containerTemplate.getWorkingDir())
382385
&& !PodVolume.volumeMountExists(containerTemplate.getWorkingDir(), volumeMounts)) {
383386
containerMounts.add(new VolumeMount(containerTemplate.getWorkingDir(), WORKSPACE_VOLUME_NAME, false, null));
@@ -406,6 +409,7 @@ private Container createContainer(KubernetesSlave slave, ContainerTemplate conta
406409
.withWorkingDir(substituteEnv(containerTemplate.getWorkingDir()))
407410
.withVolumeMounts(containerMounts.toArray(new VolumeMount[containerMounts.size()]))
408411
.addToEnv(envVars)
412+
.addToPorts(ports)
409413
.withCommand(parseDockerCommand(containerTemplate.getCommand()))
410414
.withArgs(arguments)
411415
.withLivenessProbe(livenessProbe)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.csanchez.jenkins.plugins.kubernetes;
2+
3+
import hudson.Extension;
4+
import hudson.model.AbstractDescribableImpl;
5+
import hudson.model.Descriptor;
6+
import org.jenkinsci.Symbol;
7+
import io.fabric8.kubernetes.api.model.ContainerPort;
8+
9+
import java.io.Serializable;
10+
11+
import org.kohsuke.stapler.DataBoundConstructor;
12+
import org.kohsuke.stapler.DataBoundSetter;
13+
14+
public class PortMapping extends AbstractDescribableImpl<PortMapping> implements Serializable {
15+
16+
private String name;
17+
private Integer containerPort;
18+
private Integer hostPort;
19+
20+
@DataBoundConstructor
21+
public PortMapping(String name, Integer containerPort) {
22+
this.name = name;
23+
this.containerPort = containerPort;
24+
}
25+
26+
public PortMapping(String name, Integer containerPort, Integer hostPort) {
27+
this.name = name;
28+
this.containerPort = containerPort;
29+
this.hostPort = hostPort;
30+
}
31+
32+
@DataBoundSetter
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
@DataBoundSetter
42+
public void setContainerPort(Integer containerPort) {
43+
this.containerPort = containerPort;
44+
}
45+
46+
public Integer getContainerPort() {
47+
return containerPort;
48+
}
49+
50+
@DataBoundSetter
51+
public void setHostPort(Integer hostPort) {
52+
this.hostPort = hostPort;
53+
}
54+
55+
public Integer getHostPort() {
56+
return hostPort;
57+
}
58+
59+
public ContainerPort toPort() {
60+
ContainerPort p = new ContainerPort();
61+
p.setName(name);
62+
p.setContainerPort(containerPort);
63+
if(hostPort != null) {
64+
p.setHostPort(hostPort);
65+
}
66+
return p;
67+
}
68+
69+
public String toString() {
70+
return String.format("%s,%d", name, containerPort);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
final int prime = 31;
76+
int result = 1;
77+
result = prime * result + ((name == null) ? 0 : name.hashCode());
78+
result = prime * result + ((containerPort == null) ? 0 : containerPort);
79+
result = prime * result + ((hostPort == null) ? 0 : hostPort);
80+
return result;
81+
}
82+
83+
@Override
84+
public boolean equals(Object obj) {
85+
if (this == obj)
86+
return true;
87+
if (obj == null)
88+
return false;
89+
if (getClass() != obj.getClass())
90+
return false;
91+
PortMapping other = (PortMapping) obj;
92+
if (name == null) {
93+
if (other.name != null)
94+
return false;
95+
} else if (!name.equals(other.name))
96+
return false;
97+
if (containerPort == null) {
98+
if (other.containerPort != null)
99+
return false;
100+
} else if (!containerPort.equals(other.containerPort))
101+
return false;
102+
if (hostPort == null) {
103+
if (other.hostPort != null)
104+
return false;
105+
} else if (!hostPort.equals(other.hostPort))
106+
return false;
107+
return true;
108+
}
109+
110+
@Extension
111+
@Symbol("portMapping")
112+
public static class DescriptorImpl extends Descriptor<PortMapping> {
113+
@Override
114+
public String getDisplayName() {
115+
return "Container Exposed Ports";
116+
}
117+
}
118+
}

src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@
6767
<st:include from="${descriptor}" page="${descriptor.configPage}" />
6868
</f:rowSet>
6969
</f:entry>
70+
<f:entry title="${%PortMappings}" description="${%List of exposed ports}">
71+
<f:repeatableHeteroProperty field="ports" hasHeader="true" addCaption="Add Port Mapping"
72+
deleteCaption="Delete Port Mapping" />
73+
</f:entry>
7074

7175
</f:advanced>
7276

73-
</j:jelly>
77+
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--
2+
Config page
3+
-->
4+
<?jelly escape-by-default='true'?>
5+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
6+
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
7+
8+
<f:entry field="name" title="${%Name}">
9+
<f:textbox/>
10+
</f:entry>
11+
12+
<f:entry field="containerPort" title="${%ContainerPort}">
13+
<f:textbox/>
14+
</f:entry>
15+
16+
<f:entry field="hostPort" title="${%HostPort}">
17+
<f:textbox/>
18+
</f:entry>
19+
20+
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port to expose into the pod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port to expose onto the host
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The name of the port

0 commit comments

Comments
 (0)