英文:
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));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论