Skip to content

Commit

Permalink
Track dashboards per namespace in ControllerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
addreas committed Feb 16, 2022
1 parent 1b94fe6 commit 4ffc730
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
62 changes: 37 additions & 25 deletions controllers/config/controller_config.go
Expand Up @@ -40,7 +40,7 @@ type ControllerConfig struct {
*sync.Mutex
Values map[string]interface{}
Plugins map[string]v1alpha1.PluginList
Dashboards []*v1alpha1.GrafanaDashboardRef
Dashboards map[string][]*v1alpha1.GrafanaDashboardRef
}

var instance *ControllerConfig
Expand All @@ -52,7 +52,7 @@ func GetControllerConfig() *ControllerConfig {
Mutex: &sync.Mutex{},
Values: map[string]interface{}{},
Plugins: map[string]v1alpha1.PluginList{},
Dashboards: []*v1alpha1.GrafanaDashboardRef{},
Dashboards: map[string][]*v1alpha1.GrafanaDashboardRef{},
}
})
return instance
Expand Down Expand Up @@ -86,17 +86,17 @@ func (c *ControllerConfig) SetPluginsFor(dashboard *v1alpha1.GrafanaDashboard) {
c.Plugins[id] = dashboard.Spec.Plugins
}

func (c *ControllerConfig) RemovePluginsFor(namespace, name string) {
id := c.GetDashboardId(namespace, name)
func (c *ControllerConfig) RemovePluginsFor(dashboard *v1alpha1.GrafanaDashboardRef) {
id := c.GetDashboardId(dashboard.Namespace, dashboard.Name)
delete(c.Plugins, id)
}

func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, folderId *int64, folderName string) {
ns := dashboard.Namespace
if i, exists := c.HasDashboard(dashboard.UID()); !exists {
if i, exists := c.HasDashboard(ns, dashboard.UID()); !exists {
c.Lock()
defer c.Unlock()
c.Dashboards = append(c.Dashboards, &v1alpha1.GrafanaDashboardRef{
c.Dashboards[ns] = append(c.Dashboards[ns], &v1alpha1.GrafanaDashboardRef{
Name: dashboard.Name,
Namespace: ns,
UID: dashboard.UID(),
Expand All @@ -107,7 +107,7 @@ func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, fo
} else {
c.Lock()
defer c.Unlock()
c.Dashboards[i] = &v1alpha1.GrafanaDashboardRef{
c.Dashboards[ns][i] = &v1alpha1.GrafanaDashboardRef{
Name: dashboard.Name,
Namespace: ns,
UID: dashboard.UID(),
Expand All @@ -118,9 +118,9 @@ func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, fo
}
}

func (c *ControllerConfig) HasDashboard(str string) (int, bool) {
for i, v := range c.Dashboards {
if v.UID == str {
func (c *ControllerConfig) HasDashboard(ns, uid string) (int, bool) {
for i, v := range c.Dashboards[ns] {
if v.UID == uid {
return i, true
}
}
Expand All @@ -130,25 +130,35 @@ func (c *ControllerConfig) HasDashboard(str string) (int, bool) {
func (c *ControllerConfig) InvalidateDashboards() {
c.Lock()
defer c.Unlock()
for _, v := range c.Dashboards {
v.Hash = ""
for _, ds := range c.Dashboards {
for _, v := range ds {
v.Hash = ""
}
}
}

func (c *ControllerConfig) SetDashboards(dashboards []*v1alpha1.GrafanaDashboardRef) {
func (c *ControllerConfig) SetDashboards(ns string, dashboards []*v1alpha1.GrafanaDashboardRef) {
c.Lock()
defer c.Unlock()
c.Dashboards = dashboards
if ns == "" {
for nss := range c.Dashboards {
c.Dashboards[nss] = dashboards
}
} else {
c.Dashboards[ns] = dashboards
}
}

func (c *ControllerConfig) RemoveDashboard(hash string) {
if i, exists := c.HasDashboard(hash); exists {
c.Lock()
defer c.Unlock()
list := c.Dashboards
list[i] = list[len(list)-1]
list = list[:len(list)-1]
c.Dashboards = list
for ns, _ := range c.Dashboards {
if i, exists := c.HasDashboard(ns, hash); exists {
c.Lock()
defer c.Unlock()
list := c.Dashboards[ns]
list[i] = list[len(list)-1]
list = list[:len(list)-1]
c.Dashboards[ns] = list
}
}
}

Expand All @@ -158,13 +168,15 @@ func (c *ControllerConfig) GetDashboards(namespace string) []*v1alpha1.GrafanaDa
// Checking for dashboards at the cluster level? across namespaces?
if namespace == "" {
var dashboards []*v1alpha1.GrafanaDashboardRef
dashboards = append(dashboards, c.Dashboards...)
for _, ds := range c.Dashboards {
dashboards = append(dashboards, ds...)
}

return dashboards
}

if c.Dashboards != nil {
return c.Dashboards
if c.Dashboards[namespace] != nil {
return c.Dashboards[namespace]
}
return []*v1alpha1.GrafanaDashboardRef{}
}
Expand Down Expand Up @@ -221,7 +233,7 @@ func (c *ControllerConfig) HasConfigItem(key string) bool {
func (c *ControllerConfig) Cleanup(plugins bool) {
c.Lock()
defer c.Unlock()
c.Dashboards = []*v1alpha1.GrafanaDashboardRef{}
c.Dashboards = map[string][]*v1alpha1.GrafanaDashboardRef{}

if plugins {
c.Plugins = map[string]v1alpha1.PluginList{}
Expand Down
4 changes: 2 additions & 2 deletions controllers/grafana/grafana_controller.go
Expand Up @@ -283,10 +283,10 @@ func (r *ReconcileGrafana) manageSuccess(cr *grafanav1alpha1.Grafana, state *com
// Only update the status if the dashboard controller had a chance to sync the cluster
// dashboards first. Otherwise reuse the existing dashboard config from the CR.
if r.Config.GetConfigBool(config.ConfigGrafanaDashboardsSynced, false) {
cr.Status.InstalledDashboards = r.Config.Dashboards
cr.Status.InstalledDashboards = r.Config.GetDashboards(request.Namespace)
} else {
if r.Config.Dashboards == nil {
r.Config.SetDashboards([]*grafanav1alpha1.GrafanaDashboardRef{})
r.Config.SetDashboards(request.Namespace, []*grafanav1alpha1.GrafanaDashboardRef{})
}
}

Expand Down
Expand Up @@ -363,7 +363,7 @@ func (r *GrafanaDashboardReconciler) reconcileDashboards(request reconcile.Reque

log.Log.Info(fmt.Sprintf("delete result was %v", *status.Message))

r.config.RemovePluginsFor(dashboard.Namespace, dashboard.Name)
r.config.RemovePluginsFor(dashboard)
r.config.RemoveDashboard(dashboard.UID)

// Mark the dashboards as synced so that the current state can be written
Expand Down

0 comments on commit 4ffc730

Please sign in to comment.