Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 201066d

Browse files
author
Andres
committed
Move logic into library files
1 parent 1e3eae4 commit 201066d

20 files changed

+1854
-1679
lines changed

deploy/kubelessDeploy.js

+40-419
Large diffs are not rendered by default.

info/kubelessInfo.js

+6-135
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
'use strict';
1818

1919
const _ = require('lodash');
20-
const Api = require('kubernetes-client');
2120
const BbPromise = require('bluebird');
22-
const chalk = require('chalk');
21+
const getInfo = require('../lib/get-info');
2322
const helpers = require('../lib/helpers');
2423

25-
function toMultipleWords(word) {
26-
return word.replace(/([A-Z])/, ' $1').replace(/^./, (l) => l.toUpperCase());
27-
}
28-
2924
class KubelessInfo {
3025
constructor(serverless, options) {
3126
this.serverless = serverless;
@@ -62,136 +57,12 @@ class KubelessInfo {
6257
return BbPromise.resolve();
6358
}
6459

65-
formatMessage(service, f, options) {
66-
if (options && !options.color) chalk.enabled = false;
67-
let message = '';
68-
message += `\n${chalk.yellow.underline(`Service Information "${service.name}"`)}\n`;
69-
message += `${chalk.yellow('Cluster IP: ')} ${service.ip}\n`;
70-
message += `${chalk.yellow('Type: ')} ${service.type}\n`;
71-
message += `${chalk.yellow('Ports: ')}\n`;
72-
_.each(service.ports, (port) => {
73-
// Ports can have variable properties
74-
_.each(port, (value, key) => {
75-
message += ` ${chalk.yellow(`${toMultipleWords(key)}: `)} ${value}\n`;
76-
});
77-
});
78-
if (this.options.verbose) {
79-
message += `${chalk.yellow('Metadata')}\n`;
80-
message += ` ${chalk.yellow('Self Link: ')} ${service.selfLink}\n`;
81-
message += ` ${chalk.yellow('UID: ')} ${service.uid}\n`;
82-
message += ` ${chalk.yellow('Timestamp: ')} ${service.timestamp}\n`;
83-
}
84-
message += `${chalk.yellow.underline('Function Info')}\n`;
85-
if (f.url) {
86-
message += `${chalk.yellow('URL: ')} ${f.url}\n`;
87-
}
88-
if (f.annotations && f.annotations['kubeless.serverless.com/description']) {
89-
message += `${chalk.yellow('Description:')} ` +
90-
`${f.annotations['kubeless.serverless.com/description']}\n`;
91-
}
92-
if (f.labels) {
93-
message += `${chalk.yellow('Labels:\n')}`;
94-
_.each(f.labels, (v, k) => {
95-
message += `${chalk.yellow(` ${k}:`)} ${v}\n`;
96-
});
97-
}
98-
message += `${chalk.yellow('Handler: ')} ${f.handler}\n`;
99-
message += `${chalk.yellow('Runtime: ')} ${f.runtime}\n`;
100-
if (f.type === 'PubSub' && !_.isEmpty(f.topic)) {
101-
message += `${chalk.yellow('Topic Trigger:')} ${f.topic}\n`;
102-
} else {
103-
message += `${chalk.yellow('Trigger: ')} ${f.type}\n`;
104-
}
105-
message += `${chalk.yellow('Dependencies: ')} ${f.deps}`;
106-
if (this.options.verbose) {
107-
message += `\n${chalk.yellow('Metadata:')}\n`;
108-
message += ` ${chalk.yellow('Self Link: ')} ${f.selfLink}\n`;
109-
message += ` ${chalk.yellow('UID: ')} ${f.uid}\n`;
110-
message += ` ${chalk.yellow('Timestamp: ')} ${f.timestamp}`;
111-
}
112-
return message;
113-
}
114-
11560
infoFunction(options) {
116-
let counter = 0;
117-
let message = '';
118-
return new BbPromise((resolve) => {
119-
_.each(this.serverless.service.functions, (desc, f) => {
120-
const connectionOptions = helpers.getConnectionOptions(helpers.loadKubeConfig(), {
121-
namespace: desc.namespace || this.serverless.service.provider.namespace,
122-
});
123-
const core = new Api.Core(connectionOptions);
124-
const thirdPartyResources = new Api.ThirdPartyResources(connectionOptions);
125-
const extensions = new Api.Extensions(connectionOptions);
126-
thirdPartyResources.addResource('functions');
127-
core.services.get((err, servicesInfo) => {
128-
if (err) throw new this.serverless.classes.Error(err);
129-
thirdPartyResources.ns.functions.get((ferr, functionsInfo) => {
130-
if (ferr) throw new this.serverless.classes.Error(ferr);
131-
extensions.ns.ingress.get((ierr, ingressInfo) => {
132-
if (ierr) throw this.serverless.classes.Error(ierr);
133-
const fDesc = _.find(functionsInfo.items, item => item.metadata.name === f);
134-
const functionService = _.find(
135-
servicesInfo.items,
136-
(service) => (
137-
service.metadata.labels &&
138-
service.metadata.labels.function === f
139-
)
140-
);
141-
if (_.isEmpty(functionService) || _.isEmpty(fDesc)) {
142-
this.serverless.cli.consoleLog(
143-
`Not found any information about the function "${f}"`
144-
);
145-
} else {
146-
const fIngress = _.find(ingressInfo.items, item => (
147-
item.metadata.labels && item.metadata.labels.function === f
148-
));
149-
let url = null;
150-
if (fIngress) {
151-
url = `${fIngress.spec.rules[0].host || 'API_URL'}` +
152-
`${fIngress.spec.rules[0].http.paths[0].path}`;
153-
}
154-
const service = {
155-
name: functionService.metadata.name,
156-
ip: functionService.spec.clusterIP,
157-
type: functionService.spec.type,
158-
ports: functionService.spec.ports,
159-
selfLink: functionService.metadata.selfLink,
160-
uid: functionService.metadata.uid,
161-
timestamp: functionService.metadata.creationTimestamp,
162-
};
163-
const func = {
164-
name: f,
165-
url,
166-
handler: fDesc.spec.handler,
167-
runtime: fDesc.spec.runtime,
168-
topic: fDesc.spec.topic,
169-
type: fDesc.spec.type,
170-
deps: fDesc.spec.deps,
171-
annotations: fDesc.metadata.annotations,
172-
labels: fDesc.metadata.labels,
173-
selfLink: fDesc.metadata.selfLink,
174-
uid: fDesc.metadata.uid,
175-
timestamp: fDesc.metadata.creationTimestamp,
176-
};
177-
message += this.formatMessage(
178-
service,
179-
func,
180-
_.defaults({}, options, { color: true })
181-
);
182-
}
183-
counter++;
184-
if (counter === _.keys(this.serverless.service.functions).length) {
185-
if (!_.isEmpty(message)) {
186-
this.serverless.cli.consoleLog(message);
187-
}
188-
resolve(message);
189-
}
190-
});
191-
});
192-
});
193-
});
194-
});
61+
return getInfo(this.serverless.service.functions, _.defaults({}, options, {
62+
namespace: this.serverless.service.provider.namespace,
63+
verbose: this.options.verbose,
64+
log: this.serverless.cli.consoleLog,
65+
}));
19566
}
19667
}
19768

invoke/kubelessInvoke.js

+12-90
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
const _ = require('lodash');
2020
const BbPromise = require('bluebird');
2121
const path = require('path');
22-
const request = require('request');
2322
const helpers = require('../lib/helpers');
23+
const invoke = require('../lib/invoke');
2424

2525
class KubelessInvoke {
2626
constructor(serverless, options) {
@@ -36,46 +36,7 @@ class KubelessInvoke {
3636
};
3737
}
3838

39-
getData(data) {
40-
let result = null;
41-
const d = data || this.options.data;
42-
try {
43-
if (!_.isEmpty(d)) {
44-
try {
45-
// Try to parse data as JSON
46-
result = {
47-
body: JSON.parse(d),
48-
json: true,
49-
};
50-
} catch (e) {
51-
// Assume data is a string
52-
result = {
53-
body: d,
54-
};
55-
}
56-
} else if (this.options.path) {
57-
const absolutePath = path.isAbsolute(this.options.path) ?
58-
this.options.path :
59-
path.join(this.serverless.config.servicePath, this.options.path);
60-
if (!this.serverless.utils.fileExistsSync(absolutePath)) {
61-
throw new this.serverless.classes.Error('The file you provided does not exist.');
62-
}
63-
result = {
64-
body: this.serverless.utils.readFileSync(absolutePath),
65-
json: true,
66-
};
67-
}
68-
} catch (e) {
69-
throw new this.serverless.classes.Error(
70-
`Unable to parse data given in the arguments: \n${e.message}`
71-
);
72-
}
73-
return result;
74-
}
75-
7639
validate() {
77-
// Parse data to ensure it has a correct format
78-
this.getData();
7940
const unsupportedOptions = ['stage', 'region', 'type'];
8041
helpers.warnUnsupportedOptions(
8142
unsupportedOptions,
@@ -93,58 +54,19 @@ class KubelessInvoke {
9354
invokeFunction(func, data) {
9455
const f = func || this.options.function;
9556
this.serverless.cli.log(`Calling function: ${f}...`);
96-
const config = helpers.loadKubeConfig();
97-
const APIRootUrl = helpers.getKubernetesAPIURL(config);
98-
const namespace = this.serverless.service.functions[f].namespace ||
99-
this.serverless.service.provider.namespace ||
100-
helpers.getDefaultNamespace(config);
101-
const url = `${APIRootUrl}/api/v1/proxy/namespaces/${namespace}/services/${f}/`;
102-
const connectionOptions = Object.assign(
103-
helpers.getConnectionOptions(helpers.loadKubeConfig()),
104-
{ url }
105-
);
106-
const requestData = this.getData(data);
107-
if (this.serverless.service.functions[f].sequence) {
108-
let promise = null;
109-
_.each(this.serverless.service.functions[f].sequence.slice(), sequenceFunction => {
110-
if (promise) {
111-
promise = promise.then(
112-
result => this.invokeFunction(sequenceFunction, result.body)
113-
);
114-
} else {
115-
promise = this.invokeFunction(sequenceFunction, data);
116-
}
117-
});
118-
return new BbPromise((resolve, reject) => promise.then(
119-
(response) => resolve(response),
120-
err => reject(err)
121-
));
57+
let dataPath = this.options.path;
58+
if (dataPath && !path.isAbsolute(dataPath)) {
59+
dataPath = path.join(this.serverless.config.servicePath, dataPath);
12260
}
123-
return new BbPromise((resolve, reject) => {
124-
const parseReponse = (err, response) => {
125-
if (err) {
126-
reject(new this.serverless.classes.Error(err.message, err.statusCode));
127-
} else {
128-
if (response.statusCode !== 200) {
129-
reject(new this.serverless.classes.Error(response.statusMessage, response.statusCode));
130-
}
131-
resolve(response);
132-
}
133-
};
134-
if (_.isEmpty(requestData)) {
135-
// There is no data to send, sending a GET request
136-
request.get(connectionOptions, parseReponse);
137-
} else {
138-
// Sending request data with a POST
139-
request.post(
140-
Object.assign(
141-
connectionOptions,
142-
requestData
143-
),
144-
parseReponse
145-
);
61+
return invoke(
62+
f,
63+
data || this.options.data,
64+
_.map(this.serverless.service.functions, (desc, ff) => _.assign({}, desc, { id: ff })),
65+
{
66+
namespace: this.serverless.service.provider.namespace,
67+
path: dataPath,
14668
}
147-
});
69+
);
14870
}
14971

15072
log(response) {

0 commit comments

Comments
 (0)