Dashboard: protect against missing overrides section (#35577)

* Protect against missing overrides section

We are using grafonnet-lib to generate dashboards. These dashboards do
not contain any `override` keys in `fieldConfig` by default and that is
causing this DashboardMigrator script to blow up when trying to import
the dashboards, see [1]. In Grafana v7, an empty overrides is
automatically added but using grafonnet-lib, it isn't possible to set an
empty overrides attribute e.g. [2] requires matcher/properties to be
set. Setting to null ends up giving me [3], which causes the panel to
not be displayed.

[1]
```
initDashboard.ts:137 TypeError: t.overrides is not iterable
    at v.w (DashboardMigrator.ts:960)
    at v.updateSchema (DashboardMigrator.ts:672)
    at D.updateSchema (DashboardModel.ts:993)
    at new D (DashboardModel.ts:156)
    at initDashboard.ts:134
```

[2]
https://github.com/grafana/grafonnet-lib/blob/master/grafonnet/stat_panel.libsonnet#L150-L164

[3]
```
"fieldConfig": {
   "defaults": {
      "links": [ ],
      "mappings": [ ],
      "thresholds": {
         "mode": "absolute",
         "steps": [
            {
               "color": "red",
               "value": 0
            },
            {
               "color": "orange",
               "value": 1
            },
            {
               "color": "green",
               "value": 3
            }
         ]
      },
      "unit": "none"
   },
   "overrides": [
      { }
   ]
},
```

* Update public/app/features/dashboard/state/DashboardMigrator.ts

Co-authored-by: Marcus Andersson <systemvetaren@gmail.com>
This commit is contained in:
Josh Myers 2021-06-17 07:33:36 +01:00 committed by GitHub
parent 16cc91bf33
commit eb7dd8e377
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -957,10 +957,13 @@ function upgradeValueMappingsForPanel(panel: PanelModel) {
fieldConfig.defaults.mappings = upgradeValueMappings(fieldConfig.defaults.mappings, fieldConfig.defaults.thresholds); fieldConfig.defaults.mappings = upgradeValueMappings(fieldConfig.defaults.mappings, fieldConfig.defaults.thresholds);
for (const override of fieldConfig.overrides) { // Protect against no overrides
for (const prop of override.properties) { if (Array.isArray(fieldConfig.overrides)) {
if (prop.id === 'mappings') { for (const override of fieldConfig.overrides) {
prop.value = upgradeValueMappings(prop.value); for (const prop of override.properties) {
if (prop.id === 'mappings') {
prop.value = upgradeValueMappings(prop.value);
}
} }
} }
} }