@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()); }
public Container done(){ return function.apply(builder.build()); }
public static void main(String args[]) throws InterruptedException { String master = "https://192.168.99.100:8443/"; if (args.length == 1) { master = args[0]; } log("Using master with url ", master); Config config = new ConfigBuilder().withMasterUrl(master).build(); try (final KubernetesClient client = new DefaultKubernetesClient(config)) { PriorityClass priorityClass = new PriorityClassBuilder() .withNewMetadata().withName("high-priority").endMetadata() .withValue(new Integer(100000)) .withGlobalDefault(false) .withDescription("This priority class should be used for XYZ service pods only.") .build(); client.scheduling().priorityClass().create(priorityClass); client.pods().inNamespace("default").create(new PodBuilder() .withNewMetadata().withName("nginx").withLabels(Collections.singletonMap("env", "test")).endMetadata() .withNewSpec() .addToContainers(new ContainerBuilder().withName("nginx").withImage("nginx").withImagePullPolicy("IfNotPresent").build()) .withPriorityClassName("high-priority") .endSpec() .build() ); } catch (KubernetesClientException e) { e.printStackTrace(); log("Could not create resource", e.getMessage()); } }
private List<Container> generateInitContainer(DeploymentModel deploymentModel) throws KubernetesPluginException { List<Container> initContainers = new ArrayList<>(); for (String dependsOn : deploymentModel.getDependsOn()) { String serviceName = KubernetesContext.getInstance().getServiceName(dependsOn); List<String> commands = new ArrayList<>(); commands.add("sh"); commands.add("-c"); commands.add("until nslookup " + serviceName + "; do echo waiting for " + serviceName + "; sleep 2; done;"); initContainers.add(new ContainerBuilder() .withName("wait-for-" + serviceName) .withImage("busybox") .withCommand(commands) .build()); } return initContainers; }
@Override protected List<Container> getContainers() { List<Container> containers = new ArrayList<>(); Container container = new ContainerBuilder() .withName(name) .withImage(getImage()) .withEnv(getEnvVars()) .withPorts(getContainerPortList()) .withVolumeMounts(getVolumeMounts()) .withResources(ModelUtils.resources(getResources())) .build(); containers.add(container); return containers; }
private Container createInitContainer() { return new ContainerBuilder() .withName(getConfig(Config.pemToJKSInitContainerName)) .withImage(getConfig(Config.pemToJKSInitContainerImage)) .withImagePullPolicy("IfNotPresent") .withArgs(createArgsArray()) .withVolumeMounts(createMounts()) .build(); }
private Container createInitContainer() { return new ContainerBuilder() .withName(getConfig(Config.pemToJKSInitContainerName)) .withImage(getConfig(Config.pemToJKSInitContainerImage)) .withImagePullPolicy("IfNotPresent") .withArgs(createArgsArray()) .withVolumeMounts(createMounts()) .build(); }
private Container createPvInitContainer(PodSpec podSpec) { Map<String, String> mountPoints = extractMountPoints(podSpec); return new ContainerBuilder() .withName(ENRICHER_NAME) .withImage("busybox") .withImagePullPolicy("IfNotPresent") .withCommand(createChmodCommandArray(mountPoints)) .withVolumeMounts(createMounts(mountPoints)) .build(); }
@Override protected List<Container> getContainers() { return Collections.singletonList(new ContainerBuilder() .withName(USER_OPERATOR_CONTAINER_NAME) .withImage(getImage()) .withEnv(getEnvVars()) .withPorts(singletonList(createContainerPort(HEALTHCHECK_PORT_NAME, HEALTHCHECK_PORT, "TCP"))) .withLivenessProbe(createHttpProbe(livenessPath + "healthy", HEALTHCHECK_PORT_NAME, livenessInitialDelay, livenessTimeout)) .withReadinessProbe(createHttpProbe(readinessPath + "ready", HEALTHCHECK_PORT_NAME, readinessInitialDelay, readinessTimeout)) .withResources(ModelUtils.resources(getResources())) .withVolumeMounts(getVolumeMounts()) .build()); }
private Container createPvInitContainer(PodSpec podSpec) { Map<String, String> mountPoints = extractMountPoints(podSpec); return new ContainerBuilder() .withName(ENRICHER_NAME) .withImage("busybox") .withImagePullPolicy("IfNotPresent") .withCommand(createChmodCommandArray(mountPoints)) .withVolumeMounts(createMounts(mountPoints)) .build(); }
@Override protected List<Container> getContainers() { return Collections.singletonList(new ContainerBuilder() .withName(TOPIC_OPERATOR_CONTAINER_NAME) .withImage(getImage()) .withEnv(getEnvVars()) .withPorts(singletonList(createContainerPort(HEALTHCHECK_PORT_NAME, HEALTHCHECK_PORT, "TCP"))) .withLivenessProbe(createHttpProbe(livenessPath + "healthy", HEALTHCHECK_PORT_NAME, livenessInitialDelay, livenessTimeout)) .withReadinessProbe(createHttpProbe(readinessPath + "ready", HEALTHCHECK_PORT_NAME, readinessInitialDelay, readinessTimeout)) .withResources(ModelUtils.resources(getResources())) .withVolumeMounts(getVolumeMounts()) .build()); }
@Override protected List<Container> getContainers() { List<Container> containers = new ArrayList<>(); Container container = new ContainerBuilder() .withName(name) .withImage(getImage()) .withEnv(getEnvVars()) .withPorts(getContainerPortList()) .withLivenessProbe(createHttpProbe(livenessPath, REST_API_PORT_NAME, livenessInitialDelay, livenessTimeout)) .withReadinessProbe(createHttpProbe(readinessPath, REST_API_PORT_NAME, readinessInitialDelay, readinessTimeout)) .withVolumeMounts(getVolumeMounts()) .withResources(ModelUtils.resources(getResources())) .build(); containers.add(container); return containers; }
private Container generateContainer(JobModel jobModel) { return new ContainerBuilder() .withName(jobModel.getName()) .withImage(jobModel.getImage()) .withImagePullPolicy(jobModel.getImagePullPolicy()) .withEnv(populateEnvVar(jobModel.getEnv())) .build(); }
private static Container keepaliveContainer() { return new ContainerBuilder() .withName(KEEPALIVE_CONTAINER_NAME) // Use the k8s pause container image. It sleeps forever until terminated. .withImage("k8s.gcr.io/pause:3.1") .withNewResources() .addToRequests("cpu", new QuantityBuilder() .withAmount("0") .build()) .addToRequests("memory", new QuantityBuilder() .withAmount("0") .build()) .endResources() .build(); }
private Container generateContainer(DeploymentModel deploymentModel, List<ContainerPort> containerPorts) { return new ContainerBuilder() .withName(deploymentModel.getName()) .withImage(deploymentModel.getImage()) .withImagePullPolicy(deploymentModel.getImagePullPolicy()) .withPorts(containerPorts) .withEnv(populateEnvVar(deploymentModel.getEnv())) .withVolumeMounts(populateVolumeMounts(deploymentModel)) .withLivenessProbe(generateLivenessProbe(deploymentModel)) .build(); }
@Test public void shouldCombineAllPodMounts() { VolumeMount vm1 = new VolumeMount("/host/mnt1", "volume-1", false, null); VolumeMount vm2 = new VolumeMount("/host/mnt2", "volume-2", false, null); VolumeMount vm3 = new VolumeMount("/host/mnt3", "volume-3", false, null); VolumeMount vm4 = new VolumeMount("/host/mnt1", "volume-4", false, null); Container container1 = containerBuilder().withName("jnlp").withVolumeMounts(vm1, vm2).build(); Pod pod1 = podBuilder().withContainers(container1).endSpec().build(); Container container2 = containerBuilder().withName("jnlp").withVolumeMounts(vm3, vm4).build(); Pod pod2 = podBuilder().withContainers(container2).endSpec().build(); Pod result = combine(pod1, pod2); List<Container> containers = result.getSpec().getContainers(); assertEquals(1, containers.size()); assertEquals(3, containers.get(0).getVolumeMounts().size()); assertThat(containers.get(0).getVolumeMounts(), containsInAnyOrder(vm2, vm3, vm4)); }
@Override protected DeploymentConfig resource() { return new DeploymentConfigBuilder().withNewMetadata() .withNamespace(NAMESPACE) .withName(RESOURCE_NAME) .endMetadata() .withNewSpec() .withNewTemplate() .withNewSpec() .addToContainers(new ContainerBuilder().withImage("img").build()) .endSpec() .endTemplate() .endSpec().build(); }
@Before public void configureCloud() throws Exception { client = setupCloud(this).connect(); deletePods(client, getLabels(this), false); String image = "busybox"; Container c = new ContainerBuilder().withName(image).withImagePullPolicy("IfNotPresent").withImage(image) .withCommand("cat").withTty(true).build(); String podName = "test-command-execution-" + RandomStringUtils.random(5, "bcdfghjklmnpqrstvwxz0123456789"); pod = client.pods().create(new PodBuilder().withNewMetadata().withName(podName).withLabels(getLabels(this)) .endMetadata().withNewSpec().withContainers(c).endSpec().build()); System.out.println("Created pod: " + pod.getMetadata().getName()); decorator = new ContainerExecDecorator(client, pod.getMetadata().getName(), image, client.getNamespace()); }