Skip to content

Commit

Permalink
Fix Mutate CapabilityAction failure on DELETE operation (#167)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff McCoy <[email protected]>
  • Loading branch information
Barry Waldbaum and jeff-mccoy authored Jul 1, 2023
1 parent 104b88d commit 2411dda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/lib/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -46,8 +46,17 @@ export class PeprRequest<T extends KubernetesObject> {
* @param input - The request object containing the Kubernetes resource to modify.
*/
constructor(private _input: Request<T>) {
// 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");
}
}

/**
Expand Down Expand Up @@ -100,6 +109,7 @@ export class PeprRequest<T extends KubernetesObject> {
if (this.Raw.metadata?.labels?.[key]) {
delete this.Raw.metadata.labels[key];
}

return this;
}

Expand All @@ -112,6 +122,7 @@ export class PeprRequest<T extends KubernetesObject> {
if (this.Raw.metadata?.annotations?.[key]) {
delete this.Raw.metadata.annotations[key];
}

return this;
}

Expand All @@ -122,7 +133,7 @@ export class PeprRequest<T extends KubernetesObject> {
* @returns
*/
HasLabel(key: string) {
return this.Raw?.metadata?.labels?.[key] !== undefined;
return this.Raw.metadata?.labels?.[key] !== undefined;
}

/**
Expand All @@ -132,6 +143,6 @@ export class PeprRequest<T extends KubernetesObject> {
* @returns
*/
HasAnnotation(key: string) {
return this.Raw?.metadata?.annotations?.[key] !== undefined;
return this.Raw.metadata?.annotations?.[key] !== undefined;
}
}

0 comments on commit 2411dda

Please sign in to comment.