K8s: Implement playlist api with k8s client (#77405)

This commit is contained in:
Ryan McKinley
2023-10-31 10:26:39 -07:00
committed by GitHub
parent 254648b96b
commit dd773e74f1
15 changed files with 294 additions and 53 deletions

View File

@@ -1,16 +1,48 @@
package v0alpha1
import (
"encoding/json"
"fmt"
"strconv"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"github.com/grafana/grafana/pkg/kinds"
"github.com/grafana/grafana/pkg/services/grafana-apiserver/endpoints/request"
"github.com/grafana/grafana/pkg/services/playlist"
)
func UnstructuredToLegacyPlaylist(item unstructured.Unstructured) *playlist.Playlist {
spec := item.Object["spec"].(map[string]any)
return &playlist.Playlist{
UID: item.GetName(),
Name: spec["title"].(string),
Interval: spec["interval"].(string),
Id: getLegacyID(&item),
}
}
func UnstructuredToLegacyPlaylistDTO(item unstructured.Unstructured) *playlist.PlaylistDTO {
spec := item.Object["spec"].(map[string]any)
dto := &playlist.PlaylistDTO{
Uid: item.GetName(),
Name: spec["title"].(string),
Interval: spec["interval"].(string),
Id: getLegacyID(&item),
}
items := spec["items"]
if items != nil {
b, err := json.Marshal(items)
if err == nil {
_ = json.Unmarshal(b, &dto.Items)
}
}
return dto
}
func convertToK8sResource(v *playlist.PlaylistDTO, namespacer request.NamespaceMapper) *Playlist {
spec := Spec{
Title: v.Name,
@@ -22,6 +54,15 @@ func convertToK8sResource(v *playlist.PlaylistDTO, namespacer request.NamespaceM
Value: item.Value,
})
}
meta := kinds.GrafanaResourceMetadata{}
meta.SetUpdatedTimestampMillis(v.UpdatedAt)
if v.Id > 0 {
meta.SetOriginInfo(&kinds.ResourceOriginInfo{
Name: "SQL",
Key: fmt.Sprintf("%d", v.Id),
})
}
return &Playlist{
ObjectMeta: metav1.ObjectMeta{
Name: v.Uid,
@@ -29,7 +70,23 @@ func convertToK8sResource(v *playlist.PlaylistDTO, namespacer request.NamespaceM
ResourceVersion: fmt.Sprintf("%d", v.UpdatedAt),
CreationTimestamp: metav1.NewTime(time.UnixMilli(v.CreatedAt)),
Namespace: namespacer(v.OrgID),
Annotations: meta.Annotations,
},
Spec: spec,
}
}
// Read legacy ID from metadata annotations
func getLegacyID(item *unstructured.Unstructured) int64 {
meta := kinds.GrafanaResourceMetadata{
Annotations: item.GetAnnotations(),
}
info := meta.GetOriginInfo()
if info != nil && info.Name == "SQL" {
i, err := strconv.ParseInt(info.Key, 10, 64)
if err == nil {
return i
}
}
return 0
}

View File

@@ -12,6 +12,7 @@ import (
func TestPlaylistConversion(t *testing.T) {
src := &playlist.PlaylistDTO{
Id: 123,
OrgID: 3,
Uid: "abc", // becomes k8s name
Name: "MyPlaylists", // becomes title
@@ -32,14 +33,19 @@ func TestPlaylistConversion(t *testing.T) {
out, err := json.MarshalIndent(dst, "", " ")
require.NoError(t, err)
//fmt.Printf("%s", string(out))
// fmt.Printf("%s", string(out))
require.JSONEq(t, `{
"metadata": {
"name": "abc",
"namespace": "org-3",
"uid": "abc",
"resourceVersion": "54321",
"creationTimestamp": "1970-01-01T00:00:12Z"
"creationTimestamp": "1970-01-01T00:00:12Z",
"annotations": {
"grafana.app/originKey": "123",
"grafana.app/originName": "SQL",
"grafana.app/updatedTimestamp": "1970-01-01T00:00:54Z"
}
},
"spec": {
"title": "MyPlaylists",