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

Commit f17f9c9

Browse files
ddoowaandresmgot
authored andcommitted
Support for Volumes (#194)
1 parent fa7f78c commit f17f9c9

File tree

4 files changed

+78
-16
lines changed

4 files changed

+78
-16
lines changed

lib/deploy.js

+35-10
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ function getFunctionDescription(
8080
secrets,
8181
cpu,
8282
affinity,
83-
tolerations
83+
tolerations,
84+
volumes,
85+
volumeMounts
8486
) {
8587
const funcs = {
8688
apiVersion: 'kubeless.io/v1beta1',
@@ -119,7 +121,8 @@ function getFunctionDescription(
119121
if (desc) {
120122
funcs.metadata.annotations['kubeless.serverless.com/description'] = desc;
121123
}
122-
if (image || env || memory || secrets || cpu || affinity || tolerations) {
124+
125+
if (image || env || memory || secrets || cpu || affinity || tolerations || volumes) {
123126
const container = {
124127
name: funcName,
125128
};
@@ -154,20 +157,37 @@ function getFunctionDescription(
154157
},
155158
},
156159
};
160+
161+
// convert secretes to volumes and volume mounts
157162
if (secrets !== undefined && secrets.length > 0) {
158-
if (container.volumeMounts === undefined) {
159-
container.volumeMounts = [];
163+
if (volumes === undefined) {
164+
// eslint-disable-next-line no-param-reassign
165+
volumes = [];
160166
}
161-
if (funcs.spec.deployment.spec.template.spec.volumes === undefined) {
162-
funcs.spec.deployment.spec.template.spec.volumes = [];
167+
if (volumeMounts === undefined) {
168+
// eslint-disable-next-line no-param-reassign
169+
volumeMounts = [];
163170
}
164171
secrets.forEach(secret => {
165-
container.volumeMounts.push({ name: `${secret}-vol`, mountPath: `/${secret}` });
166-
funcs.spec.deployment.spec.template.spec.volumes
167-
.push({ name: `${secret}-vol`, secret: { secretName: secret } });
172+
volumes.push({
173+
name: `${secret}-vol`,
174+
secret: { secretName: secret },
175+
});
176+
volumeMounts.push({
177+
mountPath: `/${secret}`,
178+
name: `${secret}-vol`,
179+
});
168180
});
169181
}
170182

183+
if (volumes !== undefined && volumes.length > 0) {
184+
funcs.spec.deployment.spec.template.spec.volumes = volumes;
185+
}
186+
187+
if (volumeMounts !== undefined && volumeMounts.length > 0) {
188+
container.volumeMounts = volumeMounts;
189+
}
190+
171191
if (affinity) {
172192
funcs.spec.deployment.spec.template.spec.affinity = affinity;
173193
}
@@ -178,6 +198,7 @@ function getFunctionDescription(
178198
return funcs;
179199
}
180200

201+
181202
function waitForDeployment(funcName, requestMoment, namespace, options) {
182203
const opts = _.defaults({}, options, {
183204
verbose: false,
@@ -420,6 +441,7 @@ function deployFunction(f, namespace, runtime, contentType, options) {
420441
const fenv = parseEnv(f.environment);
421442
environment = environment ? environment.concat(fenv) : fenv;
422443
}
444+
423445
const funcs = getFunctionDescription(
424446
f.id,
425447
namespace,
@@ -441,8 +463,11 @@ function deployFunction(f, namespace, runtime, contentType, options) {
441463
f.secrets,
442464
f.cpu || options.cpu,
443465
f.affinity || options.affinity,
444-
f.tolerations || options.tolerations
466+
f.tolerations || options.tolerations,
467+
f.volumes || options.volumes,
468+
f.volumeMounts || options.volumeMounts
445469
);
470+
446471
return functionsApi.getItem(funcs.metadata.name).then((res) => {
447472
if (res.code === 404) {
448473
return deployFunctionAndWait(

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-kubeless",
3-
"version": "0.8.3",
3+
"version": "0.9.0",
44
"description": "This plugin enables support for Kubeless within the [Serverless Framework](https://github.com/serverless).",
55
"main": "index.js",
66
"directories": {

test/kubelessDeploy.test.js

+41-4
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ describe('KubelessDeploy', () => {
367367
const serverlessWithSecrets = _.cloneDeep(serverlessWithFunction);
368368
serverlessWithSecrets.service.functions.myFunction.secrets = ['secret1'];
369369
kubelessDeploy = instantiateKubelessDeploy(
370-
pkgFile,
371-
depsFile,
372-
serverlessWithSecrets
370+
pkgFile,
371+
depsFile,
372+
serverlessWithSecrets
373373
);
374374
mocks.createDeploymentNocks(config.clusters[0].cluster.server, functionName, defaultFuncSpec({
375375
deployment: {
@@ -387,9 +387,46 @@ describe('KubelessDeploy', () => {
387387
},
388388
}));
389389
return expect( // eslint-disable-line no-unused-expressions
390-
kubelessDeploy.deployFunction()
390+
kubelessDeploy.deployFunction()
391391
).to.be.fulfilled;
392392
});
393+
it('should deploy a function with volumes', () => {
394+
const serverlessWithVolumes = _.cloneDeep(serverlessWithFunction);
395+
serverlessWithVolumes.service.functions.myFunction.volumes = [{
396+
name: 'vol1',
397+
persistentVolumeClaim: {
398+
claimName: 'vol-claim',
399+
},
400+
}];
401+
serverlessWithVolumes.service.functions.myFunction.volumeMounts = [{
402+
name: 'vol1',
403+
mountPath: '/foo/bar',
404+
}];
405+
kubelessDeploy = instantiateKubelessDeploy(
406+
pkgFile,
407+
depsFile,
408+
serverlessWithVolumes
409+
);
410+
mocks.createDeploymentNocks(config.clusters[0].cluster.server, functionName, defaultFuncSpec({
411+
deployment: {
412+
spec: {
413+
template: {
414+
spec: {
415+
containers: [{
416+
name: functionName,
417+
volumeMounts: [{ name: 'vol1', mountPath: '/foo/bar' }],
418+
}],
419+
volumes: [{ name: 'vol1', persistentVolumeClaim: { claimName: 'vol-claim' } }],
420+
},
421+
},
422+
},
423+
},
424+
}));
425+
return expect( // eslint-disable-line no-unused-expressions
426+
kubelessDeploy.deployFunction()
427+
).to.be.fulfilled;
428+
});
429+
393430

394431
it('should wait until a deployment is ready', () => {
395432
const funcSpec = defaultFuncSpec();

0 commit comments

Comments
 (0)