我使用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)
答案 0 :(得分:0)
最近在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);