Skip to content

Commit

Permalink
wip: saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
btlghrants committed Dec 10, 2024
1 parent ab1f19c commit d047c76
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/lib/mutate-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import jsonPatch from "fast-json-patch";
import { kind, KubernetesObject } from "kubernetes-fluent-client";
import { clone } from "ramda";

import { Capability } from "./capability";
import { Errors } from "./errors";
Expand Down Expand Up @@ -103,6 +104,29 @@ async function processRequest(
return { wrapped, response };

Check warning on line 104 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L104

Added line #L104 was not covered by tests
}

function skipDecode(wrapped: PeprMutateRequest<KubernetesObject>): string[] {
let skipped: string[] = [];

Check warning on line 108 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L107-L108

Added lines #L107 - L108 were not covered by tests

const isSecret = wrapped.Request.kind.version === "v1" && wrapped.Request.kind.kind === "Secret";
if (isSecret) {
skipped = convertFromBase64Map(wrapped.Raw as unknown as kind.Secret);

Check warning on line 112 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L112

Added line #L112 was not covered by tests
}

return skipped;

Check warning on line 115 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L115

Added line #L115 was not covered by tests
}

function unskipRecode(wrapped: PeprMutateRequest<KubernetesObject>, skipped: string[]): KubernetesObject {
const transformed = clone(wrapped.Raw);

Check warning on line 119 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L118-L119

Added lines #L118 - L119 were not covered by tests

const isSecret = wrapped.Request.kind.version === "v1" && wrapped.Request.kind.kind === "Secret";
if (isSecret && skipped.length > 0) {
convertToBase64Map(transformed as unknown as kind.Secret, skipped);

Check warning on line 123 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L123

Added line #L123 was not covered by tests
}

return transformed;

Check warning on line 126 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L126

Added line #L126 was not covered by tests
}

/* eslint max-statements: ["warn", 25] */
export async function mutateProcessor(
config: ModuleConfig,
capabilities: Capability[],
Expand All @@ -116,14 +140,8 @@ export async function mutateProcessor(
allowed: false,
};

// Track data fields that should be skipped during decoding
let skipDecode: string[] = [];

// If the resource is a secret, decode the data
const isSecret = req.kind.version === "v1" && req.kind.kind === "Secret";
if (isSecret) {
skipDecode = convertFromBase64Map(wrapped.Raw as unknown as kind.Secret);
}
// Track base64-encoded data fields that should be skipped during decoding
const skipped = skipDecode(wrapped);

Check warning on line 144 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L144

Added line #L144 was not covered by tests

Log.info(reqMetadata, `Processing request`);

Check warning on line 146 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L146

Added line #L146 was not covered by tests

Expand Down Expand Up @@ -178,12 +196,8 @@ export async function mutateProcessor(
return response;
}

const transformed = wrapped.Raw;

// Post-process the Secret requests to convert it back to the original format
if (isSecret) {
convertToBase64Map(transformed as unknown as kind.Secret, skipDecode);
}
// unskip base64-encoded data fields that were skipDecode'd
const transformed = unskipRecode(wrapped, skipped);

Check warning on line 200 in src/lib/mutate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/mutate-processor.ts#L200

Added line #L200 was not covered by tests

// Compare the original request to the modified request to get the patches
const patches = jsonPatch.compare(req.object, transformed);
Expand Down

0 comments on commit d047c76

Please sign in to comment.