From aedcb7f5911dfe8da92a26c70b013d3f7c8a983a Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 19 Sep 2024 15:13:14 +0530 Subject: [PATCH 1/2] add support to export in k8s format Signed-off-by: MUzairS15 --- converter/k8s.go | 77 +++++++++++++++++++++++++++++++++++ models/converter/converter.go | 18 ++++++++ models/converter/error.go | 15 +++++++ models/converter/formats.go | 10 +++++ 4 files changed, 120 insertions(+) create mode 100644 converter/k8s.go create mode 100644 models/converter/converter.go create mode 100644 models/converter/error.go create mode 100644 models/converter/formats.go diff --git a/converter/k8s.go b/converter/k8s.go new file mode 100644 index 00000000..7d87c3fb --- /dev/null +++ b/converter/k8s.go @@ -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 +} diff --git a/models/converter/converter.go b/models/converter/converter.go new file mode 100644 index 00000000..3fda332e --- /dev/null +++ b/models/converter/converter.go @@ -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) + } +} diff --git a/models/converter/error.go b/models/converter/error.go new file mode 100644 index 00000000..6bc9e788 --- /dev/null +++ b/models/converter/error.go @@ -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"}) +} diff --git a/models/converter/formats.go b/models/converter/formats.go new file mode 100644 index 00000000..45c1f090 --- /dev/null +++ b/models/converter/formats.go @@ -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" +) \ No newline at end of file From 1bb16d72568f083634676fece1bad9b4b23c4f59 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 19 Sep 2024 15:17:55 +0530 Subject: [PATCH 2/2] fix lint issue Signed-off-by: MUzairS15 --- config/provider/inmem.go | 3 ++- utils/kubernetes/crd.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/config/provider/inmem.go b/config/provider/inmem.go index 753c0064..0c56f7b5 100644 --- a/config/provider/inmem.go +++ b/config/provider/inmem.go @@ -18,6 +18,7 @@ import ( "sync" "github.com/layer5io/meshkit/config" + "github.com/layer5io/meshkit/encoding" "github.com/layer5io/meshkit/utils" ) @@ -54,7 +55,7 @@ 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 encoding.Unmarshal([]byte(l.store[key]), result) } // SetObject sets an object value for the key diff --git a/utils/kubernetes/crd.go b/utils/kubernetes/crd.go index b42cfbc9..0525a624 100644 --- a/utils/kubernetes/crd.go +++ b/utils/kubernetes/crd.go @@ -3,7 +3,7 @@ package kubernetes import ( "context" - "github.com/layer5io/meshkit/utils" + "github.com/layer5io/meshkit/encoding" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/rest" ) @@ -35,7 +35,7 @@ func GetAllCustomResourcesInCluster(ctx context.Context, client rest.Interface) } var xcrd CRD gvks := []*schema.GroupVersionResource{} - err = utils.Unmarshal(string(crdresult), &xcrd) + err = encoding.Unmarshal(crdresult, &xcrd) if err != nil { return nil, err }