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

Commit e64f392

Browse files
author
Andres
committed
Add linter rules from serverless. Minor refactor
1 parent efdba65 commit e64f392

File tree

8 files changed

+109
-81
lines changed

8 files changed

+109
-81
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage
2+
node_modules

.eslintrc.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
"extends": "airbnb",
3+
"plugins": [],
4+
"rules": {
5+
"func-names": "off",
6+
"strict": "off",
7+
"prefer-rest-params": "off",
8+
"react/require-extension" : "off",
9+
"import/no-extraneous-dependencies" : "off",
10+
"no-console": "off"
11+
},
12+
"env": {
13+
"mocha": true,
14+
"jest": true
15+
}
16+
};

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
npm-debug.log
3+
# ESLint cache
4+
.eslintcache

deploy/kubelessDeploy.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
const BbPromise = require('bluebird');
4-
const fs = require('fs');
54
const Api = require('kubernetes-client');
5+
const helpers = require('../lib/helpers');
66

77
class KubelessDeploy {
88
constructor(serverless, options) {
@@ -12,46 +12,50 @@ class KubelessDeploy {
1212

1313
this.hooks = {
1414
'deploy:deploy': () => BbPromise.bind(this)
15-
.then(this.deployFunction)
15+
.then(this.validate)
16+
.then(this.deployFunction),
1617
};
1718
}
1819

19-
deployFunction() {
20-
this.serverless.cli.log(`Deploying function: ${this.serverless.service.service}...`);
20+
validate() {
21+
helpers.validateEnv();
22+
return BbPromise.resolve();
23+
}
2124

22-
var funcs = {
23-
apiVersion: `k8s.io/v1`,
25+
deployFunction() {
26+
const f = this.serverless.service.service;
27+
this.serverless.cli.log(`Deploying function: ${f}...`);
28+
const funcs = {
29+
apiVersion: 'k8s.io/v1',
2430
kind: 'Function',
2531
metadata: {
2632
name: this.serverless.service.service,
2733
namespace: 'default',
2834
},
2935
spec: {
3036
deps: '',
31-
function: fs.readFileSync(this.serverless.service.functions.hello.handler.toString().split(".")[0] + ".py", 'utf8'),
32-
handler: this.serverless.service.functions.hello.handler,
37+
function: this.serverless.utils.readFileSync(
38+
`${this.serverless.service.functions[f].handler.toString().split('.')[0]}.py`
39+
),
40+
handler: this.serverless.service.functions[f].handler,
3341
runtime: this.serverless.service.provider.runtime,
3442
topic: '',
3543
type: 'HTTP',
3644
},
3745
};
3846

39-
var thirdPartyResources = new Api.ThirdPartyResources({
40-
url: process.env['K8SAPISERVER'],
41-
ca: fs.readFileSync(process.env['HOME'] + "/.minikube/ca.crt"),
42-
cert: fs.readFileSync(process.env['HOME'] + "/.minikube/apiserver.crt"),
43-
key: fs.readFileSync(process.env['HOME'] + "/.minikube/apiserver.key"),
44-
group: 'k8s.io',
45-
});
47+
const thirdPartyResources = new Api.ThirdPartyResources(
48+
Object.assign(helpers.getMinikubeCredentials(), {
49+
url: process.env.K8SAPISERVER,
50+
group: 'k8s.io',
51+
})
52+
);
4653

4754
thirdPartyResources.addResource('functions');
4855
// Create function
49-
thirdPartyResources.ns.functions.post({body: funcs}, print)
56+
thirdPartyResources.ns.functions.post({ body: funcs }, helpers.print);
57+
return BbPromise.resolve();
5058
}
5159
}
5260

53-
function print(err, result) {
54-
console.log(JSON.stringify(err || result, null, 2));
55-
}
56-
5761
module.exports = KubelessDeploy;

lib/helpers.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
function print(err, result) {
8+
console.log(JSON.stringify(err || result, null, 2));
9+
}
10+
11+
function validateEnv() {
12+
if (_.isEmpty(process.env.K8SAPISERVER)) {
13+
throw new Error(
14+
'Please specify the Kubernetes API server IP as the environment variable K8SAPISERVER'
15+
);
16+
}
17+
}
18+
19+
function getMinikubeCredentials() {
20+
return {
21+
cert: fs.readFileSync(path.join(process.env.HOME, '.minikube/apiserver.crt')),
22+
ca: fs.readFileSync(path.join(process.env.HOME, '.minikube/ca.crt')),
23+
key: fs.readFileSync(path.join(process.env.HOME, '.minikube/apiserver.key')),
24+
};
25+
}
26+
module.exports = {
27+
validateEnv,
28+
getMinikubeCredentials,
29+
print,
30+
};

package.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"example": "examples"
88
},
99
"scripts": {
10+
"lint": "eslint . --cache",
1011
"test": "echo \"Error: no test specified\" && exit 1"
1112
},
1213
"repository": {
@@ -18,5 +19,17 @@
1819
"bugs": {
1920
"url": "https://github.com/bitnami/kubeless-serverless/issues"
2021
},
21-
"homepage": "https://github.com/bitnami/kubeless-serverless#readme"
22+
"homepage": "https://github.com/bitnami/kubeless-serverless#readme",
23+
"dependencies": {
24+
"bluebird": "^3.5.0",
25+
"kubernetes-client": "^3.12.0",
26+
"lodash": "^4.17.4"
27+
},
28+
"devDependencies": {
29+
"eslint": "^3.3.1",
30+
"eslint-config-airbnb": "^10.0.1",
31+
"eslint-config-airbnb-base": "^5.0.2",
32+
"eslint-plugin-jsx-a11y": "^2.1.0",
33+
"eslint-plugin-react": "^6.1.1"
34+
}
2235
}

remove/kubelessRemove.js

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const BbPromise = require('bluebird');
4-
const fs = require('fs');
53
const Api = require('kubernetes-client');
4+
const BbPromise = require('bluebird');
5+
const helpers = require('../lib/helpers');
66

77
class KubelessRemove {
88
constructor(serverless, options) {
@@ -12,31 +12,32 @@ class KubelessRemove {
1212

1313
this.hooks = {
1414
'remove:remove': () => BbPromise.bind(this)
15-
.then(this.removeFunction)
15+
.then(this.validate)
16+
.then(this.removeFunction),
1617
};
1718
}
1819

19-
removeFunction() {
20-
this.serverless.cli.log(`Removing function: ${this.serverless.service.service}...`);
20+
validate() {
21+
helpers.validateEnv();
22+
return BbPromise.resolve();
23+
}
2124

22-
var funcName = this.serverless.service.service
25+
removeFunction() {
26+
const f = this.serverless.service.service;
27+
this.serverless.cli.log(`Removing function: ${f}...`);
2328

24-
var thirdPartyResources = new Api.ThirdPartyResources({
25-
url: process.env['K8SAPISERVER'],
26-
ca: fs.readFileSync(process.env['HOME'] + "/.minikube/ca.crt"),
27-
cert: fs.readFileSync(process.env['HOME'] + "/.minikube/apiserver.crt"),
28-
key: fs.readFileSync(process.env['HOME'] + "/.minikube/apiserver.key"),
29-
group: 'k8s.io',
30-
});
29+
const thirdPartyResources = new Api.ThirdPartyResources(
30+
Object.assign(helpers.getMinikubeCredentials(), {
31+
url: process.env.K8SAPISERVER,
32+
group: 'k8s.io',
33+
})
34+
);
3135

3236
thirdPartyResources.addResource('functions');
3337
// Delete function
34-
thirdPartyResources.ns.functions.delete(funcName, print)
38+
thirdPartyResources.ns.functions.delete(f, helpers.print);
39+
return BbPromise.resolve();
3540
}
3641
}
3742

38-
function print(err, result) {
39-
console.log(JSON.stringify(err || result, null, 2));
40-
}
41-
4243
module.exports = KubelessRemove;

remove/remove/kubelessRemove.js

-42
This file was deleted.

0 commit comments

Comments
 (0)