-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide the Jsonnet Go implementation behind an interface
Tanka currently evals jsonnet using the Go native code. However, some other implementations have come up in the past years that could be worth using (ex: https://github.com/CertainLach/jrsonnet, which is much faster) In this PR is the first step: I create an interface where all the jsonnet eval code happens. The Go Jsonnet implementation is now hidden behind this interface. The setting can either be passed as a global flag or as an env spec attribute to be used when exporting (`spec.exportJsonnetImplementation`)
- Loading branch information
1 parent
253c0f2
commit c2bd64e
Showing
14 changed files
with
139 additions
and
71 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
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,26 @@ | ||
package goimpl | ||
|
||
import ( | ||
"github.com/google/go-jsonnet" | ||
"github.com/grafana/tanka/pkg/jsonnet/implementation/types" | ||
) | ||
|
||
type JsonnetGoVM struct { | ||
vm *jsonnet.VM | ||
} | ||
|
||
func (vm *JsonnetGoVM) EvaluateAnonymousSnippet(filename, snippet string) (string, error) { | ||
return vm.vm.EvaluateAnonymousSnippet(filename, snippet) | ||
} | ||
|
||
func (vm *JsonnetGoVM) EvaluateFile(filename string) (string, error) { | ||
return vm.vm.EvaluateFile(filename) | ||
} | ||
|
||
type JsonnetGoImplementation struct{} | ||
|
||
func (i *JsonnetGoImplementation) MakeVM(importPaths []string, extCode map[string]string, tlaCode map[string]string, maxStack int) types.JsonnetVM { | ||
return &JsonnetGoVM{ | ||
vm: MakeRawVM(importPaths, extCode, tlaCode, maxStack), | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
pkg/jsonnet/tk.libsonnet.go → ...net/implementation/goimpl/tk.libsonnet.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package jsonnet | ||
package goimpl | ||
|
||
import jsonnet "github.com/google/go-jsonnet" | ||
|
||
|
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,33 @@ | ||
package goimpl | ||
|
||
import ( | ||
"github.com/google/go-jsonnet" | ||
"github.com/grafana/tanka/pkg/jsonnet/native" | ||
) | ||
|
||
// MakeRawVM returns a Jsonnet VM with some extensions of Tanka, including: | ||
// - extended importer | ||
// - extCode and tlaCode applied | ||
// - native functions registered | ||
// This is exposed because Go is used for advanced use cases, like finding transitive imports or linting. | ||
func MakeRawVM(importPaths []string, extCode map[string]string, tlaCode map[string]string, maxStack int) *jsonnet.VM { | ||
vm := jsonnet.MakeVM() | ||
vm.Importer(newExtendedImporter(importPaths)) | ||
|
||
for k, v := range extCode { | ||
vm.ExtCode(k, v) | ||
} | ||
for k, v := range tlaCode { | ||
vm.TLACode(k, v) | ||
} | ||
|
||
for _, nf := range native.Funcs() { | ||
vm.NativeFunction(nf) | ||
} | ||
|
||
if maxStack > 0 { | ||
vm.MaxStack = maxStack | ||
} | ||
|
||
return vm | ||
} |
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,16 @@ | ||
package implementation | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/grafana/tanka/pkg/jsonnet/implementation/goimpl" | ||
"github.com/grafana/tanka/pkg/jsonnet/implementation/types" | ||
) | ||
|
||
func Get(name string) (types.JsonnetImplementation, error) { | ||
if name == "go" || name == "" { | ||
return &goimpl.JsonnetGoImplementation{}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("unknown jsonnet implementation: %s", name) | ||
} |
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 types | ||
|
||
type JsonnetVM interface { | ||
EvaluateAnonymousSnippet(filename, snippet string) (string, error) | ||
EvaluateFile(filename string) (string, error) | ||
} | ||
|
||
type JsonnetImplementation interface { | ||
MakeVM(importPaths []string, extCode map[string]string, tlaCode map[string]string, maxStack int) JsonnetVM | ||
} |
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
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
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