From 763787b09ff0b318ecd301c57b5aa0b59c0ce374 Mon Sep 17 00:00:00 2001 From: malcolmholmes <42545407+malcolmholmes@users.noreply.github.com> Date: Mon, 23 Aug 2021 14:14:11 +0100 Subject: [PATCH] Simpler API for Grizzly resources (#635) * Simpler API for Grizzly resources * lint, and add necessary resource.libsonnet --- grizzly/check.libsonnet | 7 +++++++ grizzly/grafana.libsonnet | 21 +++++++++++++++++++++ grizzly/grizzly.libsonnet | 8 ++++++++ grizzly/prometheus.libsonnet | 9 +++++++++ grizzly/resource.libsonnet | 22 ++++++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 grizzly/check.libsonnet create mode 100644 grizzly/resource.libsonnet diff --git a/grizzly/check.libsonnet b/grizzly/check.libsonnet new file mode 100644 index 000000000..a1ff9ef02 --- /dev/null +++ b/grizzly/check.libsonnet @@ -0,0 +1,7 @@ +local resource = import 'resource.libsonnet'; +{ + new(type, name, check):: + resource.new('SyntheticMonitoringCheck', name) + + resource.addMetadata('type', type) + + resource.withSpec(check), +} diff --git a/grizzly/grafana.libsonnet b/grizzly/grafana.libsonnet index 2041981ef..ba19fc5e6 100644 --- a/grizzly/grafana.libsonnet +++ b/grizzly/grafana.libsonnet @@ -1,3 +1,4 @@ +local resource = import 'resource.libsonnet'; local util = import 'util.libsonnet'; { @@ -18,4 +19,24 @@ local util = import 'util.libsonnet'; } for key in std.objectFields(mixins) }, + + dashboard: { + new(name, dashboard_json):: + resource.new('Dashboard', name) + + resource.withSpec(dashboard_json), + }, + + folder: { + new(name, title):: + resource.new('DashboardFolder', name) + + resource.withSpec({ + title: title, + }), + }, + + datasource: { + new(name, datasource_json):: + resource.new('Datasource', name) + + resource.withSpec(datasource_json), + }, } diff --git a/grizzly/grizzly.libsonnet b/grizzly/grizzly.libsonnet index 61e107e43..a1395175f 100644 --- a/grizzly/grizzly.libsonnet +++ b/grizzly/grizzly.libsonnet @@ -15,4 +15,12 @@ local prometheus = import 'prometheus.libsonnet'; + prometheus.fromMapsFiltered(main.prometheusRules, mixinRules) + prometheus.fromMixins(main.mixins), }, + + resource: (import 'resource.libsonnet'), + dashboard: grafana.dashboard, + folder: grafana.folder, + datasource: grafana.datasource, + rule_group: prometheus.rule_group, + synthetic_monitoring_check: (import 'check.libsonnet'), + } diff --git a/grizzly/prometheus.libsonnet b/grizzly/prometheus.libsonnet index 8ede13d1f..a9c09b1ca 100644 --- a/grizzly/prometheus.libsonnet +++ b/grizzly/prometheus.libsonnet @@ -2,6 +2,8 @@ local util = import 'util.libsonnet'; local kind = 'PrometheusRuleGroup'; local recordingRules = 'prometheusRules'; local alertRules = 'prometheusAlerts'; +local resource = import 'resource.libsonnet'; + { getMixinRuleNames(mixins):: local flatMixins = [mixins[key] for key in std.objectFieldsAll(mixins)]; @@ -27,4 +29,11 @@ local alertRules = 'prometheusAlerts'; ) for key in std.objectFields(mixins) }, + + rule_group: { + new(namespace, name, group):: + resource.new('PrometheusRuleGroup', name) + + resource.addMetadata('namespace', namespace) + + resource.withSpec(group), + }, } diff --git a/grizzly/resource.libsonnet b/grizzly/resource.libsonnet new file mode 100644 index 000000000..9f6054a9e --- /dev/null +++ b/grizzly/resource.libsonnet @@ -0,0 +1,22 @@ +{ + defaultApiVersion:: 'grizzly.grafana.com/v1alpha1', + new(kind, name):: { + apiVersion: $.defaultApiVersion, + kind: kind, + metadata: { + name: name, + }, + }, + withApiVersion(apiVersion):: { + defaultApiVersion:: apiVersion, + apiVersion: apiVersion, + }, + addMetadata(name, value):: { + metadata+: { + [name]: value, + }, + }, + withSpec(spec):: { + spec: spec, + }, +}