Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reduce complexity in webhooks.ts #1587

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 30 additions & 45 deletions src/lib/assets/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { concat, equals, uniqWith } from "ramda";

import { Assets } from ".";
import { Event } from "../enums";
import { Binding } from "../types";

const peprIgnoreLabel: V1LabelSelectorRequirement = {
key: "pepr.dev",
Expand All @@ -20,57 +21,41 @@ const peprIgnoreLabel: V1LabelSelectorRequirement = {

const peprIgnoreNamespaces: string[] = ["kube-system", "pepr-system"];

const validateRule = (binding: Binding, isMutateWebhook: boolean): V1RuleWithOperations | undefined => {
const { event, kind, isMutate, isValidate } = binding;

// Skip invalid bindings based on webhook type
if ((isMutateWebhook && !isMutate) || (!isMutateWebhook && !isValidate)) {
return undefined;
}

// Translate event to operations
const operations = event === Event.CREATE_OR_UPDATE ? [Event.CREATE, Event.UPDATE] : [event];

// Use the plural property if it exists, otherwise use lowercase kind + s
const resource = kind.plural || `${kind.kind.toLowerCase()}s`;

const ruleObject: V1RuleWithOperations = {
apiGroups: [kind.group],
apiVersions: [kind.version || "*"],
operations,
resources: [resource, ...(resource === "pods" ? ["pods/ephemeralcontainers"] : [])],
};

return ruleObject;
};

export async function generateWebhookRules(assets: Assets, isMutateWebhook: boolean): Promise<V1RuleWithOperations[]> {
const { config, capabilities } = assets;
const rules: V1RuleWithOperations[] = [];

// Iterate through the capabilities and generate the rules
for (const capability of capabilities) {
const rules = capabilities.flatMap(capability => {
console.info(`Module ${config.uuid} has capability: ${capability.name}`);

// Read the bindings and generate the rules
for (const binding of capability.bindings) {
const { event, kind, isMutate, isValidate } = binding;

// If the module doesn't have a callback for the event, skip it
if (isMutateWebhook && !isMutate) {
continue;
}

if (!isMutateWebhook && !isValidate) {
continue;
}

const operations: string[] = [];

// CreateOrUpdate is a Pepr-specific event that is translated to Create and Update
if (event === Event.CREATE_OR_UPDATE) {
operations.push(Event.CREATE, Event.UPDATE);
} else {
operations.push(event);
}

// Use the plural property if it exists, otherwise use lowercase kind + s
const resource = kind.plural || `${kind.kind.toLowerCase()}s`;

const ruleObject = {
apiGroups: [kind.group],
apiVersions: [kind.version || "*"],
operations,
resources: [resource],
};

// If the resource is pods, add ephemeralcontainers as well
if (resource === "pods") {
ruleObject.resources.push("pods/ephemeralcontainers");
}

// Add the rule to the rules array
rules.push(ruleObject);
}
}
return capability.bindings
.map(binding => validateRule(binding, isMutateWebhook))
.filter((rule): rule is V1RuleWithOperations => !!rule);
});

// Return the rules with duplicates removed
return uniqWith(equals, rules);
}

Expand Down
Loading