Dashboards: Do not throw error if backend cannot migrate schemaVersion to latest (#102357)
* Refactor migration error handling to use MinimumVersionError for schema version checks - Updated migration logic to return MinimumVersionError instead of MigrationError for outdated schema versions. - Enhanced MinimumVersionError message for clarity on migration constraints. - Added tests for version error handling in the dashboard API to ensure proper error throwing for specific conversion errors. * Fix tests and remove folder dependencies
This commit is contained in:
@@ -12,7 +12,7 @@ func Migrate(dash map[string]interface{}, targetVersion int) error {
|
||||
// If the schema version is older than the minimum version, with migration support,
|
||||
// we don't migrate the dashboard.
|
||||
if inputVersion < schemaversion.MIN_VERSION {
|
||||
return schemaversion.NewMigrationError("schema version is too old", inputVersion, schemaversion.MIN_VERSION)
|
||||
return schemaversion.NewMinimumVersionError(inputVersion)
|
||||
}
|
||||
|
||||
for nextVersion := inputVersion + 1; nextVersion <= targetVersion; nextVersion++ {
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestMigrate(t *testing.T) {
|
||||
"schemaVersion": schemaversion.MIN_VERSION - 1,
|
||||
}, schemaversion.MIN_VERSION)
|
||||
|
||||
var minVersionErr = schemaversion.NewMigrationError("schema version is too old", schemaversion.MIN_VERSION-1, schemaversion.MIN_VERSION)
|
||||
var minVersionErr = schemaversion.NewMinimumVersionError(schemaversion.MIN_VERSION - 1)
|
||||
require.ErrorAs(t, err, &minVersionErr)
|
||||
})
|
||||
|
||||
|
||||
@@ -35,5 +35,5 @@ type MinimumVersionError struct {
|
||||
}
|
||||
|
||||
func (e *MinimumVersionError) Error() string {
|
||||
return fmt.Errorf("input schema version is below minimum version. input: %d minimum: %d", e.inputVersion, MIN_VERSION).Error()
|
||||
return fmt.Errorf("dashboard schema version %d cannot be migrated to latest version %d - migration path only exists for versions greater than %d", e.inputVersion, LATEST_VERSION, MIN_VERSION).Error()
|
||||
}
|
||||
|
||||
@@ -14,9 +14,7 @@ const mockDashboardDto: DashboardWithAccessInfo<DashboardDataDTO> = {
|
||||
name: 'dash-uid',
|
||||
resourceVersion: '1',
|
||||
creationTimestamp: '1',
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'new-folder',
|
||||
},
|
||||
annotations: {},
|
||||
},
|
||||
spec: {
|
||||
title: 'test',
|
||||
@@ -87,10 +85,13 @@ const saveDashboardResponse = {
|
||||
weekStart: '',
|
||||
},
|
||||
};
|
||||
|
||||
const mockGet = jest.fn().mockResolvedValue(mockDashboardDto);
|
||||
|
||||
jest.mock('@grafana/runtime', () => ({
|
||||
...jest.requireActual('@grafana/runtime'),
|
||||
getBackendSrv: () => ({
|
||||
get: () => mockDashboardDto,
|
||||
get: mockGet,
|
||||
put: jest.fn().mockResolvedValue(saveDashboardResponse),
|
||||
post: jest.fn().mockResolvedValue(saveDashboardResponse),
|
||||
}),
|
||||
@@ -108,7 +109,15 @@ jest.mock('app/features/live/dashboard/dashboardWatcher', () => ({
|
||||
|
||||
describe('v1 dashboard API', () => {
|
||||
it('should provide folder annotations', async () => {
|
||||
jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValue({
|
||||
mockGet.mockResolvedValueOnce({
|
||||
...mockDashboardDto,
|
||||
metadata: {
|
||||
...mockDashboardDto.metadata,
|
||||
annotations: { [AnnoKeyFolder]: 'new-folder' },
|
||||
},
|
||||
});
|
||||
|
||||
jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValueOnce({
|
||||
id: 1,
|
||||
uid: 'new-folder',
|
||||
title: 'New Folder',
|
||||
@@ -134,7 +143,16 @@ describe('v1 dashboard API', () => {
|
||||
});
|
||||
|
||||
it('throws an error if folder is not found', async () => {
|
||||
jest.spyOn(backendSrv, 'getFolderByUid').mockRejectedValue({ message: 'folder not found', status: 'not-found' });
|
||||
mockGet.mockResolvedValueOnce({
|
||||
...mockDashboardDto,
|
||||
metadata: {
|
||||
...mockDashboardDto.metadata,
|
||||
annotations: { [AnnoKeyFolder]: 'new-folder' },
|
||||
},
|
||||
});
|
||||
jest
|
||||
.spyOn(backendSrv, 'getFolderByUid')
|
||||
.mockRejectedValueOnce({ message: 'folder not found', status: 'not-found' });
|
||||
|
||||
const api = new K8sDashboardAPI();
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('Failed to load folder');
|
||||
@@ -241,4 +259,42 @@ describe('v1 dashboard API', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('version error handling', () => {
|
||||
it('should throw DashboardVersionError for v2alpha1 conversion error', async () => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
conversion: {
|
||||
failed: true,
|
||||
error: 'backend conversion not yet implemented',
|
||||
storedVersion: 'v2alpha1',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mockGet.mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardAPI();
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented');
|
||||
});
|
||||
|
||||
it.each(['v0alpha1', 'v1alpha1'])('should not throw for %s conversion errors', async (correctStoredVersion) => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
conversion: {
|
||||
failed: true,
|
||||
error: 'other-error',
|
||||
storedVersion: correctStoredVersion,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
jest.spyOn(backendSrv, 'get').mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardAPI();
|
||||
await expect(api.getDashboardDTO('test')).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -97,7 +97,7 @@ export class K8sDashboardAPI implements DashboardAPI<DashboardDTO, Dashboard> {
|
||||
const dash = await this.client.subresource<DashboardWithAccessInfo<DashboardDataDTO>>(uid, 'dto');
|
||||
|
||||
// This could come as conversion error from v0 or v2 to V1.
|
||||
if (dash.status?.conversion?.failed) {
|
||||
if (dash.status?.conversion?.failed && dash.status.conversion.storedVersion === 'v2alpha1') {
|
||||
throw new DashboardVersionError(dash.status.conversion.storedVersion, dash.status.conversion.error);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,9 +19,7 @@ const mockDashboardDto: DashboardWithAccessInfo<DashboardV2Spec> = {
|
||||
name: 'dash-uid',
|
||||
resourceVersion: '1',
|
||||
creationTimestamp: '1',
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'new-folder',
|
||||
},
|
||||
annotations: {},
|
||||
},
|
||||
spec: {
|
||||
...defaultDashboardV2Spec(),
|
||||
@@ -29,7 +27,9 @@ const mockDashboardDto: DashboardWithAccessInfo<DashboardV2Spec> = {
|
||||
access: {},
|
||||
};
|
||||
|
||||
// Create a mock put function that we can spy on
|
||||
// Create mock get and put functions that we can spy on
|
||||
const mockGet = jest.fn().mockResolvedValue(mockDashboardDto);
|
||||
|
||||
const mockPut = jest.fn().mockImplementation((url, data) => {
|
||||
return {
|
||||
apiVersion: 'dashboard.grafana.app/v2alpha1',
|
||||
@@ -48,7 +48,7 @@ const mockPut = jest.fn().mockImplementation((url, data) => {
|
||||
jest.mock('@grafana/runtime', () => ({
|
||||
...jest.requireActual('@grafana/runtime'),
|
||||
getBackendSrv: () => ({
|
||||
get: () => mockDashboardDto,
|
||||
get: mockGet,
|
||||
put: mockPut,
|
||||
}),
|
||||
config: {
|
||||
@@ -66,6 +66,14 @@ describe('v2 dashboard API', () => {
|
||||
});
|
||||
|
||||
it('should provide folder annotations', async () => {
|
||||
mockGet.mockResolvedValueOnce({
|
||||
...mockDashboardDto,
|
||||
metadata: {
|
||||
...mockDashboardDto.metadata,
|
||||
annotations: { [AnnoKeyFolder]: 'new-folder' },
|
||||
},
|
||||
});
|
||||
|
||||
jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValue({
|
||||
id: 1,
|
||||
uid: 'new-folder',
|
||||
@@ -94,104 +102,168 @@ describe('v2 dashboard API', () => {
|
||||
});
|
||||
|
||||
it('throws an error if folder is not found', async () => {
|
||||
jest.spyOn(backendSrv, 'getFolderByUid').mockRejectedValue({ message: 'folder not found', status: 'not-found' });
|
||||
mockGet.mockResolvedValueOnce({
|
||||
...mockDashboardDto,
|
||||
metadata: {
|
||||
...mockDashboardDto.metadata,
|
||||
annotations: { [AnnoKeyFolder]: 'new-folder' },
|
||||
},
|
||||
});
|
||||
jest
|
||||
.spyOn(backendSrv, 'getFolderByUid')
|
||||
.mockRejectedValueOnce({ message: 'folder not found', status: 'not-found' });
|
||||
|
||||
const api = new K8sDashboardV2API();
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('Failed to load folder');
|
||||
});
|
||||
});
|
||||
|
||||
describe('v2 dashboard API - Save', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
const defaultSaveCommand = {
|
||||
dashboard: defaultDashboardV2Spec(),
|
||||
message: 'test save',
|
||||
folderUid: 'test-folder',
|
||||
k8s: {
|
||||
name: 'test-dash',
|
||||
labels: {
|
||||
[DeprecatedInternalId]: '123',
|
||||
},
|
||||
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'new-folder',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
it('should create new dashboard', async () => {
|
||||
const api = new K8sDashboardV2API();
|
||||
const result = await api.saveDashboard({
|
||||
...defaultSaveCommand,
|
||||
dashboard: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'test-dashboard',
|
||||
},
|
||||
describe('v2 dashboard API - Save', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
id: 123,
|
||||
uid: 'test-dash',
|
||||
url: '/d/test-dash/testdashboard',
|
||||
slug: '',
|
||||
status: 'success',
|
||||
version: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('should update existing dashboard', async () => {
|
||||
const api = new K8sDashboardV2API();
|
||||
|
||||
const result = await api.saveDashboard({
|
||||
...defaultSaveCommand,
|
||||
dashboard: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'chaing-title-dashboard',
|
||||
},
|
||||
const defaultSaveCommand = {
|
||||
dashboard: defaultDashboardV2Spec(),
|
||||
message: 'test save',
|
||||
folderUid: 'test-folder',
|
||||
k8s: {
|
||||
...defaultSaveCommand.k8s,
|
||||
name: 'existing-dash',
|
||||
},
|
||||
});
|
||||
expect(result.version).toBe(2);
|
||||
});
|
||||
name: 'test-dash',
|
||||
labels: {
|
||||
[DeprecatedInternalId]: '123',
|
||||
},
|
||||
|
||||
it('should update existing dashboard that is store in a folder', async () => {
|
||||
const api = new K8sDashboardV2API();
|
||||
await api.saveDashboard({
|
||||
dashboard: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'chaing-title-dashboard',
|
||||
},
|
||||
folderUid: 'folderUidXyz',
|
||||
k8s: {
|
||||
name: 'existing-dash',
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'folderUidXyz',
|
||||
[AnnoKeyFolderUrl]: 'url folder used in the client',
|
||||
[AnnoKeyFolderId]: 42,
|
||||
[AnnoKeyFolderTitle]: 'title folder used in the client',
|
||||
[AnnoKeyFolder]: 'new-folder',
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(mockPut).toHaveBeenCalledTimes(1);
|
||||
expect(mockPut).toHaveBeenCalledWith(
|
||||
'/apis/dashboard.grafana.app/v2alpha1/namespaces/default/dashboards/existing-dash',
|
||||
{
|
||||
metadata: {
|
||||
name: 'existing-dash',
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'folderUidXyz',
|
||||
},
|
||||
};
|
||||
|
||||
it('should create new dashboard', async () => {
|
||||
const api = new K8sDashboardV2API();
|
||||
const result = await api.saveDashboard({
|
||||
...defaultSaveCommand,
|
||||
dashboard: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'test-dashboard',
|
||||
},
|
||||
spec: {
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
id: 123,
|
||||
uid: 'test-dash',
|
||||
url: '/d/test-dash/testdashboard',
|
||||
slug: '',
|
||||
status: 'success',
|
||||
version: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('should update existing dashboard', async () => {
|
||||
const api = new K8sDashboardV2API();
|
||||
|
||||
const result = await api.saveDashboard({
|
||||
...defaultSaveCommand,
|
||||
dashboard: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'chaing-title-dashboard',
|
||||
},
|
||||
}
|
||||
);
|
||||
k8s: {
|
||||
...defaultSaveCommand.k8s,
|
||||
name: 'existing-dash',
|
||||
},
|
||||
});
|
||||
expect(result.version).toBe(2);
|
||||
});
|
||||
|
||||
it('should update existing dashboard that is store in a folder', async () => {
|
||||
const api = new K8sDashboardV2API();
|
||||
await api.saveDashboard({
|
||||
dashboard: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'chaing-title-dashboard',
|
||||
},
|
||||
folderUid: 'folderUidXyz',
|
||||
k8s: {
|
||||
name: 'existing-dash',
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'folderUidXyz',
|
||||
[AnnoKeyFolderUrl]: 'url folder used in the client',
|
||||
[AnnoKeyFolderId]: 42,
|
||||
[AnnoKeyFolderTitle]: 'title folder used in the client',
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(mockPut).toHaveBeenCalledTimes(1);
|
||||
expect(mockPut).toHaveBeenCalledWith(
|
||||
'/apis/dashboard.grafana.app/v2alpha1/namespaces/default/dashboards/existing-dash',
|
||||
{
|
||||
metadata: {
|
||||
name: 'existing-dash',
|
||||
annotations: {
|
||||
[AnnoKeyFolder]: 'folderUidXyz',
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
...defaultSaveCommand.dashboard,
|
||||
title: 'chaing-title-dashboard',
|
||||
},
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('version error handling', () => {
|
||||
it('should throw DashboardVersionError for v0alpha1 conversion error', async () => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
conversion: {
|
||||
failed: true,
|
||||
error: 'backend conversion not yet implemented',
|
||||
storedVersion: 'v0alpha1',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mockGet.mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardV2API();
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented');
|
||||
});
|
||||
|
||||
it('should throw DashboardVersionError for v1alpha1 conversion error', async () => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
conversion: {
|
||||
failed: true,
|
||||
error: 'backend conversion not yet implemented',
|
||||
storedVersion: 'v1alpha1',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mockGet.mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardV2API();
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented');
|
||||
});
|
||||
|
||||
it('should not throw for other conversion errors', async () => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
conversion: {
|
||||
failed: true,
|
||||
error: 'other-error',
|
||||
storedVersion: 'v2alpha1',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mockGet.mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardV2API();
|
||||
await expect(api.getDashboardDTO('test')).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -40,7 +40,11 @@ export class K8sDashboardV2API
|
||||
try {
|
||||
const dashboard = await this.client.subresource<DashboardWithAccessInfo<DashboardV2Spec>>(uid, 'dto');
|
||||
|
||||
if (dashboard.status?.conversion?.failed) {
|
||||
if (
|
||||
dashboard.status?.conversion?.failed &&
|
||||
(dashboard.status.conversion.storedVersion === 'v1alpha1' ||
|
||||
dashboard.status.conversion.storedVersion === 'v0alpha1')
|
||||
) {
|
||||
throw new DashboardVersionError(dashboard.status.conversion.storedVersion, dashboard.status.conversion.error);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user