Skip to content

Commit

Permalink
fix: mimic --tla-code behavior from jsonnet for functions in main.jso…
Browse files Browse the repository at this point in the history
…nnet in env discovery (#1251)

Mimic tla code handling from jsonnet
  • Loading branch information
sabeechen authored Feb 6, 2025
1 parent 0e930c6 commit 3065778
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/tanka/evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func evalJsonnet(path string, impl types.JsonnetImplementation, opts jsonnet.Opt

// evaluate Jsonnet
if opts.EvalScript != "" {
// Determine if the entrypoint is a function.
isFunction, err := jsonnet.Evaluate(path, impl, fmt.Sprintf("std.isFunction(import '%s')", entrypoint), opts)
if err != nil {
return "", fmt.Errorf("evaluating jsonnet in path '%s': %w", path, err)
}
var tla []string
for k := range opts.TLACode {
tla = append(tla, k+"="+k)
Expand All @@ -29,7 +34,7 @@ func evalJsonnet(path string, impl types.JsonnetImplementation, opts jsonnet.Opt
%s
`, entrypoint, opts.EvalScript)

if len(tla) != 0 {
if isFunction == "true\n" {
tlaJoin := strings.Join(tla, ", ")
evalScript = fmt.Sprintf(`
function(%s)
Expand Down
58 changes: 58 additions & 0 deletions pkg/tanka/evaluators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,61 @@ func TestEvalJsonnetWithExpression(t *testing.T) {
})
}
}

// An EvalScript with a top-level function containing only optional arguments
// should be evaluated as a function even if no TLAs are provided.
func TestEvalWithOptionalTlas(t *testing.T) {
opts := jsonnet.Opts{
EvalScript: "main.metadata.name",
}
json, err := evalJsonnet("testdata/cases/with-optional-tlas/main.jsonnet", jsonnetImpl, opts)
assert.NoError(t, err)
assert.Equal(t, `"bar-baz"`, strings.TrimSpace(json))
}

// An EvalScript with a top-level function containing should allow passing only
// a subset of the TLAs.
func TestEvalWithOptionalTlasSpecifiedArg2(t *testing.T) {
opts := jsonnet.Opts{
EvalScript: "main.metadata.name",
TLACode: jsonnet.InjectedCode{"baz": "'changed'"},
}
json, err := evalJsonnet("testdata/cases/with-optional-tlas/main.jsonnet", jsonnetImpl, opts)
assert.NoError(t, err)
assert.Equal(t, `"bar-changed"`, strings.TrimSpace(json))
}

// An EvalScript with a top-level function having no arguments should be
// evaluated as a function even if no TLAs are provided.
func TestEvalFunctionWithNoTlas(t *testing.T) {
opts := jsonnet.Opts{
EvalScript: "main.metadata.name",
}
json, err := evalJsonnet("testdata/cases/function-with-zero-params/main.jsonnet", jsonnetImpl, opts)
assert.NoError(t, err)
assert.Equal(t, `"inline"`, strings.TrimSpace(json))
}

// An EvalScript with a top-level function should return an understandable
// error message if an incorrect TLA is provided.
func TestInvalidTlaArg(t *testing.T) {
opts := jsonnet.Opts{
EvalScript: "main",
TLACode: jsonnet.InjectedCode{"foo": "'bar'"},
}
json, err := evalJsonnet("testdata/cases/function-with-zero-params/main.jsonnet", jsonnetImpl, opts)
assert.Contains(t, err.Error(), "function has no parameter foo")
assert.Equal(t, "", json)
}

// Providing a TLA to an EvalScript with a non-function top level mainfile
// should not return an error.
func TestTlaWithNonFunction(t *testing.T) {
opts := jsonnet.Opts{
EvalScript: "main",
TLACode: jsonnet.InjectedCode{"foo": "'bar'"},
}
json, err := evalJsonnet("testdata/cases/withenv/main.jsonnet", jsonnetImpl, opts)
assert.NoError(t, err)
assert.NotEmpty(t, json)
}
16 changes: 16 additions & 0 deletions pkg/tanka/testdata/cases/function-with-zero-params/main.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function() {
apiVersion: 'tanka.dev/v1alpha1',
kind: 'Environment',
metadata: {
name: 'inline',
},
spec: {
apiServer: 'https://localhost',
namespace: 'inline',
},
data: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: { name: 'config' },
},
}
16 changes: 16 additions & 0 deletions pkg/tanka/testdata/cases/with-optional-tlas/main.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function(bar='bar', baz='baz') {
apiVersion: 'tanka.dev/v1alpha1',
kind: 'Environment',
metadata: {
name: bar + '-' + baz,
},
spec: {
apiServer: 'https://localhost',
namespace: 'inline',
},
data: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: { name: 'config' },
},
}

0 comments on commit 3065778

Please sign in to comment.