Skip to content

Commit

Permalink
feat: Add an --expose flag which when true will create a default ro…
Browse files Browse the repository at this point in the history
…ute and expose the default service
  • Loading branch information
lholmquist committed Mar 22, 2018
1 parent a2b1deb commit 6e06ec6
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ By default, if you run just `nodeshift`, it will run the `deploy` goal, which is

### `.nodeshift` Directory

The `.nodeshift` directory contains your resource fragements. These are `.yml` files that describe your services, deployments, routes, etc. By default, nodeshift will create a `Service` and `DeploymentConfig` in memory, if none are provided. A `Route` resource fragment should be provided if you want to expose your application to the outside world.
The `.nodeshift` directory contains your resource fragements. These are `.yml` files that describe your services, deployments, routes, etc. By default, nodeshift will create a `Service` and `DeploymentConfig` in memory, if none are provided. A `Route` resource fragment should be provided or use the `expose` flag if you want to expose your application to the outside world.

### Resource Fragments

Expand Down Expand Up @@ -159,6 +159,9 @@ Specify the s2i builder image of Node.js to use for the deployed applications.
#### quiet
supress INFO and TRACE lines from output logs.

#### expose
options to create a default route, if non is provided. Defaults to false

#### removeAll
option to remove builds, buildConfigs and Imagestreams. Defaults to false - **Only for the `undeploy` Command**

Expand Down
7 changes: 7 additions & 0 deletions bin/nodeshift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ yargs
type: 'boolean'
})
.array('d')
.options('expose', {
describe: 'flag to create a default Route and expose the default service',
choices: [true, false],
type: 'boolean',
default: false
})
.option('build.recreate', {
describe: 'flag to recreate a buildConfig or Imagestream',
choices: ['buildConfig', 'imageStream', false, true],
Expand Down Expand Up @@ -121,6 +127,7 @@ function createOptions (argv) {
options.cmd = argv.cmd;
// undeploy might have a positional argument
options.removeAll = argv.removeAll;
options.expose = argv.expose;
options.strictSSL = argv.strictSSL !== 'false';
options.tryServiceAccount = argv.tryServiceAccount !== 'false';
options.configLocation = argv.configLocation;
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const cli = require('./bin/cli');
@param {string} [options.projectLocation] - the location(directory) of your projects package.json. Defaults to `process.cwd`
@param {boolean} [options.strictSSL] - Set to false to allow self-signed Certs
@param {boolean} [options.tryServiceAccount] - Set to false to by-pass service account lookup
@param {boolean} [options.expose] - Set to true to create a default Route and expose the default service. defaults to false
@param {string} [options.nodeVersion] - set the nodeversion to use for the bucharest-gold/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/bucharestgold/centos7-s2i-nodejs/tags/
@param {boolean} [options.quiet] - supress INFO and TRACE lines from output logs
@param {object} [options.build] -
Expand All @@ -37,6 +38,7 @@ function deploy (options = {}) {
@param {string} [options.projectLocation] - the location(directory) of your projects package.json. Defaults to `process.cwd`
@param {boolean} [options.strictSSL] - Set to false to allow self-signed Certs
@param {boolean} [options.tryServiceAccount] - Set to false to by-pass service account lookup
@param {boolean} [options.expose] - Set to true to create a default Route and expose the default service. defaults to false
@param {string} [options.nodeVersion] - set the nodeversion to use for the bucharest-gold/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/bucharestgold/centos7-s2i-nodejs/tags/
@param {boolean} [options.quiet] - supress INFO and TRACE lines from output logs
@param {object} [options.build] -
Expand All @@ -57,6 +59,7 @@ function resource (options = {}) {
@param {string} [options.projectLocation] - the location(directory) of your projects package.json. Defaults to `process.cwd`
@param {boolean} [options.strictSSL] - Set to false to allow self-signed Certs
@param {boolean} [options.tryServiceAccount] - Set to false to by-pass service account lookup
@param {boolean} [options.expose] - Set to true to create a default Route and expose the default service. defaults to false
@param {string} [options.nodeVersion] - set the nodeversion to use for the bucharest-gold/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/bucharestgold/centos7-s2i-nodejs/tags/
@param {boolean} [options.quiet] - supress INFO and TRACE lines from output logs
@param {object} [options.build] -
Expand Down
3 changes: 3 additions & 0 deletions lib/definitions/route-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const _ = require('lodash');
const baseRouteSpec = {
to: {
kind: 'Service'
},
port: {
targetPort: 8080
}
};

Expand Down
34 changes: 29 additions & 5 deletions lib/resource-enrichers/route-enricher.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,46 @@
const _ = require('lodash');

const routeSpec = require('../definitions/route-spec');
const objectMetadata = require('../definitions/object-metadata');

const baseRouteConfig = {
apiVersion: 'v1', // Not Required,
kind: 'Route', // Not Required
spec: {}
};

function defaultRoute (config) {
const routeConfig = _.merge({}, baseRouteConfig);

// Apply MetaData
routeConfig.metadata = objectMetadata({
name: config.projectName
});

// apply resource spec
routeSpec(routeConfig, config);

return routeConfig;
}

async function createRoute (config, resourceList) {
// First check to see if we have a Route
if (_.filter(resourceList, {'kind': 'Route'}).length < 1) {
// check to see if the user wants to create a default route with the "expose" flag
if (config.expose) {
// create the default route and add in to the resource list
resourceList.push(defaultRoute(config));
return resourceList;
}
}

return resourceList.map((resource) => {
if (resource.kind !== 'Route') {
return resource;
}
const routeConfig = _.merge({}, baseRouteConfig, resource);

// apply resource spec
routeSpec(routeConfig, config);

return routeConfig;
// Merge the default Route Config with the current resource
return _.merge({}, defaultRoute(config), resource);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 48 additions & 5 deletions test/enricher-tests/route-enricher-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
const test = require('tape');
const proxyquire = require('proxyquire');

const config = {
projectName: 'Project Name',
version: '1.0.0'
};
test('Route enricher - route resource', (t) => {
const config = {
projectName: 'Project Name',
version: '1.0.0'
};

test('Route enricher - no route resource', (t) => {
const routeEnricher = proxyquire('../../lib/resource-enrichers/route-enricher', {
'../definitions/route-spec': () => {
t.fail('this should not be hit');
Expand Down Expand Up @@ -39,6 +39,11 @@ test('Route enricher - no route resource', (t) => {
});

test('Route enricher - no route resource', async (t) => {
const config = {
projectName: 'Project Name',
version: '1.0.0'
};

const routeEnricher = proxyquire('../../lib/resource-enrichers/route-enricher', {
'../definitions/route-spec': () => {}
});
Expand All @@ -64,3 +69,41 @@ test('Route enricher - no route resource', async (t) => {
t.ok(re[1].spec, 'spec should exist');
t.end();
});

test('Route enricher - no route resource - using expose', (t) => {
const config = {
projectName: 'Project Name',
version: '1.0.0',
expose: true
};

const routeEnricher = proxyquire('../../lib/resource-enrichers/route-enricher', {
'../definitions/route-spec': () => {
t.pass('this should be hit');
}
});
const resourceList = [
{
kind: 'Service',
metadata: {
name: 'service meta'
}
}
];

t.ok(routeEnricher.enrich, 'has an enrich property');
t.equal(typeof routeEnricher.enrich, 'function', 'is a function');
t.ok(routeEnricher.name, 'has an name property');
t.equal(routeEnricher.name, 'route', 'name property is route');

const p = routeEnricher.enrich(config, resourceList);
t.ok(p instanceof Promise, 'enricher should return a promise');

p.then((re) => {
t.equal(Array.isArray(re), true, 'should return an array');
t.equal(re, resourceList, 'arrays should not be equal');
t.ok(re[1].spec, 'spec should exist');
t.equal(re[1].kind, 'Route', 'should have created a route');
t.end();
});
});

0 comments on commit 6e06ec6

Please sign in to comment.