Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
feat: add container security context support (#995)
Browse files Browse the repository at this point in the history
Closes #973
  • Loading branch information
arnaud-deprez authored and diemol committed Jul 19, 2019
1 parent f214b3b commit 03559f5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
2 changes: 2 additions & 0 deletions charts/zalenium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ See Zalenium's [usage examples](https://github.com/zalando/zalenium/blob/master/
| `hub.tag` | The zalenium hub image tag | `3` |
| `hub.pullPolicy` | The pull policy for the hub image | `IfNotPresent` |
| `hub.port` | The port the hub listens on | `4444` |
| `hub.podSecurityContext` | Pod Security Context used by hub and selenium grids | |
| `hub.containerSecurityContext` | Container Security Context used by hub and selenium grids | |
| `hub.livenessTimeout` | Timeout for probe Hub liveness via HTTP request on Hub console | `1` |
| `hub.readinessTimeout` | Timeout for probe Hub readiness via HTTP request on Hub console | `1` |
| `hub.localVolumesRoot` | The root directory to store HostPath volumes (e.g. if running in minikube) | `/tmp` |
Expand Down
7 changes: 3 additions & 4 deletions charts/zalenium/templates/_pod-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ metadata:
{{- include "zalenium.labels" . | nindent 4 }}
annotations:
spec:
{{- if .Values.hub.securityContext.enabled }}
securityContext:
fsGroup: {{ .Values.hub.securityContext.fsGroup }}
runAsUser: {{ .Values.hub.securityContext.runAsUser }}
{{- end }}
{{- toYaml .Values.hub.podSecurityContext | nindent 4 }}
{{- if .Values.hub.pullSecrets }}
imagePullSecrets:
- name: {{ .Values.hub.imagePullSecret }}
Expand Down Expand Up @@ -42,6 +39,8 @@ spec:
- name: {{ .Chart.Name }}
image: "{{ .Values.hub.image }}:{{ .Values.hub.tag }}"
imagePullPolicy: {{ .Values.hub.pullPolicy }}
securityContext:
{{- toYaml .Values.hub.containerSecurityContext | nindent 8 }}
ports:
- containerPort: {{ .Values.hub.port }}
protocol: TCP
Expand Down
15 changes: 11 additions & 4 deletions charts/zalenium/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ hub:
## Pod Security Context
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
## in case it is running outside openshift, this should fix this https://github.com/zalando/zalenium/issues/631
securityContext:
enabled: false
fsGroup: 0
runAsUser: 1001
podSecurityContext:
## See all the options here https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#podsecuritycontext-v1-core
# fsGroup: 1000
# runAsUser: 1000
# runAsGroup: 1000

## Container Security Context
containerSecurityContext:
## See all the options here https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#securitycontext-v1-core
# capabilities: []
# privileged: false

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.zalando.ep.zalenium.container.ContainerClientRegistration;
import de.zalando.ep.zalenium.container.ContainerCreationStatus;
import de.zalando.ep.zalenium.util.Environment;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerStateTerminated;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.DoneablePod;
Expand All @@ -14,6 +15,7 @@
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodSecurityContext;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.SecurityContext;
import io.fabric8.kubernetes.api.model.Toleration;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class KubernetesContainerClient implements ContainerClient {

private static final Logger logger = LoggerFactory.getLogger(KubernetesContainerClient.class.getName());

private static final String DEFAULT_ZALENIUM_CONTAINER_NAME = "zalenium";
private static final String ZALENIUM_KUBERNETES_TOLERATIONS = "ZALENIUM_KUBERNETES_TOLERATIONS";
private static final String ZALENIUM_KUBERNETES_NODE_SELECTOR = "ZALENIUM_KUBERNETES_NODE_SELECTOR";

Expand All @@ -71,7 +74,8 @@ public class KubernetesContainerClient implements ContainerClient {
private List<Toleration> tolerations = new ArrayList<>();
private String imagePullPolicy;
private List<LocalObjectReference> imagePullSecrets;
private PodSecurityContext configuredSecurityContext;
private PodSecurityContext configuredPodSecurityContext;
private SecurityContext configuredContainerSecurityContext;

private final Map<String, Quantity> seleniumPodLimits = new HashMap<>();
private final Map<String, Quantity> seleniumPodRequests = new HashMap<>();
Expand Down Expand Up @@ -109,7 +113,8 @@ public KubernetesContainerClient(Environment environment,
discoverNodeSelector();
discoverTolerations();
discoverImagePullSecrets();
discoverSecurityContext();
discoverPodSecurityContext();
discoverContainerSecurityContext();
buildResourceMaps();

logger.info(String.format(
Expand Down Expand Up @@ -221,8 +226,17 @@ private String findHostname() {
return hostname;
}

private void discoverSecurityContext() {
configuredSecurityContext = zaleniumPod.getSpec().getSecurityContext();
private void discoverPodSecurityContext() {
configuredPodSecurityContext = zaleniumPod.getSpec().getSecurityContext();
}

private void discoverContainerSecurityContext() {
configuredContainerSecurityContext = zaleniumPod.getSpec().getContainers()
.stream()
.filter(c -> DEFAULT_ZALENIUM_CONTAINER_NAME.equals(c.getName()))
.map(Container::getSecurityContext)
.findFirst()
.orElse(null);
}

@Override
Expand Down Expand Up @@ -353,7 +367,8 @@ public ContainerCreationStatus createContainer(String zaleniumContainerName, Str
config.setPodLimits(seleniumPodLimits);
config.setPodRequests(seleniumPodRequests);
config.setOwner(zaleniumPod);
config.setPodSecurityContext(configuredSecurityContext);
config.setPodSecurityContext(configuredPodSecurityContext);
config.setContainerSecurityContext(configuredContainerSecurityContext);

DoneablePod doneablePod = createDoneablePod.apply(config);

Expand Down Expand Up @@ -584,6 +599,7 @@ public static DoneablePod createDoneablePodDefaultImpl(PodConfiguration config)
.withImage(config.getImage())
.withImagePullPolicy(config.getImagePullPolicy())
.addAllToEnv(config.getEnvVars())
.withSecurityContext(config.getContainerSecurityContext())
.addNewVolumeMount()
.withName("dshm")
.withMountPath("/dev/shm")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.fabric8.kubernetes.api.model.HostAlias;
import io.fabric8.kubernetes.api.model.LocalObjectReference;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.SecurityContext;
import io.fabric8.kubernetes.api.model.Toleration;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class PodConfiguration {
private List<Toleration> tolerations;
private OwnerReference ownerReference;
private PodSecurityContext podSecurityContext;
private SecurityContext containerSecurityContext;

public String getNodePort() {
return nodePort;
Expand Down Expand Up @@ -137,4 +139,11 @@ public void setPodSecurityContext(PodSecurityContext podSecurityContext) {
this.podSecurityContext = podSecurityContext;
}

public SecurityContext getContainerSecurityContext() {
return containerSecurityContext;
}

public void setContainerSecurityContext(SecurityContext containerSecurityContext) {
this.containerSecurityContext = containerSecurityContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.fabric8.kubernetes.api.model.LocalObjectReference;
import io.fabric8.kubernetes.api.model.PodSecurityContext;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.SecurityContext;
import io.fabric8.kubernetes.api.model.Toleration;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
Expand Down Expand Up @@ -155,4 +156,11 @@ public void testSetPodSecurityContext() {
podConfiguration.setPodSecurityContext(securityContext);
assertThat(podConfiguration.getPodSecurityContext(), is(securityContext));
}

@Test
public void testSetContainerSecurityContext() {
SecurityContext securityContext = mock(SecurityContext.class);
podConfiguration.setContainerSecurityContext(securityContext);
assertThat(podConfiguration.getContainerSecurityContext(), is(securityContext));
}
}

0 comments on commit 03559f5

Please sign in to comment.