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()); } }
.endMetadata() .withNewSpec() .withNodeSelector(config.getNodeSelector()) .withTolerations(config.getTolerations()) .addNewVolume() .withName("dshm") .withNewEmptyDir() .endEmptyDir() .endVolume() .addNewContainer() .withName("selenium-node") .withImage(config.getImage()) .endReadinessProbe() .endContainer() .withRestartPolicy("Never") .endSpec(); doneablePod = doneablePod .editSpec() .addNewVolumeLike(entry.getValue()) .and() .editFirstContainer() .addNewVolumeMountLike(entry.getKey()) .endVolumeMount() .endContainer() .endSpec();
builder = builder.withActiveDeadlineSeconds(Long.valueOf(template.getActiveDeadlineSeconds())); builder.withVolumes(volumes.values().toArray(new Volume[volumes.size()])); builder.withServiceAccount(substituteEnv(template.getServiceAccount())); .map((x) -> x.toLocalObjectReference()).collect(Collectors.toList()); if (!imagePullSecrets.isEmpty()) { builder.withImagePullSecrets(imagePullSecrets); builder.withNodeSelector(nodeSelector); builder.withContainers(containers.values().toArray(new Container[containers.size()])); Pod pod = builder.endSpec().build();
.endMetadata() .withNewSpec() .withNodeSelector(node != null ? node.getMetadata().getLabels() : new HashMap<String, String>()) .withVolumes(volumes) .withContainers(containers) .withRestartPolicy("Never") .withServiceAccount(podTemplate.getServiceAccount()) .endSpec() .done(); return p;
.withNodeSelector(nodeSelector) // .withServiceAccount(serviceAccount) // .withContainers(Lists.newArrayList(combinedContainers.values())) // .withVolumes(combinedVolumes) // .withTolerations(combinedTolerations) // .withImagePullSecrets(Lists.newArrayList(imagePullSecrets)); Pod pod = specBuilder.endSpec().build(); LOGGER.log(Level.FINE, "Pods combined: {0}", pod); return pod;
@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)); }
public SpecNested<T> withNewSpecLike( PodSpec item){ return new SpecNested<T>(item); } public SpecNested<T> editSpec(){
public SpecNested<T> withNewSpec(){ return new SpecNested<T>(); } public SpecNested<T> withNewSpecLike( PodSpec item){
@Test public void shouldCombineAllLabels() { Pod pod1 = new PodBuilder().withNewMetadata().withLabels( // ImmutableMap.of("label1", "pod1", "label2", "pod1") // ).endMetadata().withNewSpec().endSpec().build(); Pod pod2 = new PodBuilder().withNewMetadata().withLabels( // ImmutableMap.of("label1", "pod2", "label3", "pod2") // ).endMetadata().withNewSpec().endSpec().build(); Map<String, String> labels = combine(pod1, pod2).getMetadata().getLabels(); assertThat(labels, hasEntry("label1", "pod2")); assertThat(labels, hasEntry("label2", "pod1")); assertThat(labels, hasEntry("label3", "pod2")); }
public SpecNested<T> withNewSpec(){ return new SpecNested<T>(); } public SpecNested<T> withNewSpecLike( PodSpec item){
@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()); }
public SpecNested<T> withNewSpecLike( PodSpec item){ return new SpecNested<T>(item); } public SpecNested<T> editSpec(){
@Override protected Pod resource() { return new PodBuilder() .withNewMetadata() .withNamespace(NAMESPACE) .withName(RESOURCE_NAME) .endMetadata() .withNewSpec() .withHostname("foo") .endSpec().build(); }
.endMetadata() .withNewSpec() .addNewContainer().withName("nginx").withImage("nginx") .addNewPort().withContainerPort(80).endPort() .endContainer() .endSpec().done(); log("Created testPod:", createdPod);