Skip to content
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

Jsonnet: Move the JsonnetImplentation option upwards #917

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/tk/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func exportCmd() *cli.Command {
Format: *format,
Extension: *extension,
Opts: tanka.Opts{
JsonnetOpts: getJsonnetOpts(),
Filters: filters,
Name: vars.name,
JsonnetImplementation: vars.jsonnetImplementation,
JsonnetOpts: getJsonnetOpts(),
Filters: filters,
Name: vars.name,
},
Selector: getLabelSelector(),
Parallelism: *parallel,
Expand Down
14 changes: 7 additions & 7 deletions cmd/tk/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import (
)

type workflowFlagVars struct {
name string
targets []string
name string
targets []string
jsonnetImplementation string
}

func workflowFlags(fs *pflag.FlagSet) *workflowFlagVars {
v := workflowFlagVars{}
fs.StringVar(&v.name, "name", "", "string that only a single inline environment contains in its name")
fs.StringSliceVarP(&v.targets, "target", "t", nil, "Regex filter on '<kind>/<name>'. See https://tanka.dev/output-filtering")
fs.StringVar(&v.jsonnetImplementation, "jsonnet-implementation", "go", "Use `go` to use native go-jsonnet implementation and `binary:<path>` to delegate evaluation to a binary (with the same API as the regular `jsonnet` binary)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is binary merged?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, no, but it'll definitely be before a release so 🤷

return &v
}

Expand Down Expand Up @@ -58,14 +60,12 @@ func labelSelectorFlag(fs *pflag.FlagSet) func() labels.Selector {
func jsonnetFlags(fs *pflag.FlagSet) func() tanka.JsonnetOpts {
getExtCode, getTLACode := cliCodeParser(fs)
maxStack := fs.Int("max-stack", 0, "Jsonnet VM max stack. The default value is the value set in the go-jsonnet library. Increase this if you get: max stack frames exceeded")
jsonnetImplementation := fs.String("jsonnet-implementation", "go", "Only go is supported for now.")

return func() tanka.JsonnetOpts {
return tanka.JsonnetOpts{
MaxStack: *maxStack,
ExtCode: getExtCode(),
TLACode: getTLACode(),
JsonnetImplementation: *jsonnetImplementation,
MaxStack: *maxStack,
ExtCode: getExtCode(),
TLACode: getTLACode(),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/tk/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func statusCmd() *cli.Command {

cmd.Run = func(cmd *cli.Command, args []string) error {
status, err := tanka.Status(args[0], tanka.Opts{
JsonnetOpts: getJsonnetOpts(),
Name: vars.name,
JsonnetImplementation: vars.jsonnetImplementation,
JsonnetOpts: getJsonnetOpts(),
Name: vars.name,
})
if err != nil {
return err
Expand Down
10 changes: 7 additions & 3 deletions cmd/tk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func applyCmd() *cli.Command {
opts.Filters = filters
opts.JsonnetOpts = getJsonnetOpts()
opts.Name = vars.name
opts.JsonnetImplementation = vars.jsonnetImplementation

return tanka.Apply(args[0], opts)
}
Expand Down Expand Up @@ -200,6 +201,7 @@ func deleteCmd() *cli.Command {
opts.Filters = filters
opts.JsonnetOpts = getJsonnetOpts()
opts.Name = vars.name
opts.JsonnetImplementation = vars.jsonnetImplementation

return tanka.Delete(args[0], opts)
}
Expand Down Expand Up @@ -238,6 +240,7 @@ func diffCmd() *cli.Command {
opts.Filters = filters
opts.JsonnetOpts = getJsonnetOpts()
opts.Name = vars.name
opts.JsonnetImplementation = vars.jsonnetImplementation

changes, err := tanka.Diff(args[0], opts)
if err != nil {
Expand Down Expand Up @@ -291,9 +294,10 @@ Otherwise run tk show --dangerous-allow-redirect to bypass this check.`)
}

pretty, err := tanka.Show(args[0], tanka.Opts{
JsonnetOpts: getJsonnetOpts(),
Filters: filters,
Name: vars.name,
JsonnetOpts: getJsonnetOpts(),
Filters: filters,
Name: vars.name,
JsonnetImplementation: vars.jsonnetImplementation,
})

if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions pkg/jsonnet/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ func (i *InjectedCode) Set(key, value string) {

// Opts are additional properties for the Jsonnet VM
type Opts struct {
JsonnetImplementation string
MaxStack int
ExtCode InjectedCode
TLACode InjectedCode
ImportPaths []string
EvalScript string
CachePath string
MaxStack int
ExtCode InjectedCode
TLACode InjectedCode
ImportPaths []string
EvalScript string
CachePath string

CachePathRegexes []*regexp.Regexp
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/tanka/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func List(path string, opts Opts) ([]*v1alpha1.Environment, error) {
}

func getJsonnetImplementation(path string, opts Opts) (types.JsonnetImplementation, error) {
switch opts.JsonnetOpts.JsonnetImplementation {
switch opts.JsonnetImplementation {
case "go", "":
return &goimpl.JsonnetGoImplementation{
Path: path,
}, nil
default:
return nil, fmt.Errorf("unknown jsonnet implementation: %s", opts.JsonnetOpts.JsonnetImplementation)
return nil, fmt.Errorf("unknown jsonnet implementation: %s", opts.JsonnetImplementation)
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/tanka/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ func parallelLoadEnvironments(envs []*v1alpha1.Environment, opts parallelOpts) (
for _, env := range envs {
o := opts.Opts

if o.JsonnetImplementation == "" {
o.JsonnetImplementation = env.Spec.ExportJsonnetImplementation
}

// TODO: This is required because the map[string]string in here is not
// concurrency-safe. Instead of putting this burden on the caller, find
// a way to handle this inside the jsonnet package. A possible way would
// be to make the jsonnet package less general, more tightly coupling it
// to Tanka workflow thus being able to handle such cases
o.JsonnetOpts = o.JsonnetOpts.Clone()

if o.JsonnetOpts.JsonnetImplementation == "" {
o.JsonnetOpts.JsonnetImplementation = env.Spec.ExportJsonnetImplementation
}

o.Name = env.Metadata.Name
path := env.Metadata.Namespace
rootDir, err := jpath.FindRoot(path)
Expand Down
1 change: 1 addition & 0 deletions pkg/tanka/tanka.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type JsonnetOpts = jsonnet.Opts
// Opts specify general, optional properties that apply to all actions
type Opts struct {
JsonnetOpts
JsonnetImplementation string

// Filters are used to optionally select a subset of the resources
Filters process.Matchers
Expand Down