Failed to create the velero.io/v1-BackupStorageLocation using io.fabric8 client

4/26/2019

I using io.fabric8.kubernetes-client, version 4.1.1. I'm trying to load the yaml using io.fabric library.

---
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();

Getting the following exception:

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)
-- Arundathi G Vardhan
fabric8
java
kubernetes

1 Answer

4/28/2019

Support for creating custom resources has been added in Kubernetes client recently. You could load Custom Resource Definitions but in order to provide use custom resources you needed to provide model for that custom resource, see old CrdExample. But it has now been made less typed(without providing any custom resource model(Pojos) to client. You can now create custom resources like this(I'm on 4.2.2 bdw):

For a custom resource definition named 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

In order to create custom resources you need to provide CustomResourceDefinitionContext to client. The below example shows creation via InputStream or raw string. For more details, see 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);
-- Rohan Kumar
Source: StackOverflow