AmitKumarDas / decide

Apache License 2.0
0 stars 1 forks source link

translate any json and/or yaml as k8s resource #13

Open AmitKumarDas opened 6 years ago

AmitKumarDas commented 6 years ago

ref: https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/crds/crds.go

import (
  "encoding/json"

  "github.com/ghodss/yaml"
  "k8s.io/apimachinery/pkg/runtime/schema"
  "k8s.io/kube-openapi/pkg/common"
)
package crds

// Annotation is to mark a field as annotations.
// "x-kubernetes-annotation": ""
const Annotation = "x-kubernetes-annotation"

// LabelSelector is to mark a field as LabelSelector
// "x-kubernetes-label-selector": ""
const LabelSelector = "x-kubernetes-label-selector"

// Identity is to mark a field as Identity
// "x-kubernetes-identity": ""
const Identity = "x-kubernetes-identity"

// Version marks the type version of an object ref field
// "x-kubernetes-object-ref-api-version": <apiVersion name>
const Version = "x-kubernetes-object-ref-api-version"

// Kind marks the type name of an object ref field
// "x-kubernetes-object-ref-kind": <kind name>
const Kind = "x-kubernetes-object-ref-kind"

// NameKey marks the field key that refers to an object of an object ref field
// "x-kubernetes-object-ref-name-key": "name"
// default is "name"
const NameKey = "x-kubernetes-object-ref-name-key"
    var types map[string]common.OpenAPIDefinition
    if content[0] == '{' {
        err = json.Unmarshal(content, &types)
        if err != nil {
            return nil, err
        }
    } else {
        err = yaml.Unmarshal(content, &types)
        if err != nil {
            return nil, err
        }
    }
// getCRDs get all CRD types
func getCRDs(types map[string]common.OpenAPIDefinition) map[string]schema.GroupVersionKind 
{
    crds := map[string]schema.GroupVersionKind{}

    for typename, t := range types {
        properties := t.Schema.SchemaProps.Properties
        _, foundKind := properties["kind"]
        _, foundAPIVersion := properties["apiVersion"]
        _, foundMetadata := properties["metadata"]
        if foundKind && foundAPIVersion && foundMetadata {
                  // TODO: Get Group and Version for CRD from the openAPI definition once
          // "x-kubernetes-group-version-kind" is available in CRD
          kind := strings.Split(typename, ".")[len(strings.Split(typename, "."))-1]
          crds[typename] = schema.GroupVersionKind{Kind: kind}
        }
    }
    return crds
}
types map[string]common.OpenAPIDefinition

for propname, property := range types[atype].Schema.SchemaProps.Properties {
    _, annotate := property.Extensions.GetString(Annotation)
    if annotate {...}
    _, label := property.Extensions.GetString(LabelSelector)
    if label {...}
    _, identity := property.Extensions.GetString(Identity)
    if identity {...}
    version, ok := property.Extensions.GetString(Version)
    if ok {
        kind, ok := property.Extensions.GetString(Kind)
        if ok {
            nameKey, ok := property.Extensions.GetString(NameKey)
            if !ok {...}
        }
    }

    if property.Ref.GetURL() != nil {...}
}