如何使用 io.fabric8 生成 Kubernetes 的 YAML 文件?

huangapple go评论74阅读模式
英文:

how to generate kubernetes yml file with io.fabric8?

问题

我想使用fabric8生成Kubernetes清单文件,请帮助我。

@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,生成清单文件???

    }
}
英文:

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???

    }
}

答案1

得分: 0

你可以使用Serialization.asYaml()方法将对象转换为YAML字符串,使用Serialization.asJson()方法将对象转换为JSON字符串:

我已在KubernetesClient v6.7.2上测试了此代码,似乎可以正常工作:

try (KubernetesClient k8s = new KubernetesClientBuilder().build()) {
  Pod pod = k8s.pods()
      .inNamespace("default")
      .withName("excj496")
      .get();
  // 获取YAML字符串
  System.out.println(Serialization.asYaml(pod));
  // 获取JSON字符串
  System.out.println(Serialization.asJson(pod));
}
英文:

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));
}

huangapple
  • 本文由 发表于 2023年6月16日 15:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定