Skip to content

Commit

Permalink
feat:rebase changes from @d-kuro
Browse files Browse the repository at this point in the history
  • Loading branch information
hubeadmin committed Nov 5, 2021
1 parent b623b07 commit cbd2a03
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 269 deletions.
12 changes: 12 additions & 0 deletions api/integreatly/v1alpha1/grafana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,18 @@ type GrafanaStatus struct {
InstalledPlugins PluginList `json:"installedPlugins,omitempty"`
// +nullable
FailedPlugins PluginList `json:"failedPlugins,omitempty"`
// +nullable
Ready *bool `json:"ready,omitempty"`
// +nullable
AdminURL *string `json:"adminUrl,omitempty"`
AdminUser *SecretKeyRef `json:"adminUser,omitempty"`
AdminPassword *SecretKeyRef `json:"adminPassword,omitempty"`
}

// SecretKeyRef indicates a reference to Secret.
type SecretKeyRef struct {
SecretName string `json:"secretName"`
Key string `json:"key"`
}

// GrafanaPlugin contains information about a single plugin
Expand Down
2 changes: 1 addition & 1 deletion api/integreatly/v1alpha1/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (d *GrafanaDashboard) matchesSelector(s *metav1.LabelSelector) (bool, error
return selector.Empty() || selector.Matches(labels.Set(d.Labels)), nil
}

// Check if the dashboard matches at least one of the selectors
// MatchesSelectors check if the dashboard matches at least one of the selectors.
func (d *GrafanaDashboard) MatchesSelectors(s []*metav1.LabelSelector) (bool, error) {
result := false

Expand Down
48 changes: 28 additions & 20 deletions controllers/config/controller_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ func (c *ControllerConfig) RemovePluginsFor(namespace, name string) {
delete(c.Plugins, id)
}

func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, folderId *int64, folderName string) {
func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, grafana *v1alpha1.Grafana, folderId *int64, folderName string) []*v1alpha1.GrafanaDashboardRef {
var dashboardRefs []*v1alpha1.GrafanaDashboardRef
ns := dashboard.Namespace
if i, exists := c.HasDashboard(dashboard.UID()); !exists {
c.Lock()
defer c.Unlock()
c.Dashboards = append(c.Dashboards, &v1alpha1.GrafanaDashboardRef{

if grafana.Status.InstalledDashboards != nil {
copy(dashboardRefs, grafana.Status.InstalledDashboards)
}

if _, exists := c.HasDashboard(grafana, dashboard.UID()); !exists {
dashboardRefs = append(dashboardRefs, &v1alpha1.GrafanaDashboardRef{
Name: dashboard.Name,
Namespace: ns,
UID: dashboard.UID(),
Expand All @@ -105,25 +109,26 @@ func (c *ControllerConfig) AddDashboard(dashboard *v1alpha1.GrafanaDashboard, fo
FolderName: dashboard.Spec.CustomFolderName,
})
} else {
c.Lock()
defer c.Unlock()
c.Dashboards[i] = &v1alpha1.GrafanaDashboardRef{
dashboardRefs = append(dashboardRefs, &v1alpha1.GrafanaDashboardRef{
Name: dashboard.Name,
Namespace: ns,
UID: dashboard.UID(),
Hash: dashboard.Hash(),
FolderId: folderId,
FolderName: folderName,
}
})
}

return dashboardRefs
}

func (c *ControllerConfig) HasDashboard(str string) (int, bool) {
for i, v := range c.Dashboards {
if v.UID == str {
func (c *ControllerConfig) HasDashboard(grafana *v1alpha1.Grafana, uid string) (int, bool) {
for i, d := range grafana.Status.InstalledDashboards {
if d.UID == uid {
return i, true
}
}

return -1, false
}

Expand All @@ -141,15 +146,18 @@ func (c *ControllerConfig) SetDashboards(dashboards []*v1alpha1.GrafanaDashboard
c.Dashboards = 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
func (c *ControllerConfig) RemoveDashboard(grafana *v1alpha1.Grafana, uid string) []*v1alpha1.GrafanaDashboardRef {
var dashboardRefs []*v1alpha1.GrafanaDashboardRef

if grafana.Status.InstalledDashboards != nil {
copy(dashboardRefs, grafana.Status.InstalledDashboards)
}

if i, exists := c.HasDashboard(grafana, uid); exists {
dashboardRefs = append(dashboardRefs[:i], dashboardRefs[i+1:]...)
}

return dashboardRefs
}

func (c *ControllerConfig) GetDashboards(namespace string) []*v1alpha1.GrafanaDashboardRef {
Expand Down
1 change: 1 addition & 0 deletions controllers/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ const (
GrafanaHttpPort int = 3000
GrafanaHttpPortName = "grafana"
GrafanaSuccessMsg = "success"
GrafanaDefaultClientTimeoutSeconds int = 5
)
15 changes: 7 additions & 8 deletions controllers/grafanadashboard/dashboard_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@ type DashboardPipelineImpl struct {
Context context.Context
}

func NewDashboardPipeline(client client.Client, dashboard *v1alpha1.GrafanaDashboard, ctx context.Context) DashboardPipeline {
func NewDashboardPipeline(client client.Client, dashboard *v1alpha1.GrafanaDashboard) *DashboardPipelineImpl {
return &DashboardPipelineImpl{
Client: client,
Dashboard: dashboard,
JSON: "",
Logger: log.Log.WithName(fmt.Sprintf("dashboard-%v", dashboard.Name)),
Context: ctx,
}
}

func (r *DashboardPipelineImpl) ProcessDashboard(knownHash string, folderId *int64, folderName string, forceRecreate bool) ([]byte, error) {
err := r.obtainJson()
func (r *DashboardPipelineImpl) ProcessDashboard(ctx context.Context, knownHash string, folderId *int64, folderName string) ([]byte, error) {
err := r.obtainJson(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,7 +117,7 @@ func (r *DashboardPipelineImpl) validateJson() error {
// 2) url or grafanaCom fails or not provided: try to fetch from configmap ref
// 3) no configmap specified: try to use embedded json
// 4) no json specified: try to use embedded jsonnet
func (r *DashboardPipelineImpl) obtainJson() error {
func (r *DashboardPipelineImpl) obtainJson(ctx context.Context) error {
// TODO(DeanBrunt): Add earlier validation for this
if r.Dashboard.Spec.Url != "" && r.Dashboard.Spec.GrafanaCom != nil {
return errors.New("both dashboard url and grafana.com source specified")
Expand All @@ -133,7 +132,7 @@ func (r *DashboardPipelineImpl) obtainJson() error {
}

if r.Dashboard.Spec.Url != "" {
err := r.loadDashboardFromURL()
err := r.loadDashboardFromURL(ctx)
if err != nil {
r.Logger.Error(err, "failed to request dashboard url, falling back to config map; if specified")
} else {
Expand Down Expand Up @@ -184,7 +183,7 @@ func (r *DashboardPipelineImpl) loadJsonnet(source string) (string, error) {
}

// Try to obtain the dashboard json from a provided url
func (r *DashboardPipelineImpl) loadDashboardFromURL() error {
func (r *DashboardPipelineImpl) loadDashboardFromURL(ctx context.Context) error {
url, err := url.ParseRequestURI(r.Dashboard.Spec.Url)
if err != nil {
return fmt.Errorf("invalid url %v", r.Dashboard.Spec.Url)
Expand Down Expand Up @@ -221,7 +220,7 @@ func (r *DashboardPipelineImpl) loadDashboardFromURL() error {
// Update dashboard spec so that URL would not be refetched
if r.JSON != r.Dashboard.Spec.Json {
r.Dashboard.Spec.Json = r.JSON
err := r.Client.Update(r.Context, r.Dashboard)
err := r.Client.Update(ctx, r.Dashboard)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions controllers/grafanadashboard/grafana_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grafanadashboard
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -14,6 +15,14 @@ import (
"github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
)

var (
// ErrFolderNotFound is an error indicating that the Folder API returned a status code 404.
ErrFolderNotFound = errors.New("folder not found")

// ErrDashboardNotFound is an error indicating that the Dashboard API returned a status code 404.
ErrDashboardNotFound = errors.New("dashboard not found")
)

const (
DeleteDashboardByUIDUrl = "%v/api/dashboards/uid/%v"
CreateOrUpdateDashboardUrl = "%v/api/dashboards/db"
Expand Down Expand Up @@ -347,6 +356,10 @@ func (r *GrafanaClientImpl) DeleteDashboardByUID(UID string) (GrafanaResponse, e
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return response, ErrDashboardNotFound
}

if resp.StatusCode != 200 {
return response, fmt.Errorf(
"error deleting dashboard, expected status 200 but got %v",
Expand Down Expand Up @@ -483,6 +496,10 @@ func (r *GrafanaClientImpl) DeleteFolder(deleteID *int64) error {
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return ErrFolderNotFound
}

if resp.StatusCode != 200 {
return fmt.Errorf(
"error deleting folder, expected status 200 but got %v",
Expand Down
Loading

0 comments on commit cbd2a03

Please sign in to comment.