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
This commit is contained in:
Haris Rozajac
2025-03-26 16:54:55 -06:00
committed by GitHub
parent e5ff56ae7b
commit 960c13c1e5
2 changed files with 20 additions and 2 deletions
+15 -1
View File
@@ -15,10 +15,13 @@ const mockDashboardDto: DashboardWithAccessInfo<DashboardDataDTO> = {
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,
+5 -1
View File
@@ -110,7 +110,11 @@ export class K8sDashboardAPI implements DashboardAPI<DashboardDTO, Dashboard> {
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]) {