Skip to content

Commit

Permalink
Merge pull request #169 from scality/vendored-code
Browse files Browse the repository at this point in the history
Properly vendor all Grafana dashboards
  • Loading branch information
NicolasT authored Jul 20, 2018
2 parents de0d003 + b8a3646 commit cacb028
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 19 deletions.
11 changes: 3 additions & 8 deletions roles/kube_prometheus/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Adding new dashboards

- Drop json files into `src` directory
- Import the dashboard file in the repository
- List the dashboard in `aggregate_dashboard.ini`
- run `hack/aggregate_dashboard.py`
- Commit new jsonfile under `src` and modification to `files/additionnal_dashboard.yml`

# Current dashboards

[prometheus_2_stats.json](https://github.com/grafana/grafana/blob/master/public/app/plugins/datasource/prometheus/dashboards/prometheus_2_stats.json)
[node-exporter-full.json](https://github.com/rfrail3/grafana-dashboards/blob/master/prometheus/node-exporter-full.json)
[elasticsearch-exporter.json](https://github.com/justwatchcom/elasticsearch_exporter/blob/master/examples/grafana/dashboard.json)
- Commit modification to `files/additionnal_dashboard.yml`
9 changes: 8 additions & 1 deletion roles/kube_prometheus/aggregate_dashboard.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ target=files/additionnal_dashboard.yml
dest=../../vendor/etcd/Documentation/op-guide/grafana.json

[source:2]
dest=src/*.json
dest = ../../vendor/justwatchcom/elasticsearch_exporter/examples/grafana/dashboard.json
name = elasticsearch-exporter

[source:3]
dest = ../../vendor/rfrail3/grafana-dashboards/prometheus/node-exporter-full.json

[source:4]
dest = ../../vendor/grafana/grafana/public/app/plugins/datasource/prometheus/dashboards/prometheus_2_stats.json
34 changes: 24 additions & 10 deletions roles/kube_prometheus/hack/aggregate_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class Dashboard:

NOT_A_DASHBOARD_ERROR = "File '{}' doesn't look like a dashboard"

def __init__(self, filename, force=False):
def __init__(self, filename, name=None, force=False):
self.filename = filename
self.name = name
self.force = force
self._spec = None
self._basename = None
Expand Down Expand Up @@ -100,8 +101,10 @@ def basename(self):
@property
def grafana_name(self):
"""return basename(of the filename) of the dashboard"""
return os.path.basename(self.basename).replace(
'.json', '-dashboard.json')
prefix = self.name if self.name else \
os.path.basename(self.basename).replace('.json', '')

return '{}-dashboard.json'.format(prefix)

def compute_dashboard_string(self):
spec = dict(self.spec)
Expand Down Expand Up @@ -173,35 +176,46 @@ def iter_sources(self):
def iter_filenames_in_source(self, source_name):
source_path = '{}/{}'.format(self.configdir,
self.config.get(source_name, 'dest'))

name = None
try:
name = self.config.get(source_name, 'name')
except configparser.NoOptionError:
pass

iter_filename = glob.iglob(source_path)
first_file = next(iter_filename)
try:
second_file = next(iter_filename)
except StopIteration:
if first_file == source_path and os.path.isdir(source_path):
return glob.iglob('{}/*'.format(source_path))
assert name is None
return ((path, None) for path in
glob.iglob('{}/*'.format(source_path)))
else:
return iter([first_file])
return iter([(first_file, name)])
else:
return itertools.chain([first_file, second_file], iter_filename)
assert name is None
return itertools.chain([(first_file, None), (second_file, None)],
iter_filename)

def iter_filenames(self):
for source in self.iter_sources():
for filename in self.iter_filenames_in_source(source):
yield filename

def get_dashboard(self, filename):
return self.dashboard_class(filename, force=self.force)
def get_dashboard(self, filename, name=None):
return self.dashboard_class(filename, name=name, force=self.force)

def run(self):
target_filename = self.config.get('default', 'target')
target_path = os.path.join(self.configdir, target_filename)
with open(target_path, 'w') as target:
target.write('grafana:\n')
target.write(' serverDashboardFiles:')
for filename in self.iter_filenames():
for (filename, name) in self.iter_filenames():
log.info('Take {}'.format(filename))
dashboard = self.get_dashboard(filename)
dashboard = self.get_dashboard(filename, name)
target.write('\n')
target.write(indent(
dashboard.compute_dashboard_string(),
Expand Down
15 changes: 15 additions & 0 deletions third-party.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ modules:
repository: https://github.com/openstack/ansible-hardening.git
ref: 17.0.4
license: Apache-2.0
- path: vendor/grafana/grafana
source:
type: url
base: https://raw.githubusercontent.com/grafana/grafana/master/
license: Apache-2.0
- path: vendor/justwatchcom/elasticsearch_exporter
source:
type: url
base: https://raw.githubusercontent.com/justwatchcom/elasticsearch_exporter/master/
license: Apache-2.0
- path: vendor/rfrail3/grafana-dashboards
source:
type: url
base: https://raw.githubusercontent.com/rfrail3/grafana-dashboards/master/
license: GPL-3.0-or-later

charts:
- role: kube_heapster
Expand Down

0 comments on commit cacb028

Please sign in to comment.