Skip to content

Commit

Permalink
finalize changes
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <[email protected]>
  • Loading branch information
MUzairS15 committed Sep 30, 2024
1 parent ad5c265 commit 45521cd
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions utils/component/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 @@ -63,46 +62,36 @@ func Generate(resource string) (component.ComponentDefinition, error) {
cmp.SchemaVersion = v1beta1.ComponentSchemaVersion

cmp.Metadata = component.ComponentDefinition_Metadata{}
isCRD := kubernetes.IsCRD(resource)

cueValue, err := cueValueFromResource(resource, isCRD)
crdCue, err := utils.YamlToCue(resource)
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 {
cfg.SpecPath = specPath
schema, err = getSchema(cueValue, cfg)
schema, err = getSchema(crdCue, cfg)
if err == nil {
break
}
}
cmp.Component.Schema = schema
name, err := extractCueValueFromPath(cueValue, DefaultPathConfig.NamePath)
name, err := extractCueValueFromPath(crdCue, DefaultPathConfig.NamePath)
if err != nil {
return cmp, err
}
version, err := extractCueValueFromPath(cueValue, DefaultPathConfig.VersionPath)
version, err := extractCueValueFromPath(crdCue, DefaultPathConfig.VersionPath)
if err != nil {
return cmp, err
}
group, err := extractCueValueFromPath(cueValue, DefaultPathConfig.GroupPath)
group, err := extractCueValueFromPath(crdCue, 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(cueValue, DefaultPathConfig.ScopePath)
scope, _ := extractCueValueFromPath(crdCue, DefaultPathConfig.ScopePath)
if scope == "Cluster" {
cmp.Metadata.IsNamespaced = false
} else if scope == "Namespaced" {
Expand All @@ -120,14 +109,6 @@ func Generate(resource 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

0 comments on commit 45521cd

Please sign in to comment.