Skip to content

Commit

Permalink
refactor(generator): move schema fixes into patches directory (#139)
Browse files Browse the repository at this point in the history
* refactor(generator): move schema fixes into adhesives

* review: s/adhesives/patches

* style: rename function s/process/patch

* style: consistent naming
  • Loading branch information
Duologic authored Oct 18, 2023
1 parent b8f5ba5 commit 9409bc3
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 209 deletions.
17 changes: 8 additions & 9 deletions generator/main.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ local panel = import './panel.libsonnet';
local query = import './query.libsonnet';
local row = import './row.libsonnet';

local spec = import './spec.libsonnet';
local utils = import './utils.libsonnet';
local patches = import './patches/main.libsonnet';

function(version, schemas, openapiSpec)
local processedSchemas = utils.processSchemas(version, schemas);
local processedSpec = spec.process(openapiSpec);
local patchedSchemas = patches.schemas.patch(version, schemas);
local patchedSpec = patches.spec.patch(openapiSpec);
local files =
core.render(version, processedSchemas.core)
+ panel.render(processedSchemas.panel)
+ query.render(processedSchemas.query)
+ row.render(processedSchemas.row)
+ alerting.render(processedSpec);
core.render(version, patchedSchemas.core)
+ panel.render(patchedSchemas.panel)
+ query.render(patchedSchemas.query)
+ row.render(patchedSchemas.row)
+ alerting.render(patchedSpec);
{
['grafonnet-' + version + '/' + file]:
'// This file is generated, do not manually edit.\n'
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions generator/patches/main.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
schemas: import './schemas.libsonnet',
spec: import './spec.libsonnet',
}
193 changes: 193 additions & 0 deletions generator/patches/schemas.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
local utils = import '../utils.libsonnet';

{
local root = self,

// Patch the JSON Schemas and put them into categories.
// This function also restructures the schemas for processing by CRDsonnet.
patch(version, schemas): {
core:
std.filterMap(
function(schema)
!std.endsWith(schema.info.title, 'PanelCfg')
&& !std.endsWith(schema.info.title, 'DataQuery'),
root.restructure,
schemas
)
+ [root.getPanelSchema(version, schemas)],

panel:
root.getMissingPanelSchemas(schemas)
+ root.getMissingAlertListPanel(schemas)
+ std.filterMap(
function(schema) std.endsWith(schema.info.title, 'PanelCfg'),
root.restructure,
schemas
),

query:
std.filterMap(
function(schema) std.endsWith(schema.info.title, 'DataQuery'),
root.restructure,
schemas,
),

row:
[root.getRowSchema(schemas)],
},

restructure(schema):
local title = schema.info.title;
local formatted = utils.formatSchemaName(title);

schema
+ root.addTableFieldConfig(schema)
+ {
info+: {
title: formatted,
},
components+: {
schemas+: {
[formatted]:
super[title]
+ root.removePanelsFromDashboardSchema(schema),
},
},
},

getPanelSchema(version, schemas):
root.restructure(
root.getDashboardSchema(schemas)
+ {
info+: { title: 'Panel' },
components+: { schemas+: { Panel+: { properties+: {
pluginVersion: {
// HACK: Grafana uses the pluginVersion to decide which migrations to execute
// however the pluginVersion is currently not part of the plugin schema's.
// This hack ensures that the pluginVersion matches the Grafana version.
const: version,
},
} } } },
},
),

// FIXME: Should we care about missing panel schemas at this level?
getMissingPanelSchemas(schemas):
local genericSchema(title) =
root.restructure({
info: {
title: title,
},
components: {
schemas: {
[title]: {
type: 'object',
},
},
},
});
local allSchemaTitles = std.map(function(x) x.info.title, schemas);
local missingPanelSchemas = [
'CandlestickPanelCfg',
'CanvasPanelCfg',
];
[
genericSchema(title)
for title in missingPanelSchemas
if !std.member(allSchemaTitles, title)
],

// ref: https://github.com/grafana/grafonnet/issues/137
getMissingAlertListPanel(schemas):
local title = 'AlertListPanelCfg';
local allSchemaTitles = std.map(function(x) x.info.title, schemas);
if !std.member(allSchemaTitles, title)
then
[
root.restructure({
info: {
title: title,
},
definitions: (import './custom_schemas/alertList.json').definitions,
components: {
schemas: {
[title]: {
type: 'object',
properties: {
Options: {
oneOf: [
{ '$ref': '#/definitions/AlertListOptions' },
{ '$ref': '#/definitions/UnifiedAlertListOptions' },
],
},
},
},
},
},
}),
]
else [],

getRowSchema(schemas):
root.restructure(
root.getDashboardSchema(schemas)
+ {
info+: { title: 'RowPanelCfg' },
components+: { schemas+: {
RowPanelCfg:
super.RowPanel
+ {
properties+: {
type: { const: 'row' },
panels: { type: 'array' },
},
},
} },
}
),

// ref: https://github.com/grafana/grafana/issues/75610
addTableFieldConfig(schema):
if schema.info.title == 'TablePanelCfg'
&& !('FieldConfig' in schema.components.schemas.TablePanelCfg.properties)
&& !('PanelFieldConfig' in schema.components.schemas.TablePanelCfg.properties)
then {
definitions: (import './custom_schemas/table_FieldConfig.json').definitions,
components+: {
schemas+: {
TablePanelCfg+: {
properties+: {
PanelFieldConfig: {
'$ref': '#/definitions/TableFieldOptions',
},
},
},
},
},
}
else {},

removePanelsFromDashboardSchema(schema):
if schema.info.title == 'dashboard'
then
if 'spec' in schema.components.schemas.dashboard.properties
then {
properties+: { spec+: {
properties+: {
panels: { type: 'array' },
},
} },
}
else {
properties+: {
panels: { type: 'array' },
},
}
else {},

getDashboardSchema(schemas):
std.filter(
function(s) s.info.title == 'dashboard',
schemas
)[0],
}
4 changes: 2 additions & 2 deletions generator/spec.libsonnet → generator/patches/spec.libsonnet
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// Fix a few issues in the openapi spec so it renders properly, see individual function descriptions for more details.
process(spec):
// Patch a few issues in the openapi spec so it renders properly, see individual function descriptions for more details.
patch(spec):
self.renameTitleToDescription(spec)
+ self.addIsPausedToProvisionedAlertRule()
+ self.removeRecursiveRefOnRoute()
Expand Down
20 changes: 10 additions & 10 deletions generator/query.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ local utils = import './utils.libsonnet';

// FIXME: Some schemas follow a different structure, temporarily covering for this.
local fixes = {
[utils.formatPanelName('CloudWatchDataQuery')]: {
[utils.formatPanelName('CloudWatchDataQuery')]: {
[utils.formatSchemaName('CloudWatchDataQuery')]: {
[utils.formatSchemaName('CloudWatchDataQuery')]: {
type: 'object',
oneOf: [
{ '$ref': '#/components/schemas/CloudWatchAnnotationQuery' },
Expand All @@ -119,23 +119,23 @@ local utils = import './utils.libsonnet';
},
},
},
[utils.formatPanelName('AzureMonitorDataQuery')]: {
[utils.formatPanelName('AzureMonitorDataQuery')]: {
[utils.formatSchemaName('AzureMonitorDataQuery')]: {
[utils.formatSchemaName('AzureMonitorDataQuery')]: {
'$ref': '#/components/schemas/AzureMonitorQuery',
},
},
[utils.formatPanelName('TempoDataQuery')]: {
[utils.formatPanelName('TempoDataQuery')]: {
[utils.formatSchemaName('TempoDataQuery')]: {
[utils.formatSchemaName('TempoDataQuery')]: {
'$ref': '#/components/schemas/TempoQuery',
},
},
[utils.formatPanelName('GoogleCloudMonitoringDataQuery')]: {
[utils.formatPanelName('GoogleCloudMonitoringDataQuery')]: {
[utils.formatSchemaName('GoogleCloudMonitoringDataQuery')]: {
[utils.formatSchemaName('GoogleCloudMonitoringDataQuery')]: {
'$ref': '#/components/schemas/CloudMonitoringQuery',
},
},
[utils.formatPanelName('TestDataDataQuery')]: {
[utils.formatPanelName('TestDataDataQuery')]+: {
[utils.formatSchemaName('TestDataDataQuery')]: {
[utils.formatSchemaName('TestDataDataQuery')]+: {
properties+: {
// `points` is an array of arrays, this renders awkwardly with CRDsonnet
points: { type: 'array' },
Expand Down
Loading

0 comments on commit 9409bc3

Please sign in to comment.