Merge branch 'master' of github.com:fabxc/kube-prometheus
This commit is contained in:
@@ -43,6 +43,11 @@ spec:
|
||||
containers:
|
||||
- name: grafana
|
||||
image: grafana/grafana:3.1.1
|
||||
env:
|
||||
- name: GF_AUTH_BASIC_ENABLED
|
||||
value: "true"
|
||||
- name: GF_AUTH_ANONYMOUS_ENABLED
|
||||
value: "true"
|
||||
volumeMounts:
|
||||
- name: grafana-storage
|
||||
mountPath: /var/grafana-storage
|
||||
|
File diff suppressed because one or more lines are too long
76
grafana-watcher/grafana/datasource.go
Normal file
76
grafana-watcher/grafana/datasource.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package grafana
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type DatasourcesInterface interface {
|
||||
All() ([]GrafanaDatasource, error)
|
||||
Create(datasourceJson io.Reader) error
|
||||
Delete(id int) error
|
||||
}
|
||||
|
||||
// DatasourcesClient is an implementation of the DatasourcesInterface. The
|
||||
// datasources HTTP API of Grafana requires admin access.
|
||||
type DatasourcesClient struct {
|
||||
BaseUrl string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type GrafanaDatasource struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func NewDatasourcesClient(baseUrl string, c *http.Client) DatasourcesInterface {
|
||||
return &DatasourcesClient{
|
||||
BaseUrl: baseUrl,
|
||||
HTTPClient: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *DatasourcesClient) All() ([]GrafanaDatasource, error) {
|
||||
allUrl := c.BaseUrl + "/api/datasources"
|
||||
resp, err := c.HTTPClient.Get(allUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
datasources := make([]GrafanaDatasource, 0)
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&datasources)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return datasources, nil
|
||||
}
|
||||
|
||||
func (c *DatasourcesClient) Delete(id int) error {
|
||||
deleteUrl := c.BaseUrl + "/api/datasources/" + strconv.Itoa(id)
|
||||
req, err := http.NewRequest("DELETE", deleteUrl, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *DatasourcesClient) Create(datasourceJson io.Reader) error {
|
||||
createUrl := c.BaseUrl + "/api/datasources"
|
||||
_, err := c.HTTPClient.Post(createUrl, "application/json", datasourceJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
type Interface interface {
|
||||
Dashboards() DashboardsInterface
|
||||
Datasources() DatasourcesInterface
|
||||
}
|
||||
|
||||
type Clientset struct {
|
||||
@@ -23,3 +24,7 @@ func New(baseUrl string) Interface {
|
||||
func (c *Clientset) Dashboards() DashboardsInterface {
|
||||
return NewDashboardsClient(c.BaseUrl, c.HTTPClient)
|
||||
}
|
||||
|
||||
func (c *Clientset) Datasources() DatasourcesInterface {
|
||||
return NewDatasourcesClient(c.BaseUrl, c.HTTPClient)
|
||||
}
|
||||
|
@@ -86,10 +86,10 @@ func main() {
|
||||
|
||||
g := grafana.New(*grafanaUrl)
|
||||
du := updater.NewGrafanaDashboardUpdater(g.Dashboards(), filepath.Join(*watchDir, "*-dashboard.json"))
|
||||
//su := updater.NewGrafanaSourceUpdater(g.Sources(), filepath.Join(*watchDir, "*-source.json"))
|
||||
su := updater.NewGrafanaDatasourceUpdater(g.Datasources(), filepath.Join(*watchDir, "*-datasource.json"))
|
||||
w := newVolumeWatcher(*watchDir)
|
||||
w.AddEventHandler(du)
|
||||
//w.AddEventHandler(su)
|
||||
w.AddEventHandler(su)
|
||||
|
||||
w.Run()
|
||||
}
|
||||
|
82
grafana-watcher/updater/datasource.go
Normal file
82
grafana-watcher/updater/datasource.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/coreos/kube-prometheus/grafana-watcher/grafana"
|
||||
)
|
||||
|
||||
type GrafanaDatasourceUpdater struct {
|
||||
client grafana.DatasourcesInterface
|
||||
glob string
|
||||
}
|
||||
|
||||
func NewGrafanaDatasourceUpdater(c grafana.DatasourcesInterface, g string) Updater {
|
||||
return &GrafanaDatasourceUpdater{
|
||||
client: c,
|
||||
glob: g,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *GrafanaDatasourceUpdater) OnModify() error {
|
||||
err := u.deleteAllDatasources()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = u.createDatasourcesFromFiles()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *GrafanaDatasourceUpdater) deleteAllDatasources() error {
|
||||
log.Println("Retrieving existing datasources")
|
||||
datasources, err := u.client.All()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Deleting %d datasources\n", len(datasources))
|
||||
for _, d := range datasources {
|
||||
log.Println("Deleting datasource:", d.Id)
|
||||
|
||||
err := u.client.Delete(d.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *GrafanaDatasourceUpdater) createDatasourcesFromFiles() error {
|
||||
filePaths, err := filepath.Glob(u.glob)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, fp := range filePaths {
|
||||
u.createDatasourceFromFile(fp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *GrafanaDatasourceUpdater) createDatasourceFromFile(filePath string) error {
|
||||
log.Println("Creating datasource from:", filePath)
|
||||
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return u.client.Create(f)
|
||||
}
|
Reference in New Issue
Block a user