1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add checks to block edits to key fields (#2426)

- Referece kubetcl:pkg/cmd/util/editor/editoptions.go:649

- disallow editing status field

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-06-14 08:49:55 -04:00 committed by GitHub
parent 80f66f0569
commit 5bd1d079de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,7 @@ import { apiKube } from "./index";
import type { JsonApiParams } from "./json-api"; import type { JsonApiParams } from "./json-api";
import { resourceApplierApi } from "./endpoints/resource-applier.api"; import { resourceApplierApi } from "./endpoints/resource-applier.api";
import { hasOptionalProperty, hasTypedProperty, isObject, isString, bindPredicate, isTypedArray, isRecord } from "../../common/utils/type-narrowing"; import { hasOptionalProperty, hasTypedProperty, isObject, isString, bindPredicate, isTypedArray, isRecord } from "../../common/utils/type-narrowing";
import _ from "lodash";
export type IKubeObjectConstructor<T extends KubeObject = any> = (new (data: KubeJsonApiData | any) => T) & { export type IKubeObjectConstructor<T extends KubeObject = any> = (new (data: KubeJsonApiData | any) => T) & {
kind?: string; kind?: string;
@ -96,6 +97,7 @@ export class KubeObject<Metadata extends IKubeObjectMetadata = IKubeObjectMetada
metadata: Metadata; metadata: Metadata;
status?: Status; status?: Status;
spec?: Spec; spec?: Spec;
managedFields?: any;
static create(data: KubeJsonApiData) { static create(data: KubeJsonApiData) {
return new KubeObject(data); return new KubeObject(data);
@ -179,6 +181,17 @@ export class KubeObject<Metadata extends IKubeObjectMetadata = IKubeObjectMetada
return Object.entries(labels).map(([name, value]) => `${name}=${value}`); return Object.entries(labels).map(([name, value]) => `${name}=${value}`);
} }
protected static readonly nonEditableFields = [
"apiVersion",
"kind",
"metadata.name",
"metadata.selfLink",
"metadata.resourceVersion",
"metadata.uid",
"managedFields",
"status",
];
constructor(data: KubeJsonApiData) { constructor(data: KubeJsonApiData) {
Object.assign(this, data); Object.assign(this, data);
autoBind(this); autoBind(this);
@ -267,6 +280,12 @@ export class KubeObject<Metadata extends IKubeObjectMetadata = IKubeObjectMetada
// use unified resource-applier api for updating all k8s objects // use unified resource-applier api for updating all k8s objects
async update<T extends KubeObject>(data: Partial<T>): Promise<T> { async update<T extends KubeObject>(data: Partial<T>): Promise<T> {
for (const field of KubeObject.nonEditableFields) {
if (!_.isEqual(_.get(this, field), _.get(data, field))) {
throw new Error(`Failed to update Kube Object: ${field} has been modified`);
}
}
return resourceApplierApi.update<T>({ return resourceApplierApi.update<T>({
...this.toPlainObject(), ...this.toPlainObject(),
...data, ...data,