Last active
July 15, 2021 07:41
-
-
Save Duologic/7c1f1be09b76d7aca8c735eb39dab8de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output looks like this: