private void discoverFolderMounts() { List<VolumeMount> volumeMounts = zaleniumPod.getSpec().getContainers().get(0).getVolumeMounts(); List<VolumeMount> validMounts = new ArrayList<>(); volumeMounts.stream() .filter(volumeMount -> !Arrays.asList(PROTECTED_NODE_MOUNT_POINTS).contains(volumeMount.getMountPath())) .forEach(validMounts::add); // Look through the volume mounts to see if the shared folder is mounted if (!validMounts.isEmpty()) { List<Volume> volumes = zaleniumPod.getSpec().getVolumes(); for (VolumeMount validMount : validMounts) { volumes.stream() .filter(volume -> validMount.getName().equalsIgnoreCase(volume.getName())) .findFirst() .ifPresent(volume -> mountedSharedFoldersMap.put(validMount, volume)); } } }
List<EnvVar> podEnvironmentVariables = currentPod.getSpec().getContainers().get(0).getEnv(); Optional<EnvVar> noVncPort = podEnvironmentVariables.stream().filter(env -> "NOVNC_PORT".equals(env.getName())).findFirst();
@Override public StatefulSet updateImage(String image) { StatefulSet oldRC = get(); if (oldRC == null) { throw new KubernetesClientException("Existing StatefulSet doesn't exist"); } if (oldRC.getSpec().getTemplate().getSpec().getContainers().size() > 1) { throw new KubernetesClientException("Image update is not supported for multicontainer pods"); } if (oldRC.getSpec().getTemplate().getSpec().getContainers().size() == 0) { throw new KubernetesClientException("Pod has no containers!"); } Container updatedContainer = new ContainerBuilder(oldRC.getSpec().getTemplate().getSpec().getContainers().iterator().next()).withImage(image).build(); StatefulSetBuilder newRCBuilder = new StatefulSetBuilder(oldRC); newRCBuilder.editMetadata().withResourceVersion(null).endMetadata() .editSpec().editTemplate().editSpec().withContainers(Collections.singletonList(updatedContainer)) .endSpec().endTemplate().endSpec(); return new StatefulSetRollingUpdater(client, config, namespace).rollUpdate(oldRC, newRCBuilder.build()); }
@Override public ReplicaSet updateImage(String image) { ReplicaSet oldRC = get(); if (oldRC == null) { throw new KubernetesClientException("Existing replica set doesn't exist"); } if (oldRC.getSpec().getTemplate().getSpec().getContainers().size() > 1) { throw new KubernetesClientException("Image update is not supported for multicontainer pods"); } if (oldRC.getSpec().getTemplate().getSpec().getContainers().size() == 0) { throw new KubernetesClientException("Pod has no containers!"); } Container updatedContainer = new ContainerBuilder(oldRC.getSpec().getTemplate().getSpec().getContainers().iterator().next()).withImage(image).build(); ReplicaSetBuilder newRCBuilder = new ReplicaSetBuilder(oldRC); newRCBuilder.editMetadata().withResourceVersion(null).endMetadata() .editSpec().editTemplate().editSpec().withContainers(Collections.singletonList(updatedContainer)) .endSpec().endTemplate().endSpec(); return new ReplicaSetRollingUpdater(client, config, namespace).rollUpdate(oldRC, newRCBuilder.build()); }
@Override public ReplicationController updateImage(String image) { ReplicationController oldRC = get(); if (oldRC == null) { throw new KubernetesClientException("Existing replication controller doesn't exist"); } if (oldRC.getSpec().getTemplate().getSpec().getContainers().size() > 1) { throw new KubernetesClientException("Image update is not supported for multicontainer pods"); } if (oldRC.getSpec().getTemplate().getSpec().getContainers().size() == 0) { throw new KubernetesClientException("Pod has no containers!"); } Container updatedContainer = new ContainerBuilder(oldRC.getSpec().getTemplate().getSpec().getContainers().iterator().next()).withImage(image).build(); ReplicationControllerBuilder newRCBuilder = new ReplicationControllerBuilder(oldRC); newRCBuilder.editMetadata().withResourceVersion(null).endMetadata() .editSpec().editTemplate().editSpec().withContainers(Collections.singletonList(updatedContainer)) .endSpec().endTemplate().endSpec(); return new ReplicationControllerRollingUpdater(client, config, namespace).rollUpdate(oldRC, newRCBuilder.build()); }
@SuppressWarnings("unchecked") public static List<Container> getContainers(PodSpec podSpec) { if (podSpec != null) { return podSpec.getContainers(); } return Collections.EMPTY_LIST; }
private static boolean isLocalCustomisation(PodSpec podSpec) { List<Container> containers = podSpec.getContainers() != null ? podSpec.getContainers() : Collections.<Container>emptyList(); for (Container container : containers) { if (StringUtils.isNotBlank(container.getImage())) { return false; } } return true; } }
public static List<Container> getOrCreatePodDesiredContainers(Pod pod) { PodSpec podSpec = NodeHelper.getOrCreatePodSpec(pod); List<Container> containers = podSpec.getContainers(); if (containers == null) { containers = new ArrayList<>(); podSpec.setContainers(containers); } return containers; }
public List<String> getPodContainerNames(String podName) { return kubernetesClient .pods() .inNamespace(environment.getKubernetesNamespace()) .withName(podName) .get() .getSpec() .getContainers() .stream() .map(Container::getName) .collect(Collectors.toList()); }
private Map<String, String> extractMountPoints(PodSpec podSpec) { Map<String, String> nameToMount = new LinkedHashMap<>(); List<Volume> volumes = podSpec.getVolumes(); if (volumes != null) { for (Volume volume : volumes) { PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim(); if (persistentVolumeClaim != null) { String name = volume.getName(); String mountPath = getMountPath(podSpec.getContainers(), name); nameToMount.put(name, mountPath); } } } return nameToMount; }
@Override protected Future<ReconcileResult<DeploymentConfig>> internalPatch(String namespace, String name, DeploymentConfig current, DeploymentConfig desired) { desired.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(current.getSpec().getTemplate().getSpec().getContainers().get(0).getImage()); return super.internalPatch(namespace, name, current, desired); } }
/*** * Returns list of running containers in pod * @param podName name of pod * @return list of containers */ public List<Container> getContainersFromPod(String podName) { return client.pods().inNamespace(globalNamespace).withName(podName).get().getSpec().getContainers(); }
public static Map<String, String> getKafkaContainerEnv(StatefulSet ss) { for (Container container : ss.getSpec().getTemplate().getSpec().getContainers()) { if ("kafka".equals(container.getName())) { LinkedHashMap<String, String> map = new LinkedHashMap<>(container.getEnv() == null ? 2 : container.getEnv().size()); if (container.getEnv() != null) { for (EnvVar envVar : container.getEnv()) { map.put(envVar.getName(), envVar.getValue()); } } return map; } } throw new KafkaUpgradeException("Could not find 'kafka' container in StatefulSet " + ss.getMetadata().getName()); }
@Test public void testOverridesFromYaml() throws Exception { PodTemplate template = new PodTemplate(); template.setYaml(new String(IOUtils.toByteArray(getClass().getResourceAsStream("pod-overrides.yaml")))); setupStubs(); Pod pod = new PodTemplateBuilder(template).withSlave(slave).build(); Map<String, Container> containers = pod.getSpec().getContainers().stream() .collect(Collectors.toMap(Container::getName, Function.identity())); assertEquals(1, containers.size()); validateJnlpContainer(containers.get("jnlp"), slave); }
@Test public void testOverridesContainerSpec() throws Exception { PodTemplate template = new PodTemplate(); ContainerTemplate cT = new ContainerTemplate("jnlp", "jenkinsci/jnlp-slave:latest"); template.setContainers(Lists.newArrayList(cT)); template.setYaml(new String(IOUtils.toByteArray(getClass().getResourceAsStream("pod-overrides.yaml")))); setupStubs(); Pod pod = new PodTemplateBuilder(template).withSlave(slave).build(); Map<String, Container> containers = pod.getSpec().getContainers().stream() .collect(Collectors.toMap(Container::getName, Function.identity())); assertEquals(1, containers.size()); validateJnlpContainer(containers.get("jnlp"), slave); } }
public Pod(io.fabric8.kubernetes.api.model.Pod pod) { this.name = pod.getMetadata().getName(); if (pod.getMetadata().getAnnotations() != null) { this.annotations.putAll(pod.getMetadata().getAnnotations()); } this.kind = pod.getKind(); this.host = pod.getStatus().getPodIP(); this.phase = pod.getStatus().getPhase(); this.ready = getReadyCondition(pod.getStatus().getConditions()); this.portMap = getPortMap(pod.getSpec().getContainers()); }
public PodSpecBuilder( PodSpecFluent<?> fluent , PodSpec instance ){ this.fluent = fluent; fluent.withActiveDeadlineSeconds(instance.getActiveDeadlineSeconds()); fluent.withContainers(instance.getContainers()); fluent.withDnsPolicy(instance.getDnsPolicy()); fluent.withHost(instance.getHost()); fluent.withHostNetwork(instance.getHostNetwork()); fluent.withImagePullSecrets(instance.getImagePullSecrets()); fluent.withNodeSelector(instance.getNodeSelector()); fluent.withRestartPolicy(instance.getRestartPolicy()); fluent.withServiceAccount(instance.getServiceAccount()); fluent.withTerminationGracePeriodSeconds(instance.getTerminationGracePeriodSeconds()); fluent.withVolumes(instance.getVolumes()); } public PodSpecBuilder( PodSpec instance ){
public PodSpecBuilder( PodSpecFluent<?> fluent , PodSpec instance ){ this.fluent = fluent; fluent.withActiveDeadlineSeconds(instance.getActiveDeadlineSeconds()); fluent.withContainers(instance.getContainers()); fluent.withDnsPolicy(instance.getDnsPolicy()); fluent.withHost(instance.getHost()); fluent.withHostNetwork(instance.getHostNetwork()); fluent.withImagePullSecrets(instance.getImagePullSecrets()); fluent.withNodeSelector(instance.getNodeSelector()); fluent.withRestartPolicy(instance.getRestartPolicy()); fluent.withServiceAccount(instance.getServiceAccount()); fluent.withTerminationGracePeriodSeconds(instance.getTerminationGracePeriodSeconds()); fluent.withVolumes(instance.getVolumes()); } public PodSpecBuilder( PodSpec instance ){
public PodSpecBuilder( PodSpec instance ){ this.fluent = this; this.withActiveDeadlineSeconds(instance.getActiveDeadlineSeconds()); this.withContainers(instance.getContainers()); this.withDnsPolicy(instance.getDnsPolicy()); this.withHost(instance.getHost()); this.withHostNetwork(instance.getHostNetwork()); this.withImagePullSecrets(instance.getImagePullSecrets()); this.withNodeSelector(instance.getNodeSelector()); this.withRestartPolicy(instance.getRestartPolicy()); this.withServiceAccount(instance.getServiceAccount()); this.withTerminationGracePeriodSeconds(instance.getTerminationGracePeriodSeconds()); this.withVolumes(instance.getVolumes()); }
public PodSpecBuilder( PodSpec instance ){ this.fluent = this; this.withActiveDeadlineSeconds(instance.getActiveDeadlineSeconds()); this.withContainers(instance.getContainers()); this.withDnsPolicy(instance.getDnsPolicy()); this.withHost(instance.getHost()); this.withHostNetwork(instance.getHostNetwork()); this.withImagePullSecrets(instance.getImagePullSecrets()); this.withNodeSelector(instance.getNodeSelector()); this.withRestartPolicy(instance.getRestartPolicy()); this.withServiceAccount(instance.getServiceAccount()); this.withTerminationGracePeriodSeconds(instance.getTerminationGracePeriodSeconds()); this.withVolumes(instance.getVolumes()); }