Persist apiVersion when editing resources in monaco (#4406)

* Persist apiVersion when editing resources in monaco

- Use a new custom k8slens prefixed label

- Means that users aren't surprised when they use lens to update a
  resource to a new apiVersionWithGroup

- Doesn't touch the versions in the stores

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix lint

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* fix: Fix lint issues

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* chore: make lint not bail on failure

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* chore: Run lint:fix on all files

Signed-off-by: Sebastian Malton <sebastian@malton.name>

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2023-04-05 10:56:23 -04:00
1 parent 058494bc73
commit 807f98ed1b
57 files changed
+295 -400

No files matched your search

@@ -109,7 +109,25 @@ export function parseKubeApi(path: string): IKubeApiParsed {
};
}
export function createKubeApiURL({ apiPrefix = "/apis", resource, apiVersion, name, namespace }: IKubeApiLinkRef): string {
function isIKubeApiParsed(refOrParsed: IKubeApiLinkRef | IKubeApiParsed): refOrParsed is IKubeApiParsed {
return "apiGroup" in refOrParsed;
}
export function createKubeApiURL(linkRef: IKubeApiLinkRef): string;
export function createKubeApiURL(linkParsed: IKubeApiParsed): string;
export function createKubeApiURL(ref: IKubeApiLinkRef | IKubeApiParsed): string {
if (isIKubeApiParsed(ref)) {
return createKubeApiURL({
apiPrefix: ref.apiPrefix,
resource: ref.resource,
name: ref.name,
namespace: ref.namespace,
apiVersion: `${ref.apiGroup}/${ref.apiVersion}`,
});
}
const { apiPrefix = "/apis", resource, apiVersion, name, namespace } = ref;
const parts = [apiPrefix, apiVersion];
if (namespace) {
+1 -1
View File
@@ -193,7 +193,7 @@ export interface KubeApiWatchOptions<Object extends KubeObject, Data extends Kub
export type KubeApiPatchType = "merge" | "json" | "strategic";
const patchTypeHeaders: Record<KubeApiPatchType, string> = {
export const patchTypeHeaders: Record<KubeApiPatchType, string> = {
"merge": "application/merge-patch+json",
"json": "application/json-patch+json",
"strategic": "application/strategic-merge-patch+json",
+13 -29
View File
@@ -437,6 +437,19 @@ const resourceApplierAnnotationsForFiltering = [
const filterOutResourceApplierAnnotations = (label: string) => !resourceApplierAnnotationsForFiltering.some(key => label.startsWith(key));
export interface RawKubeObject<
Metadata extends KubeObjectMetadata = KubeObjectMetadata,
Status = Record<string, unknown>,
Spec = Record<string, unknown>,
> {
apiVersion: string;
kind: string;
metadata: Metadata;
status?: Status;
spec?: Spec;
managedFields?: any;
}
export class KubeObject<
Metadata extends KubeObjectMetadata<KubeObjectScope> = KubeObjectMetadata<KubeObjectScope>,
Status = unknown,
@@ -538,23 +551,6 @@ export class KubeObject<
return Object.entries(labels).map(([name, value]) => `${name}=${value}`);
}
/**
* These must be RFC6902 compliant paths
*/
private static readonly nonEditablePathPrefixes = [
"/metadata/managedFields",
"/status",
];
private static readonly nonEditablePaths = new Set([
"/apiVersion",
"/kind",
"/metadata/name",
"/metadata/selfLink",
"/metadata/resourceVersion",
"/metadata/uid",
...KubeObject.nonEditablePathPrefixes,
]);
constructor(data: KubeJsonApiData<Metadata, Status, Spec>) {
if (typeof data !== "object") {
throw new TypeError(`Cannot create a KubeObject from ${typeof data}`);
@@ -684,18 +680,6 @@ export class KubeObject<
* @deprecated use KubeApi.patch instead
*/
async patch(patch: Patch): Promise<KubeJsonApiData | null> {
for (const op of patch) {
if (KubeObject.nonEditablePaths.has(op.path)) {
throw new Error(`Failed to update ${this.kind}: JSON pointer ${op.path} has been modified`);
}
for (const pathPrefix of KubeObject.nonEditablePathPrefixes) {
if (op.path.startsWith(`${pathPrefix}/`)) {
throw new Error(`Failed to update ${this.kind}: Child JSON pointer of ${op.path} has been modified`);
}
}
}
const di = getLegacyGlobalDiForExtensionApi();
const requestKubeObjectPatch = di.inject(requestKubeObjectPatchInjectable);
const result = await requestKubeObjectPatch(this.getName(), this.kind, this.getNs(), patch);