generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #590 from MUzairS15/k8s-export
Add support to export design in k8s format
- Loading branch information
Showing
6 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters