package main import ( "encoding/json" "fmt" "log" gy "github.com/ghodss/yaml" "github.com/pkg/errors" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" // "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" ) var crd = `apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: mayastorpools.openebs.io spec: group: openebs.io versions: - name: v1alpha1 served: true storage: true subresources: status: {} schema: openAPIV3Schema: type: object properties: apiVersion: type: string kind: type: string metadata: type: object spec: description: Specification of the mayastor pool. type: object required: - node - disks properties: node: description: Name of the k8s node where the storage pool is located. type: string disks: description: Disk devices (paths or URIs) that should be used for the pool. type: array items: type: string status: description: Status part updated by the pool controller. type: object properties: state: description: Pool state. type: string reason: description: Reason for the pool state value if applicable. type: string disks: description: Disk device URIs that are actually used for the pool. type: array items: type: string capacity: description: Capacity of the pool in bytes. type: integer format: int64 minimum: 0 used: description: How many bytes are used in the pool. type: integer format: int64 minimum: 0 additionalPrinterColumns: - name: Node type: string description: Node where the storage pool is located jsonPath: .spec.node - name: State type: string description: State of the storage pool jsonPath: .status.state - name: Age type: date jsonPath: .metadata.creationTimestamp scope: Namespaced names: kind: MayastorPool listKind: MayastorPoolList plural: mayastorpools singular: mayastorpool shortNames: ["msp"] ` type Data struct { Value float64 `json: value` } func main() { var unstructObj *unstructured.Unstructured err := gy.Unmarshal([]byte(crd), &unstructObj) if err != nil { fmt.Println("error during decoding:", err) if e, ok := err.(*json.SyntaxError); ok { log.Printf("syntax error at byte offset %d", e.Offset) } return } //fmt.Printf("Object: %+v", unstructObj) //var crdObj *v1beta1.CustomResourceDefinition var crdObj *v1.CustomResourceDefinition err = UnstructToTyped(unstructObj, &crdObj) if err != nil { fmt.Printf("Conversion failed %v\n", err) return } fmt.Println("Succeeded: %v", crdObj) } // UnstructToTyped transforms the provided unstruct instance // to target type func UnstructToTyped( src *unstructured.Unstructured, target interface{}, ) error { if src == nil || src.UnstructuredContent() == nil { return errors.Errorf( "Failed to transform unstruct to typed: Nil unstruct", ) } if target == nil { return errors.Errorf( "Failed to transform unstruct to typed: Nil target", ) } return runtime.DefaultUnstructuredConverter.FromUnstructured( src.UnstructuredContent(), target, ) }