Search in sources :

Example 1 with BindingBuilder

use of io.fabric8.kubernetes.api.model.BindingBuilder in project kubernetes-client by fabric8io.

the class BindingExample method main.

@SuppressWarnings("java:S106")
public static void main(String[] args) {
    final String podName = "binding-example-" + UUID.randomUUID();
    try (final KubernetesClient client = new KubernetesClientBuilder().build()) {
        final String namespace;
        if (client.getConfiguration().getNamespace() != null) {
            namespace = client.getConfiguration().getNamespace();
        } else if (client.getNamespace() != null) {
            namespace = client.getNamespace();
        } else {
            namespace = client.namespaces().list().getItems().stream().findFirst().orElseThrow(() -> new IllegalStateException("No namespace available")).getMetadata().getName();
        }
        client.pods().inNamespace(namespace).create(new PodBuilder().withMetadata(new ObjectMetaBuilder().withName(podName).build()).withSpec(new PodSpecBuilder().withSchedulerName("random-scheduler-name-which-does-not-exist").addNewContainer().withName(podName).withImage("nginx:latest").endContainer().build()).build());
        final Node firstNode = client.nodes().list().getItems().stream().findFirst().orElseThrow(() -> new IllegalStateException("No nodes available"));
        client.bindings().inNamespace(namespace).create(new BindingBuilder().withNewMetadata().withName(podName).endMetadata().withNewTarget().withKind(firstNode.getKind()).withApiVersion(firstNode.getApiVersion()).withName(firstNode.getMetadata().getName()).endTarget().build());
        System.out.printf("Successfully bound Pod %s to Node %s%n", podName, firstNode.getMetadata().getName());
    }
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) BindingBuilder(io.fabric8.kubernetes.api.model.BindingBuilder) KubernetesClientBuilder(io.fabric8.kubernetes.client.KubernetesClientBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Node(io.fabric8.kubernetes.api.model.Node) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder)

Example 2 with BindingBuilder

use of io.fabric8.kubernetes.api.model.BindingBuilder in project kubernetes-client by fabric8io.

the class BindingTest method createOK.

@DisplayName("create, with created response, should return created resource")
@Test
void createOK() {
    server.expect().post().withPath("/api/v1/namespaces/default/bindings").andReturn(201, "{\"metadata\": {\"name\": \"binding-name\"}}").once();
    // When
    final Binding result = client.bindings().inNamespace("default").create(new BindingBuilder().withNewMetadata().withName("binding-name").endMetadata().withNewTarget().withKind("Node").withApiVersion("v1").withName("node-name").endTarget().build());
    // Then
    assertThat(result).isNotNull().hasFieldOrPropertyWithValue("metadata.name", "binding-name");
}
Also used : Binding(io.fabric8.kubernetes.api.model.Binding) BindingBuilder(io.fabric8.kubernetes.api.model.BindingBuilder) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

BindingBuilder (io.fabric8.kubernetes.api.model.BindingBuilder)2 Binding (io.fabric8.kubernetes.api.model.Binding)1 Node (io.fabric8.kubernetes.api.model.Node)1 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)1 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1