Skip to content

Commit

Permalink
[v3.2] Add grafana dashboard importing API (kubesphere#11)
Browse files Browse the repository at this point in the history
* Add API to import grafana templates to kubesphere dashboard
* Merge and fix the latest codes from kubesphere kubesphere#2501

Signed-off-by: zhu733756 <[email protected]>
  • Loading branch information
zhu733756 committed Aug 31, 2021
1 parent 9df6df5 commit 242ceb5
Show file tree
Hide file tree
Showing 217 changed files with 119,028 additions and 96 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ require (
github.com/yvasiyarov/gorelic v0.0.6 // indirect
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
golang.org/x/tools v0.1.5 // indirect
google.golang.org/grpc v1.30.0
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/cas.v2 v2.2.0
Expand Down Expand Up @@ -122,7 +124,7 @@ require (
k8s.io/utils v0.0.0-20210527160623-6fdb442a123b
kubesphere.io/api v0.0.0
kubesphere.io/client-go v0.0.0
kubesphere.io/monitoring-dashboard v0.1.2
kubesphere.io/monitoring-dashboard v0.2.2
rsc.io/letsencrypt v0.0.1 // indirect
sigs.k8s.io/application v0.8.4-0.20201016185654-c8e2959e57a0
sigs.k8s.io/controller-runtime v0.9.3
Expand Down Expand Up @@ -797,7 +799,7 @@ replace (
k8s.io/utils => k8s.io/utils v0.0.0-20200603063816-c1c6865ac451
kubesphere.io/api => ./staging/src/kubesphere.io/api
kubesphere.io/client-go => ./staging/src/kubesphere.io/client-go
kubesphere.io/monitoring-dashboard => kubesphere.io/monitoring-dashboard v0.1.2
kubesphere.io/monitoring-dashboard => kubesphere.io/monitoring-dashboard v0.2.2
rsc.io/binaryregexp => rsc.io/binaryregexp v0.2.0
rsc.io/letsencrypt => rsc.io/letsencrypt v0.0.1
rsc.io/pdf => rsc.io/pdf v0.1.1
Expand Down
23 changes: 21 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5/go.mod h1:/iP1qXHoty45bqomnu2LM+VVyAEdWN+vtSHGlQgyxbw=
github.com/chromedp/cdproto v0.0.0-20200424080200-0de008e41fa0 h1:Mf2aT0YmWsdNULwaHeCktDLWHb1s+VoDi9xEcFboLQ4=
github.com/chromedp/cdproto v0.0.0-20200424080200-0de008e41fa0/go.mod h1:PfAWWKJqjlGFYJEidUM6aVIWPr0EpobeyVWEEmplX7g=
github.com/chromedp/chromedp v0.5.3 h1:F9LafxmYpsQhWQBdCs+6Sret1zzeeFyHS5LkRF//Ffg=
github.com/chromedp/chromedp v0.5.3/go.mod h1:YLdPtndaHQ4rCpSpBG+IPpy9JvX0VD+7aaLxYgYj28w=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -152,6 +154,7 @@ github.com/containernetworking/cni v0.8.0 h1:BT9lpgGoH4jw3lFC7Odz2prU5ruiYKcgAjM
github.com/containernetworking/cni v0.8.0/go.mod h1:LGwApLUm2FpoOfxTDEeq8T9ipbpZ61X79hmU3w8FmsY=
github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.17+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-oidc v2.1.0+incompatible h1:sdJrfw8akMnCuUlaZU3tE/uYXFgfqom8DBE9so9EBsM=
github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
Expand Down Expand Up @@ -207,6 +210,7 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down Expand Up @@ -336,8 +340,11 @@ github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754 h1:tpom+2CJmpzAWj5
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/gocql/gocql v0.0.0-20200526081602-cd04bd7f22a7/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/gocraft/dbr v0.0.0-20180507214907-a0fd650918f6 h1:kumyNm8Vr8cbVm/aLQYTbDE3SKCbbn5HEVoDp/Dyyfc=
Expand Down Expand Up @@ -402,8 +409,12 @@ github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gosimple/slug v1.1.1 h1:fRu/digW+NMwBIP+RmviTK97Ho/bEj/C9swrCspN3D4=
github.com/gosimple/slug v1.1.1/go.mod h1:ER78kgg1Mv0NQGlXiDe57DpCyfbNywXXZ9mIorhxAf0=
github.com/gosuri/uitable v0.0.1 h1:M9sMNgSZPyAu1FJZJLpJ16ofL8q5ko2EDUkICsynvlY=
github.com/gosuri/uitable v0.0.1/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/grafana-tools/sdk v0.0.0-20210625151406-43693eb2f02b h1:I93owT2rHuA+t7YsUSnu88McWKllcXbPUbrkpMzIQbU=
github.com/grafana-tools/sdk v0.0.0-20210625151406-43693eb2f02b/go.mod h1:uby+6hPUCRVNG/iAZKCOlaq5YhyK0oKMRke+FDesZdw=
github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f h1:ShTPMJQes6tubcjzGMODIVG5hlrCeImaBnZzKF2N8SM=
github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg=
Expand Down Expand Up @@ -500,6 +511,7 @@ github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08 h1:V0an7KRw92wmJysvFvtqtKMAPmvS5O0jtB0nYo6t+gs=
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08/go.mod h1:dFWs1zEqDjFtnBXsd1vPOZaLsESovai349994nHx3e0=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -701,6 +713,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/prometheus v1.8.2-0.20200507164740-ecee9c8abfd1 h1:Oh/bmW9DXCbMeAZbxMmt2wuY6Q4cD0IIbR6vJP3kdHg=
github.com/prometheus/prometheus v1.8.2-0.20200507164740-ecee9c8abfd1/go.mod h1:S5n0C6tSgdnwWshBUceRx5G1OsjLv/EeZ9t3wIfEtsY=
github.com/rafaeljusto/redigomock v0.0.0-20190202135759-257e089e14a1/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a h1:9ZKAASQSHhDYGoxY8uLVpewe1GDZ2vu2Tr/vTdVAkFQ=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/retailnext/hllpp v1.0.1-0.20180308014038-101a6d2f8b52/go.mod h1:RDpi1RftBQPUCDRw6SmxeaREsAaRKnOclghuzp/WRzc=
Expand Down Expand Up @@ -774,6 +788,7 @@ github.com/uber/jaeger-client-go v2.23.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMW
github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/GfSYVCjK7dyaw=
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
Expand Down Expand Up @@ -969,8 +984,8 @@ k8s.io/metrics v0.21.2 h1:6ajprhWZnI64RSrNqET0cBdwzaxPxr9Vh8zURBkR1zY=
k8s.io/metrics v0.21.2/go.mod h1:wzlOINZMCtWq8dR9gHlyaOemmYlOpAoldEIXE82gAhI=
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 h1:v8ud2Up6QK1lNOKFgiIVrZdMg7MpmSnvtrOieolJKoE=
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
kubesphere.io/monitoring-dashboard v0.1.2 h1:KRAQbjpconHYwOYBmbpKWzE6k2DHpqQAPyZnxB5FKso=
kubesphere.io/monitoring-dashboard v0.1.2/go.mod h1:VSP7rZv0dG4lq6aVaFB1khdjma4SPtWNAOCzgdnYvXQ=
kubesphere.io/monitoring-dashboard v0.2.2 h1:aniATtXLgRAAvKOjd2UxWWHMh4/T7a0HoQ9bd+/bGcA=
kubesphere.io/monitoring-dashboard v0.2.2/go.mod h1:ksDjmOuoN0C0GuYp0s5X3186cPgk2asLUaO1WlEKISY=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/letsencrypt v0.0.1 h1:DV0d09Ne9E7UUa9ZqWktZ9L2VmybgTgfq7xlfFR/bbU=
rsc.io/letsencrypt v0.0.1/go.mod h1:buyQKZ6IXrRnB7TdkHP0RyEybLx18HHyOSoTyoOLqNY=
Expand All @@ -995,6 +1010,10 @@ sigs.k8s.io/kustomize/cmd/config v0.9.10/go.mod h1:Mrby0WnRH7hA6OwOYnYpfpiY0WJIM
sigs.k8s.io/kustomize/kustomize/v4 v4.1.2/go.mod h1:PxBvo4WGYlCLeRPL+ziT64wBXqbgfcalOS/SXa/tcyo=
sigs.k8s.io/kustomize/kyaml v0.10.17 h1:4zrV0ym5AYa0e512q7K3Wp1u7mzoWW0xR3UHJcGWGIg=
sigs.k8s.io/kustomize/kyaml v0.10.17/go.mod h1:mlQFagmkm1P+W4lZJbJ/yaxMd8PqMRSC4cPcfUVt5Hg=
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 h1:zD2IemQ4LmOcAumeiyDWXKUI2SO0NYDe3H6QGvPOVgU=
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06/go.mod h1:/ULNhyfzRopfcjskuui0cTITekDduZ7ycKN3oUT9R18=
sigs.k8s.io/structured-merge-diff/v3 v3.0.0 h1:dOmIZBMfhcHS09XZkMyUgkq5trg3/jRyJYFZUiaOp8E=
sigs.k8s.io/structured-merge-diff/v3 v3.0.0/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
sigs.k8s.io/structured-merge-diff/v4 v4.1.0 h1:C4r9BgJ98vrKnnVCjwCSXcWjWe0NKcUQkmzDXZXGwH8=
sigs.k8s.io/structured-merge-diff/v4 v4.1.0/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/addtoscheme_dashboard_v1alpha2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package apis

import monitoringdashboardv1alpha2 "kubesphere.io/monitoring-dashboard/api/v1alpha2"

func init() {
AddToSchemes = append(AddToSchemes, monitoringdashboardv1alpha2.SchemeBuilder.AddToScheme)
}
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ const (
NotificationTag = "Notification"
NotificationSecretNamespace = "kubesphere-monitoring-federated"
NotificationManagedLabel = "notification.kubesphere.io/managed"

DashboardTag = "Dashboard"
)

var (
Expand Down
3 changes: 3 additions & 0 deletions pkg/kapis/monitoring/v1alpha3/dashboard_template_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash

curl -d '{"grafanaDashboardName":"test2","grafanaDashboardUrl":"https://grafana.com/api/dashboards/7362/revisions/5/download"}' -H "Content-Type: application/json" localhost:9090/kapis/monitoring.kubesphere.io/v1alpha3/dashboard/template
123 changes: 123 additions & 0 deletions pkg/kapis/monitoring/v1alpha3/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@
package v1alpha3

import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"

"k8s.io/klog"

converter "kubesphere.io/monitoring-dashboard/tools/converter"

openpitrixoptions "kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
"kubesphere.io/kubesphere/pkg/simple/client/s3"

"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
"kubesphere.io/kubesphere/pkg/models/openpitrix"

"github.com/emicklei/go-restful"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

monitoringdashboardv1alpha2 "kubesphere.io/monitoring-dashboard/api/v1alpha2"

"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/informers"
model "kubesphere.io/kubesphere/pkg/models/monitoring"
Expand Down Expand Up @@ -302,3 +312,116 @@ func (h handler) handleAdhocQuery(req *restful.Request, resp *restful.Response)
resp.WriteAsJson(res)
}
}

// handleGrafanaDashboardImport imports Grafana template and converts it to KubeSphere dashboard.
// The description of the Parameters:
// grafanaDashboardName: the name of this Grafana template needed to convert.
// grafanaDashboardUrl: the link to download this Grafana template.
// grafanaDashboardContent: the whole JSON content needed to convert.
// Note that the parameter grafanaDashboardName is indispensable,
// and the requested parameter grafanaDashboardUrl and grafanaDashboardContent cannot be empty at the same time.
func (h handler) handleGrafanaDashboardImport(req *restful.Request, resp *restful.Response) {
var entity monitoring.DashboardEntity
err := req.ReadEntity(&entity)
if err != nil {
api.HandleBadRequest(resp, nil, err)
return
}

if entity.GrafanaDashboardName == "" {
err := errors.New("the requested parameter grafanaDashboardName cannot be empty")
api.HandleBadRequest(resp, nil, err)
return
}
if entity.GrafanaDashboardUrl == "" && entity.GrafanaDashboardContent == "" {
err := errors.New("the requested parameter grafanaDashboardUrl and grafanaDashboardContent cannot be empty at the same time")
api.HandleBadRequest(resp, nil, err)
return
}

grafanaDashboardContent := []byte(entity.GrafanaDashboardContent)
if entity.GrafanaDashboardUrl != "" {
c, err := func(u string) ([]byte, error) {
_, err := url.ParseRequestURI(u)
if err != nil {
return nil, err
}
client := &http.Client{}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

r, err := client.Do(req)
if err != nil {
return nil, err
}

defer r.Body.Close()

c, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
return c, nil
}(entity.GrafanaDashboardUrl)

if err != nil {
api.HandleBadRequest(resp, nil, err)
return
}

grafanaDashboardContent = []byte(c)
}

c := converter.NewConverter()
convertedDashboard, err := c.ConvertToDashboard(grafanaDashboardContent, true, "", entity.GrafanaDashboardName)
if err != nil {
api.HandleBadRequest(resp, nil, err)
return
}

dashboard := monitoringdashboardv1alpha2.ClusterDashboard{
TypeMeta: v1.TypeMeta{
APIVersion: convertedDashboard.APIVersion,
Kind: convertedDashboard.Kind,
},
ObjectMeta: v1.ObjectMeta{
Name: convertedDashboard.Metadata["name"],
},
Spec: *convertedDashboard.Spec,
}

jsonDashbaord, err := json.Marshal(dashboard)
if err != nil {
api.HandleBadRequest(resp, nil, err)
return
}

// a dashboard with the same name cannot post.
ctx := context.TODO()
_, err = h.k.Discovery().RESTClient().
Get().
AbsPath("/apis/monitoring.kubesphere.io/v1alpha2/clusterdashboards/" + dashboard.Name).
DoRaw(ctx)

if err == nil {
api.HandleBadRequest(resp, nil, errors.New("a dashboard with the same name already exists!"))
return
}

// create this dashboard
_, err = h.k.Discovery().RESTClient().
Post().
AbsPath("/apis/monitoring.kubesphere.io/v1alpha2/clusterdashboards").
Body(jsonDashbaord).
DoRaw(ctx)

if err != nil {
api.HandleBadRequest(resp, nil, err)
return
}

resp.WriteAsJson(dashboard)

}
10 changes: 10 additions & 0 deletions pkg/kapis/monitoring/v1alpha3/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package v1alpha3
import (
"net/http"

monitoringdashboardv1alpha2 "kubesphere.io/monitoring-dashboard/api/v1alpha2"

openpitrixoptions "kubesphere.io/kubesphere/pkg/simple/client/openpitrix"

"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -504,6 +506,14 @@ func AddToContainer(c *restful.Container, k8sClient kubernetes.Interface, monito
Returns(http.StatusOK, respOK, monitoring.Metric{})).
Produces(restful.MIME_JSON)

ws.Route(ws.POST("/dashboard/template").
To(h.handleGrafanaDashboardImport).
Doc("Convert Grafana templates to KubeSphere dashboards.").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.DashboardTag}).
Writes(monitoringdashboardv1alpha2.ClusterDashboard{}).
Returns(http.StatusOK, respOK, monitoringdashboardv1alpha2.ClusterDashboard{})).
Produces(restful.MIME_JSON)

c.Add(ws)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1"
monitoringdashboardv1alpha2 "kubesphere.io/monitoring-dashboard/api/v1alpha2"
"sigs.k8s.io/controller-runtime/pkg/client"

"kubesphere.io/kubesphere/pkg/api"
Expand All @@ -39,7 +39,7 @@ func New(c client.Reader) v1alpha3.Interface {
}

func (d *dashboardGetter) Get(_, name string) (runtime.Object, error) {
dashboard := monitoringdashboardv1alpha1.ClusterDashboard{}
dashboard := monitoringdashboardv1alpha2.ClusterDashboard{}
err := d.c.Get(context.Background(), types.NamespacedName{Name: name}, &dashboard)
if err != nil {
klog.Error(err)
Expand All @@ -49,7 +49,7 @@ func (d *dashboardGetter) Get(_, name string) (runtime.Object, error) {
}

func (d *dashboardGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
dashboards := monitoringdashboardv1alpha1.ClusterDashboardList{}
dashboards := monitoringdashboardv1alpha2.ClusterDashboardList{}
err := d.c.List(context.Background(), &dashboards, &client.ListOptions{LabelSelector: query.Selector()})
if err != nil {
klog.Error(err)
Expand All @@ -65,12 +65,12 @@ func (d *dashboardGetter) List(_ string, query *query.Query) (*api.ListResult, e

func (d *dashboardGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {

leftClusterDashboard, ok := left.(*monitoringdashboardv1alpha1.ClusterDashboard)
leftClusterDashboard, ok := left.(*monitoringdashboardv1alpha2.ClusterDashboard)
if !ok {
return false
}

rightClusterDashboard, ok := right.(*monitoringdashboardv1alpha1.ClusterDashboard)
rightClusterDashboard, ok := right.(*monitoringdashboardv1alpha2.ClusterDashboard)
if !ok {
return false
}
Expand All @@ -79,7 +79,7 @@ func (d *dashboardGetter) compare(left runtime.Object, right runtime.Object, fie
}

func (d *dashboardGetter) filter(object runtime.Object, filter query.Filter) bool {
dashboard, ok := object.(*monitoringdashboardv1alpha1.ClusterDashboard)
dashboard, ok := object.(*monitoringdashboardv1alpha2.ClusterDashboard)
if !ok {
return false
}
Expand Down
Loading

0 comments on commit 242ceb5

Please sign in to comment.