Skip to content

Commit

Permalink
Merge branch 'master' into fix-issue-575
Browse files Browse the repository at this point in the history
Signed-off-by: Shivanshu Gupta <[email protected]>
  • Loading branch information
ShivanshuGupta07 authored Sep 23, 2024
2 parents 8ffa249 + 4abc81b commit 8377d5a
Show file tree
Hide file tree
Showing 20 changed files with 668 additions and 100 deletions.
1 change: 0 additions & 1 deletion config/provider/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func (l *InMem) GetKey(key string) string {
func (l *InMem) GetObject(key string, result interface{}) error {
l.mutex.Lock()
defer l.mutex.Unlock()
//return utils.Unmarshal(l.store[key], result)
return json.Unmarshal([]byte(l.store[key]), result)
}

Expand Down
77 changes: 77 additions & 0 deletions converter/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package converter

import (
"bytes"

"github.com/layer5io/meshkit/models/patterns"
"github.com/layer5io/meshkit/utils"
"github.com/meshery/schemas/models/v1beta1/component"
"github.com/meshery/schemas/models/v1beta1/pattern"
"gopkg.in/yaml.v3"
)

type K8sConverter struct{}

func (k *K8sConverter) Convert(patternFile string) (string, error) {
pattern, err := patterns.GetPatternFormat(patternFile)
if err != nil {
return "", err
}
return NewK8sManifestsFromPatternfile(pattern)
}

func NewK8sManifestsFromPatternfile(patternFile *pattern.PatternFile) (string, error) {

buf := bytes.NewBufferString("")

enc := yaml.NewEncoder(buf)
for _, comp := range patternFile.Components {
err := enc.Encode(CreateK8sResourceStructure(comp))
if err != nil {
return "", err
}
}
return buf.String(), nil
}

func CreateK8sResourceStructure(comp *component.ComponentDefinition) map[string]interface{} {
annotations := map[string]interface{}{}
labels := map[string]interface{}{}

_confMetadata, ok := comp.Configuration["metadata"]
if ok {
confMetadata, err := utils.Cast[map[string]interface{}](_confMetadata)
if err == nil {

_annotations, ok := confMetadata["annotations"]
if ok {
annotations, _ = utils.Cast[map[string]interface{}](_annotations)
}

_label, ok := confMetadata["labels"]

if ok {
labels, _ = utils.Cast[map[string]interface{}](_label)
}
}
}

component := map[string]interface{}{
"apiVersion": comp.Component.Version,
"kind": comp.Component.Kind,
"metadata": map[string]interface{}{
"name": comp.DisplayName,
"annotations": annotations,
"labels": labels,
},
}

for k, v := range comp.Configuration {
if k == "apiVersion" || k == "kind" || k == "metadata" {
continue
}

component[k] = v
}
return component
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/google/uuid v1.6.0
github.com/kubernetes/kompose v1.31.1
github.com/layer5io/meshery-operator v0.7.0
github.com/meshery/schemas v0.7.20
github.com/meshery/schemas v0.7.31
github.com/nats-io/nats.go v1.31.0
github.com/open-policy-agent/opa v0.67.1
github.com/opencontainers/image-spec v1.1.0
Expand Down Expand Up @@ -193,7 +193,7 @@ require (
github.com/novln/docker-parser v1.0.0 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/openshift/api v0.0.0-20200803131051-87466835fcc0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
Expand Down Expand Up @@ -233,7 +233,7 @@ require (
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/meshery/kompose v1.0.1 h1:lg8B/pkLh6762jeFsQATD8UJZZwXZf/aviC3/dzw78A=
github.com/meshery/kompose v1.0.1/go.mod h1:TWhWTEMbJBUzENf4JTEtBmZRFm/r8n0nS6v4/nSD2vA=
github.com/meshery/schemas v0.7.20 h1:1v5+PKjwq/jq0/dayVNJVDbsrh8GXI5QmDYvEBPDdac=
github.com/meshery/schemas v0.7.20/go.mod h1:UfiO+zm92yLkaJP0aroNwVnjuozoh793AWDXrKDYmT0=
github.com/meshery/schemas v0.7.31 h1:FfVR+oErAiiEomt6sTZI5uKhoyU26QXawT6UDZHbthI=
github.com/meshery/schemas v0.7.31/go.mod h1:UfiO+zm92yLkaJP0aroNwVnjuozoh793AWDXrKDYmT0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
Expand Down Expand Up @@ -707,8 +707,8 @@ github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2sz
github.com/opencontainers/runc v0.0.0-20161109192122-51371867a01c/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
github.com/opencontainers/runc v0.0.0-20190115041553-12f6a991201f/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
github.com/opencontainers/runc v1.1.12 h1:BOIssBaW1La0/qbNZHXOOa71dZfZEQOzW7dqQf3phss=
github.com/opencontainers/runc v1.1.12/go.mod h1:S+lQwSfncpBha7XTy/5lBwWgm5+y5Ma/O44Ekby9FK8=
github.com/opencontainers/runc v1.1.14 h1:rgSuzbmgz5DUJjeSnw337TxDbRuqjs6iqQck/2weR6w=
github.com/opencontainers/runc v1.1.14/go.mod h1:E4C2z+7BxR7GHXp0hAY53mek+x49X1LjPNeMTfRGvOA=
github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs=
github.com/openshift/api v0.0.0-20200803131051-87466835fcc0 h1:ngLoHyAD7dNUzZY6cBA+X/DWIRLT56n6PjdN9+hqdvs=
Expand Down Expand Up @@ -927,8 +927,8 @@ golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek=
golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
18 changes: 18 additions & 0 deletions models/converter/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package converter

import (
"github.com/layer5io/meshkit/converter"
)

type ConvertFormat interface {
Convert(string) (string, error)
}

func NewFormatConverter(format DesignFormat) (ConvertFormat, error) {
switch format {
case K8sManifest:
return &converter.K8sConverter{}, nil
default:
return nil, ErrUnknownFormat(format)
}
}
15 changes: 15 additions & 0 deletions models/converter/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package converter

import (
"fmt"

"github.com/layer5io/meshkit/errors"
)

const (
ErrUnknownFormatCode = "meshkit-11245"
)

func ErrUnknownFormat(format DesignFormat) error {
return errors.New(ErrUnknownFormatCode, errors.Alert, []string{fmt.Sprintf("\"%s\" format is not supported", format)}, []string{fmt.Sprintf("Failed to export design in \"%s\" format", format)}, []string{"The format is not supported by the current version of Meshery server"}, []string{"Make sure to export design in one of the supported format"})
}
10 changes: 10 additions & 0 deletions models/converter/formats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package converter

type DesignFormat string

const (
HelmChart DesignFormat = "Helm Chart"
DockerCompose DesignFormat = "Docker Compose"
K8sManifest DesignFormat = "Kubernetes Manifest"
Design DesignFormat = "Design"
)
4 changes: 2 additions & 2 deletions models/meshmodel/registry/v1beta1/model_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (mf *ModelFilter) Get(db *database.Handler) ([]entity.Entity, int64, int, e

if mf.Greedy {
if mf.Id != "" {
finder = finder.First("model_dbs.id = ?", mf.Id)
finder = finder.Where("model_dbs.id = ?", mf.Id)
}
if mf.Name != "" && mf.DisplayName != "" {
finder = finder.Where("model_dbs.name LIKE ? OR model_dbs.display_name LIKE ?", "%"+mf.Name+"%", "%"+mf.DisplayName+"%")
Expand Down Expand Up @@ -182,7 +182,7 @@ func (mf *ModelFilter) Get(db *database.Handler) ([]entity.Entity, int64, int, e
if includeComponents {
var components []component.ComponentDefinition
finder := db.Model(&component.ComponentDefinition{}).
Select("component_definition_dbs.id, component_definition_dbs.component, component_definition_dbs.display_name, component_definition_dbs.metadata, component_definition_dbs.schema_version, component_definition_dbs.version,component_definition_dbs.styles").
Select("component_definition_dbs.id, component_definition_dbs.component, component_definition_dbs.display_name, component_definition_dbs.metadata, component_definition_dbs.schema_version, component_definition_dbs.version,component_definition_dbs.styles,component_definition_dbs.capabilities").
Where("component_definition_dbs.model_id = ?", _modelDB.Id)
if err := finder.Scan(&components).Error; err != nil {
return nil, 0, 0, err
Expand Down
Loading

0 comments on commit 8377d5a

Please sign in to comment.