gpt4 book ai didi

java - 无法使用 io.fabric8 客户端创建 velero.io/v1-BackupStorageLocation

转载 作者:行者123 更新时间:2023-12-02 06:16:17 24 4
gpt4 key购买 nike

我使用 io.fabric8.kubernetes-client,版本 4.1.1。我正在尝试使用 io.fabric 库加载 yaml。

---
apiVersion: "velero.io/v1"
kind: "BackupStorageLocation"
spec:
providerType: "aws"
objectStorage:
bucket: "test"
config:
region: "us-west-1"
metadata:
annotations: {}
name: "default"
namespace: "velero"
labels: {}
String content = "---\n" + 
"apiVersion: \"velero.io/v1\"\n" +
"kind: \"BackupStorageLocation\"\n" +
"spec:\n" +
" providerType: \"aws\"\n" +
" objectStorage:\n" +
" bucket: \"test\"\n" +
" config:\n" +
" region: \"us-west-1\"\n" +
"metadata:\n" +
" annotations: {}\n" +
" name: \"default\"\n" +
" namespace: \"velero\"\n" +
" labels: {}\n" +
"";
List<HasMetadata> list = client.load(new ByteArrayInputStream(content.trim().getBytes())).createOrReplace();

出现以下异常:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No resource type found for:velero.io/v1#BackupStorageLocation
at [Source: (BufferedInputStream); line: 14, column: 13]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:271)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:1718)
at io.fabric8.kubernetes.internal.KubernetesDeserializer.deserialize(KubernetesDeserializer.java:78)
at io.fabric8.kubernetes.internal.KubernetesDeserializer.deserialize(KubernetesDeserializer.java:32)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1611)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1188)
at io.fabric8.kubernetes.client.utils.Serialization.unmarshal(Serialization.java:129)

最佳答案

Kubernetes 客户端最近添加了对创建自定义资源的支持。您可以加载自定义资源定义,但为了提供使用自定义资源,您需要为该自定义资源提供模型,请参阅旧的 CrdExample 。但现在它的类型减少了(无需向客户端提供任何自定义资源模型(Pojos)。您现在可以创建这样的自定义资源(我使用的是 4.2.2 bdw):

对于名为animal的自定义资源定义:

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: animals.jungle.example.com
spec:
group: jungle.example.com
versions:
- name: v1
served: true
storage: true
scope: Namespaced
names:
plural: animals
singular: animals
kind: Animal
shortNames:
- al

为了创建自定义资源,您需要向客户端提供 CustomResourceDefinitionContext。下面的示例显示了通过 InputStream 或原始字符串进行创建。更多详情请参见this .

    CustomResourceDefinitionContext customResourceDefinitionContext = new CustomResourceDefinitionContext.Builder()
.withName("animals.jungle.example.com")
.withGroup("jungle.example.com")
.withVersion("v1")
.withPlural("animals")
.withScope("Namespaced")
.build();

// Create via file
Map<String, Object> object = client.customResource(customResourceDefinitionContext).create(currentNamespace, getClass().getResourceAsStream("/test-rawcustomresource.yml"));

// Create via raw json/yaml
String rawJsonCustomResourceObj = "{\"apiVersion\":\"jungle.example.com/v1\"," +
"\"kind\":\"Animal\",\"metadata\": {\"name\": \"walrus\"}," +
"\"spec\": {\"image\": \"my-awesome-walrus-image\"}}";
object = client.customResource(customResourceDefinitionContext).create(currentNamespace, rawJsonCustomResourceObj);

关于java - 无法使用 io.fabric8 客户端创建 velero.io/v1-BackupStorageLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55869890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com