Skip to content

Instantly share code, notes, and snippets.

@cmwylie19
Last active June 27, 2024 12:51
Show Gist options
  • Save cmwylie19/e4496e08060840df1d3289bb2dc56000 to your computer and use it in GitHub Desktop.
Save cmwylie19/e4496e08060840df1d3289bb2dc56000 to your computer and use it in GitHub Desktop.
const fs = require('fs');
import { K8s } from "kubernetes-fluent-client"
import { GenericKind, RegisterKind } from "kubernetes-fluent-client";
class PeprStore extends GenericKind {
declare data: {
[key: string]: string;
};
}
export const peprStoreGVK = {
kind: "PeprStore",
version: "v1",
group: "pepr.dev",
};
RegisterKind(PeprStore, peprStoreGVK);
const args = process.argv.slice(2);
if (args.length < 1) {
console.log("Usage: npx ts-node migrate.ts <capability1-name> <capability2-name> <capabilityN-name>");
process.exit(1);
}
const prefixes = args;
async function fetchAndProcessResources() {
try {
const resources = await K8s(PeprStore).InNamespace("pepr-system").Get();
resources.items.forEach((item: PeprStore) => {
const name = item.metadata!.name;
const data = item.data || {};
// Strip out the unwanted metadata fields
delete item.metadata!.creationTimestamp;
delete item.metadata!.generation;
delete item.metadata!.resourceVersion;
delete item.metadata!.uid;
// Process data keys
if (data) {
Object.keys(data).forEach(key => {
prefixes.forEach(prefix => {
if (key.startsWith(`${prefix}-`)) {
const suffix = key.split('-').pop() as string;
const encodedSuffix = base64Encode(suffix);
const newKey = `${prefix}-${encodedSuffix}`;
data[newKey] = data[key];
delete data[key];
}
})
});
}
// Write modified item to a JSON file
fs.writeFileSync(`${name}.json`, JSON.stringify(item, null, 2));
});
console.log('Processing complete.');
} catch (error) {
console.error('Error processing resources:', error);
}
}
fetchAndProcessResources();
function base64Encode(data: string) {
return Buffer.from(data).toString("base64");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment