-
Notifications
You must be signed in to change notification settings - Fork 174
feat(jsonnet): add support for importing all files in a directory #1374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5700ef5
af47706
692ef56
dfba8d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,18 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"strings" | ||
|
||
jsonnet "github.com/google/go-jsonnet" | ||
"github.com/google/go-jsonnet/ast" | ||
"github.com/grafana/tanka/pkg/helm" | ||
"github.com/grafana/tanka/pkg/kustomize" | ||
"github.com/pkg/errors" | ||
"github.com/rs/zerolog/log" | ||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
|
@@ -42,6 +46,14 @@ func Funcs() []*jsonnet.NativeFunction { | |
} | ||
} | ||
|
||
// VMFuncs returns a slice of functions similar to Funcs but are passed the jsonnet VM | ||
// for in-line evaluation | ||
func VMFuncs(vm *jsonnet.VM) []*jsonnet.NativeFunction { | ||
return []*jsonnet.NativeFunction{ | ||
importFiles(vm), | ||
} | ||
} | ||
|
||
// parseJSON wraps `json.Unmarshal` to convert a json string into a dict | ||
func parseJSON() *jsonnet.NativeFunction { | ||
return &jsonnet.NativeFunction{ | ||
|
@@ -178,3 +190,87 @@ func regexSubst() *jsonnet.NativeFunction { | |
}, | ||
} | ||
} | ||
|
||
type importFilesOpts struct { | ||
CalledFrom string `json:"calledFrom"` | ||
Exclude []string `json:"exclude"` | ||
Extension string `json:"extension"` | ||
} | ||
|
||
func parseImportOpts(data interface{}) (*importFilesOpts, error) { | ||
c, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// default extension to `.libsonnet` | ||
opts := importFilesOpts{ | ||
Extension: ".libsonnet", | ||
} | ||
if err := json.Unmarshal(c, &opts); err != nil { | ||
return nil, err | ||
} | ||
if opts.CalledFrom == "" { | ||
return nil, fmt.Errorf("importFiles: `opts.calledFrom` is unset or empty\nTanka needs this to find your directory.") | ||
} | ||
// Make sure calledFrom is inside the current working directory | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return nil, fmt.Errorf("importFiles: failed to get current working directory: %s", err) | ||
} | ||
calledFromAbs, err := filepath.Abs(opts.CalledFrom) | ||
if err != nil { | ||
return nil, fmt.Errorf("importFiles: failed to get absolute path to `opts.calledFrom`: %s", err) | ||
} | ||
if !strings.HasPrefix(calledFromAbs, cwd) { | ||
return nil, fmt.Errorf("importFiles: `opts.calledFrom` must be a subdirectory of the current working directory: %s", cwd) | ||
} | ||
return &opts, nil | ||
} | ||
|
||
// importFiles imports and evaluates all matching jsonnet files in the given relative directory | ||
func importFiles(vm *jsonnet.VM) *jsonnet.NativeFunction { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: Please add documentation about this new function to https://github.com/grafana/tanka/blob/main/docs/src/content/docs/jsonnet/native.md 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zerok I am happy to do this, but held off because I don't see documents for That being said, I am happy to also add documentation for the raw native function here, but I would like to get some clarity on whether this will be accepted before I do the work to add it. |
||
return &jsonnet.NativeFunction{ | ||
Name: "importFiles", | ||
Params: ast.Identifiers{"directory", "opts"}, | ||
Func: func(data []interface{}) (interface{}, error) { | ||
dir, ok := data[0].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("first argument 'directory' must be of 'string' type, got '%T' instead", data[0]) | ||
} | ||
opts, err := parseImportOpts(data[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dirPath := filepath.Join(filepath.Dir(opts.CalledFrom), dir) | ||
imports := make(map[string]interface{}) | ||
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() || !strings.HasSuffix(info.Name(), opts.Extension) { | ||
return nil | ||
} | ||
if slices.Contains(opts.Exclude, info.Name()) { | ||
return nil | ||
} | ||
log.Debug().Msgf("importFiles: parsing file %s", info.Name()) | ||
resultStr, err := vm.EvaluateFile(path) | ||
if err != nil { | ||
return fmt.Errorf("importFiles: failed to evaluate %s: %s", path, err) | ||
} | ||
var result interface{} | ||
err = json.Unmarshal([]byte(resultStr), &result) | ||
if err != nil { | ||
return err | ||
} | ||
imports[info.Name()] = result | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return imports, nil | ||
}, | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.