Skip to content

Instantly share code, notes, and snippets.

@Duologic
Last active July 15, 2021 07:41
Show Gist options
  • Save Duologic/7c1f1be09b76d7aca8c735eb39dab8de to your computer and use it in GitHub Desktop.
Save Duologic/7c1f1be09b76d7aca8c735eb39dab8de to your computer and use it in GitHub Desktop.
local environments = [
{
apiVersion: 'tanka.dev/v1alpha1',
kind: 'Environment',
metadata: {
labels: {
team: 'infra',
},
name: 'default',
namespace: 'environments/default/main.jsonnet',
},
spec: {
apiServer: 'https://127.0.0.1',
expectVersions: {},
injectLabels: true,
namespace: 'default',
resourceDefaults: {},
},
},
{
apiVersion: 'tanka.dev/v1alpha1',
kind: 'Environment',
metadata: {
labels: {
app: 'other',
},
name: 'default',
namespace: 'environments/other/main.jsonnet',
},
spec: {
apiServer: 'https://127.0.0.1',
expectVersions: {},
injectLabels: true,
namespace: 'other',
resourceDefaults: {},
},
},
];
function(envs=environments)
local tests = std.prune(
{
'Team label is missing or empty': [
(
if !std.objectHas(env.metadata.labels, 'team')
|| std.length(env.metadata.labels.team) == 0
then env.metadata.name
else null
)
for env in envs
],
'Environment names not unique':
local count = std.foldr(
function(env, k) k + (
if std.objectHas(k, env.metadata.name)
then { [env.metadata.name]+: 1 }
else { [env.metadata.name]: 1 }
),
envs,
{}
);
std.foldr(
function(i, k) k + (
if count[i] > 1
then { [i]: count[i] }
else {}
),
std.objectFields(count),
{}
),
}
);
if tests != {}
then error std.manifestJsonEx(tests, ' ')
else {}
@Duologic
Copy link
Author

Duologic commented Jul 15, 2021

The output looks like this:

jsonnet test.jsonnet
RUNTIME ERROR: {
  "Environment names not unique": {
    "default": 2
  },
  "Team label is missing or empty": [
    "default"
  ]
}
        test.jsonnet:77:8-45    function <anonymous>
        Top-level function call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment