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

Pod Template "Annotations" Field #105

Merged
merged 6 commits into from
Feb 12, 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Either way it provides access to the following fields:
* **nodeSelector** The node selector of the pod.
* **volumes** Volumes that are defined for the pod and are mounted by **ALL** containers.
* **envVars*** Environment variables that are applied to **ALL** containers.
* **annotations** Annotations to apply to the pod.
* **inheritFrom** List of one or more pod templates to inherit from *(more details below)*.

The `containerTemplate` is a template of container that will be added to the pod. Again, its configurable via the user interface or via pipeline and allows you to set the following fields:
Expand Down Expand Up @@ -195,6 +196,10 @@ volumes: [
hostPathVolume(mountPath: '/etc/mount4', hostPath: '/mnt/my-mount'),
nfsVolume(mountPath: '/etc/mount5', serverAddress: '127.0.0.1', serverPath: '/', readOnly: true),
persistentVolumeClaim(mountPath: '/etc/mount6', claimName: 'myClaim', readOnly: true)
],
annotations: [
podAnnotation(key: "my-key", value: "my-value")
...
]) {
...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;
Expand Down Expand Up @@ -37,6 +38,7 @@ public void setValue(String value) {
}

@Extension
@Symbol("podAnnotation")
public static class DescriptorImpl extends Descriptor<PodAnnotation> {
@Override
public String getDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class PodTemplate extends AbstractDescribableImpl<PodTemplate> implements

private final List<PodEnvVar> envVars = new ArrayList<PodEnvVar>();

private final List<PodAnnotation> annotations = new ArrayList<PodAnnotation>();
private List<PodAnnotation> annotations = new ArrayList<PodAnnotation>();

private final List<PodImagePullSecret> imagePullSecrets = new ArrayList<PodImagePullSecret>();

Expand Down Expand Up @@ -460,6 +460,11 @@ protected Object readResolve() {
containerTemplate.setWorkingDir(remoteFs);
containers.add(containerTemplate);
}

if (annotations == null) {
annotations = new ArrayList<>();
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;

import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate;
import org.csanchez.jenkins.plugins.kubernetes.PodAnnotation;
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
import org.jenkinsci.plugins.workflow.steps.Step;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class PodTemplateStep extends Step implements Serializable {
private List<ContainerTemplate> containers = new ArrayList<>();
private List<PodVolume> volumes = new ArrayList<PodVolume>();
private WorkspaceVolume workspaceVolume;
private List<PodAnnotation> annotations = new ArrayList<>();

private int instanceCap;
private String serviceAccount;
Expand Down Expand Up @@ -142,6 +144,15 @@ public StepExecution start(StepContext context) throws Exception {
return new PodTemplateStepExecution(this, context);
}

public List<PodAnnotation> getAnnotations() {
return annotations;
}

@DataBoundSetter
public void setAnnotations(List<PodAnnotation> annotations) {
this.annotations = annotations;
}

@Extension
public static class DescriptorImpl extends StepDescriptor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public boolean start() throws Exception {
newTemplate.setContainers(step.getContainers());
newTemplate.setNodeSelector(step.getNodeSelector());
newTemplate.setServiceAccount(step.getServiceAccount());
newTemplate.setAnnotations(step.getAnnotations());
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI when upgrading getAnnotations() is going to be null, because is loaded from xml config where it doesn't exist, have you checked that the API won't break with a null value?

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 was unaware of that. I just did a test where I upgraded from 0.7, to my version, and all the data remained.

I believe it worked because I declare annotations here with an initial value: https://github.com/jenkinsci/kubernetes-plugin/pull/105/files#diff-37ffb9809d7b186d5733b266c664ba41R36

Copy link
Contributor

Choose a reason for hiding this comment

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

the initial value won't be used when loading the xml config from disk, so maybe it's better if you explicitly add a readResolve that sets annotations to empty list if it is null

As an example
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java#L446


kubernetesCloud.addTemplate(newTemplate);
getContext().newBodyInvoker().withContext(step).withCallback(new PodTemplateCallback(newTemplate)).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<f:textbox/>
</f:entry>

<f:entry title="${%Annotations}" description="${%List of annotations to set in slave pod}">
<f:entry title="${%Annotations}" description="${%List of annotations to set in slave pod}" field="annotations">
<f:repeatableHeteroProperty field="annotations" hasHeader="true" addCaption="Add Annotation"
deleteCaption="Delete annotation Variable" />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Annotations to set on pod metadata<br/>
<a href="https://kubernetes.io/docs/user-guide/annotations/">https://kubernetes.io/docs/user-guide/annotations/</a>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
deleteCaption="Delete Volume" />
</f:entry>

<f:entry title="${%Annotations}" description="${%List of annotations to add to the pod metadata}" field="annotations">
<f:repeatableHeteroProperty field="annotations" hasHeader="true" addCaption="Add Annotation"
deleteCaption="Delete Annotation" />
</f:entry>

<f:advanced>
<f:entry field="instanceCap" title="${%Max number of instances}">
<f:textbox default="0"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Annotations to set on pod metadata<br/>
<a href="https://kubernetes.io/docs/user-guide/annotations/">https://kubernetes.io/docs/user-guide/annotations/</a>