@hologit/lens-lib-k8s 0.1.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 +137 -0
- package/package.json +14 -0
package/index.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const { yaml } = require('js-yaml');
|
|
2
|
+
|
|
3
|
+
// List of kinds that don't support namespaces
|
|
4
|
+
const namespacelessKinds = [
|
|
5
|
+
'apiservices',
|
|
6
|
+
'bgpconfigurations',
|
|
7
|
+
'bgppeers',
|
|
8
|
+
'blockaffinities',
|
|
9
|
+
'certificatesigningrequests',
|
|
10
|
+
'clusterinformations',
|
|
11
|
+
'clusterissuers',
|
|
12
|
+
'clusterrolebindings',
|
|
13
|
+
'clusterroles',
|
|
14
|
+
'componentstatuses',
|
|
15
|
+
'csidrivers',
|
|
16
|
+
'csinodeinfos',
|
|
17
|
+
'csinodes',
|
|
18
|
+
'customresourcedefinitions',
|
|
19
|
+
'felixconfigurations',
|
|
20
|
+
'globalnetworkpolicies',
|
|
21
|
+
'globalnetworksets',
|
|
22
|
+
'hostendpoints',
|
|
23
|
+
'ipamblocks',
|
|
24
|
+
'ipamconfigs',
|
|
25
|
+
'ipamhandles',
|
|
26
|
+
'ippools',
|
|
27
|
+
'mutatingwebhookconfigurations',
|
|
28
|
+
'namespaces',
|
|
29
|
+
'nodes',
|
|
30
|
+
'persistentvolumes',
|
|
31
|
+
'podsecuritypolicies',
|
|
32
|
+
'priorityclasses',
|
|
33
|
+
'runtimeclasses',
|
|
34
|
+
'selfsubjectaccessreviews',
|
|
35
|
+
'selfsubjectrulesreviews',
|
|
36
|
+
'storageclasses',
|
|
37
|
+
'subjectaccessreviews',
|
|
38
|
+
'tokenreviews',
|
|
39
|
+
'validatingwebhookconfigurations',
|
|
40
|
+
'volumeattachments',
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
class K8sManifestHandler {
|
|
44
|
+
constructor(runner, options = {}) {
|
|
45
|
+
this.runner = runner;
|
|
46
|
+
this.options = options;
|
|
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
|
+
}
|
|
55
|
+
|
|
56
|
+
// Generate namespace manifest
|
|
57
|
+
generateNamespaceManifest(namespace) {
|
|
58
|
+
if (!namespace) return '';
|
|
59
|
+
|
|
60
|
+
return `---
|
|
61
|
+
kind: Namespace
|
|
62
|
+
apiVersion: v1
|
|
63
|
+
metadata:
|
|
64
|
+
name: "${namespace}"
|
|
65
|
+
`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Patch namespaces in a manifest file
|
|
69
|
+
async patchNamespaces(yamlPath, envPrefix = 'HOLOLENS') {
|
|
70
|
+
console.error('Patching namespaces...');
|
|
71
|
+
|
|
72
|
+
// read options
|
|
73
|
+
const fill = this.runner.getEnv(`${envPrefix}_NAMESPACE_FILL`) === 'true';
|
|
74
|
+
const override = this.runner.getEnv(`${envPrefix}_NAMESPACE_OVERRIDE`) === 'true';
|
|
75
|
+
const defaultNamespace = this.runner.getEnv(`${envPrefix}_NAMESPACE`);
|
|
76
|
+
|
|
77
|
+
if (!yamlPath) {
|
|
78
|
+
throw new Error('yaml-path required');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!fill && !override) {
|
|
82
|
+
console.error('neither namespace_fill or namespace_override is enabled, doing nothing');
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// load objects
|
|
87
|
+
const objects = yaml.loadAll(await this.runner.readFile(yamlPath));
|
|
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
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// save changes
|
|
120
|
+
await this.runner.writeFile(
|
|
121
|
+
yamlPath,
|
|
122
|
+
objects
|
|
123
|
+
.filter(obj => obj !== null)
|
|
124
|
+
.map(object => yaml.dump(object))
|
|
125
|
+
.join('\n---\n\n')
|
|
126
|
+
);
|
|
127
|
+
console.error(`patched ${patchedCount} namespaces in ${yamlPath}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = {
|
|
132
|
+
K8sManifestHandler,
|
|
133
|
+
namespacelessKinds,
|
|
134
|
+
|
|
135
|
+
// Export common utilities that might be needed by lenses
|
|
136
|
+
yaml
|
|
137
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hologit/lens-lib-k8s",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared kubernetes library for hologit lenses",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"js-yaml": "^4.1.0"
|
|
13
|
+
}
|
|
14
|
+
}
|