Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support generation of model from openapi schemas #582

Merged
merged 13 commits into from
Sep 30, 2024
34 changes: 27 additions & 7 deletions utils/component/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"cuelang.org/go/cue"
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/kubernetes"
"github.com/layer5io/meshkit/utils/manifests"
"github.com/meshery/schemas/models/v1beta1"
"github.com/meshery/schemas/models/v1beta1/component"
Expand Down Expand Up @@ -47,40 +48,51 @@ var DefaultPathConfig2 = CuePathConfig{

var Configs = []CuePathConfig{DefaultPathConfig, DefaultPathConfig2}

func Generate(crd string) (component.ComponentDefinition, error) {
func Generate(resource string) (component.ComponentDefinition, error) {
cmp := component.ComponentDefinition{}
cmp.SchemaVersion = v1beta1.ComponentSchemaVersion

cmp.Metadata = component.ComponentDefinition_Metadata{}
crdCue, err := utils.YamlToCue(crd)
isCRD := kubernetes.IsCRD(resource)

cueValue, err := cueValueFromResource(resource, isCRD)
if err != nil {
return cmp, err
}

var specPath string
if isCRD {
specPath = DefaultPathConfig.SpecPath
} else {
specPath = "components.schemas"
}

var schema string
for _, cfg := range Configs {
schema, err = getSchema(crdCue, cfg)
cfg.SpecPath = specPath
schema, err = getSchema(cueValue, cfg)
if err == nil {
break
}
}
cmp.Component.Schema = schema
name, err := extractCueValueFromPath(crdCue, DefaultPathConfig.NamePath)
name, err := extractCueValueFromPath(cueValue, DefaultPathConfig.NamePath)
if err != nil {
return cmp, err
}
version, err := extractCueValueFromPath(crdCue, DefaultPathConfig.VersionPath)
version, err := extractCueValueFromPath(cueValue, DefaultPathConfig.VersionPath)
if err != nil {
return cmp, err
}
group, err := extractCueValueFromPath(crdCue, DefaultPathConfig.GroupPath)
group, err := extractCueValueFromPath(cueValue, DefaultPathConfig.GroupPath)
if err != nil {
return cmp, err
}
// return component, err Ignore error if scope isn't found
if cmp.Metadata.AdditionalProperties == nil {
cmp.Metadata.AdditionalProperties = make(map[string]interface{})
}
scope, _ := extractCueValueFromPath(crdCue, DefaultPathConfig.ScopePath)
scope, _ := extractCueValueFromPath(cueValue, DefaultPathConfig.ScopePath)
if scope == "Cluster" {
cmp.Metadata.AdditionalProperties["isNamespaced"] = false
} else if scope == "Namespaced" {
Expand All @@ -98,6 +110,14 @@ func Generate(crd string) (component.ComponentDefinition, error) {
return cmp, nil
}

func cueValueFromResource(resource string, isCRD bool) (cue.Value, error) {
if isCRD {
return utils.YamlToCue(resource)
} else {
return utils.JsonToCue([]byte(resource))
}
}

/*
Find and modify specific schema properties.
1. Identify interesting properties by walking entire schema.
Expand Down
11 changes: 1 addition & 10 deletions utils/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/kubernetes"
"github.com/layer5io/meshkit/utils/manifests"
"gopkg.in/yaml.v2"
)

// Remove the fields which is either not required by end user (like status) or is prefilled by system (like apiVersion, kind and metadata)
Expand Down Expand Up @@ -81,15 +80,7 @@ func FilterCRDs(manifests [][]byte) ([]string, []error) {
var errs []error
var filteredManifests []string
for _, m := range manifests {

var crd map[string]interface{}
err := yaml.Unmarshal(m, &crd)
if err != nil {
errs = append(errs, err)
continue
}

isCrd := kubernetes.IsCRD(crd)
isCrd := kubernetes.IsCRD(string(m))
if !isCrd {
continue
}
Expand Down
20 changes: 16 additions & 4 deletions utils/kubernetes/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ func GetGVRForCustomResources(crd *CRDItem) *schema.GroupVersionResource {
}
}

func IsCRD(manifest map[string]interface{}) bool {
kind, ok := manifest["kind"].(string)
return ok && kind == "CustomResourceDefinition"
}
func IsCRD(manifest string) bool {
cueValue, err := utils.YamlToCue(manifest)
if err!= nil {
return false
}
kind, err := utils.Lookup(cueValue, "kind")
if err!= nil {
return false
}
kindStr, err := kind.String()
if err != nil {
return false
}

return kindStr == "CustomResourceDefinition"
}
Loading