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; }
/** * For StatefulSets, create an init container to parse ${HOSTNAME} to get the `instance.index` and write it to * config/application.properties on a shared volume so the main container has it. Using the legacy annotation * configuration since the current client version does not directly support InitContainers. * * Since 1.8 the annotation method has been removed, and the initContainer API is supported since 1.6 * * @return a container definition with the above mentioned configuration */ private Container createInitContainer() { List<String> command = new LinkedList<>(); String commandString = String .format("%s && %s", setIndexProperty("INSTANCE_INDEX"), setIndexProperty("spring.application.index")); command.add("sh"); command.add("-c"); command.add(commandString); return new ContainerBuilder().withName("index-provider") .withImage("busybox") .withImagePullPolicy("IfNotPresent") .withCommand(command) .withVolumeMounts(new VolumeMountBuilder().withName("config").withMountPath("/config").build()) .build(); }
@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()); }
@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()); }
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() { 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; }
@Override protected List<Container> getContainers() { List<Container> containers = new ArrayList<>(); if (topicOperator != null) { containers.addAll(topicOperator.getContainers()); } if (userOperator != null) { containers.addAll(userOperator.getContainers()); } String tlsSidecarImage = EntityOperatorSpec.DEFAULT_TLS_SIDECAR_IMAGE; if (tlsSidecar != null && tlsSidecar.getImage() != null) { tlsSidecarImage = tlsSidecar.getImage(); } Container tlsSidecarContainer = new ContainerBuilder() .withName(TLS_SIDECAR_NAME) .withImage(tlsSidecarImage) .withLivenessProbe(ModelUtils.tlsSidecarLivenessProbe(tlsSidecar)) .withReadinessProbe(ModelUtils.tlsSidecarReadinessProbe(tlsSidecar)) .withResources(ModelUtils.tlsSidecarResources(tlsSidecar)) .withEnv(asList(ModelUtils.tlsSidecarLogEnvVar(tlsSidecar), buildEnvVar(ENV_VAR_ZOOKEEPER_CONNECT, zookeeperConnect))) .withVolumeMounts(createVolumeMount(TLS_SIDECAR_EO_CERTS_VOLUME_NAME, TLS_SIDECAR_EO_CERTS_VOLUME_MOUNT), createVolumeMount(TLS_SIDECAR_CA_CERTS_VOLUME_NAME, TLS_SIDECAR_CA_CERTS_VOLUME_MOUNT)) .withLifecycle(new LifecycleBuilder().withNewPreStop().withNewExec().withCommand("/opt/stunnel/stunnel_pre_stop.sh", String.valueOf(templateTerminationGracePeriodSeconds)).endExec().endPreStop().build()) .build(); containers.add(tlsSidecarContainer); 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 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(); }
List<Container> getContainers(ResourceConfig config, List<ImageConfiguration> images) { List<Container> ret = new ArrayList<>(); for (ImageConfiguration imageConfig : images) { if (imageConfig.getBuildConfiguration() != null) { Probe livenessProbe = probeHandler.getProbe(config.getLiveness()); Probe readinessProbe = probeHandler.getProbe(config.getReadiness()); Container container = new ContainerBuilder() .withName(KubernetesResourceUtil.extractContainerName(this.groupArtifactVersion, imageConfig)) .withImage(getImageName(imageConfig)) .withImagePullPolicy(getImagePullPolicy(config)) .withEnv(getEnvVars(config)) .withSecurityContext(createSecurityContext(config)) .withPorts(getContainerPorts(imageConfig)) .withVolumeMounts(getVolumeMounts(config)) .withLivenessProbe(livenessProbe) .withReadinessProbe(readinessProbe) .build(); ret.add(container); } } return ret; }
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(); }
@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 ContainerBuilder( Container instance ){ this.fluent = this; this.withArgs(instance.getArgs()); this.withCommand(instance.getCommand()); this.withEnv(instance.getEnv()); this.withImage(instance.getImage()); this.withImagePullPolicy(instance.getImagePullPolicy()); this.withLifecycle(instance.getLifecycle()); this.withLivenessProbe(instance.getLivenessProbe()); this.withName(instance.getName()); this.withPorts(instance.getPorts()); this.withReadinessProbe(instance.getReadinessProbe()); this.withResources(instance.getResources()); this.withSecurityContext(instance.getSecurityContext()); this.withTerminationMessagePath(instance.getTerminationMessagePath()); this.withVolumeMounts(instance.getVolumeMounts()); this.withWorkingDir(instance.getWorkingDir()); }
public ContainerBuilder( Container instance ){ this.fluent = this; this.withArgs(instance.getArgs()); this.withCapabilities(instance.getCapabilities()); this.withCommand(instance.getCommand()); this.withEnv(instance.getEnv()); this.withImage(instance.getImage()); this.withImagePullPolicy(instance.getImagePullPolicy()); this.withLifecycle(instance.getLifecycle()); this.withLivenessProbe(instance.getLivenessProbe()); this.withName(instance.getName()); this.withPorts(instance.getPorts()); this.withPrivileged(instance.getPrivileged()); this.withReadinessProbe(instance.getReadinessProbe()); this.withResources(instance.getResources()); this.withSecurityContext(instance.getSecurityContext()); this.withTerminationMessagePath(instance.getTerminationMessagePath()); this.withVolumeMounts(instance.getVolumeMounts()); this.withWorkingDir(instance.getWorkingDir()); }
@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()); }