【问题标题】:Failed to create the velero.io/v1-BackupStorageLocation using io.fabric8 client使用 io.fabric8 客户端创建 velero.io/v1-BackupStorageLocation 失败
【发布时间】:2019-04-29 01:22:00
【问题描述】:

我使用 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中存在对应的CRD吗?
  • @Vasily Angapov 是的
  • @ArundathiGVardhan:嗨,你能解决这个问题吗?

标签: java kubernetes fabric8


【解决方案1】:

最近在 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2022-11-10
    • 2021-07-25
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    相关资源
    最近更新 更多