Skip to content

Commit 097eb76

Browse files
authored
Merge branch 'master' into import-schema
2 parents 12ec8f6 + a8d2a6d commit 097eb76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1165
-313
lines changed

config/provider/inmem.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"sync"
1919

2020
"github.com/layer5io/meshkit/config"
21+
"github.com/layer5io/meshkit/encoding"
2122
"github.com/layer5io/meshkit/utils"
2223
)
2324

@@ -54,7 +55,7 @@ func (l *InMem) GetKey(key string) string {
5455
func (l *InMem) GetObject(key string, result interface{}) error {
5556
l.mutex.Lock()
5657
defer l.mutex.Unlock()
57-
return utils.Unmarshal(l.store[key], result)
58+
return encoding.Unmarshal([]byte(l.store[key]), result)
5859
}
5960

6061
// SetObject sets an object value for the key

converter/k8s.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package converter
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/layer5io/meshkit/models/patterns"
7+
"github.com/layer5io/meshkit/utils"
8+
"github.com/meshery/schemas/models/v1beta1/component"
9+
"github.com/meshery/schemas/models/v1beta1/pattern"
10+
"gopkg.in/yaml.v3"
11+
)
12+
13+
type K8sConverter struct{}
14+
15+
func (k *K8sConverter) Convert(patternFile string) (string, error) {
16+
pattern, err := patterns.GetPatternFormat(patternFile)
17+
if err != nil {
18+
return "", err
19+
}
20+
21+
patterns.ProcessAnnotations(pattern)
22+
return NewK8sManifestsFromPatternfile(pattern)
23+
}
24+
25+
func NewK8sManifestsFromPatternfile(patternFile *pattern.PatternFile) (string, error) {
26+
27+
buf := bytes.NewBufferString("")
28+
29+
enc := yaml.NewEncoder(buf)
30+
for _, comp := range patternFile.Components {
31+
err := enc.Encode(CreateK8sResourceStructure(comp))
32+
if err != nil {
33+
return "", err
34+
}
35+
}
36+
return buf.String(), nil
37+
}
38+
39+
func CreateK8sResourceStructure(comp *component.ComponentDefinition) map[string]interface{} {
40+
annotations := map[string]interface{}{}
41+
labels := map[string]interface{}{}
42+
43+
_confMetadata, ok := comp.Configuration["metadata"]
44+
if ok {
45+
confMetadata, err := utils.Cast[map[string]interface{}](_confMetadata)
46+
if err == nil {
47+
48+
_annotations, ok := confMetadata["annotations"]
49+
if ok {
50+
annotations, _ = utils.Cast[map[string]interface{}](_annotations)
51+
}
52+
53+
_label, ok := confMetadata["labels"]
54+
55+
if ok {
56+
labels, _ = utils.Cast[map[string]interface{}](_label)
57+
}
58+
}
59+
}
60+
61+
component := map[string]interface{}{
62+
"apiVersion": comp.Component.Version,
63+
"kind": comp.Component.Kind,
64+
"metadata": map[string]interface{}{
65+
"name": comp.DisplayName,
66+
"annotations": annotations,
67+
"labels": labels,
68+
},
69+
}
70+
71+
for k, v := range comp.Configuration {
72+
if k == "apiVersion" || k == "kind" || k == "metadata" {
73+
continue
74+
}
75+
76+
component[k] = v
77+
}
78+
return component
79+
}

encoding/encoding.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package encoding
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/layer5io/meshkit/utils"
7+
"gopkg.in/yaml.v2"
8+
)
9+
10+
// Unmarshal parses the JSON/YAML data and stores the result in the value pointed to by out
11+
func Unmarshal(data []byte, out interface{}) error {
12+
err := unmarshalJSON(data, out)
13+
if err != nil {
14+
err = unmarshalYAML(data, out)
15+
if err != nil {
16+
return err
17+
}
18+
}
19+
return nil
20+
}
21+
22+
func unmarshalYAML(data []byte, result interface{}) error {
23+
err := yaml.Unmarshal(data, result)
24+
if err != nil {
25+
return ErrDecodeYaml(err)
26+
}
27+
return nil
28+
}
29+
30+
func unmarshalJSON(data []byte, result interface{}) error {
31+
32+
err := json.Unmarshal(data, result)
33+
if err != nil {
34+
if e, ok := err.(*json.SyntaxError); ok {
35+
return ErrUnmarshalSyntax(err, e.Offset)
36+
}
37+
if e, ok := err.(*json.UnmarshalTypeError); ok {
38+
return ErrUnmarshalType(err, e.Value)
39+
}
40+
if e, ok := err.(*json.UnsupportedTypeError); ok {
41+
return ErrUnmarshalUnsupportedType(err, e.Type)
42+
}
43+
if e, ok := err.(*json.UnsupportedValueError); ok {
44+
return ErrUnmarshalUnsupportedValue(err, e.Value)
45+
}
46+
if e, ok := err.(*json.InvalidUnmarshalError); ok {
47+
return ErrUnmarshalInvalid(err, e.Type)
48+
}
49+
return ErrUnmarshal(err)
50+
}
51+
return nil
52+
}
53+
54+
func Marshal(in interface{}) ([]byte, error) {
55+
result, err := json.Marshal(in)
56+
if err != nil {
57+
result, err = yaml.Marshal(in)
58+
if err != nil {
59+
return nil, utils.ErrMarshal(err)
60+
}
61+
}
62+
63+
return result, nil
64+
}

encoding/error.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package encoding
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
7+
"github.com/layer5io/meshkit/errors"
8+
)
9+
10+
const (
11+
ErrDecodeYamlCode = ""
12+
ErrUnmarshalCode = ""
13+
ErrUnmarshalInvalidCode = ""
14+
ErrUnmarshalSyntaxCode = ""
15+
ErrUnmarshalTypeCode = ""
16+
ErrUnmarshalUnsupportedTypeCode = ""
17+
ErrUnmarshalUnsupportedValueCode = ""
18+
)
19+
20+
// ErrDecodeYaml is the error when the yaml unmarshal fails
21+
func ErrDecodeYaml(err error) error {
22+
return errors.New(ErrDecodeYamlCode, errors.Alert, []string{"Error occurred while decoding YAML"}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid YAML object"})
23+
}
24+
25+
func ErrUnmarshal(err error) error {
26+
return errors.New(ErrUnmarshalCode, errors.Alert, []string{"Unmarshal unknown error: "}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
27+
}
28+
29+
func ErrUnmarshalInvalid(err error, typ reflect.Type) error {
30+
return errors.New(ErrUnmarshalInvalidCode, errors.Alert, []string{"Unmarshal invalid error for type: ", typ.String()}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
31+
}
32+
33+
func ErrUnmarshalSyntax(err error, offset int64) error {
34+
return errors.New(ErrUnmarshalSyntaxCode, errors.Alert, []string{"Unmarshal syntax error at offest: ", strconv.Itoa(int(offset))}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
35+
}
36+
37+
func ErrUnmarshalType(err error, value string) error {
38+
return errors.New(ErrUnmarshalTypeCode, errors.Alert, []string{"Unmarshal type error at key: %s. Error: %s", value}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
39+
}
40+
41+
func ErrUnmarshalUnsupportedType(err error, typ reflect.Type) error {
42+
return errors.New(ErrUnmarshalUnsupportedTypeCode, errors.Alert, []string{"Unmarshal unsupported type error at key: ", typ.String()}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
43+
}
44+
45+
func ErrUnmarshalUnsupportedValue(err error, value reflect.Value) error {
46+
return errors.New(ErrUnmarshalUnsupportedValueCode, errors.Alert, []string{"Unmarshal unsupported value error at key: ", value.String()}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
47+
}

generators/artifacthub/package.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"github.com/layer5io/meshkit/utils"
1111
"github.com/layer5io/meshkit/utils/component"
1212
"github.com/layer5io/meshkit/utils/manifests"
13-
_component "github.com/meshery/schemas/models/v1beta1/component"
1413
"github.com/meshery/schemas/models/v1beta1/category"
14+
_component "github.com/meshery/schemas/models/v1beta1/component"
15+
"github.com/meshery/schemas/models/v1beta1/model"
1516
"gopkg.in/yaml.v2"
1617
)
1718

@@ -53,7 +54,10 @@ func (pkg AhPackage) GenerateComponents() ([]_component.ComponentDefinition, err
5354
if err != nil {
5455
continue
5556
}
56-
57+
if comp.Model.Metadata == nil {
58+
comp.Model.Metadata = &model.ModelDefinition_Metadata{}
59+
}
60+
5761
if comp.Model.Metadata.AdditionalProperties == nil {
5862
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
5963
}

generators/github/git_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func fileInterceptor(br *bufio.Writer) walker.FileInterceptor {
105105
// Add more calrifying commment and entry inside docs.
106106
func dirInterceptor(br *bufio.Writer) walker.DirInterceptor {
107107
return func(d walker.Directory) error {
108-
err := helm.ConvertToK8sManifest(d.Path, br)
108+
err := helm.ConvertToK8sManifest(d.Path, "", br)
109109
if err != nil {
110110
return err
111111
}

generators/github/package.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/layer5io/meshkit/utils/manifests"
1010
"github.com/meshery/schemas/models/v1beta1/category"
1111
_component "github.com/meshery/schemas/models/v1beta1/component"
12+
"github.com/meshery/schemas/models/v1beta1/model"
1213
)
1314

1415
type GitHubPackage struct {
@@ -40,6 +41,9 @@ func (gp GitHubPackage) GenerateComponents() ([]_component.ComponentDefinition,
4041
if err != nil {
4142
continue
4243
}
44+
if comp.Model.Metadata == nil {
45+
comp.Model.Metadata = &model.ModelDefinition_Metadata{}
46+
}
4347
if comp.Model.Metadata.AdditionalProperties == nil {
4448
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
4549
}

generators/github/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func ProcessContent(w io.Writer, downloadDirPath, downloadfilePath string) error
7777
}
7878

7979
err = utils.ProcessContent(downloadDirPath, func(path string) error {
80-
err = helm.ConvertToK8sManifest(path, w)
80+
err = helm.ConvertToK8sManifest(path, "", w)
8181
if err != nil {
8282
return err
8383
}

generators/models/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package models
22

3-
import "github.com/meshery/schemas/models/v1beta1/component"
3+
import "github.com/meshery/schemas/models/v1beta1/component"
44

55
// anything that can be validated is a Validator
66
type Validator interface {

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/google/uuid v1.6.0
2525
github.com/kubernetes/kompose v1.31.1
2626
github.com/layer5io/meshery-operator v0.7.0
27-
github.com/meshery/schemas v0.7.17
27+
github.com/meshery/schemas v0.7.31
2828
github.com/nats-io/nats.go v1.31.0
2929
github.com/open-policy-agent/opa v0.67.1
3030
github.com/opencontainers/image-spec v1.1.0
@@ -193,7 +193,7 @@ require (
193193
github.com/novln/docker-parser v1.0.0 // indirect
194194
github.com/oapi-codegen/runtime v1.1.1 // indirect
195195
github.com/opencontainers/go-digest v1.0.0 // indirect
196-
github.com/opencontainers/runc v1.1.12 // indirect
196+
github.com/opencontainers/runc v1.1.14 // indirect
197197
github.com/openshift/api v0.0.0-20200803131051-87466835fcc0 // indirect
198198
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
199199
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
@@ -233,7 +233,7 @@ require (
233233
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
234234
go.uber.org/multierr v1.11.0 // indirect
235235
golang.org/x/crypto v0.25.0 // indirect
236-
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
236+
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
237237
golang.org/x/net v0.27.0 // indirect
238238
golang.org/x/sync v0.7.0 // indirect
239239
golang.org/x/sys v0.22.0 // indirect

0 commit comments

Comments
 (0)