-
Notifications
You must be signed in to change notification settings - Fork 146
feat(ddgr): add dashboard support #2906
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
Open
wdhif
wants to merge
4
commits into
main
Choose a base branch
from
CONTP-1586/wassim.dhif/ddgr_dashboard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,54 @@ | ||
| apiVersion: datadoghq.com/v1alpha1 | ||
| kind: DatadogGenericResource | ||
| metadata: | ||
| name: ddgr-dashboard-sample | ||
| spec: | ||
| type: dashboard | ||
| jsonSpec: |- | ||
| { | ||
| "title": "Example Dashboard (DatadogGenericResource)", | ||
| "layout_type": "ordered", | ||
| "tags": [ | ||
| "team:example" | ||
| ], | ||
| "widgets": [ | ||
| { | ||
| "definition": { | ||
| "type": "timeseries", | ||
| "title": "System CPU Usage", | ||
| "title_size": "16", | ||
| "title_align": "left", | ||
| "show_legend": true, | ||
| "requests": [ | ||
| { | ||
| "formulas": [ | ||
| { | ||
| "formula": "query1" | ||
| } | ||
| ], | ||
| "queries": [ | ||
| { | ||
| "name": "query1", | ||
| "data_source": "metrics", | ||
| "query": "avg:system.cpu.user{*} by {host}" | ||
| } | ||
| ], | ||
| "response_format": "timeseries", | ||
| "style": { | ||
| "palette": "dog_classic", | ||
| "line_type": "solid", | ||
| "line_width": "normal" | ||
| }, | ||
| "display_type": "line" | ||
| } | ||
| ] | ||
| }, | ||
| "layout": { | ||
| "x": 0, | ||
| "y": 0, | ||
| "width": 6, | ||
| "height": 3 | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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,95 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2016-present Datadog, Inc. | ||
|
|
||
| package datadoggenericresource | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
|
|
||
| "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" | ||
| "github.com/go-logr/logr" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1" | ||
| ) | ||
|
|
||
| type DashboardHandler struct{} | ||
|
|
||
| func (h *DashboardHandler) createResourcefunc(r *Reconciler, logger logr.Logger, instance *v1alpha1.DatadogGenericResource, status *v1alpha1.DatadogGenericResourceStatus, now metav1.Time, hash string) error { | ||
| createdDashboard, err := createDashboard(r.datadogAuth, r.datadogDashboardsClient, instance) | ||
| if err != nil { | ||
| logger.Error(err, "error creating dashboard") | ||
| updateErrStatus(status, now, v1alpha1.DatadogSyncStatusCreateError, "CreatingCustomResource", err) | ||
| return err | ||
| } | ||
| logger.Info("created a new dashboard", "dashboard Id", createdDashboard.GetId()) | ||
| updateStatusFromDashboard(createdDashboard, status, hash) | ||
| return nil | ||
| } | ||
|
|
||
| // updateStatusFromDashboard populates the status fields from a Datadog Dashboard API response. | ||
| func updateStatusFromDashboard(dashboard datadogV1.Dashboard, status *v1alpha1.DatadogGenericResourceStatus, hash string) { | ||
| status.Id = dashboard.GetId() | ||
| createdTime := metav1.NewTime(dashboard.GetCreatedAt()) | ||
| status.Created = &createdTime | ||
| status.LastForceSyncTime = &createdTime | ||
| status.Creator = dashboard.GetAuthorHandle() | ||
| status.SyncStatus = v1alpha1.DatadogSyncStatusOK | ||
| status.CurrentHash = hash | ||
| } | ||
|
|
||
| func (h *DashboardHandler) getResourcefunc(r *Reconciler, instance *v1alpha1.DatadogGenericResource) error { | ||
| _, err := getDashboard(r.datadogAuth, r.datadogDashboardsClient, instance.Status.Id) | ||
| return err | ||
| } | ||
|
|
||
| func (h *DashboardHandler) updateResourcefunc(r *Reconciler, instance *v1alpha1.DatadogGenericResource) error { | ||
| _, err := updateDashboard(r.datadogAuth, r.datadogDashboardsClient, instance) | ||
| return err | ||
| } | ||
|
|
||
| func (h *DashboardHandler) deleteResourcefunc(r *Reconciler, instance *v1alpha1.DatadogGenericResource) error { | ||
| return deleteDashboard(r.datadogAuth, r.datadogDashboardsClient, instance.Status.Id) | ||
| } | ||
|
|
||
| func getDashboard(auth context.Context, client *datadogV1.DashboardsApi, dashboardID string) (datadogV1.Dashboard, error) { | ||
| dashboard, _, err := client.GetDashboard(auth, dashboardID) | ||
| if err != nil { | ||
| return datadogV1.Dashboard{}, translateClientError(err, "error getting dashboard") | ||
| } | ||
| return dashboard, nil | ||
| } | ||
|
|
||
| func createDashboard(auth context.Context, client *datadogV1.DashboardsApi, instance *v1alpha1.DatadogGenericResource) (datadogV1.Dashboard, error) { | ||
| dashboardCreateData := &datadogV1.Dashboard{} | ||
| if err := json.Unmarshal([]byte(instance.Spec.JsonSpec), dashboardCreateData); err != nil { | ||
| return datadogV1.Dashboard{}, translateClientError(err, "error unmarshalling dashboard spec") | ||
| } | ||
| dashboard, _, err := client.CreateDashboard(auth, *dashboardCreateData) | ||
| if err != nil { | ||
|
wdhif marked this conversation as resolved.
|
||
| return datadogV1.Dashboard{}, translateClientError(err, "error creating dashboard") | ||
| } | ||
| return dashboard, nil | ||
| } | ||
|
|
||
| func updateDashboard(auth context.Context, client *datadogV1.DashboardsApi, instance *v1alpha1.DatadogGenericResource) (datadogV1.Dashboard, error) { | ||
| dashboardUpdateData := &datadogV1.Dashboard{} | ||
| if err := json.Unmarshal([]byte(instance.Spec.JsonSpec), dashboardUpdateData); err != nil { | ||
| return datadogV1.Dashboard{}, translateClientError(err, "error unmarshalling dashboard spec") | ||
| } | ||
| dashboardUpdated, _, err := client.UpdateDashboard(auth, instance.Status.Id, *dashboardUpdateData) | ||
| if err != nil { | ||
| return datadogV1.Dashboard{}, translateClientError(err, "error updating dashboard") | ||
|
wdhif marked this conversation as resolved.
|
||
| } | ||
| return dashboardUpdated, nil | ||
| } | ||
|
|
||
| func deleteDashboard(auth context.Context, client *datadogV1.DashboardsApi, dashboardID string) error { | ||
| if _, _, err := client.DeleteDashboard(auth, dashboardID); err != nil { | ||
| return translateClientError(err, "error deleting dashboard") | ||
| } | ||
| return nil | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
internal/controller/datadoggenericresource/dashboards_test.go
This file contains hidden or 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,78 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2016-present Datadog, Inc. | ||
|
|
||
| package datadoggenericresource | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1" | ||
| ) | ||
|
|
||
| func Test_updateStatusFromDashboard(t *testing.T) { | ||
| hash := "test-hash" | ||
| createdAt := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| dashboard datadogV1.Dashboard | ||
| expectedStatus v1alpha1.DatadogGenericResourceStatus | ||
| }{ | ||
| { | ||
| name: "all fields populated", | ||
| dashboard: func() datadogV1.Dashboard { | ||
| d := datadogV1.Dashboard{} | ||
| d.SetId("abc-123") | ||
| d.SetAuthorHandle("user@example.com") | ||
| d.SetCreatedAt(createdAt) | ||
| return d | ||
| }(), | ||
| expectedStatus: v1alpha1.DatadogGenericResourceStatus{ | ||
| Id: "abc-123", | ||
| Creator: "user@example.com", | ||
| SyncStatus: v1alpha1.DatadogSyncStatusOK, | ||
| CurrentHash: hash, | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing author handle", | ||
| dashboard: func() datadogV1.Dashboard { | ||
| d := datadogV1.Dashboard{} | ||
| d.SetId("abc-456") | ||
| d.SetCreatedAt(createdAt) | ||
| return d | ||
| }(), | ||
| expectedStatus: v1alpha1.DatadogGenericResourceStatus{ | ||
| Id: "abc-456", | ||
| Creator: "", | ||
| SyncStatus: v1alpha1.DatadogSyncStatusOK, | ||
| CurrentHash: hash, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| status := &v1alpha1.DatadogGenericResourceStatus{} | ||
| updateStatusFromDashboard(tt.dashboard, status, hash) | ||
|
|
||
| assert.Equal(t, tt.expectedStatus.Id, status.Id) | ||
| assert.Equal(t, tt.expectedStatus.Creator, status.Creator) | ||
| assert.Equal(t, tt.expectedStatus.SyncStatus, status.SyncStatus) | ||
| assert.Equal(t, tt.expectedStatus.CurrentHash, status.CurrentHash) | ||
| assert.Equal(t, createdAt, status.Created.Time) | ||
| assert.Equal(t, createdAt, status.LastForceSyncTime.Time) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_DashboardHandler_getHandler(t *testing.T) { | ||
| handler := getHandler(v1alpha1.Dashboard) | ||
| assert.IsType(t, &DashboardHandler{}, handler) | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.