forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
K8s/Dashboards: Delegate large objects to blob store (grafana#94943)
- Loading branch information
Showing
20 changed files
with
442 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 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,52 @@ | ||
package dashboard | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
commonV0 "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1" | ||
dashboard "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1" | ||
"github.com/grafana/grafana/pkg/storage/unified/apistore" | ||
) | ||
|
||
func newDashboardLargeObjectSupport() *apistore.BasicLargeObjectSupport { | ||
return &apistore.BasicLargeObjectSupport{ | ||
TheGroupResource: dashboard.DashboardResourceInfo.GroupResource(), | ||
|
||
// byte size, while testing lets do almost everything (10bytes) | ||
ThresholdSize: 10, | ||
|
||
// 10mb -- we should check what the largest ones are... might be bigger | ||
MaxByteSize: 10 * 1024 * 1024, | ||
|
||
ReduceSpec: func(obj runtime.Object) error { | ||
dash, ok := obj.(*dashboard.Dashboard) | ||
if !ok { | ||
return fmt.Errorf("expected dashboard") | ||
} | ||
old := dash.Spec.Object | ||
spec := commonV0.Unstructured{Object: make(map[string]any)} | ||
dash.Spec = spec | ||
dash.SetManagedFields(nil) // this could be bigger than the object! | ||
|
||
keep := []string{"title", "description", "schemaVersion"} | ||
for _, k := range keep { | ||
v, ok := old[k] | ||
if ok { | ||
spec.Object[k] = v | ||
} | ||
} | ||
return nil | ||
}, | ||
|
||
RebuildSpec: func(obj runtime.Object, blob []byte) error { | ||
dash, ok := obj.(*dashboard.Dashboard) | ||
if !ok { | ||
return fmt.Errorf("expected dashboard") | ||
} | ||
return json.Unmarshal(blob, &dash.Spec) | ||
}, | ||
} | ||
} |
This file contains 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,60 @@ | ||
package dashboard | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
dashboard "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1" | ||
) | ||
|
||
func TestLargeDashboardSupport(t *testing.T) { | ||
devdash := "../../../../devenv/dev-dashboards/all-panels.json" | ||
|
||
// nolint:gosec | ||
// We can ignore the gosec G304 warning because this is a test with hardcoded input values | ||
f, err := os.ReadFile(devdash) | ||
require.NoError(t, err) | ||
|
||
dash := &dashboard.Dashboard{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: "test", | ||
}, | ||
} | ||
err = json.Unmarshal(f, &dash.Spec) | ||
require.NoError(t, err) | ||
|
||
expectedPanelCount := 19 | ||
panels, found, err := unstructured.NestedSlice(dash.Spec.Object, "panels") | ||
require.NoError(t, err) | ||
require.True(t, found) | ||
require.Len(t, panels, expectedPanelCount) | ||
|
||
largeObject := newDashboardLargeObjectSupport() | ||
|
||
// Convert the dashboard to a small value | ||
err = largeObject.ReduceSpec(dash) | ||
require.NoError(t, err) | ||
|
||
small, err := json.MarshalIndent(&dash.Spec, "", " ") | ||
require.NoError(t, err) | ||
require.JSONEq(t, `{ | ||
"schemaVersion": 33, | ||
"title": "Panel tests - All panels" | ||
}`, string(small)) | ||
|
||
// Now make it big again | ||
err = largeObject.RebuildSpec(dash, f) | ||
require.NoError(t, err) | ||
|
||
// check that all panels exist again | ||
panels, found, err = unstructured.NestedSlice(dash.Spec.Object, "panels") | ||
require.NoError(t, err) | ||
require.True(t, found) | ||
require.Len(t, panels, expectedPanelCount) | ||
} |
This file contains 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 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 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 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 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 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 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 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 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.