Dashboards: Make serializer a single source of truth for passing k8s props on save (#105576)

* make serializer a single source of truth for passing k8s props on save

* remove resourceVersion when updating

* don't pass result.version to resourceVersion

* remove k8s update on saveCompleted from dash scene
This commit is contained in:
Haris Rozajac
2025-05-21 07:24:41 -06:00
committed by GitHub
parent 269226cb50
commit d11d8ec111
3 changed files with 26 additions and 2 deletions

View File

@ -33,7 +33,7 @@ export function SaveDashboardForm({ dashboard, drawer, changeInfo }: Props) {
// we need to set the uid here in order to save the dashboard
// in schema v2 we don't have the uid in the spec
k8s: {
...dashboard.state.meta.k8s,
...dashboard.serializer.getK8SMetadata(),
},
});
const { t } = useTranslate();

View File

@ -1,6 +1,6 @@
import { Dashboard } from '@grafana/schema';
import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen';
import { AnnoKeyDashboardSnapshotOriginalUrl } from 'app/features/apiserver/types';
import { AnnoKeyDashboardSnapshotOriginalUrl, ObjectMeta } from 'app/features/apiserver/types';
import { DashboardWithAccessInfo } from 'app/features/dashboard/api/types';
import { isDashboardV2Spec } from 'app/features/dashboard/api/utils';
import { SaveDashboardAsOptions } from 'app/features/dashboard/components/SaveDashboard/types';
@ -56,6 +56,7 @@ export interface DashboardSceneSerializerLike<T, M, I = T, E = T | { error: unkn
getElementPanelMapping: () => Map<string, number>;
getDSReferencesMapping: () => DSReferencesMapping;
makeExportableExternally: (s: DashboardScene) => Promise<E | { error: unknown }>;
getK8SMetadata: () => Partial<ObjectMeta> | undefined;
}
interface DashboardTrackingInfo {
@ -178,6 +179,17 @@ export class V1DashboardSerializer
uid: result.uid,
version: result.version,
};
this.metadata = {
...this.metadata,
k8s: {
...this.metadata?.k8s,
generation: result.version,
},
};
}
getK8SMetadata() {
return this.metadata?.k8s;
}
getTrackingInformation(): DashboardTrackingInfo | undefined {
@ -376,6 +388,16 @@ export class V2DashboardSerializer
this.initialSaveModel = {
...saveModel,
};
if (this.metadata) {
this.metadata = {
...this.metadata,
generation: result.version,
};
}
}
getK8SMetadata() {
return this.metadata;
}
getTrackingInformation(s: DashboardScene): DashboardTrackingInfo | undefined {

View File

@ -69,6 +69,8 @@ export class K8sDashboardAPI implements DashboardAPI<DashboardDTO, Dashboard> {
// as we implement the necessary backend conversions, we will drop this query param
if (dashboard.uid) {
obj.metadata.name = dashboard.uid;
// remove resource version when updating
delete obj.metadata.resourceVersion;
return this.client.update(obj, { fieldValidation: 'Ignore' }).then((v) => this.asSaveDashboardResponseDTO(v));
}
obj.metadata.annotations = {