generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 93
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 #97 from Revolyssup/Revolyssup/generatingComponents
Generating components
- Loading branch information
Showing
6 changed files
with
432 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"helm.sh/helm/v3/pkg/chart/loader" | ||
) | ||
|
||
func GetManifestsFromHelm(url string) (string, error) { | ||
chartLocation, err := fetchHelmChart(url) | ||
if err != nil { | ||
return "", ErrApplyHelmChart(err) | ||
} | ||
|
||
chart, err := loader.Load(chartLocation) | ||
if err != nil { | ||
return "", ErrApplyHelmChart(err) | ||
} | ||
var manifests string = "" | ||
for _, crdobject := range chart.CRDObjects() { | ||
manifests += "\n---\n" | ||
manifests += string(crdobject.File.Data) | ||
} | ||
return manifests, nil | ||
} |
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,32 @@ | ||
package manifests | ||
|
||
// Type of resource | ||
const ( | ||
// service mesh resource | ||
SERVICE_MESH = iota | ||
// native Kubernetes resource | ||
K8s | ||
// native Meshery resource | ||
MESHERY | ||
) | ||
|
||
// Json Paths | ||
type JsonPath []string | ||
type Component struct { | ||
Schemas []string | ||
Definitions []string | ||
} | ||
|
||
type Config struct { | ||
Name string // Name of the service mesh,or k8 or meshery | ||
MeshVersion string | ||
Filter CrdFilter //json path filters | ||
} | ||
|
||
type CrdFilter struct { | ||
RootFilter JsonPath //This would be the first filter to get a modified yaml | ||
NameFilter JsonPath // This will be the json path passed in order to get the names of crds | ||
GroupFilter JsonPath //This will specify the path to get to group name | ||
VersionFilter JsonPath //This will specify the path to get to version name. [Version should have a name field] | ||
SpecFilter JsonPath //This will specify the path to get spec | ||
} |
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,29 @@ | ||
package manifests | ||
|
||
import "github.com/layer5io/meshkit/errors" | ||
|
||
const ( | ||
ErrGetCrdNamesCode = "1001" | ||
ErrGetSchemasCode = "1002" | ||
ErrGetAPIVersionCode = "1003" | ||
ErrGetAPIGroupCode = "1004" | ||
ErrPopulatingYamlCode = "1005" | ||
) | ||
|
||
func ErrGetCrdNames(err error) error { | ||
return errors.New(ErrGetCrdNamesCode, errors.Alert, []string{"Error getting crd names"}, []string{err.Error()}, []string{"Could not execute kubeopenapi-jsonschema correctly"}, []string{"Make sure the binary is valid and correct", "Make sure the filter passed is correct"}) | ||
} | ||
|
||
func ErrGetSchemas(err error) error { | ||
return errors.New(ErrGetSchemasCode, errors.Alert, []string{"Error getting schemas"}, []string{err.Error()}, []string{"Schemas Json could not be produced from given crd."}, []string{"Make sure the filter passed is correct"}) | ||
} | ||
func ErrGetAPIVersion(err error) error { | ||
return errors.New(ErrGetAPIVersionCode, errors.Alert, []string{"Error getting api version"}, []string{err.Error()}, []string{"Api version could not be parsed"}, []string{"Make sure the filter passed is correct"}) | ||
} | ||
func ErrGetAPIGroup(err error) error { | ||
return errors.New(ErrGetAPIGroupCode, errors.Alert, []string{"Error getting api group"}, []string{err.Error()}, []string{"Api group could not be parsed"}, []string{"Make sure the filter passed is correct"}) | ||
} | ||
|
||
func ErrPopulatingYaml(err error) error { | ||
return errors.New(ErrPopulatingYamlCode, errors.Alert, []string{"Error populating yaml"}, []string{err.Error()}, []string{"Yaml could not be populated with the returned manifests"}, []string{""}) | ||
} |
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,93 @@ | ||
package manifests | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/layer5io/meshkit/utils" | ||
) | ||
|
||
func generateComponents(manifest string, resource int, cfg Config) (*Component, error) { | ||
wd := filepath.Join(utils.GetHome(), ".meshery", "bin") | ||
fmt.Println("Looking for kubeopenapi-jsonschema in ", wd) | ||
var binPath string = filepath.Join(wd, "kubeopenapi-jsonschema") | ||
var url string = "https://github.com/layer5io/kubeopenapi-jsonschema/releases/download/v0.1.0/kubeopenapi-jsonschema" | ||
switch runtime.GOOS { | ||
case "windows": | ||
binPath += ".exe" | ||
url += ".exe" | ||
case "darwin": | ||
binPath += "-darwin" | ||
url += "-darwin" | ||
|
||
} | ||
//download the binary on that path if it doesn't exist | ||
if _, err := os.Stat(binPath); os.IsNotExist(err) { | ||
fmt.Println("Downloading kubeopenapi-jsonschema at " + binPath + "...") | ||
errdownload := utils.DownloadFile(binPath, url) | ||
if errdownload != nil { | ||
return nil, errdownload | ||
} | ||
fmt.Println("Download Completed") | ||
} | ||
|
||
//make the binary executable | ||
if err := os.Chmod(binPath, 0750); err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
out bytes.Buffer | ||
er bytes.Buffer | ||
) | ||
path := filepath.Join(wd, "test.yaml") | ||
err := populateTempyaml(manifest, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
filteroot := cfg.Filter.RootFilter //cfg.Filter.RootFilter | ||
err = filterYaml(path, filteroot, binPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := &Component{ | ||
Schemas: []string{}, | ||
Definitions: []string{}, | ||
} | ||
filter := cfg.Filter.NameFilter //cfg.Filter.Name | ||
filteroot = append(filteroot, "-o", "json", "--o-filter") | ||
filteroot = append(filteroot, filter...) | ||
getCrdsCmdArgs := append([]string{"--location", path, "-t", "yaml", "--filter"}, filteroot...) | ||
cmd := exec.Command(binPath, getCrdsCmdArgs...) | ||
//emptying buffers | ||
out.Reset() | ||
er.Reset() | ||
cmd.Stdout = &out | ||
cmd.Stderr = &er | ||
err = cmd.Run() | ||
if err != nil { | ||
return nil, ErrGetCrdNames(err) | ||
} | ||
crds := getCrdnames(out.String()) | ||
for _, crd := range crds { | ||
out, err := getDefinitions(crd, resource, cfg, path, binPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.Definitions = append(c.Definitions, out) | ||
out, err = getSchema(crd, path, binPath, cfg) | ||
if err != nil { | ||
return nil, ErrGetSchemas(err) | ||
} | ||
c.Schemas = append(c.Schemas, out) | ||
} | ||
err = deleteFile(path) | ||
if err != nil { | ||
fmt.Println("error in cleanup" + err.Error()) | ||
} | ||
return c, nil | ||
} |
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,30 @@ | ||
package manifests | ||
|
||
import ( | ||
"github.com/layer5io/meshkit/utils" | ||
k8s "github.com/layer5io/meshkit/utils/kubernetes" | ||
) | ||
|
||
func GetFromManifest(url string, resource int, cfg Config) (*Component, error) { | ||
manifest, err := utils.ReadFileSource(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
comp, err := generateComponents(manifest, resource, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return comp, nil | ||
} | ||
|
||
func GetFromHelm(url string, resource int, cfg Config) (*Component, error) { | ||
manifest, err := k8s.GetManifestsFromHelm(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
comp, err := generateComponents(manifest, resource, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return comp, nil | ||
} |
Oops, something went wrong.