我来自 Fabric8 团队。 Fabric8 Kubernetes Client 支持两种方式创建自定义对象:
- 类型化 API(需要提供自定义资源模型 (POJO))
- 无类型 API(将自定义资源作为普通 HashMap 处理)
类型化 API:
假设您已经拥有ClusterResourceQuota 和ClusterResourceQuotaList 的 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();
}
您还可以查看这些博客,了解typed 和typeless 方法的详细信息。