@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.
Files changed (2) hide show
  1. package/index.js +65 -70
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
- const { yaml } = require('js-yaml');
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
- 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
- }
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
- // Generate namespace manifest
57
- generateNamespaceManifest(namespace) {
58
- if (!namespace) return '';
51
+ // Generate namespace manifest
52
+ function generateNamespaceManifest(namespace) {
53
+ if (!namespace) return '';
59
54
 
60
- return `---
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
- // Patch namespaces in a manifest file
69
- async patchNamespaces(yamlPath, envPrefix = 'HOLOLENS') {
70
- console.error('Patching namespaces...');
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
- // 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`);
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 (!yamlPath) {
78
- throw new Error('yaml-path required');
91
+ if (!object.metadata) {
92
+ throw new Error('encountered object with no metadata');
79
93
  }
80
94
 
81
- if (!fill && !override) {
82
- console.error('neither namespace_fill or namespace_override is enabled, doing nothing');
83
- return;
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
- // 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
- }
101
+ // some kinds don't have namespaces
102
+ if (!isNamespaced(kind)) {
103
+ continue;
117
104
  }
118
105
 
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}`);
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hologit/lens-lib-k8s",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "Shared kubernetes library for hologit lenses",
5
5
  "main": "index.js",
6
6
  "scripts": {