Skip to content

Commit

Permalink
Revert "support generation of model from openapi schemas"
Browse files Browse the repository at this point in the history
  • Loading branch information
leecalcote authored Sep 30, 2024
1 parent 167771c commit d889a68
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 285 deletions.
15 changes: 0 additions & 15 deletions encoding/convert.go

This file was deleted.

8 changes: 0 additions & 8 deletions generators/artifacthub/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ func (pkg AhPackage) GetVersion() string {
return pkg.Version
}

func (pkg AhPackage) GetSourceURL() string {
return pkg.ChartUrl
}

func (pkg AhPackage) GetName() string {
return pkg.Name
}

func (pkg AhPackage) GenerateComponents() ([]_component.ComponentDefinition, error) {
components := make([]_component.ComponentDefinition, 0)
// TODO: Move this to the configuration
Expand Down
59 changes: 19 additions & 40 deletions generators/github/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/component"
"github.com/layer5io/meshkit/utils/kubernetes"
"github.com/layer5io/meshkit/utils/manifests"
"github.com/meshery/schemas/models/v1beta1/category"
_component "github.com/meshery/schemas/models/v1beta1/component"
Expand All @@ -26,14 +25,6 @@ func (gp GitHubPackage) GetVersion() string {
return gp.version
}

func (gp GitHubPackage) GetSourceURL() string {
return gp.SourceURL
}

func (gp GitHubPackage) GetName() string {
return gp.Name
}

func (gp GitHubPackage) GenerateComponents() ([]_component.ComponentDefinition, error) {
components := make([]_component.ComponentDefinition, 0)

Expand All @@ -43,40 +34,28 @@ func (gp GitHubPackage) GenerateComponents() ([]_component.ComponentDefinition,
}

manifestBytes := bytes.Split(data, []byte("\n---\n"))
errs := []error{}

for _, crd := range manifestBytes {
isCrd := kubernetes.IsCRD(string(crd))
if !isCrd {
crds, errs := component.FilterCRDs(manifestBytes)

comps, err := component.GenerateFromOpenAPI(string(crd), gp)
if err != nil {
errs = append(errs, component.ErrGetSchema(err))
continue
}
components = append(components, comps...)
} else {
comp, err := component.Generate(string(crd))
if err != nil {
continue
}
if comp.Model.Metadata == nil {
comp.Model.Metadata = &model.ModelDefinition_Metadata{}
}
if comp.Model.Metadata.AdditionalProperties == nil {
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
}

comp.Model.Metadata.AdditionalProperties["source_uri"] = gp.SourceURL
comp.Model.Version = gp.version
comp.Model.Name = gp.Name
comp.Model.Category = category.CategoryDefinition{
Name: "",
}
comp.Model.DisplayName = manifests.FormatToReadableString(comp.Model.Name)
components = append(components, comp)
for _, crd := range crds {
comp, err := component.Generate(crd)
if err != nil {
continue
}
if comp.Model.Metadata == nil {
comp.Model.Metadata = &model.ModelDefinition_Metadata{}
}
if comp.Model.Metadata.AdditionalProperties == nil {
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
}

comp.Model.Metadata.AdditionalProperties["source_uri"] = gp.SourceURL
comp.Model.Version = gp.version
comp.Model.Name = gp.Name
comp.Model.Category = category.CategoryDefinition{
Name: "",
}
comp.Model.DisplayName = manifests.FormatToReadableString(comp.Model.Name)
components = append(components, comp)
}

return components, utils.CombineErrors(errs, "\n")
Expand Down
2 changes: 0 additions & 2 deletions generators/models/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ type Validator interface {
type Package interface {
GenerateComponents() ([]component.ComponentDefinition, error)
GetVersion() string
GetSourceURL() string
GetName() string
}

// Supports pulling packages from Artifact Hub and other sources like Docker Hub.
Expand Down
15 changes: 2 additions & 13 deletions utils/component/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,17 @@ var DefaultPathConfig2 = CuePathConfig{
SpecPath: "spec.validation.openAPIV3Schema",
}

var OpenAPISpecPathConfig = CuePathConfig{
NamePath: `x-kubernetes-group-version-kind"[0].kind`,
IdentifierPath: "spec.names.kind",
VersionPath: `"x-kubernetes-group-version-kind"[0].version`,
GroupPath: `"x-kubernetes-group-version-kind"[0].group`,
ScopePath: "spec.scope",
SpecPath: "spec.versions[0].schema.openAPIV3Schema",
PropertiesPath: "properties",
}

var Configs = []CuePathConfig{DefaultPathConfig, DefaultPathConfig2}

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

cmp.Metadata = component.ComponentDefinition_Metadata{}
crdCue, err := utils.YamlToCue(resource)
crdCue, err := utils.YamlToCue(crd)
if err != nil {
return cmp, err
}

var schema string
for _, cfg := range Configs {
schema, err = getSchema(crdCue, cfg)
Expand Down
172 changes: 0 additions & 172 deletions utils/component/openapi_generator.go

This file was deleted.

16 changes: 13 additions & 3 deletions utils/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -80,10 +81,19 @@ func FilterCRDs(manifests [][]byte) ([]string, []error) {
var errs []error
var filteredManifests []string
for _, m := range manifests {
isCrd := kubernetes.IsCRD(string(m))
if isCrd {
filteredManifests = append(filteredManifests, string(m))

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

isCrd := kubernetes.IsCRD(crd)
if !isCrd {
continue
}
filteredManifests = append(filteredManifests, string(m))
}
return filteredManifests, errs
}
9 changes: 1 addition & 8 deletions utils/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"regexp"
"strings"

"github.com/layer5io/meshkit/encoding"
"github.com/layer5io/meshkit/utils"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -109,13 +108,7 @@ func writeToFile(w io.Writer, path string) error {
if err != nil {
return utils.ErrReadFile(err, path)
}

byt, err := encoding.ToYaml(data)
if err != nil {
return utils.ErrWriteFile(err, path)
}

_, err = w.Write(byt)
_, err = w.Write(data)
if err != nil {
return utils.ErrWriteFile(err, path)
}
Expand Down
Loading

0 comments on commit d889a68

Please sign in to comment.