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

Namespaces for Container Label Keys #660

Merged
merged 14 commits into from
Jun 10, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ public synchronized void removeTemplate(DockerTemplate t) {
*/
public int countContainersInDocker(final String imageName) throws Exception {
final Map<String, String> labelFilter = new HashMap<>();
labelFilter.put(DockerTemplateBase.CONTAINER_LABEL_JENKINS_INSTANCE_ID,
labelFilter.put(DockerContainerLabelKeys.JENKINS_INSTANCE_ID,
DockerTemplateBase.getJenkinsInstanceIdForContainerLabel());
if (imageName != null) {
labelFilter.put(DockerTemplateBase.CONTAINER_LABEL_IMAGE, imageName);
labelFilter.put(DockerContainerLabelKeys.CONTAINER_IMAGE, imageName);
}
final List<?> containers;
try(final DockerClient client = dockerApi.getClient()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.nirima.jenkins.plugins.docker;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import hudson.model.Node;
import jenkins.model.Jenkins;

/**
* This constant interface defines the identifiers of label keys used at containers, which
* are generated using this plugin.
*
* @author eaglerainbow
*
*/
public final class DockerContainerLabelKeys {

/**
* As requested by https://docs.docker.com/config/labels-custom-metadata/, keys of labels used
* in docker shall be prefixed by a namespace using the reverse DNS notation.
* All label keys of this plugin shall use this namespace as a prefix.
*
* Label keys defined in this interface already have this namespace prefixed.
*/
private static final String PLUGIN_LABEL_KEY_NAMESPACE = DockerContainerLabelKeys.class.getPackage().getName()+".";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link DockerTemplateBase#getJenkinsInstanceIdForContainerLabel()}, so that we
* can recognize our own containers later.
*/
static final String JENKINS_INSTANCE_ID = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsId";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link Jenkins#getRootUrl()}, so that we
* can recognize our own containers later.
*/
static final String JENKINS_URL = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsServerUrl";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to the value of {@link DockerTemplateBase#getImage()}, so that we
* can recognize our own containers later.
*/
static final String CONTAINER_IMAGE = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsContainerImage";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link Node#getNodeName()}, so that we
* can recognize our own containers later.
*/
@Restricted(NoExternalUse.class)
static final String NODE_NAME = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsNodeName";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to the value of {@link DockerTemplate#getName()}, so that we
* can recognize our own containers later.
*/
@Restricted(NoExternalUse.class)
static final String TEMPLATE_NAME = PLUGIN_LABEL_KEY_NAMESPACE + "JenkinsTemplateName";

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ public class DockerTemplate implements Describable<DockerTemplate> {

private static final UniqueIdGenerator ID_GENERATOR = new UniqueIdGenerator(36);

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link #getName()}, so that we
* can recognize our own containers later.
*/
@Restricted(NoExternalUse.class)
static String CONTAINER_LABEL_TEMPLATE_NAME = "JenkinsTemplateName";

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link Node#getNodeName()}, so that we
* can recognize our own containers later.
*/
@Restricted(NoExternalUse.class)
static String CONTAINER_LABEL_NODE_NAME = "JenkinsNodeName";

/** Default value for {@link #getName()} if {@link #name} is null. */
private static final String DEFAULT_NAME = "docker";

Expand Down Expand Up @@ -239,14 +223,14 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig
final CreateContainerCmd result = dockerTemplateBase.fillContainerConfig(containerConfig);
final String templateName = getName();
final String nodeName = calcUnusedNodeName(templateName);
result.getLabels().put(CONTAINER_LABEL_TEMPLATE_NAME, templateName);
result.getLabels().put(DockerContainerLabelKeys.TEMPLATE_NAME, templateName);
setNodeNameInContainerConfig(result, nodeName);
return result;
}

@Restricted(NoExternalUse.class) // public for tests only
public static void setNodeNameInContainerConfig(CreateContainerCmd containerConfig, String nodeName) {
containerConfig.getLabels().put(CONTAINER_LABEL_NODE_NAME, nodeName);
containerConfig.getLabels().put(DockerContainerLabelKeys.NODE_NAME, nodeName);
}

/**
Expand All @@ -260,7 +244,7 @@ public static void setNodeNameInContainerConfig(CreateContainerCmd containerConf
* node for the container that will be created by this command.
*/
public static String getNodeNameFromContainerConfig(CreateContainerCmd containerConfig) {
return containerConfig.getLabels().get(CONTAINER_LABEL_NODE_NAME);
return containerConfig.getLabels().get(DockerContainerLabelKeys.NODE_NAME);
}

// --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@
*/
public class DockerTemplateBase implements Describable<DockerTemplateBase>, Serializable {

/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link #getJenkinsInstanceIdForContainerLabel()}, so that we
* can recognize our own containers later.
*/
static String CONTAINER_LABEL_JENKINS_INSTANCE_ID = "JenkinsId";
/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link Jenkins#getRootUrl()}, so that we
* can recognize our own containers later.
*/
static String CONTAINER_LABEL_JENKINS_URL = "JenkinsServerUrl";
/**
* Name of the Docker "label" that we'll put into every container we start,
* setting its value to our {@link #getImage()}, so that we
* can recognize our own containers later.
*/
static String CONTAINER_LABEL_IMAGE = "JenkinsContainerImage";

private final String image;

private String pullCredentialsId;
Expand Down Expand Up @@ -499,9 +480,9 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig
containerConfig.withPrivileged(privileged);

Map<String,String> map = new HashMap<>();
map.put(CONTAINER_LABEL_JENKINS_INSTANCE_ID, getJenkinsInstanceIdForContainerLabel());
map.put(CONTAINER_LABEL_JENKINS_URL, getJenkinsUrlForContainerLabel());
map.put(CONTAINER_LABEL_IMAGE, getImage());
map.put(DockerContainerLabelKeys.JENKINS_INSTANCE_ID, getJenkinsInstanceIdForContainerLabel());
map.put(DockerContainerLabelKeys.JENKINS_URL, getJenkinsUrlForContainerLabel());
map.put(DockerContainerLabelKeys.CONTAINER_IMAGE, getImage());

containerConfig.withLabels(map);

Expand Down Expand Up @@ -591,7 +572,7 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig

/**
* Calculates the value we use for the Docker label called
* {@link #CONTAINER_LABEL_JENKINS_URL} that we put into every
* {@link DockerContainerLabelKeys#JENKINS_URL} that we put into every
* container we make, so that we can recognize our own containers later.
*/
static String getJenkinsUrlForContainerLabel() {
Expand All @@ -602,7 +583,7 @@ static String getJenkinsUrlForContainerLabel() {

/**
* Calculates the value we use for the Docker label called
* {@link #CONTAINER_LABEL_JENKINS_INSTANCE_ID} that we put into every
* {@link DockerContainerLabelKeys#JENKINS_INSTANCE_ID} that we put into every
* container we make, so that we can recognize our own containers later.
*/
static String getJenkinsInstanceIdForContainerLabel() {
Expand Down