【问题标题】:Fabric8: openshift/kubernetes java client, create custom objects like ClusterResourceQuotaFabric8:openshift/kubernetes java 客户端,创建自定义对象,如 ClusterResourceQuota
【发布时间】:2020-09-03 19:25:33
【问题描述】:

我正在开发基于 CRD 自动创建命名空间、标签、注释的 Kubernetes 运算符。

我被 fabric8io-kubernetes-client 未公开的特定 openshift 对象 ClusterResourceQuota (quota.openshift.io/v1) 卡住。

即使从文件中加载,是否还有其他方法可以创建此类对象...?

【问题讨论】:

标签: kubernetes openshift fabric8 kubernetes-operator


【解决方案1】:

我来自 Fabric8 团队。 Fabric8 Kubernetes Client 支持两种方式创建自定义对象:

  • 类型化 API(需要提供自定义资源模型 (POJO))
  • 无类型 API(将自定义资源作为普通 HashMap 处理)

类型化 API:

假设您已经拥有ClusterResourceQuotaClusterResourceQuotaList 的 POJO。您可以像这样为该特定自定义资源创建一个 kubernetes 客户端实例,并将其用于您的自定义资源操作:

try (KubernetesClient client = new DefaultKubernetesClient()) {
    // Create ClusterResourceQuota object
    ClusterResourceQuota clusterResourceQuota = getClusterResourceQuota();

    // ClusterResourceQuota Client
    MixedOperation<ClusterResourceQuota, ClusterResourceQuotaList, DoneableClusterResourceQuota, Resource<ClusterResourceQuota, DoneableClusterResourceQuota>> clusterResourceQuotaClient = null;
    CustomResourceDefinitionContext context = new CustomResourceDefinitionContext
            .Builder()
            .withGroup("quota.openshift.io")
            .withKind("ClusterResourceQuota")
            .withName("clusterresourcequota-crd")
            .withPlural("clusterresourcequotas")
            .withScope("Namespaced")
            .withVersion("v1")
            .build();

    // Initializing ClusterResourceQuota Client, POJOs to be provided
    clusterResourceQuotaClient = client.customResources(context, ClusterResourceQuota.class, ClusterResourceQuotaList.class, DoneableClusterResourceQuota.class);
    // Using ClusterResourceQuota Client to create ClusterResourceQuota resource
    clusterResourceQuotaClient.inNamespace("default").createOrReplace(clusterResourceQuota);
}

无类型 API

如果您没有 POJO,您可以使用 Fabric8 Kubernetes 客户端的原始 API 来处理自定义资源。以下是你的做法:

try (KubernetesClient client = new DefaultKubernetesClient()) {
    // Create Custom Resource Context
    CustomResourceDefinitionContext context = new CustomResourceDefinitionContext
            .Builder()
            .withGroup("quota.openshift.io")
            .withKind("ClusterResourceQuota")
            .withName("clusterresourcequota-crd")
            .withPlural("clusterresourcequotas")
            .withScope("Namespaced")
            .withVersion("v1")
            .build();

    // Load from Yaml
    Map<String, Object> clusterResourceQuota = client.customResource(context)
            .load(CustomResourceCreateDemoTypeless.class.getResourceAsStream("/clusterquota-cr.yml"));
    // Create Custom Resource
    client.customResource(context).create("default", clusterResourceQuota);
} catch (IOException e) {
    e.printStackTrace();
}

您还可以查看这些博客,了解typedtypeless 方法的详细信息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 2021-09-04
  • 2019-03-14
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多