Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Adding dashboard directory for grafana dashboards and script to combine the dashboards #432

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkg/provider/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ 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 {
err := provider.CreateGrafanaDashboardsConfigMap()
if err != nil {
return fmt.Errorf("error applying grafana dashboards config err:%v", err)
}

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

return nil
}

// ResourceDelete deletes 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) ResourceDelete(deployments []Resource) error {
err := provider.DeleteGrafanaDashboardsConfigMap()
if err != nil {
return fmt.Errorf("error deleting grafana dashboards config err:%v", err)
}

var err error
for _, deployment := range deployments {
for _, resource := range deployment.Objects {
switch kind := strings.ToLower(resource.GetObjectKind().GroupVersionKind().Kind); kind {
Expand Down
78 changes: 78 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 Down Expand Up @@ -138,3 +144,75 @@ func MergeDeploymentVars(ms ...map[string]string) map[string]string {
}
return res
}

func getK8sClientSet() (*kubernetes.Clientset, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the clientset at pkg/provider/k8s/k8s.go :)

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 err
}
PromBench, err := ioutil.ReadFile("manifests/dashboards/prombench.json")
if err != nil {
return err
}

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 err
}

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
}