Folders/K8s: Fix createdBy and updatedBy fields in response (#99569)

This commit is contained in:
Arati R.
2025-01-28 10:46:07 -06:00
committed by GitHub
parent d81b1bf803
commit 94a844977e
6 changed files with 143 additions and 72 deletions
-49
View File
@@ -6,13 +6,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
authlib "github.com/grafana/authlib/types"
"github.com/grafana/grafana/pkg/apimachinery/utils"
"github.com/grafana/grafana/pkg/apis/folder/v0alpha1"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
gapiutil "github.com/grafana/grafana/pkg/services/apiserver/utils"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/util"
)
@@ -41,52 +38,6 @@ func LegacyCreateCommandToUnstructured(cmd *folder.CreateFolderCommand) (*unstru
return obj, nil
}
func UnstructuredToLegacyFolder(item *unstructured.Unstructured) (*folder.Folder, error) {
meta, err := utils.MetaAccessor(item)
if err != nil {
return nil, err
}
info, _ := authlib.ParseNamespace(meta.GetNamespace())
if info.OrgID < 0 {
info.OrgID = 1 // This resolves all test cases that assume org 1
}
title, _, _ := unstructured.NestedString(item.Object, "spec", "title")
description, _, _ := unstructured.NestedString(item.Object, "spec", "description")
uid := meta.GetName()
url := ""
if uid != folder.RootFolder.UID {
slug := slugify.Slugify(title)
url = dashboards.GetFolderURL(uid, slug)
}
created := meta.GetCreationTimestamp().Time.UTC()
updated, _ := meta.GetUpdatedTimestamp()
if updated == nil {
updated = &created
} else {
tmp := updated.UTC()
updated = &tmp
}
return &folder.Folder{
UID: uid,
Title: title,
Description: description,
ID: meta.GetDeprecatedInternalID(), // nolint:staticcheck
ParentUID: meta.GetFolder(),
Version: int(meta.GetGeneration()),
Repository: meta.GetRepositoryName(),
URL: url,
Created: created,
Updated: *updated,
OrgID: info.OrgID,
}, nil
}
func LegacyFolderToUnstructured(v *folder.Folder, namespacer request.NamespaceMapper) (*v0alpha1.Folder, error) {
return convertToK8sResource(v, namespacer)
}
@@ -1,62 +0,0 @@
package folders
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/grafana/grafana/pkg/services/folder"
)
func TestFolderConversions(t *testing.T) {
input := &unstructured.Unstructured{}
err := input.UnmarshalJSON([]byte(`{
"kind": "Folder",
"apiVersion": "folder.grafana.app/v0alpha1",
"metadata": {
"name": "be79sztagf20wd",
"namespace": "default",
"uid": "wfi3RARqQREzEKtUJCWurWevwbQ7i9ii0cA7JUIbMtEX",
"resourceVersion": "1734509107000",
"creationTimestamp": "2022-12-02T02:02:02Z",
"generation": 4,
"labels": {
"grafana.app/deprecatedInternalID": "234"
},
"annotations": {
"grafana.app/folder": "parent-folder-name",
"grafana.app/updatedTimestamp": "2022-12-02T07:02:02Z",
"grafana.app/repoName": "example-repo",
"grafana.app/createdBy": "user:abc",
"grafana.app/updatedBy": "service:xyz"
}
},
"spec": {
"title": "test folder",
"description": "Something set in the file"
}
}`))
require.NoError(t, err)
created, err := time.Parse(time.RFC3339, "2022-12-02T02:02:02Z")
created = created.UTC()
require.NoError(t, err)
converted, err := UnstructuredToLegacyFolder(input)
require.NoError(t, err)
require.Equal(t, folder.Folder{
ID: 234,
OrgID: 1,
Version: 4,
UID: "be79sztagf20wd",
ParentUID: "parent-folder-name",
Title: "test folder",
Description: "Something set in the file",
URL: "/dashboards/f/be79sztagf20wd/test-folder",
Repository: "example-repo",
Created: created,
Updated: created.Add(time.Hour * 5),
}, *converted)
}