Skip to content

Instantly share code, notes, and snippets.

@brunoluiz
Last active September 28, 2021 08:15
Show Gist options
  • Save brunoluiz/3091472dcbab578c70ab29bac55311bb to your computer and use it in GitHub Desktop.
Save brunoluiz/3091472dcbab578c70ab29bac55311bb to your computer and use it in GitHub Desktop.
kustomize-migrate-bases-to-resources

Converts kustomize bases entries to resources

As bases is deprecated since version 2.10, I've created this script to automate the migration. Bear in mind that it might need some spot checks, as it seems it deletes YAML comments and, if you are using YAML comments, you might need to do it manually.

Running

  • npm install
  • MANIFESTS_PATH="path-to-your-kube-manifests" node index.js
  • Review the folder under $PATH
{
"name": "tmp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"yaml": "^1.10.2"
}
}
const yaml = require("yaml");
const fs = require("fs");
const path = require("path");
async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
(async () => {
for await (const p of walk(process.env.MANIFESTS_PATH)) {
try {
if (p.includes(".git")) continue;
if (!p.includes("kustomization.y")) continue;
const data = fs.readFileSync(p, "utf8");
if (!data.includes("bases:")) continue;
const y = yaml.parse(data);
if (!y.resources) y.resources = [];
if (!y.bases) y.bases = [];
y.resources = [...y.resources, ...y.bases];
y.resources.sort() // comment this if you don't want your resources to be sorted 👀
delete y.bases;
const out = yaml.stringify(y);
fs.writeFileSync(p, out);
} catch (e) {
console.error(p, e);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment