Skip to content

Instantly share code, notes, and snippets.

@Duologic
Last active June 7, 2022 00:26
Show Gist options
  • Save Duologic/d179134cf86bbcda7a59ac3cf000876e to your computer and use it in GitHub Desktop.
Save Duologic/d179134cf86bbcda7a59ac3cf000876e to your computer and use it in GitHub Desktop.
Pure jsonnet implementation of `tanka export` (POC)
{
local this = self,
local clusterWideKinds = [
'APIService',
'CertificateSigningRequest',
'ClusterRole',
'ClusterRoleBinding',
'ComponentStatus',
'CSIDriver',
'CSINode',
'CustomResourceDefinition',
'MutatingWebhookConfiguration',
'Namespace',
'Node',
'NodeMetrics',
'PersistentVolume',
'PodSecurityPolicy',
'PriorityClass',
'RuntimeClass',
'SelfSubjectAccessReview',
'SelfSubjectRulesReview',
'StorageClass',
'SubjectAccessReview',
'TokenReview',
'ValidatingWebhookConfiguration',
'VolumeAttachment',
],
local isNamespaced(object) =
if std.member(clusterWideKinds, object.kind)
|| (
'annotations' in object.metadata
&& 'tanka.dev/namespaced' in object.metadata.annotations
)
then false
else true,
local namespacedMixin(object, namespace) =
if isNamespaced(object)
then
if 'namespace' in object.metadata
then {}
else { metadata+: { namespace: namespace } }
else {},
getKubernetesObjects(object, env)::
if std.isObject(object)
then
// a Kubernetes object is characterized by having an apiVersion and Kind
if std.objectHas(object, 'apiVersion') && std.objectHas(object, 'kind')
then
local manifest =
object
+ namespacedMixin(object, env.spec.namespace);
local namespace =
if isNamespaced(object)
then env.spec.namespace
else '_cluster';
local key = std.join('/', [
env.metadata.labels.cluster_name,
namespace,
object.kind + '-' + object.metadata.name + '.yaml',
]);
{ [key]: manifest }
else
std.foldl(
function(acc, o)
acc + this.getKubernetesObjects(object[o], env),
std.objectFields(object),
{}
)
else if std.isArray(object)
then
std.map(
function(obj)
this.getKubernetesObjects(obj, env),
object
)
else object,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment