-
Notifications
You must be signed in to change notification settings - Fork 40
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
Initial changes for dashboards automation #78
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,6 @@ defaults: | |
run: | ||
shell: bash | ||
|
||
env: | ||
# Space separated list as a string of all dashboard json files in "rendered" to load | ||
DASHBOARDS: "kube-burner.json" | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
|
@@ -25,13 +21,10 @@ jobs: | |
# The secret GRAFANA_URL must be set with the format http://username:[email protected] without a trailing / | ||
- name: Import dashboards to grafana | ||
run: > | ||
dashboard_list=($(echo $DASHBOARDS)); | ||
for path in "${dashboard_list[@]}"; do | ||
full_path="rendered/${path}"; | ||
echo "Importing ${full_path}"; | ||
dashboard=$(cat ${full_path}); | ||
for t in rendered/**/*.json; do | ||
echo "Importing ${t}"; | ||
dashboard=$(cat ${t}); | ||
echo "{\"dashboard\": ${dashboard}, \"overwrite\": true}" | | ||
curl -k -Ss -XPOST -H "Content-Type: application/json" -H "Accept: application/json" -d@- | ||
"${{ secrets.GRAFANA_URL }}/api/dashboards/db" -o /dev/null; | ||
done | ||
|
||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM registry.access.redhat.com/ubi8/ubi-minimal | ||
|
||
# Set the working directory | ||
WORKDIR /performance-dashboards | ||
|
||
# Install necessary libraries for subsequent commands | ||
RUN microdnf install -y podman python3 python3-pip && \ | ||
microdnf clean all && \ | ||
rm -rf /var/cache/yum | ||
|
||
COPY . . | ||
|
||
# Set permissions | ||
RUN chmod -R 775 /performance-dashboards | ||
|
||
# Install dependencies | ||
RUN pip3 install --upgrade pip && \ | ||
pip3 install -r requirements.txt | ||
|
||
# Start the command | ||
CMD ["python3", "dittybopper/syncer/entrypoint.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import json | ||
import logging | ||
import os | ||
import requests | ||
import uuid | ||
import time | ||
from collections import defaultdict | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
class GrafanaOperations: | ||
""" | ||
This class is responsible for Grafana operations | ||
""" | ||
def __init__(self, grafana_url: str, input_directory: str): | ||
self.grafana_url = grafana_url | ||
self.input_directory = input_directory | ||
self.dashboards = defaultdict(list) | ||
self.folder_map = dict() | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def fetch_all_dashboards(self): | ||
""" | ||
This method fetches all rendered dashboards | ||
:return: | ||
""" | ||
self.get_all_folders() | ||
self.folder_map['General'] = None | ||
for root, _, files in os.walk(self.input_directory): | ||
folder_name = os.path.basename(root) | ||
json_files = [os.path.join(root, filename) for filename in files if filename.endswith(".json")] | ||
folder_name = "General" if (folder_name == "") else folder_name | ||
if folder_name in self.folder_map: | ||
folder_id = self.folder_map[folder_name] | ||
else: | ||
folder_id = self.create_folder(folder_name) | ||
self.dashboards[folder_id].extend(json_files) | ||
|
||
def get_all_folders(self): | ||
""" | ||
This method gets the entire list of folders in grafana | ||
:return: | ||
""" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
try: | ||
response = requests.get( | ||
f"{self.grafana_url}/api/folders", | ||
headers=headers, | ||
) | ||
response_json = response.json() | ||
self.folder_map = {each_folder['title']: each_folder['id'] for each_folder in response_json} | ||
except requests.exceptions.RequestException as e: | ||
raise Exception(f"Error listing folders. Message: {e}") | ||
|
||
def create_folder(self, folder_name): | ||
""" | ||
This method creates a folder in grafana | ||
:return: | ||
""" | ||
uid = str(uuid.uuid4()) | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
try: | ||
response = requests.post( | ||
f"{self.grafana_url}/api/folders", | ||
headers=headers, | ||
json={ | ||
"title": folder_name, | ||
"uid": uid, | ||
}, | ||
) | ||
response_json = response.json() | ||
self.folder_map[folder_name] = id | ||
return response_json['id'] | ||
|
||
except requests.exceptions.RequestException as e: | ||
raise Exception(f"Error creating folder with name:'{self.folder_name}' and uid:'{uid}'. Message: {e}") | ||
|
||
def read_dashboard_json(self, json_file): | ||
""" | ||
This method reads dashboard from json file | ||
:return: | ||
""" | ||
with open(json_file, 'r') as f: | ||
return json.load(f) | ||
|
||
def create_dashboards(self): | ||
""" | ||
This method creates/updates dashboard with new json | ||
:return: | ||
""" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
for folder_id, files in self.dashboards.items(): | ||
for json_file in set(files): | ||
dashboard_json = self.read_dashboard_json(json_file) | ||
try: | ||
response = requests.post( | ||
f"{self.grafana_url}/api/dashboards/db", | ||
headers=headers, | ||
json={ | ||
"dashboard": dashboard_json, | ||
"folderId": folder_id, | ||
"overwrite": True, | ||
}, | ||
) | ||
if response.status_code == 200: | ||
self.logger.info(f"Dashboard '{dashboard_json['title']}' created successfully in folder '{folder_id}'") | ||
else: | ||
raise Exception( | ||
f"Failed to create dashboard '{dashboard_json['title']}' in folder '{folder_id}'. Status code: {response.status_code}. Message: {response.text}") | ||
|
||
except requests.exceptions.RequestException as e: | ||
raise Exception(f"Error creating dashboard '{dashboard_json['title']}' in folder '{folder_id}'. Message: {e}") | ||
|
||
if __name__ == '__main__': | ||
grafana_operations = GrafanaOperations(os.environ.get("GRAFANA_URL"), os.environ.get("INPUT_DIR")) | ||
grafana_operations.fetch_all_dashboards() | ||
grafana_operations.create_dashboards() | ||
while True: | ||
time.sleep(60) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests==2.26.0 | ||
|
2 changes: 1 addition & 1 deletion
2
templates/api-performance-overview.jsonnet → .../General/api-performance-overview.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/cilium-k8s-perf.jsonnet → templates/General/cilium-k8s-perf.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/etcd-on-cluster-dashboard.jsonnet → ...General/etcd-on-cluster-dashboard.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/hypershift-performance.jsonnet → ...es/General/hypershift-performance.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/k8s-perf.jsonnet → templates/General/k8s-perf.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/kube-burner.jsonnet → templates/General/kube-burner.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/ocp-performance.jsonnet → templates/General/ocp-performance.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The internal grafana won't be accessible from outside
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Those actions in workflows are not of any use right now. They just help us to do some sanity checks, if we are able to build jsons and deploy them into grafana. To start with lets keep it that way and our plan is to extend the same actions for our automatic deployments in future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As is now this workflow is gonna fail, is that expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is expected and I am planning to take it outside of this PR scope. It has been failing since a while: https://github.com/cloud-bulldozer/performance-dashboards/actions/runs/6252714644/job/16976553973 as github actions won't be able to reach our observability cluster.