Kubernetes Fabric8 API

Sep 19 2020

ปัจจุบันฉันใช้ fabric8 API เพื่ออ่านเนื้อหาของ PODs, Services และอื่น ๆ

KubernetesClient client = new DefaultKubernetesClient();
client.configMaps().withName("ConfigMapName");

มีวิธีดึงเนื้อหาของ HTTPProxy จาก httpproxies ของ api group projectcontour.io หรือไม่

ขอบคุณล่วงหน้า!!!

คำตอบ

1 RohanKumar Sep 20 2020 at 00:42

HTTPProxyดูเหมือนว่าจะเป็นทรัพยากรที่กำหนดเอง Kubernetes Client มีTyped API (ซึ่งคุณต้องให้ POJO สำหรับทรัพยากรที่กำหนดเอง) และTypeless API (การจัดการ Custom Resouces โดยใช้ Raw HashMaps) นี่คือตัวอย่างวิธีที่คุณสามารถทำได้โดยใช้ Typeless API:

try (KubernetesClient client = new DefaultKubernetesClient()) {
    CustomResourceDefinitionContext httpProxyContext = new CustomResourceDefinitionContext.Builder()
            .withGroup("projectcontour.io") // <-  Group of Custom Resource
            .withVersion("v1")              // <-  Version of Custom Resource
            .withPlural("httpproxies")      // <-  Plural form as specified in CRD
            .withScope("Namespaced")        // <-  Whether Custom Resource is Cluster Scoped or Namespaced
            .build();

    // List all HTTPProxies
    Map<String, Object> httpProxyList = client.customResource(httpProxyContext).list("ns1");
    // Get a specific HTTPProxy
    Map<String, Object> myHttpProxy = client.customResource(httpProxyContext).get("ns1", "tls-example");
}

คุณสามารถเลือกแนวทางที่คุณคิดว่าเหมาะกับความต้องการของคุณ หากสนใจคุณสามารถตรวจสอบบล็อกของฉันเกี่ยวกับวิธีการเหล่านี้โดยละเอียด:

  1. การจัดการ Kubernetes Custom Resources ใน Java โดยใช้ Fabric8 Kubernetes Client: Part-1 (Typeless)
  2. การจัดการทรัพยากรแบบกำหนดเองของ Kubernetes ใน Java โดยใช้ Fabric8 Kubernetes Client: Part-2 (Typed)