forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Built grafana for multiple architectures and distributes them as a ma…
…nifest list. Cleaned up the Dockerfile to be based on busybox => much smaller. Improved the Makefile and README.md. Added a Go binary that does the pushing of the .json files to grafana instead of using a bash script
- Loading branch information
Showing
6 changed files
with
264 additions
and
92 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
# | ||
# Stock Grafana + a few custom dashboards | ||
# | ||
|
||
FROM grafana/grafana:3.1.1 | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y curl | ||
FROM BASEIMAGE | ||
|
||
ADD grafana.tar / | ||
COPY dashboards /dashboards | ||
COPY run.sh /run.sh | ||
COPY run.sh / | ||
COPY setup_grafana /usr/bin/ | ||
|
||
EXPOSE 3000 | ||
ENTRYPOINT /run.sh | ||
ENTRYPOINT ["/run.sh"] |
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 |
---|---|---|
@@ -1,17 +1,31 @@ | ||
# Grafana Image For Heapster/InfluxDB | ||
|
||
## What's In It | ||
* Stock Grafana. | ||
* Create a datasource for InfluxDB. | ||
* Create a couple of dashboards during startup. These dashboards leverage templating and repeating of panels features in Grafana 2.0, to discover nodes, pods, and containers automatically. | ||
## What's in it: | ||
- Grafana 4 | ||
- A Go binary that: | ||
- creates a datasource for InfluxDB | ||
- creates a couple of dashboards during startup. | ||
- these dashboards leverage templating and repeating of panels features in Grafana, to discover nodes, pods, and containers automatically. | ||
|
||
## How To Use It | ||
* InfluxDB service URL can be passed in via the environment variable __INFLUXDB_SERVICE_URL__. | ||
* If __INFLUXDB_SERVICE_URL__ isn't defined, it will discover and use the external service URL, if available. | ||
* Otherwise, it will fall back to http://monitoring-influxdb:8086. | ||
## How to use it: | ||
- InfluxDB service URL can be passed in via the environment variable `INFLUXDB_SERVICE_URL`. | ||
- Otherwise, it will fall back to http://monitoring-influxdb:8086. | ||
|
||
## How To Build It | ||
## How to build: | ||
|
||
cd $GOPATH/src/k8s.io/heapster/grafana | ||
```console | ||
$ ARCH=${ARCH} make build | ||
``` | ||
|
||
make all | ||
## How to release: | ||
|
||
This image supports multiple architecures, which means the Makefile cross-compiles and builds docker images for all architectures automatically when pushing. | ||
If you are releasing a new version, please bump the `VERSION` value in the `Makefile` before building the images. | ||
|
||
How to build and push all images: | ||
|
||
```console | ||
# Optional: Set PREFIX if you want to push to a temporary user or another registry for testing | ||
# This command will build images and push for all architectures | ||
$ make push | ||
``` |
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 |
---|---|---|
@@ -1,69 +1,17 @@ | ||
#!/bin/bash | ||
|
||
HEADER_CONTENT_TYPE="Content-Type: application/json" | ||
HEADER_ACCEPT="Accept: application/json" | ||
|
||
GRAFANA_USER=${GRAFANA_USER:-admin} | ||
GRAFANA_PASSWD=${GRAFANA_PASSWD:-admin} | ||
GRAFANA_PORT=${GRAFANA_PORT:-3000} | ||
|
||
INFLUXDB_HOST=${INFLUXDB_HOST:-"monitoring-influxdb"} | ||
INFLUXDB_DATABASE=${INFLUXDB_DATABASE:-k8s} | ||
INFLUXDB_PASSWORD=${INFLUXDB_PASSWORD:-root} | ||
INFLUXDB_PORT=${INFLUXDB_PORT:-8086} | ||
INFLUXDB_USER=${INFLUXDB_USER:-root} | ||
|
||
DASHBOARD_LOCATION=${DASHBOARD_LOCATION:-"/dashboards"} | ||
#!/bin/sh | ||
|
||
# Allow access to dashboards without having to log in | ||
# Export these variables so grafana picks them up | ||
export GF_AUTH_ANONYMOUS_ENABLED=${GF_AUTH_ANONYMOUS_ENABLED:-true} | ||
export GF_SERVER_HTTP_PORT=${GRAFANA_PORT} | ||
export GF_SERVER_PROTOCOL=${GF_SERVER_PROTOCOL:-http} | ||
|
||
GF_SERVER_PROTOCOL=${GF_SERVER_PROTOCOL:-http} | ||
|
||
BACKEND_ACCESS_MODE=${BACKEND_ACCESS_MODE:-proxy} | ||
INFLUXDB_SERVICE_URL=${INFLUXDB_SERVICE_URL} | ||
if [ -n "$INFLUXDB_SERVICE_URL" ]; then | ||
echo "Influxdb service URL is provided." | ||
else | ||
INFLUXDB_SERVICE_URL="http://${INFLUXDB_HOST}:${INFLUXDB_PORT}" | ||
fi | ||
|
||
echo "Using the following URL for InfluxDB: ${INFLUXDB_SERVICE_URL}" | ||
echo "Using the following backend access mode for InfluxDB: ${BACKEND_ACCESS_MODE}" | ||
|
||
set -m | ||
echo "Starting Grafana in the background" | ||
exec /usr/sbin/grafana-server --homepath=/usr/share/grafana --config=/etc/grafana/grafana.ini cfg:default.paths.data=/var/lib/grafana cfg:default.paths.logs=/var/log/grafana & | ||
|
||
echo "Waiting for Grafana to come up..." | ||
until $(curl -k --fail --output /dev/null --silent ${GF_SERVER_PROTOCOL}://${GRAFANA_USER}:${GRAFANA_PASSWD}@localhost:${GRAFANA_PORT}/api/org); do | ||
printf "." | ||
sleep 2 | ||
done | ||
echo "Grafana is up and running." | ||
echo "Creating default influxdb datasource..." | ||
curl -k -i -XPOST -H "${HEADER_ACCEPT}" -H "${HEADER_CONTENT_TYPE}" "${GF_SERVER_PROTOCOL}://${GRAFANA_USER}:${GRAFANA_PASSWD}@localhost:${GRAFANA_PORT}/api/datasources" -d ' | ||
{ | ||
"name": "influxdb-datasource", | ||
"type": "influxdb", | ||
"access": "'"${BACKEND_ACCESS_MODE}"'", | ||
"isDefault": true, | ||
"url": "'"${INFLUXDB_SERVICE_URL}"'", | ||
"password": "'"${INFLUXDB_PASSWORD}"'", | ||
"user": "'"${INFLUXDB_USER}"'", | ||
"database": "'"${INFLUXDB_DATABASE}"'" | ||
}' | ||
|
||
echo "" | ||
echo "Importing default dashboards..." | ||
for filename in ${DASHBOARD_LOCATION}/*.json; do | ||
echo "Importing ${filename} ..." | ||
curl -k -i -XPOST --data "@${filename}" -H "${HEADER_ACCEPT}" -H "${HEADER_CONTENT_TYPE}" "${GF_SERVER_PROTOCOL}://${GRAFANA_USER}:${GRAFANA_PASSWD}@localhost:${GRAFANA_PORT}/api/dashboards/db" | ||
echo "" | ||
echo "Done importing ${filename}" | ||
done | ||
echo "" | ||
echo "Bringing Grafana back to the foreground" | ||
fg | ||
echo "Starting a utility program that will configure Grafana" | ||
setup_grafana >/dev/stdout 2>/dev/stderr & | ||
|
||
echo "Starting Grafana in foreground mode" | ||
exec /usr/sbin/grafana-server \ | ||
--homepath=/usr/share/grafana \ | ||
--config=/etc/grafana/grafana.ini \ | ||
cfg:default.paths.data=/var/lib/grafana \ | ||
cfg:default.paths.logs=/var/log/grafana |
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,143 @@ | ||
// Copyright 2014 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// How many seconds the program should wait before trying to connect to the dashboard again | ||
const RetryTimeout = 5 | ||
|
||
type grafanaConfig struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Access string `json:"access"` | ||
IsDefault bool `json:"isDefault"` | ||
URL string `json:"url"` | ||
Password string `json:"password"` | ||
User string `json:"user"` | ||
Database string `json:"database"` | ||
} | ||
|
||
func main() { | ||
|
||
envParams := map[string]string{ | ||
"grafana_user": "admin", | ||
"grafana_passwd": "admin", | ||
"grafana_port": "3000", | ||
"influxdb_host": "monitoring-influxdb", | ||
"influxdb_port": "8086", | ||
"influxdb_database": "k8s", | ||
"influxdb_user": "root", | ||
"influxdb_password": "root", | ||
"influxdb_service_url": "", | ||
"dashboard_location": "/dashboards", | ||
"gf_auth_anonymous_enabled": "true", | ||
"gf_server_protocol": "http", | ||
"backend_access_mode": "proxy", | ||
} | ||
|
||
for k := range envParams { | ||
if v := os.Getenv(strings.ToUpper(k)); v != "" { | ||
envParams[k] = v | ||
} | ||
} | ||
|
||
if envParams["influxdb_service_url"] == "" { | ||
envParams["influxdb_service_url"] = fmt.Sprintf("http://%s:%s", envParams["influxdb_host"], envParams["influxdb_port"]) | ||
} | ||
|
||
cfg := grafanaConfig{ | ||
Name: "influxdb-datasource", | ||
Type: "influxdb", | ||
Access: envParams["backend_access_mode"], | ||
IsDefault: true, | ||
URL: envParams["influxdb_service_url"], | ||
User: envParams["influxdb_user"], | ||
Password: envParams["influxdb_password"], | ||
Database: envParams["influxdb_database"], | ||
} | ||
|
||
grafanaURL := fmt.Sprintf("%s://%s:%s@localhost:%s", envParams["gf_server_protocol"], envParams["grafana_user"], envParams["grafana_passwd"], envParams["grafana_port"]) | ||
|
||
for { | ||
res, err := http.Get(grafanaURL + "/api/org") | ||
if err != nil { | ||
fmt.Printf("Can't access the Grafana dashboard. Error: %v. Retrying after %d seconds...\n", err, RetryTimeout) | ||
time.Sleep(RetryTimeout * time.Second) | ||
continue | ||
} | ||
|
||
_, err = ioutil.ReadAll(res.Body) | ||
res.Body.Close() | ||
if err != nil { | ||
fmt.Printf("Can't access the Grafana dashboard. Error: %v. Retrying after %d seconds...\n", err, RetryTimeout) | ||
time.Sleep(RetryTimeout * time.Second) | ||
continue | ||
} | ||
|
||
fmt.Println("Connected to the Grafana dashboard.") | ||
break | ||
} | ||
|
||
b := new(bytes.Buffer) | ||
json.NewEncoder(b).Encode(cfg) | ||
|
||
for { | ||
_, err := http.Post(grafanaURL+"/api/datasources", "application/json; charset=utf-8", b) | ||
if err != nil { | ||
fmt.Printf("Failed to configure the Grafana dashboard. Error: %v. Retrying after %d seconds...\n", err, RetryTimeout) | ||
time.Sleep(RetryTimeout * time.Second) | ||
continue | ||
} | ||
|
||
fmt.Println("The datasource for the Grafana dashboard is now set.") | ||
break | ||
} | ||
|
||
dashboardDir := envParams["dashboard_location"] | ||
files, err := ioutil.ReadDir(dashboardDir) | ||
if err != nil { | ||
fmt.Printf("Failed to read the the directory the json files should be in. Exiting... Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
|
||
filePath := filepath.Join(dashboardDir, file.Name()) | ||
jsonbytes, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
fmt.Printf("Failed to read the json file: %s. Proceeding with the next one. Error: %v\n", filePath, err) | ||
continue | ||
} | ||
|
||
_, err = http.Post(grafanaURL+"/api/dashboards/db", "application/json; charset=utf-8", bytes.NewReader(jsonbytes)) | ||
if err != nil { | ||
fmt.Printf("Failed to post the json file: %s. Proceeding with the next one. Error: %v\n", filePath, err) | ||
continue | ||
} | ||
} | ||
} |