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

Mimic --tla-code behavior from jsonnet for functions in main.jsonnet during envrionment discovery #1251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 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' },
},
}