From 960c13c1e5d7f95c4bf4e29edee4279b6a0fb1ea Mon Sep 17 00:00:00 2001 From: Haris Rozajac <58232930+harisrozajac@users.noreply.github.com> Date: Wed, 26 Mar 2025 16:54:55 -0600 Subject: [PATCH] K8S Dashboards: Prevent duplicate dashboards when updating an existing dashboard (#102835) * use metadata for k8s v1 * handle version and uid update in the api layer * add test * cleanup --- public/app/features/dashboard/api/v1.test.ts | 16 +++++++++++++++- public/app/features/dashboard/api/v1.ts | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/public/app/features/dashboard/api/v1.test.ts b/public/app/features/dashboard/api/v1.test.ts index 4a51041f140..46c1fc47ef6 100644 --- a/public/app/features/dashboard/api/v1.test.ts +++ b/public/app/features/dashboard/api/v1.test.ts @@ -15,10 +15,13 @@ const mockDashboardDto: DashboardWithAccessInfo = { resourceVersion: '1', creationTimestamp: '1', annotations: {}, + generation: 1, }, spec: { title: 'test', - uid: 'test', + // V1 API doesn't return the uid or version in the spec + // setting it as empty string here because it's required in DashboardDataDTO + uid: '', schemaVersion: 0, }, access: {}, @@ -142,6 +145,17 @@ describe('v1 dashboard API', () => { expect(result.meta.folderUid).toBe('new-folder'); }); + it('should correctly set uid and version in the spec', async () => { + const api = new K8sDashboardAPI(); + // we are fetching the mockDashboardDTO, which doesn't have a uid or version + // and this is expected because V1 API doesn't return the uid or version in the spec + // however, we need these fields to be set in the dashboard object to avoid creating duplicates when editing an existing dashboard + // getDashboardDTO should set the uid and version from the metadata.name (uid) and metadata.generation (version) + const result = await api.getDashboardDTO('dash-uid'); + expect(result.dashboard.uid).toBe('dash-uid'); + expect(result.dashboard.version).toBe(1); + }); + it('throws an error if folder is not found', async () => { mockGet.mockResolvedValueOnce({ ...mockDashboardDto, diff --git a/public/app/features/dashboard/api/v1.ts b/public/app/features/dashboard/api/v1.ts index 9a8824e5b0d..6cd98ab3e67 100644 --- a/public/app/features/dashboard/api/v1.ts +++ b/public/app/features/dashboard/api/v1.ts @@ -110,7 +110,11 @@ export class K8sDashboardAPI implements DashboardAPI { k8s: dash.metadata, version: dash.metadata.generation, }, - dashboard: dash.spec, + dashboard: { + ...dash.spec, + version: dash.metadata.generation, + uid: dash.metadata.name, + }, }; if (dash.metadata.labels?.[DeprecatedInternalId]) {