From 2411dda6eaa953dfe2c18a7173a41d5379e655cf Mon Sep 17 00:00:00 2001 From: Barry Waldbaum Date: Sat, 1 Jul 2023 00:19:19 -0400 Subject: [PATCH] Fix Mutate CapabilityAction failure on DELETE operation (#167) Co-authored-by: Jeff McCoy --- src/lib/processor.ts | 5 +++++ src/lib/request.ts | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/lib/processor.ts b/src/lib/processor.ts index e3392fde4..e8d5ff6b8 100644 --- a/src/lib/processor.ts +++ b/src/lib/processor.ts @@ -104,6 +104,11 @@ export async function processor( return response; } + // delete operations can't be mutate, just return before the transformation + if (req.operation == "DELETE") { + return response; + } + const transformed = wrapped.Raw; // Post-process the Secret requests to convert it back to the original format diff --git a/src/lib/request.ts b/src/lib/request.ts index f7e6f3a9d..8bca80461 100644 --- a/src/lib/request.ts +++ b/src/lib/request.ts @@ -3,7 +3,7 @@ import { clone, mergeDeepRight } from "ramda"; -import { KubernetesObject, Request } from "./k8s/types"; +import { KubernetesObject, Operation, Request } from "./k8s/types"; import { DeepPartial } from "./types"; /** @@ -46,8 +46,17 @@ export class PeprRequest { * @param input - The request object containing the Kubernetes resource to modify. */ constructor(private _input: Request) { - // Deep clone the object to prevent mutation of the original object - this.Raw = clone(_input.object); + // If this is a DELETE operation, use the oldObject instead + if (_input.operation === Operation.DELETE) { + this.Raw = clone(_input.oldObject as T); + } else { + // Otherwise, use the incoming object + this.Raw = clone(_input.object); + } + + if (!this.Raw) { + throw new Error("unable to load the request object into PeprRequest.RawP"); + } } /** @@ -100,6 +109,7 @@ export class PeprRequest { if (this.Raw.metadata?.labels?.[key]) { delete this.Raw.metadata.labels[key]; } + return this; } @@ -112,6 +122,7 @@ export class PeprRequest { if (this.Raw.metadata?.annotations?.[key]) { delete this.Raw.metadata.annotations[key]; } + return this; } @@ -122,7 +133,7 @@ export class PeprRequest { * @returns */ HasLabel(key: string) { - return this.Raw?.metadata?.labels?.[key] !== undefined; + return this.Raw.metadata?.labels?.[key] !== undefined; } /** @@ -132,6 +143,6 @@ export class PeprRequest { * @returns */ HasAnnotation(key: string) { - return this.Raw?.metadata?.annotations?.[key] !== undefined; + return this.Raw.metadata?.annotations?.[key] !== undefined; } }