kubepile 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/package.json +1 -1
- package/dist/src/kubepile.js +50 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,6 +46,13 @@ This reads `~/.config/kubepile/*.yaml`, then writes `~/.kube/config`. If
|
|
|
46
46
|
`~/.kube/config` already exists, `kubepile compile` prompts before copying it to
|
|
47
47
|
`~/.kube/config.bak`.
|
|
48
48
|
|
|
49
|
+
Kubepile skips that backup prompt for safe rebuilds. A rebuild is safe when the
|
|
50
|
+
existing generated kubeconfig can be reproduced from a subset of the current
|
|
51
|
+
`*.yaml` files. In practice, this means no-op rebuilds and adding a new
|
|
52
|
+
provider file do not ask for a backup. If the existing generated kubeconfig was
|
|
53
|
+
manually edited, was not generated by kubepile, or can no longer be reproduced
|
|
54
|
+
from the current inputs, kubepile asks before replacing it.
|
|
55
|
+
|
|
49
56
|
Explicit command and options:
|
|
50
57
|
|
|
51
58
|
```sh
|
package/dist/package.json
CHANGED
package/dist/src/kubepile.js
CHANGED
|
@@ -11,6 +11,9 @@ export function defaultKubeConfigPath() {
|
|
|
11
11
|
export async function buildMergedConfig(options = {}) {
|
|
12
12
|
const inputDir = options.inputDir ?? defaultKubepileDir();
|
|
13
13
|
const inputFiles = await listKubeConfigFiles(inputDir);
|
|
14
|
+
return buildMergedConfigFromFiles(inputFiles);
|
|
15
|
+
}
|
|
16
|
+
async function buildMergedConfigFromFiles(inputFiles) {
|
|
14
17
|
const configs = await Promise.all(inputFiles.map(async (filePath) => ({
|
|
15
18
|
filePath,
|
|
16
19
|
config: await readKubeConfigFile(filePath),
|
|
@@ -41,7 +44,7 @@ export async function compileToKubeConfig(options = {}) {
|
|
|
41
44
|
const { config, inputFiles } = await buildMergedConfig({ ...options, inputDir });
|
|
42
45
|
let backedUpTo;
|
|
43
46
|
if (await pathExists(outputPath)) {
|
|
44
|
-
const shouldBackup = await
|
|
47
|
+
const shouldBackup = await shouldBackUpExistingKubeConfig(outputPath, inputDir, inputFiles, options);
|
|
45
48
|
if (shouldBackup) {
|
|
46
49
|
await mkdir(path.dirname(backupPath), { recursive: true });
|
|
47
50
|
await copyFile(outputPath, backupPath);
|
|
@@ -52,6 +55,52 @@ export async function compileToKubeConfig(options = {}) {
|
|
|
52
55
|
await writeFile(outputPath, serializeGeneratedKubeConfig(config, inputDir), "utf8");
|
|
53
56
|
return { config, inputFiles, outputPath, backedUpTo };
|
|
54
57
|
}
|
|
58
|
+
async function shouldBackUpExistingKubeConfig(outputPath, inputDir, currentInputFiles, options) {
|
|
59
|
+
if (await canSafelyRegenerate(outputPath, inputDir, currentInputFiles)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return Boolean(await options.shouldBackup?.(outputPath, `${outputPath}.bak`));
|
|
63
|
+
}
|
|
64
|
+
async function canSafelyRegenerate(outputPath, inputDir, currentInputFiles) {
|
|
65
|
+
const existingSource = await readFile(outputPath, "utf8");
|
|
66
|
+
if (!existingSource.startsWith(generatedKubeConfigHeader(inputDir))) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const existingConfig = parseKubeConfig(existingSource, outputPath);
|
|
71
|
+
const previousInputFiles = await findInputFilesRepresentedInConfig(currentInputFiles, existingConfig);
|
|
72
|
+
if (previousInputFiles.length === 0) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const { config } = await buildMergedConfigFromFiles(previousInputFiles);
|
|
76
|
+
return existingSource === serializeGeneratedKubeConfig(config, inputDir);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function findInputFilesRepresentedInConfig(inputFiles, config) {
|
|
83
|
+
const representedInputFiles = [];
|
|
84
|
+
for (const inputFile of inputFiles) {
|
|
85
|
+
const inputConfig = await readKubeConfigFile(inputFile);
|
|
86
|
+
if (configContainsAllNamedEntries(config, inputConfig, inputFile)) {
|
|
87
|
+
representedInputFiles.push(inputFile);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return representedInputFiles;
|
|
91
|
+
}
|
|
92
|
+
function configContainsAllNamedEntries(haystack, needle, sourceLabel) {
|
|
93
|
+
return hasAllNamedEntries(getNamedClusters(haystack, sourceLabel), getNamedClusters(needle, sourceLabel))
|
|
94
|
+
&& hasAllNamedEntries(getNamedUsers(haystack, sourceLabel), getNamedUsers(needle, sourceLabel))
|
|
95
|
+
&& hasAllNamedEntries(getNamedContexts(haystack, sourceLabel), getNamedContexts(needle, sourceLabel));
|
|
96
|
+
}
|
|
97
|
+
function hasAllNamedEntries(haystack, needles) {
|
|
98
|
+
const entriesByName = new Map(haystack.map((entry) => [entry.name, entry]));
|
|
99
|
+
return needles.every((needle) => deepEqual(entriesByName.get(needle.name), needle));
|
|
100
|
+
}
|
|
101
|
+
function deepEqual(left, right) {
|
|
102
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
103
|
+
}
|
|
55
104
|
async function pathExists(filePath) {
|
|
56
105
|
try {
|
|
57
106
|
await stat(filePath);
|