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

feat(grafanaplane): expose GroupVersionKind tuples #21

Open
wants to merge 4 commits 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
77 changes: 69 additions & 8 deletions generator/configurations.libsonnet
Original file line number Diff line number Diff line change
@@ -1,21 +1,82 @@
local namespaced = import './namespaced.libsonnet';
local crossplane = import 'github.com/jsonnet-libs/crossplane-libsonnet/crossplane/1.17/main.libsonnet';

local configuration(key, version) =
local conf = crossplane.pkg.v1.configuration;
conf.new(key)
+ conf.spec.withPackage('ghcr.io/grafana/crossplane/' + key + ':' + version);

local groups =
local xrds =
std.map(
function(o) o.definition,
(import './namespaced.libsonnet'),
);

local crds =
std.filter(
function(crd) crd.spec.group != 'grafana.crossplane.io',
std.parseYaml(importstr './crds.yaml'),
);

local gvkXRDs =
std.flatMap(
function(definition) [
{
group: definition.spec.group,
version: v.name,
kind: definition.spec.claimNames.kind,
plural: definition.spec.claimNames.plural,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plural key is intended for consumption by the ksm library, see crdsonnet/kube-state-metrics-libsonnet#2

}
for v in definition.spec.versions
],
xrds
);

local gvkCRDs =
std.flatMap(
function(definition) [
{
group: definition.spec.group,
version: v.name,
kind: definition.spec.names.kind,
plural: definition.spec.names.plural,
}
for v in definition.spec.versions
],
crds
);

local groupSet(gvks) =
std.set(
std.map(
function(def)
std.splitLimit(def.definition.spec.group, '.', 1)[0],
namespaced
function(gvk)
gvk.group,
gvks,
)
);

function(configurationVersion) {
[group]: configuration('grafana-namespaced-' + group, configurationVersion)
for group in groups
local shortGroupName(group) =
std.splitLimit(group, '.', 1)[0];

local groupFilter(group) =
function(gvk) gvk.group == group;

local gvkByGroup(name, gvks) = {
[shortGroupName(group)]+: {
[name]+: [
gvk
for gvk in std.filter(groupFilter(group), gvks)
],
}
for group in groupSet(gvks)
};

function(version='main') {
configurations: {
[shortGroupName(group)]: configuration('grafana-namespaced-' + shortGroupName(group), version)
for group in groupSet(gvkXRDs)
},

gvks:
gvkByGroup('xrd', gvkXRDs)
+ gvkByGroup('crd', gvkCRDs),
}
113 changes: 18 additions & 95 deletions generator/main.libsonnet
Original file line number Diff line number Diff line change
@@ -1,75 +1,35 @@
local parser = import 'github.com/Duologic/jsonnet-parser/parser.libsonnet';
local a = import 'github.com/crdsonnet/astsonnet/main.libsonnet';
local autils = import 'github.com/crdsonnet/astsonnet/utils.libsonnet';

local helpers = import 'github.com/crdsonnet/crdsonnet/crdsonnet/helpers.libsonnet';
local crdsonnet = import 'github.com/crdsonnet/crdsonnet/crdsonnet/main.libsonnet';
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';

local processor = crdsonnet.processor.new('ast');

local definitions = import './namespaced.libsonnet';
local utils = import './utils.libsonnet';
local configurations = import './configurations.libsonnet';

local definitions =
std.map(
function(o) o.definition,
(import './namespaced.libsonnet'),
);

local globalDefinitions =
std.filter(
function(crd) crd.spec.group == 'grafana.crossplane.io',
std.parseYaml(importstr './crds.yaml'),
);


local subPackageDocstring(name, help='') =
a.object.new([
a.field.new(
a.string.new('#'),
a.literal.new(
std.manifestJsonEx(
d.package.newSub(name, help)
, ' ', ''
),
),
),
]);

local mergeDocstring(group, version, name, obj, help='') =
autils.deepMergeObjects([
a.object.new([
a.field.new(
a.id.new(group),
a.object.new([
a.field.new(
a.string.new('#'),
a.literal.new(
std.manifestJsonEx(
d.package.newSub(group, '')
, ' ', ''
),
),
),
a.field.new(
a.id.new(version),
a.object.new([
a.field.new(
a.id.new(name),
subPackageDocstring(name, help)
),
]),
),
]),
),
]),
obj,
]);

local compositions =
std.foldl(
function(acc, def)
local render = crdsonnet.xrd.render(def.definition, 'grafana.net', processor);
function(acc, definition)
local render = crdsonnet.xrd.render(definition, 'grafana.net', processor);

local group = helpers.getGroupKey(def.definition.spec.group, 'grafana.net');
local group = helpers.getGroupKey(definition.spec.group, 'grafana.net');
local version = 'v1alpha1';
local kind = helpers.camelCaseKind(crdsonnet.xrd.getKind(def.definition));
local kind = helpers.camelCaseKind(crdsonnet.xrd.getKind(definition));

local renderWithDocs = mergeDocstring(group, version, kind, render);
local renderWithDocs = utils.mergeDocstring(group, version, kind, render);

autils.deepMergeObjects([acc, renderWithDocs]),
definitions,
Expand All @@ -85,7 +45,7 @@ local global =
local version = definition.spec.versions[0].name;
local kind = helpers.camelCaseKind(crdsonnet.crd.getKind(definition));

local renderWithDocs = mergeDocstring(group, version, kind, render);
local renderWithDocs = utils.mergeDocstring(group, version, kind, render);

autils.deepMergeObjects([acc, renderWithDocs]),
globalDefinitions,
Expand All @@ -94,52 +54,15 @@ local global =

local ast = autils.deepMergeObjects([compositions, global]);

local splitIntoFiles(objast, sub='', depth=1, maxDepth=5) =
local subdir = if sub != '' then sub + '/' else '';
std.foldl(
function(acc, member)
if member.type == 'field'
&& member.expr.type == 'object'
&& !std.startsWith(member.fieldname.string, '#')
then
acc
+ {
[subdir + 'main.libsonnet']+:
a.object.withMembersMixin([
member
+ a.field.withExpr(
if depth != maxDepth
then a.import_statement.new('./' + member.fieldname.string + '/main.libsonnet')
else a.import_statement.new('./' + member.fieldname.string + '.libsonnet')
),
]),
}
+ (if depth != maxDepth && member.fieldname.string != 'global'
then splitIntoFiles(member.expr, subdir + member.fieldname.string, depth + 1)
else {
[subdir + member.fieldname.string + '.libsonnet']: member.expr,
})
else
acc
+ {
[subdir + 'main.libsonnet']+:
a.object.withMembersMixin([member]),
}
,
objast.members,
{
[subdir + 'main.libsonnet']:
a.object.new([]),
}
);

function(version='main')
local files = splitIntoFiles(ast, 'zz');
local files = utils.splitIntoFiles(ast, 'zz');
{
[file.key]: file.value.toString()
for file in std.objectKeysValues(files)
}
+ {
'zz/configurations.libsonnet': std.manifestJson(configurations(version)),
local conf = configurations(version),
'zz/configurations.libsonnet': std.manifestJson(conf.configurations),
'zz/version.libsonnet': std.manifestJson(version),
'zz/gvks.libsonnet': std.manifestJson(conf.gvks),
}
89 changes: 89 additions & 0 deletions generator/utils.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local a = import 'github.com/crdsonnet/astsonnet/main.libsonnet';
local autils = import 'github.com/crdsonnet/astsonnet/utils.libsonnet';
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';

{
local root = self,

subPackageDocstring(name, help=''):
a.object.new([
a.field.new(
a.string.new('#'),
a.literal.new(
std.manifestJsonEx(
d.package.newSub(name, help)
, ' ', ''
),
),
),
]),

mergeDocstring(group, version, name, obj, help=''):
autils.deepMergeObjects([
a.object.new([
a.field.new(
a.id.new(group),
a.object.new([
a.field.new(
a.string.new('#'),
a.literal.new(
std.manifestJsonEx(
d.package.newSub(group, '')
, ' ', ''
),
),
),
a.field.new(
a.id.new(version),
a.object.new([
a.field.new(
a.id.new(name),
root.subPackageDocstring(name, help)
),
]),
),
]),
),
]),
obj,
]),

splitIntoFiles(objast, sub='', depth=1, maxDepth=5):
local subdir = if sub != '' then sub + '/' else '';
std.foldl(
function(acc, member)
if member.type == 'field'
&& member.expr.type == 'object'
&& !std.startsWith(member.fieldname.string, '#')
then
acc
+ {
[subdir + 'main.libsonnet']+:
a.object.withMembersMixin([
member
+ a.field.withExpr(
if depth != maxDepth
then a.import_statement.new('./' + member.fieldname.string + '/main.libsonnet')
else a.import_statement.new('./' + member.fieldname.string + '.libsonnet')
),
]),
}
+ (if depth != maxDepth && member.fieldname.string != 'global'
then root.splitIntoFiles(member.expr, subdir + member.fieldname.string, depth + 1)
else {
[subdir + member.fieldname.string + '.libsonnet']: member.expr,
})
else
acc
+ {
[subdir + 'main.libsonnet']+:
a.object.withMembersMixin([member]),
}
,
objast.members,
{
[subdir + 'main.libsonnet']:
a.object.new([]),
}
),
}
2 changes: 1 addition & 1 deletion grafanaplane/jsonnetfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"subdir": ""
}
},
"version": "duologic/rfc1123"
"version": "master"
}
],
"legacyImports": true
Expand Down
Loading