Skip to content

Commit

Permalink
Moving grafana-dashbaord configmap to dashboards directory with speci…
Browse files Browse the repository at this point in the history
…fic json file. Adding create and delete configmap function to combine prombench.json and node-metrics.json into an single configmap

Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com>
Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com>
  • Loading branch information
imrajdas committed Aug 27, 2020
1 parent 1d0d20b commit 7f2a543
Show file tree
Hide file tree
Showing 5 changed files with 5,643 additions and 5,558 deletions.
14 changes: 13 additions & 1 deletion pkg/provider/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,13 @@ func (c *K8s) DeploymentsParse(*kingpin.ParseContext) error {
// ResourceApply applies k8s objects.
// The input is a slice of structs containing the filename and the slice of k8s objects present in the file.
func (c *K8s) ResourceApply(deployments []Resource) error {

var err error

err = provider.CreateGrafanaDashboardsConfigMap()
if err != nil {
return fmt.Errorf("error applying grafana dashboards config err:%v", err)
}

for _, deployment := range deployments {
for _, resource := range deployment.Objects {
switch kind := strings.ToLower(resource.GetObjectKind().GroupVersionKind().Kind); kind {
Expand Down Expand Up @@ -190,6 +195,7 @@ func (c *K8s) ResourceApply(deployments []Resource) error {
}
}
}

return nil
}

Expand All @@ -198,6 +204,12 @@ func (c *K8s) ResourceApply(deployments []Resource) error {
func (c *K8s) ResourceDelete(deployments []Resource) error {

var err error

err = provider.DeleteGrafanaDashboardsConfigMap()
if err != nil {
return fmt.Errorf("error deleting grafana dashboards config err:%v", err)
}

for _, deployment := range deployments {
for _, resource := range deployment.Objects {
switch kind := strings.ToLower(resource.GetObjectKind().GroupVersionKind().Kind); kind {
Expand Down
80 changes: 80 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ package provider

import (
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"log"
"os"
"path/filepath"
Expand All @@ -39,6 +45,8 @@ type DeploymentResource struct {
FlagDeploymentVars map[string]string
// Default DeploymentVars.
DefaultDeploymentVars map[string]string
// Dashboards
Dashboards map[string]string
}

// NewDeploymentResource returns DeploymentResource with default values.
Expand Down Expand Up @@ -138,3 +146,75 @@ func MergeDeploymentVars(ms ...map[string]string) map[string]string {
}
return res
}

func getK8sClientSet() (*kubernetes.Clientset, error) {
kubeconfig := flag.String("kubeconfig", "/home/raj/.kube/config", "absolute path to the kubeconfig file")

config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
return nil, err
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return clientset, nil
}

func CreateGrafanaDashboardsConfigMap() error {

clientset, err := getK8sClientSet()
if err != nil {
return err
}

NodeMetrics, err := ioutil.ReadFile("manifests/dashboards/node-metrics.json")
if err != nil {
return nil
}
PromBench, err := ioutil.ReadFile("manifests/dashboards/prombench.json")
if err != nil {
return nil
}

ConfigMapData := map[string]string{
"node-metrics.json": string(NodeMetrics),
"prombench.json": string(PromBench),
}

newConfigMap := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "grafana-dashboards",
},
Data: ConfigMapData,
}

_, err = clientset.CoreV1().ConfigMaps("default").Create(context.TODO(), &newConfigMap, metav1.CreateOptions{})
if err != nil {
return nil
}

log.Printf("resource created - kind: ConfigMap, name: grafana-dashboards")
return nil
}

func DeleteGrafanaDashboardsConfigMap() error {

clientset, err := getK8sClientSet()
if err != nil {
return err
}

err = clientset.CoreV1().ConfigMaps("default").Delete(context.TODO(), "grafana-dashboards", metav1.DeleteOptions{})
if err != nil {
return err
}

log.Printf("resource created - kind: ConfigMap, name: grafana-dashboards")
return nil
}
Loading

0 comments on commit 7f2a543

Please sign in to comment.