@kapeta/local-cluster-service 0.5.5 → 0.5.7

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [0.5.7](https://github.com/kapetacom/local-cluster-service/compare/v0.5.6...v0.5.7) (2023-06-17)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Register non-docker processes correctly ([#32](https://github.com/kapetacom/local-cluster-service/issues/32)) ([a6e1693](https://github.com/kapetacom/local-cluster-service/commit/a6e1693c32df233f1a16dd406ac0008f07fb3a1d))
7
+
8
+ ## [0.5.6](https://github.com/kapetacom/local-cluster-service/compare/v0.5.5...v0.5.6) (2023-06-17)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Resolve operator versions from resources ([#31](https://github.com/kapetacom/local-cluster-service/issues/31)) ([dfd6471](https://github.com/kapetacom/local-cluster-service/commit/dfd647118ffdb5544d87743543152efd124d3310))
14
+
1
15
  ## [0.5.5](https://github.com/kapetacom/local-cluster-service/compare/v0.5.4...v0.5.5) (2023-06-16)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapeta/local-cluster-service",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
4
4
  "description": "Manages configuration, ports and service discovery for locally running Kapeta systems",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -129,7 +129,7 @@ router.get('/provides/:type', async (req, res) => {
129
129
  * assign port numbers to it etc.
130
130
  */
131
131
  router.get('/consumes/resource/:resourceType/:portType/:name', async (req, res) => {
132
- const operatorInfo = await operatorManager.getResourceInfo(
132
+ const operatorInfo = await operatorManager.getConsumerResourceInfo(
133
133
  req.kapeta.systemId,
134
134
  req.kapeta.instanceId,
135
135
  req.params.resourceType,
@@ -136,8 +136,18 @@ class ContainerManager {
136
136
  return;
137
137
  }
138
138
  }
139
+
140
+ const imageTagList = (await this.docker().image.list())
141
+ .filter(image => !!image.data.RepoTags)
142
+ .map(image => image.data.RepoTags);
143
+ if (imageTagList.some((imageTags) => imageTags.indexOf(image) > -1)) {
144
+ console.log('Image found: %s', image);
145
+ return;
146
+ }
147
+ console.log('Image not found: %s', image);
139
148
  }
140
149
 
150
+ console.log('Pulling image: %s', image);
141
151
  await this.docker()
142
152
  .image.create(
143
153
  {},
@@ -149,6 +159,8 @@ class ContainerManager {
149
159
  .then((stream) => promisifyStream(stream));
150
160
 
151
161
  IMAGE_PULL_CACHE[image] = Date.now();
162
+
163
+ console.log('Image pulled: %s', image);
152
164
  }
153
165
 
154
166
  toDockerMounts(mounts) {
@@ -193,12 +205,8 @@ class ContainerManager {
193
205
  kapeta: 'true',
194
206
  };
195
207
 
196
- console.log('Pulling image: %s', image);
197
-
198
208
  await this.pull(image);
199
209
 
200
- console.log('Image pulled: %s', image);
201
-
202
210
  const ExposedPorts = {};
203
211
 
204
212
  _.forEach(opts.ports, (portInfo, containerPort) => {
@@ -227,8 +235,10 @@ class ContainerManager {
227
235
  const dockerContainer = await this.startContainer({
228
236
  name: name,
229
237
  Image: image,
238
+ Hostname: name + '.kapeta',
230
239
  Labels,
231
240
  Cmd: opts.cmd,
241
+
232
242
  ExposedPorts,
233
243
  Env,
234
244
  HealthCheck,
@@ -99,7 +99,6 @@ router.use('/', (req, res, next) => {
99
99
  router.put('/', async (req, res) => {
100
100
 
101
101
  let instance = JSON.parse(req.stringBody);
102
-
103
102
  if (req.kapeta.environment === 'docker') {
104
103
  //A bit hacky but we want to avoid overwriting the docker PID with a process PID
105
104
  const oldInstance = instanceManager.getInstance(
@@ -109,7 +108,11 @@ router.put('/', async (req, res) => {
109
108
  if (oldInstance) {
110
109
  instance.pid = oldInstance.pid;
111
110
  }
111
+ instance.type = 'docker';
112
+ } else if (req.kapeta.environment === 'process') {
113
+ instance.type = 'process';
112
114
  }
115
+
113
116
  await instanceManager.registerInstance(
114
117
  req.kapeta.systemId,
115
118
  req.kapeta.instanceId,
@@ -6,6 +6,7 @@ const _ = require('lodash');
6
6
  const mkdirp = require('mkdirp');
7
7
  const Path = require('path');
8
8
  const md5 = require('md5');
9
+ const {parseKapetaUri} = require("@kapeta/nodejs-utils");
9
10
 
10
11
  const KIND_OPERATOR = 'core/resource-type-operator';
11
12
 
@@ -38,31 +39,33 @@ class OperatorManager {
38
39
  /**
39
40
  * Get operator definition for resource type
40
41
  *
41
- * @param resourceType
42
+ * @param {string} resourceType
43
+ * @param {string} version
42
44
  * @return {Operator}
43
45
  */
44
- getOperator(resourceType) {
46
+ getOperator(resourceType, version) {
45
47
  const operators = ClusterConfiguration.getDefinitions(KIND_OPERATOR);
46
48
 
47
49
  const operator = _.find(operators, (operator) => operator.definition &&
48
50
  operator.definition.metadata &&
49
51
  operator.definition.metadata.name &&
50
- operator.definition.metadata.name.toLowerCase() === resourceType.toLowerCase());
52
+ operator.definition.metadata.name.toLowerCase() === resourceType.toLowerCase() &&
53
+ operator.version === version);
51
54
 
52
55
  if (!operator) {
53
- throw new Error('Unknown resource type: ' + resourceType);
56
+ throw new Error(`Unknown resource type: ${resourceType}:${version}`);
54
57
  }
55
58
 
56
59
  if (!operator.definition.spec ||
57
60
  !operator.definition.spec.local) {
58
- throw new Error('Operator missing local definition: ' + resourceType);
61
+ throw new Error(`Operator missing local definition: ${resourceType}:${version}`);
59
62
  }
60
63
 
61
64
  return new Operator(operator.definition.spec.local);
62
65
  }
63
66
 
64
67
  /**
65
- * Get information about a specific resource
68
+ * Get information about a specific consumed resource
66
69
  *
67
70
  * @param {string} systemId
68
71
  * @param {string} fromServiceId
@@ -71,14 +74,40 @@ class OperatorManager {
71
74
  * @param {string} name
72
75
  * @returns {Promise<{host: string, port: (*|string), type: *, protocol: *, credentials: *}>}
73
76
  */
74
- async getResourceInfo(systemId, fromServiceId, resourceType, portType, name, environment) {
77
+ async getConsumerResourceInfo(systemId, fromServiceId, resourceType, portType, name, environment) {
75
78
 
76
- const operator = this.getOperator(resourceType);
79
+ const plans = ClusterConfiguration.getDefinitions('core/plan');
77
80
 
78
- const credentials = operator.getCredentials();
81
+ const planUri = parseKapetaUri(systemId);
82
+ const currentPlan = plans.find(plan => plan.definition.metadata.name === planUri.fullName && plan.version === planUri.version);
83
+ if (!currentPlan) {
84
+ throw new Error(`Unknown plan: ${systemId}`);
85
+ }
86
+
87
+ const currentInstance = currentPlan.definition.spec.blocks?.find(instance => instance.id === fromServiceId);
88
+ if (!currentInstance) {
89
+ throw new Error(`Unknown instance: ${fromServiceId} in plan ${systemId}`);
90
+ }
91
+
92
+ const blockUri = parseKapetaUri(currentInstance.block.ref);
93
+ const blockDefinition = ClusterConfiguration.getDefinitions().find(definition =>
94
+ definition.version === blockUri.version &&
95
+ definition.definition.metadata.name === blockUri.fullName
96
+ );
79
97
 
80
- const container = await this.ensureResource(systemId, resourceType);
98
+ if (!blockDefinition) {
99
+ throw new Error(`Unknown block: ${currentInstance.block.ref} in plan ${systemId}`);
100
+ }
101
+
102
+ const blockResource = blockDefinition.definition.spec?.consumers?.find(resource => resource.metadata.name === name);
103
+ if (!blockResource) {
104
+ throw new Error(`Unknown resource: ${name} in block ${currentInstance.block.ref} in plan ${systemId}`);
105
+ }
81
106
 
107
+ const kindUri = parseKapetaUri(blockResource.kind);
108
+ const operator = this.getOperator(resourceType, kindUri.version);
109
+ const credentials = operator.getCredentials();
110
+ const container = await this.ensureResource(systemId, resourceType, kindUri.version);
82
111
  const portInfo = await container.getPort(portType);
83
112
 
84
113
  if (!portInfo) {
@@ -102,12 +131,13 @@ class OperatorManager {
102
131
  /**
103
132
  * Ensure we have a running operator of given type
104
133
  *
105
- * @param systemId
106
- * @param resourceType
134
+ * @param {string} systemId
135
+ * @param {string} resourceType
136
+ * @param {string} version
107
137
  * @return {Promise<ContainerInfo>}
108
138
  */
109
- async ensureResource(systemId, resourceType) {
110
- const operator = this.getOperator(resourceType);
139
+ async ensureResource(systemId, resourceType, version) {
140
+ const operator = this.getOperator(resourceType, version);
111
141
 
112
142
  const operatorData = operator.getData();
113
143
 
@@ -250,6 +250,7 @@ class BlockInstanceRunner {
250
250
  container = await containerManager.startContainer({
251
251
  Image: dockerImage,
252
252
  name: containerName,
253
+ Hostname: containerName + '.kapeta',
253
254
  WorkingDir: workingDir,
254
255
  Labels: {
255
256
  'instance': blockInstance.id
@@ -397,6 +398,7 @@ class BlockInstanceRunner {
397
398
  container = await containerManager.startContainer({
398
399
  Image: dockerImage,
399
400
  name: containerName,
401
+ Hostname: containerName + '.kapeta',
400
402
  Labels: {
401
403
  'instance': blockInstance.id
402
404
  },
@@ -528,6 +530,7 @@ class BlockInstanceRunner {
528
530
  container = await containerManager.startContainer({
529
531
  Image: dockerImage,
530
532
  name: containerName,
533
+ Hostname: containerName + '.kapeta',
531
534
  ExposedPorts,
532
535
  HealthCheck,
533
536
  HostConfig: {