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

fix(sso): delete orphaned SSO secrets #578

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion src/pepr/operator/controllers/keycloak/client-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UDSConfig } from "../../../config";
import { Component, setupLogger } from "../../../logger";
import { Store } from "../../common";
import { Sso, UDSPackage } from "../../crd";
import { getOwnerRef } from "../utils";
import { getOwnerRef, purgeOrphans } from "../utils";
import { Client } from "./types";

let apiURL =
Expand Down Expand Up @@ -47,13 +47,20 @@ export async function keycloak(pkg: UDSPackage) {
// Get the list of clients from the package
const clientReqs = pkg.spec?.sso || [];
const clients: Map<string, Client> = new Map();
const generation = (pkg.metadata?.generation ?? 0).toString();

for (const clientReq of clientReqs) {
const client = await syncClient(clientReq, pkg);
clients.set(client.clientId, client);
}

await purgeSSOClients(pkg, [...clients.keys()]);
// Purge orphaned SSO secrets
try {
await purgeOrphans(generation, pkg.metadata!.namespace!, pkg.metadata!.name!, kind.Secret, log);
} catch (e) {
log.error(e, `Failed to purge orphaned SSO secrets in for ${pkg.metadata!.name!}: ${e}`);
}

return clients;
}
Expand Down Expand Up @@ -151,13 +158,15 @@ async function syncClient(
}

// Create or update the client secret
const generation = (pkg.metadata?.generation ?? 0).toString();
await K8s(kind.Secret).Apply({
metadata: {
namespace: pkg.metadata!.namespace,
// Use the CR secret name if provided, otherwise use the client name
name: secretName || name,
labels: {
"uds/package": pkg.metadata!.name,
"uds/generation": generation,
},

// Use the CR as the owner ref for each VirtualService
Expand Down
11 changes: 11 additions & 0 deletions src/pepr/operator/controllers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ export function getOwnerRef(cr: GenericKind): V1OwnerReference[] {
];
}

/**
* Purges orphaned Kubernetes resources of a specified kind within a namespace that do not match the provided generation.
*
* @template T
* @param {string} generation - The generation label to retain.
* @param {string} namespace - The namespace to search for resources.
* @param {string} pkgName - The package name label to filter resources.
* @param {T} kind - The Kubernetes resource kind to purge.
* @param {Logger} log - Logger instance for logging debug messages.
* @returns {Promise<void>} - A promise that resolves when the operation is complete.
*/
export async function purgeOrphans<T extends GenericClass>(
generation: string,
namespace: string,
Expand Down