-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the --deploy.env flag (#226)
This will allow users to pass in environment variables that should be added to a deployment config. ex: nodeshift --deploy.env NODE_ENV=development If a deployment.yaml in the .nodeshift directory already exists with environment variables, they will be merged together If there are duplicate vars, the ones passed in from the cli/api will take precedence. fixes #223
- Loading branch information
1 parent
98576e6
commit 74c482c
Showing
6 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[ | ||
"deployment-config", "service", "route", "labels", "git-info", "health-check" | ||
"deployment-config", "deployment-env", "service", "route", "labels", "git-info", "health-check" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* | ||
* Copyright 2016-2017 Red Hat, Inc, and individual contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
async function addDeploymentEnvVars (config, resourceList) { | ||
return resourceList.map((resource) => { | ||
if (resource.kind === 'DeploymentConfig') { | ||
// Check for the env array in the spec.template.spec.containers array of the deployment config | ||
// If there is one, we need to see if it already has values and merge if necessarry | ||
const env = resource.spec.template.spec.containers[0].env; | ||
if (!env) { | ||
// If there isn't one, create it and add the --deploy.env stuff | ||
resource.spec.template.spec.containers[0].env = config.deploy && config.deploy.env ? config.deploy.env : []; | ||
return resource; | ||
} | ||
resource.spec.template.spec.containers[0].env = _.unionBy(config.deploy && config.deploy.env ? config.deploy.env : [], resource.spec.template.spec.containers[0].env, 'name'); | ||
return resource; | ||
} | ||
return resource; | ||
}); | ||
} | ||
|
||
module.exports = { | ||
enrich: addDeploymentEnvVars, | ||
name: 'deployment-env' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
'use strict'; | ||
|
||
const test = require('tape'); | ||
const deploymentenEnvEnricher = require('../../lib/resource-enrichers/deployment-env-enricher'); | ||
|
||
test('deployment env enricher - no deployment config', (t) => { | ||
const resourceList = [ | ||
{ | ||
kind: 'Service', | ||
metadata: { | ||
name: 'service meta' | ||
} | ||
} | ||
]; | ||
|
||
const config = { | ||
projectName: 'Project Name', | ||
version: '1.0.0', | ||
context: { | ||
namespace: 'namespace' | ||
} | ||
}; | ||
|
||
t.ok(deploymentenEnvEnricher.enrich, 'has an enrich property'); | ||
t.equal(typeof deploymentenEnvEnricher.enrich, 'function', 'is a function'); | ||
|
||
t.ok(deploymentenEnvEnricher.name, 'has an name property'); | ||
t.equal(deploymentenEnvEnricher.name, 'deployment-env'); | ||
|
||
const p = deploymentenEnvEnricher.enrich(config, resourceList); | ||
t.ok(p instanceof Promise, 'enricher should return a promise'); | ||
|
||
p.then((dce) => { | ||
t.equal(Array.isArray(dce), true, 'should return an array'); | ||
t.equal(dce.length, 1, 'array should have 1 thing'); | ||
t.equal(dce[0].kind, 'Service', 'should have just the Service type'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('deployment env enricher - deployment config has no env', t => { | ||
const resourceList = [ | ||
{ | ||
kind: 'DeploymentConfig', | ||
spec: { | ||
template: { | ||
spec: { | ||
containers: [ | ||
{ | ||
name: 'project name' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
|
||
const config = { | ||
projectName: 'Project Name', | ||
version: '1.0.0', | ||
context: { | ||
namespace: 'namespace' | ||
} | ||
}; | ||
|
||
deploymentenEnvEnricher.enrich(config, resourceList).then(dce => { | ||
t.equal(Array.isArray(dce), true, 'should return an array'); | ||
t.equal(dce.length, 1, 'array should have 1 thing'); | ||
t.equal(dce[0].kind, 'DeploymentConfig', 'should have just the Service type'); | ||
t.ok(dce[0].spec.template.spec.containers[0].env, 'env prop should be added'); | ||
t.equal(Array.isArray(dce[0].spec.template.spec.containers[0].env), true, 'env prop is an array'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('deployment env enricher - deployment config has a env, but none passed in', t => { | ||
const resourceList = [ | ||
{ | ||
kind: 'DeploymentConfig', | ||
spec: { | ||
template: { | ||
spec: { | ||
containers: [ | ||
{ | ||
name: 'project name', | ||
env: [ | ||
{ | ||
name: 'NOT_PORT', | ||
value: 'NOT_8080' | ||
}, | ||
{ | ||
name: 'ENV2', | ||
value: 'VALUE2' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
|
||
const config = { | ||
projectName: 'Project Name', | ||
version: '1.0.0', | ||
context: { | ||
namespace: 'namespace' | ||
} | ||
}; | ||
deploymentenEnvEnricher.enrich(config, resourceList).then(dce => { | ||
t.equal(Array.isArray(dce), true, 'should return an array'); | ||
t.equal(dce.length, 1, 'array should have 1 thing'); | ||
t.equal(dce[0].kind, 'DeploymentConfig', 'should have just the Service type'); | ||
t.ok(dce[0].spec.template.spec.containers[0].env, 'env prop should be added'); | ||
t.equal(Array.isArray(dce[0].spec.template.spec.containers[0].env), true, 'env prop is an array'); | ||
t.equal(dce[0].spec.template.spec.containers[0].env.length, 2, 'should have 2 entries now'); | ||
|
||
t.end(); | ||
}); | ||
}); | ||
|
||
test('deployment env enricher - deployment config has a env, but none passed in', t => { | ||
const resourceList = [ | ||
{ | ||
kind: 'DeploymentConfig', | ||
spec: { | ||
template: { | ||
spec: { | ||
containers: [ | ||
{ | ||
name: 'project name' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
|
||
const config = { | ||
projectName: 'Project Name', | ||
version: '1.0.0', | ||
context: { | ||
namespace: 'namespace' | ||
}, | ||
deploy: { | ||
env: [ | ||
{name: 'PORT', value: '4000'} | ||
] | ||
} | ||
}; | ||
deploymentenEnvEnricher.enrich(config, resourceList).then(dce => { | ||
t.equal(Array.isArray(dce), true, 'should return an array'); | ||
t.equal(dce.length, 1, 'array should have 1 thing'); | ||
t.equal(dce[0].kind, 'DeploymentConfig', 'should have just the Service type'); | ||
t.ok(dce[0].spec.template.spec.containers[0].env, 'env prop should be added'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('deployment env enricher - deployment config has a env, passed in envs also, some overlapping', t => { | ||
const resourceList = [ | ||
{ | ||
kind: 'DeploymentConfig', | ||
spec: { | ||
template: { | ||
spec: { | ||
containers: [ | ||
{ | ||
name: 'project name', | ||
env: [ | ||
{ | ||
name: 'NOT_PORT', | ||
value: 'NOT_8080' | ||
}, | ||
{ | ||
name: 'ENV2', | ||
value: 'VALUE2' | ||
}, | ||
{ | ||
name: 'PORT', | ||
value: '3000' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
|
||
const config = { | ||
projectName: 'Project Name', | ||
version: '1.0.0', | ||
context: { | ||
namespace: 'namespace' | ||
}, | ||
deploy: { | ||
env: [ | ||
{name: 'PORT', value: '4000'} | ||
] | ||
} | ||
}; | ||
deploymentenEnvEnricher.enrich(config, resourceList).then(dce => { | ||
t.equal(Array.isArray(dce), true, 'should return an array'); | ||
t.equal(dce.length, 1, 'array should have 1 thing'); | ||
t.equal(dce[0].kind, 'DeploymentConfig', 'should have just the Service type'); | ||
t.ok(dce[0].spec.template.spec.containers[0].env, 'env prop should be added'); | ||
t.equal(Array.isArray(dce[0].spec.template.spec.containers[0].env), true, 'env prop is an array'); | ||
t.equal(dce[0].spec.template.spec.containers[0].env.length, 3, 'should have 3 entries now'); | ||
const envs = dce[0].spec.template.spec.containers[0].env.filter(e => e.name === 'PORT'); | ||
t.equal(envs.length, 1, 'only has 1 env with name PORT'); | ||
t.equal(envs[0].value, '4000', 'the value should be 4000'); | ||
|
||
t.end(); | ||
}); | ||
}); |