From 75f12bfe78259902307b5033b8978d07048f5805 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Tue, 6 Jul 2021 08:23:39 +0300 Subject: [PATCH] add option to add Copy button for code snippets (#1433) To add a Copy button wrap code snippet with the following element: ```
``` See the changes to `Kubernetes monitoring with VictoriaMetrics Single` for details. --- docs/_includes/extra/head.html | 2 + docs/_includes/extra/script.js | 18 +++++++ docs/assets/css/clipboard.css | 23 +++++++++ docs/assets/js/clipboard.min.js | 7 +++ docs/guides/k8s-monitoring-with-vm-single.md | 50 ++++++++++++++++++-- 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 docs/_includes/extra/head.html create mode 100644 docs/assets/css/clipboard.css create mode 100644 docs/assets/js/clipboard.min.js diff --git a/docs/_includes/extra/head.html b/docs/_includes/extra/head.html new file mode 100644 index 000000000..c317fa7c3 --- /dev/null +++ b/docs/_includes/extra/head.html @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/docs/_includes/extra/script.js b/docs/_includes/extra/script.js index e5810943b..0eb90cd9c 100644 --- a/docs/_includes/extra/script.js +++ b/docs/_includes/extra/script.js @@ -15,3 +15,21 @@ $(document).on("click", '.shift li.toc a', function(e) { location.hash = segments.pop(); },1) }); + +/* Clipboard-copy snippet from https://github.com/marcoaugustoandrade/jekyll-clipboardjs/blob/master/copy.js */ +let codes = document.querySelectorAll('.with-copy .highlight > pre > code'); +let countID = 0; +codes.forEach((code) => { + + code.setAttribute("id", "code" + countID); + + let btn = document.createElement('button'); + btn.innerHTML = "Copy"; + btn.className = "btn-copy"; + btn.setAttribute("data-clipboard-action", "copy"); + btn.setAttribute("data-clipboard-target", "#code" + countID); + code.before(btn); + countID++; +}); + +let clipboard = new ClipboardJS('.btn-copy'); \ No newline at end of file diff --git a/docs/assets/css/clipboard.css b/docs/assets/css/clipboard.css new file mode 100644 index 000000000..6c2c04a0f --- /dev/null +++ b/docs/assets/css/clipboard.css @@ -0,0 +1,23 @@ +/* modification for button copy */ +.btn-copy { + position: absolute; + color: #FFF; + background: rgba(0, 0, 0, .15); + border: 0; + padding: 2px 10px; + cursor: pointer; + right: 0px; + top: 0px; +} + +.btn-copy:hover { + background: rgba(0, 0, 0, .25); +} + +/* shift snippet syntax type label if Copy button is enabled */ +.with-copy div.highlighter-rouge:after { + right: 50px !important; +} +.markdown-body .highlight pre, .markdown-body pre { + padding: 20px !important; +} \ No newline at end of file diff --git a/docs/assets/js/clipboard.min.js b/docs/assets/js/clipboard.min.js new file mode 100644 index 000000000..1e7eca40f --- /dev/null +++ b/docs/assets/js/clipboard.min.js @@ -0,0 +1,7 @@ +/*! + * clipboard.js v2.0.4 + * https://zenorocha.github.io/clipboard.js + * + * Licensed MIT © Zeno Rocha + */ +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ClipboardJS=e():t.ClipboardJS=e()}(this,function(){return function(n){var o={};function r(t){if(o[t])return o[t].exports;var e=o[t]={i:t,l:!1,exports:{}};return n[t].call(e.exports,e,e.exports,r),e.l=!0,e.exports}return r.m=n,r.c=o,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=function(){function o(t,e){for(var n=0;n + ```bash helm repo add vm https://victoriametrics.github.io/helm-charts/ ``` + + Update helm repositories: +
+ ```bash helm repo update ``` +
+ To verify that everything is set up correctly you may run this command: +
+ ```bash helm search repo vm/ ``` +
+ The expected output is: ```bash @@ -62,6 +74,8 @@ vm/victoria-metrics-single 0.7.4 1.62.0 Victoria Metrics Single Run this command in your terminal: +
+ ```yaml cat < + * By running `helm install victoria-metrics vm/victoria-metrics-single` we will install `VictoriaMetrics Single` to default namespace inside your cluster * By adding `scrape: enable: true` we add and enable autodiscovery scraping from kubernetes cluster to `VictoriaMetrics Single` @@ -118,10 +134,14 @@ For us it’s important to remember the url for the datasource (copy lines from Verify that VictoriaMetrics pod is up and running by executing the following command: +
+ ```bash kubectl get pods ``` +
+ The expected output is: ```bash @@ -134,13 +154,20 @@ victoria-metrics-victoria-metrics-single-server-0 1/1 Running 0 Add the Grafana helm repository. +
+ ```bash helm repo add grafana https://grafana.github.io/helm-charts helm repo update ``` + +
+ See more info on Grafana ArtifactHUB [https://artifacthub.io/packages/helm/grafana/grafana](https://artifacthub.io/packages/helm/grafana/grafana) By installing the Chart with the release name `my-grafana`, you add the VictoriaMetrics datasource with official dashboard and kubernetes dashboard: +
+ ```yaml cat < + By running this command we: * Install Grafana from helm repository. * Provision VictoriaMetrics datasource with the url from the output above which we copied before. @@ -188,18 +218,32 @@ By running this command we: * Add this [https://grafana.com/grafana/dashboards/14205](https://grafana.com/grafana/dashboards/14205) dashboard to see Kubernetes cluster metrics. -See the output log in your terminal. Copy, paste and run these commands. -The first one will show `admin` password for Grafana admin. -The second and the third will forward Grafana to `127.0.0.1:3000`: +Check the output log in your terminal. +To see the password for Grafana `admin` user use the following command: + +
```bash kubectl get secret --namespace default my-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo +``` +
+ +Expose Grafana service on `127.0.0.1:3000`: + +
+ +```bash export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=my-grafana" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace default port-forward $POD_NAME 3000 ``` +
+ +Now Grafana should be accessible on the [http://127.0.0.1:3000](http://127.0.0.1:3000) address. + + **4. Check the obtained result in your browser** To check that VictoriaMetrics has collected metrics from the k8s cluster open in browser [http://127.0.0.1:3000/dashboards](http://127.0.0.1:3000/dashboards) and choose `Kubernetes Cluster Monitoring (via Prometheus)` dashboard. Use `admin` for login and `password` that you previously obtained from kubectl.