Skip to content

Commit

Permalink
Merge pull request #590 from MUzairS15/k8s-export
Browse files Browse the repository at this point in the history
Add support to export design in k8s format
  • Loading branch information
Mohd Uzair authored Sep 19, 2024
2 parents 7404ba8 + 1bb16d7 commit 7033f62
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/provider/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sync"

"github.com/layer5io/meshkit/config"
"github.com/layer5io/meshkit/encoding"
"github.com/layer5io/meshkit/utils"
)

Expand Down Expand Up @@ -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
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
}
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 utils/kubernetes/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7033f62

Please sign in to comment.