@hologit/lens-lib-k8s 0.1.5 → 0.2.0
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/index.js +65 -70
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
const
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const yaml = require('js-yaml');
|
|
2
3
|
|
|
3
4
|
// List of kinds that don't support namespaces
|
|
4
5
|
const namespacelessKinds = [
|
|
@@ -40,97 +41,91 @@ const namespacelessKinds = [
|
|
|
40
41
|
'volumeattachments',
|
|
41
42
|
];
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// Check if a kind supports namespaces
|
|
50
|
-
isNamespaced(kind) {
|
|
51
|
-
kind = kind.toLowerCase();
|
|
52
|
-
return namespacelessKinds.indexOf(kind) === -1
|
|
53
|
-
&& namespacelessKinds.indexOf(`${kind}s`) === -1;
|
|
54
|
-
}
|
|
44
|
+
// Check if a kind supports namespaces
|
|
45
|
+
function isNamespaced(kind) {
|
|
46
|
+
kind = kind.toLowerCase();
|
|
47
|
+
return namespacelessKinds.indexOf(kind) === -1
|
|
48
|
+
&& namespacelessKinds.indexOf(`${kind}s`) === -1;
|
|
49
|
+
}
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
51
|
+
// Generate namespace manifest
|
|
52
|
+
function generateNamespaceManifest(namespace) {
|
|
53
|
+
if (!namespace) return '';
|
|
59
54
|
|
|
60
|
-
|
|
55
|
+
return `---
|
|
61
56
|
kind: Namespace
|
|
62
57
|
apiVersion: v1
|
|
63
58
|
metadata:
|
|
64
59
|
name: "${namespace}"
|
|
65
60
|
`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Patch namespaces in a manifest file
|
|
64
|
+
async function patchNamespaces(yamlPath, {
|
|
65
|
+
namespace,
|
|
66
|
+
fill = false,
|
|
67
|
+
override = false
|
|
68
|
+
}) {
|
|
69
|
+
console.error('Patching namespaces...');
|
|
70
|
+
|
|
71
|
+
if (!yamlPath) {
|
|
72
|
+
throw new Error('yaml-path required');
|
|
66
73
|
}
|
|
67
74
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
75
|
+
if (!fill && !override) {
|
|
76
|
+
console.error('neither namespace_fill or namespace_override is enabled, doing nothing');
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// load objects
|
|
81
|
+
const objects = yaml.safeLoadAll(fs.readFileSync(yamlPath, 'utf8'));
|
|
71
82
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
83
|
+
// patch namespaces
|
|
84
|
+
let patchedCount = 0;
|
|
85
|
+
for (const object of objects) {
|
|
86
|
+
// null values indicate empty documents
|
|
87
|
+
if (!object) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
76
90
|
|
|
77
|
-
if (!
|
|
78
|
-
throw new Error('
|
|
91
|
+
if (!object.metadata) {
|
|
92
|
+
throw new Error('encountered object with no metadata');
|
|
79
93
|
}
|
|
80
94
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
95
|
+
const { kind, metadata: { name, namespace: currentNamespace } } = object;
|
|
96
|
+
|
|
97
|
+
if (!name) {
|
|
98
|
+
throw new Error('encountered object with no name');
|
|
84
99
|
}
|
|
85
100
|
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// patch namespaces
|
|
90
|
-
let patchedCount = 0;
|
|
91
|
-
for (const object of objects) {
|
|
92
|
-
// null values indicate empty documents
|
|
93
|
-
if (!object) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (!object.metadata) {
|
|
98
|
-
throw new Error('encountered object with no metadata');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const { kind, metadata: { name, namespace } } = object;
|
|
102
|
-
|
|
103
|
-
if (!name) {
|
|
104
|
-
throw new Error('encountered object with no name');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// some kinds don't have namespaces
|
|
108
|
-
if (!this.isNamespaced(kind)) {
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (override || (fill && !namespace)) {
|
|
113
|
-
object.metadata.namespace = defaultNamespace;
|
|
114
|
-
console.error(`namespacing ${defaultNamespace}/${kind}/${name}`);
|
|
115
|
-
patchedCount++;
|
|
116
|
-
}
|
|
101
|
+
// some kinds don't have namespaces
|
|
102
|
+
if (!isNamespaced(kind)) {
|
|
103
|
+
continue;
|
|
117
104
|
}
|
|
118
105
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
.map(object => yaml.dump(object))
|
|
125
|
-
.join('\n---\n\n')
|
|
126
|
-
);
|
|
127
|
-
console.error(`patched ${patchedCount} namespaces in ${yamlPath}`);
|
|
106
|
+
if (override || (fill && !currentNamespace)) {
|
|
107
|
+
object.metadata.namespace = namespace;
|
|
108
|
+
console.error(`namespacing ${namespace}/${kind}/${name}`);
|
|
109
|
+
patchedCount++;
|
|
110
|
+
}
|
|
128
111
|
}
|
|
112
|
+
|
|
113
|
+
// save changes
|
|
114
|
+
fs.writeFileSync(
|
|
115
|
+
yamlPath,
|
|
116
|
+
objects
|
|
117
|
+
.filter(obj => obj !== null)
|
|
118
|
+
.map(object => yaml.safeDump(object))
|
|
119
|
+
.join('\n---\n\n')
|
|
120
|
+
);
|
|
121
|
+
console.error(`patched ${patchedCount} namespaces in ${yamlPath}`);
|
|
129
122
|
}
|
|
130
123
|
|
|
131
124
|
module.exports = {
|
|
132
|
-
K8sManifestHandler,
|
|
133
125
|
namespacelessKinds,
|
|
126
|
+
isNamespaced,
|
|
127
|
+
generateNamespaceManifest,
|
|
128
|
+
patchNamespaces,
|
|
134
129
|
|
|
135
130
|
// Export common utilities that might be needed by lenses
|
|
136
131
|
yaml
|