Skip to content

Commit

Permalink
Merge pull request #157 from Revolyssup/fix
Browse files Browse the repository at this point in the history
Use context for running kubeopenapi binary
  • Loading branch information
leecalcote authored Feb 28, 2022
2 parents 9d4dc7c + 4acb5a5 commit 373d55a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
11 changes: 6 additions & 5 deletions utils/manifests/generateComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifests

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
Expand All @@ -12,7 +13,7 @@ import (
)

// GenerateComponents generates components given manifest(yaml/json) ,resource type, and additional configuration
func GenerateComponents(manifest string, resource int, cfg Config) (*Component, error) {
func GenerateComponents(ctx context.Context, manifest string, resource int, cfg Config) (*Component, error) {
var inputFormat = "yaml"
if cfg.Filter.IsJson {
inputFormat = "json"
Expand Down Expand Up @@ -61,15 +62,15 @@ func GenerateComponents(manifest string, resource int, cfg Config) (*Component,
er bytes.Buffer
)
filteroot := cfg.Filter.RootFilter
err = filterYaml(path, filteroot, binPath, inputFormat)
err = filterYaml(ctx, path, filteroot, binPath, inputFormat)
if err != nil {
return nil, err
}
filter := cfg.Filter.NameFilter
filteroot = append(filteroot, "-o", "json", "--o-filter")
filteroot = append(filteroot, filter...)
getCrdsCmdArgs := append([]string{"--location", path, "-t", inputFormat, "--filter"}, filteroot...)
cmd := exec.Command(binPath, getCrdsCmdArgs...)
cmd := exec.CommandContext(ctx, binPath, getCrdsCmdArgs...)
//emptying buffers
out.Reset()
er.Reset()
Expand All @@ -85,11 +86,11 @@ func GenerateComponents(manifest string, resource int, cfg Config) (*Component,
}

for _, crd := range crds {
outDef, err := getDefinitions(crd, resource, cfg, path, binPath)
outDef, err := getDefinitions(crd, resource, cfg, path, binPath, ctx)
if err != nil {
return nil, err
}
outSchema, err := getSchema(crd, path, binPath, cfg)
outSchema, err := getSchema(crd, path, binPath, cfg, ctx)
if err != nil {
return nil, ErrGetSchemas(err)
}
Expand Down
10 changes: 6 additions & 4 deletions utils/manifests/getComponents.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package manifests

import (
"context"

"github.com/layer5io/meshkit/utils"
k8s "github.com/layer5io/meshkit/utils/kubernetes"
)

func GetFromManifest(url string, resource int, cfg Config) (*Component, error) {
func GetFromManifest(ctx context.Context, 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)
comp, err := GenerateComponents(ctx, manifest, resource, cfg)
if err != nil {
return nil, err
}
return comp, nil
}

func GetFromHelm(url string, resource int, cfg Config) (*Component, error) {
func GetFromHelm(ctx context.Context, 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)
comp, err := GenerateComponents(ctx, manifest, resource, cfg)
if err != nil {
return nil, err
}
Expand Down
25 changes: 13 additions & 12 deletions utils/manifests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifests

import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
Expand All @@ -15,19 +16,19 @@ import (

var templateExpression *regexp.Regexp

func getDefinitions(crd string, resource int, cfg Config, filepath string, binPath string) (string, error) {
func getDefinitions(crd string, resource int, cfg Config, filepath string, binPath string, ctx context.Context) (string, error) {
//the default input format is "yaml"
inputFormat := "yaml"
if cfg.Filter.IsJson {
inputFormat = "json"
}
var def v1alpha1.WorkloadDefinition
definitionRef := strings.ToLower(crd) + ".meshery.layer5.io"
apiVersion, err := getApiVersion(binPath, filepath, crd, inputFormat, cfg)
apiVersion, err := getApiVersion(binPath, filepath, crd, inputFormat, cfg, ctx)
if err != nil {
return "", ErrGetAPIVersion(err)
}
apiGroup, err := getApiGrp(binPath, filepath, crd, inputFormat, cfg)
apiGroup, err := getApiGrp(ctx, binPath, filepath, crd, inputFormat, cfg)
if err != nil {
return "", ErrGetAPIGroup(err)
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func getDefinitions(crd string, resource int, cfg Config, filepath string, binPa
return string(out), nil
}

func getSchema(crd string, fp string, binPath string, cfg Config) (string, error) {
func getSchema(crd string, fp string, binPath string, cfg Config, ctx context.Context) (string, error) {
//few checks to avoid index out of bound panic
if len(cfg.Filter.ItrSpecFilter) == 0 {
return "", ErrAbsentFilter(errors.New("Empty ItrSpecFilter"))
Expand All @@ -93,7 +94,7 @@ func getSchema(crd string, fp string, binPath string, cfg Config) (string, error
}
getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", itr, "--o-filter"}
getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.SpecFilter...)
schemaCmd := exec.Command(binPath, getAPIvCmdArgs...)
schemaCmd := exec.CommandContext(ctx, binPath, getAPIvCmdArgs...)
schemaCmd.Stdout = &out
schemaCmd.Stderr = &er
err := schemaCmd.Run()
Expand All @@ -111,7 +112,7 @@ func getSchema(crd string, fp string, binPath string, cfg Config) (string, error
if len(cfg.Filter.ResolveFilter) != 0 {
getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.ResolveFilter...)
}
schemaCmd := exec.Command(binPath, getAPIvCmdArgs...)
schemaCmd := exec.CommandContext(ctx, binPath, getAPIvCmdArgs...)
schemaCmd.Stdout = &out
schemaCmd.Stderr = &er
err := schemaCmd.Run()
Expand Down Expand Up @@ -191,7 +192,7 @@ func getCrdnames(s string) []string {
return crds[1 : len(crds)-2] // first and last characters are "[" and "]" respectively
}

func getApiVersion(binPath string, fp string, crd string, inputFormat string, cfg Config) (string, error) {
func getApiVersion(binPath string, fp string, crd string, inputFormat string, cfg Config, ctx context.Context) (string, error) {
//few checks to avoid index out of bound panic
if len(cfg.Filter.ItrFilter) == 0 {
return "", ErrAbsentFilter(errors.New("Empty ItrFilter"))
Expand All @@ -208,7 +209,7 @@ func getApiVersion(binPath string, fp string, crd string, inputFormat string, cf
getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", itr, "--o-filter"}
getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.VersionFilter...)

schemaCmd := exec.Command(binPath, getAPIvCmdArgs...)
schemaCmd := exec.CommandContext(ctx, binPath, getAPIvCmdArgs...)
schemaCmd.Stdout = &out
schemaCmd.Stderr = &er
err := schemaCmd.Run()
Expand All @@ -230,7 +231,7 @@ func getApiVersion(binPath string, fp string, crd string, inputFormat string, cf
s := strings.ReplaceAll(string(output), "\"", "")
return s, nil
}
func getApiGrp(binPath string, fp string, crd string, inputFormat string, cfg Config) (string, error) {
func getApiGrp(ctx context.Context, binPath string, fp string, crd string, inputFormat string, cfg Config) (string, error) {
//few checks to avoid index out of bound panic
if len(cfg.Filter.ItrFilter) == 0 {
return "", ErrAbsentFilter(errors.New("Empty ItrFilter"))
Expand All @@ -245,7 +246,7 @@ func getApiGrp(binPath string, fp string, crd string, inputFormat string, cfg Co
}
getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", itr, "--o-filter"}
getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.GroupFilter...)
schemaCmd := exec.Command(binPath, getAPIvCmdArgs...)
schemaCmd := exec.CommandContext(ctx, binPath, getAPIvCmdArgs...)
schemaCmd.Stdout = &out
schemaCmd.Stderr = &er

Expand All @@ -269,14 +270,14 @@ func getApiGrp(binPath string, fp string, crd string, inputFormat string, cfg Co
return s, nil
}

func filterYaml(yamlPath string, filter []string, binPath string, inputFormat string) error {
func filterYaml(ctx context.Context, yamlPath string, filter []string, binPath string, inputFormat string) error {
var (
out bytes.Buffer
er bytes.Buffer
)
filter = append(filter, "-o", "yaml")
getCrdsCmdArgs := append([]string{"--location", yamlPath, "-t", inputFormat, "--filter"}, filter...)
cmd := exec.Command(binPath, getCrdsCmdArgs...)
cmd := exec.CommandContext(ctx, binPath, getCrdsCmdArgs...)
//emptying buffers
out.Reset()
er.Reset()
Expand Down

0 comments on commit 373d55a

Please sign in to comment.