K8s/Playlist: Support full CRUD from k8s to existing storage (#75709)

This commit is contained in:
Ryan McKinley
2023-11-01 12:32:24 -07:00
committed by GitHub
parent a59588a62e
commit e3641d925c
10 changed files with 349 additions and 19 deletions

View File

@@ -126,7 +126,16 @@ func (c *K8sTestHelper) AsStatusError(err error) *errors.StatusError {
func (c *K8sResourceClient) SanitizeJSON(v *unstructured.Unstructured) string {
c.t.Helper()
copy := v.DeepCopy().Object
deep := v.DeepCopy()
anno := deep.GetAnnotations()
if anno["grafana.app/originKey"] != "" {
anno["grafana.app/originKey"] = "${originKey}"
}
if anno["grafana.app/updatedTimestamp"] != "" {
anno["grafana.app/updatedTimestamp"] = "${updatedTimestamp}"
}
deep.SetAnnotations(anno)
copy := deep.Object
meta, ok := copy["metadata"].(map[string]any)
require.True(c.t, ok)
@@ -139,6 +148,7 @@ func (c *K8sResourceClient) SanitizeJSON(v *unstructured.Unstructured) string {
}
out, err := json.MarshalIndent(copy, "", " ")
//fmt.Printf("%s", out)
require.NoError(c.t, err)
return string(out)
}

View File

@@ -1,13 +1,17 @@
package playlist
import (
"cmp"
"context"
"encoding/json"
"net/http"
"slices"
"strings"
"testing"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/grafana/grafana/pkg/services/playlist"
@@ -127,6 +131,11 @@ func TestPlaylist(t *testing.T) {
"apiVersion": "playlist.grafana.app/v0alpha1",
"kind": "Playlist",
"metadata": {
"annotations": {
"grafana.app/originKey": "${originKey}",
"grafana.app/originName": "SQL",
"grafana.app/updatedTimestamp": "${updatedTimestamp}"
},
"creationTimestamp": "${creationTimestamp}",
"name": "` + uid + `",
"namespace": "default",
@@ -134,7 +143,6 @@ func TestPlaylist(t *testing.T) {
"uid": "${uid}"
},
"spec": {
"title": "Test",
"interval": "20s",
"items": [
{
@@ -145,7 +153,8 @@ func TestPlaylist(t *testing.T) {
"type": "dashboard_by_tag",
"value": "graph-ng"
}
]
],
"title": "Test"
}
}`
@@ -191,4 +200,172 @@ func TestPlaylist(t *testing.T) {
require.Nil(t, found)
require.Equal(t, metav1.StatusReasonNotFound, statusError.Status().Reason)
})
t.Run("Do CRUD via k8s (and check that legacy api still works)", func(t *testing.T) {
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Editor,
GVR: gvr,
})
// Create the playlist "test"
first, err := client.Resource.Create(context.Background(),
helper.LoadYAMLOrJSONFile("testdata/playlist-test-create.yaml"),
metav1.CreateOptions{},
)
require.NoError(t, err)
require.Equal(t, "test", first.GetName())
uids := []string{first.GetName()}
// Create (with name generation) two playlists
for i := 0; i < 2; i++ {
out, err := client.Resource.Create(context.Background(),
helper.LoadYAMLOrJSONFile("testdata/playlist-generate.yaml"),
metav1.CreateOptions{},
)
require.NoError(t, err)
uids = append(uids, out.GetName())
}
slices.Sort(uids) // make list compare stable
// Check that everything is returned from the List command
list, err := client.Resource.List(context.Background(), metav1.ListOptions{})
require.NoError(t, err)
require.Equal(t, uids, SortSlice(Map(list.Items, func(item unstructured.Unstructured) string {
return item.GetName()
})))
// The legacy endpoint has the same results
searchResponse := apis.DoRequest(helper, apis.RequestParams{
User: client.Args.User,
Method: http.MethodGet,
Path: "/api/playlists",
}, &playlist.Playlists{})
require.NotNil(t, searchResponse.Result)
require.Equal(t, uids, SortSlice(Map(*searchResponse.Result, func(item *playlist.Playlist) string {
return item.UID
})))
// Check all playlists
for _, uid := range uids {
getFromBothAPIs(t, helper, client, uid, nil)
}
// PUT :: Update the title (full payload)
updated, err := client.Resource.Update(context.Background(),
helper.LoadYAMLOrJSONFile("testdata/playlist-test-replace.yaml"),
metav1.UpdateOptions{},
)
require.NoError(t, err)
require.Equal(t, first.GetName(), updated.GetName())
require.Equal(t, first.GetUID(), updated.GetUID())
require.Less(t, first.GetResourceVersion(), updated.GetResourceVersion())
out := getFromBothAPIs(t, helper, client, "test", &playlist.PlaylistDTO{
Name: "Test playlist (replaced from k8s; 22m; 1 items; PUT)",
Interval: "22m",
})
require.Equal(t, updated.GetResourceVersion(), out.GetResourceVersion())
// PATCH :: apply only some fields
updated, err = client.Resource.Apply(context.Background(), "test",
helper.LoadYAMLOrJSONFile("testdata/playlist-test-apply.yaml"),
metav1.ApplyOptions{
Force: true,
FieldManager: "testing",
},
)
require.NoError(t, err)
require.Equal(t, first.GetName(), updated.GetName())
require.Equal(t, first.GetUID(), updated.GetUID())
require.Less(t, first.GetResourceVersion(), updated.GetResourceVersion())
getFromBothAPIs(t, helper, client, "test", &playlist.PlaylistDTO{
Name: "Test playlist (apply from k8s; ??m; ?? items; PATCH)",
Interval: "22m", // has not changed from previous update
})
// Now delete all playlist (three)
for _, uid := range uids {
err := client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
require.NoError(t, err)
// Second call is not found!
err = client.Resource.Delete(context.Background(), uid, metav1.DeleteOptions{})
statusError := helper.AsStatusError(err)
require.Equal(t, metav1.StatusReasonNotFound, statusError.Status().Reason)
// Not found from k8s getter
_, err = client.Resource.Get(context.Background(), uid, metav1.GetOptions{})
statusError = helper.AsStatusError(err)
require.Equal(t, metav1.StatusReasonNotFound, statusError.Status().Reason)
}
// Check that they are all gone
list, err = client.Resource.List(context.Background(), metav1.ListOptions{})
require.NoError(t, err)
require.Empty(t, list.Items)
})
}
// typescript style map function
func Map[A any, B any](input []A, m func(A) B) []B {
output := make([]B, len(input))
for i, element := range input {
output[i] = m(element)
}
return output
}
func SortSlice[A cmp.Ordered](input []A) []A {
slices.Sort(input)
return input
}
// This does a get with both k8s and legacy API, and verifies the results are the same
func getFromBothAPIs(t *testing.T,
helper *apis.K8sTestHelper,
client *apis.K8sResourceClient,
uid string,
// Optionally match some expect some values
expect *playlist.PlaylistDTO,
) *unstructured.Unstructured {
t.Helper()
found, err := client.Resource.Get(context.Background(), uid, metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, uid, found.GetName())
dto := apis.DoRequest(helper, apis.RequestParams{
User: client.Args.User,
Method: http.MethodGet,
Path: "/api/playlists/" + uid,
}, &playlist.PlaylistDTO{}).Result
require.NotNil(t, dto)
require.Equal(t, uid, dto.Uid)
spec, ok := found.Object["spec"].(map[string]any)
require.True(t, ok)
require.Equal(t, dto.Uid, found.GetName())
require.Equal(t, dto.Name, spec["title"])
require.Equal(t, dto.Interval, spec["interval"])
a, errA := json.Marshal(spec["items"])
b, errB := json.Marshal(dto.Items)
require.NoError(t, errA)
require.NoError(t, errB)
require.JSONEq(t, string(a), string(b))
if expect != nil {
if expect.Name != "" {
require.Equal(t, expect.Name, dto.Name)
require.Equal(t, expect.Name, spec["title"])
}
if expect.Interval != "" {
require.Equal(t, expect.Interval, dto.Interval)
require.Equal(t, expect.Interval, spec["interval"])
}
if expect.Uid != "" {
require.Equal(t, expect.Uid, dto.Uid)
require.Equal(t, expect.Uid, found.GetName())
}
}
return found
}

View File

@@ -0,0 +1,12 @@
apiVersion: playlist.grafana.app/v0alpha1
kind: Playlist
metadata:
generateName: x # anything is ok here... except yes or true -- they become boolean!
spec:
title: Playlist with auto generated UID
interval: 5m
items:
- type: dashboard_by_tag
value: panel-tests
- type: dashboard_by_uid
value: vmie2cmWz # dashboard from devenv

View File

@@ -0,0 +1,6 @@
apiVersion: playlist.grafana.app/v0alpha1
kind: Playlist
metadata:
name: test
spec:
title: Test playlist (apply from k8s; ??m; ?? items; PATCH)

View File

@@ -0,0 +1,12 @@
apiVersion: playlist.grafana.app/v0alpha1
kind: Playlist
metadata:
name: test
spec:
title: Test playlist (created from k8s; 2 items; POST)
interval: 5m
items:
- type: dashboard_by_tag
value: panel-tests
- type: dashboard_by_uid
value: vmie2cmWz # dashboard from devenv

View File

@@ -0,0 +1,10 @@
apiVersion: playlist.grafana.app/v0alpha1
kind: Playlist
metadata:
name: test
spec:
title: Test playlist (replaced from k8s; 22m; 1 items; PUT)
interval: 22m
items:
- type: dashboard_by_tag
value: panel-tests