kubernetesfabric8

how to generate kubernetes yml file with io.fabric8?


I want generate kubernetes manifest files with fabric8. please help me.

@EnableKubernetesMockClient
public class KubernetesClientTests {
    KubernetesClient client;

    @Test
    public void testCreateManifest() {
        Assert.assertNotNull(client);
        //CREATE
        Pod pod = client.pods().inNamespace("ns1").resource(new   PodBuilder().withNewMetadata().withName("pod1").endMetadata().build())
                .create();
        Assert.assertNotNull(pod);
        //READ
        //TODO, generate manifest file???

    }
}

Solution

  • You can use Serialization.asYaml() method to convert object to YAML string and Serialization.asJson() method to convert object to JSON string:

    I've tested this code on KubernetesClient v6.7.2 and it seems to be working okay:

    try (KubernetesClient k8s = new KubernetesClientBuilder().build()) {
      Pod pod = k8s.pods()
          .inNamespace("default")
          .withName("excj496")
          .get();
      // Get YAML string
      System.out.println(Serialization.asYaml(pod));
      // Get JSON string
      System.out.println(Serialization.asJson(pod));
    }