Skip to content

Commit

Permalink
feat: Define a subdirectory below .nodeshift/ that indicates where Op…
Browse files Browse the repository at this point in the history
…enshift resources are stored (#493)

* adding --resourceProfile flag
  • Loading branch information
aalykiot committed Sep 11, 2020
1 parent 12225ba commit ed269ea
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ Specify the s2i builder image of Node.js to use for the deployed applications.
#### web-app
Flag to automatically set the appropriate docker image for web app deployment. Defaults to false
#### resourceProfile
Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored
#### outputImageStream
The name of the ImageStream to output to. Defaults to project name from package.json
Expand Down Expand Up @@ -235,6 +238,8 @@ Shows the below help
--web-app flag to automatically set the appropriate docker image
for web app deployment
[boolean] [default: false]
--resourceProfile Define a subdirectory below .nodeshift/ that indicates
where Openshift resources are stored [string]
--outputImageStream The name of the ImageStream to output to. Defaults
to project name from package.json [string]
--outputImageStreamTag The tag of the ImageStream to output to. [string]
Expand Down
5 changes: 5 additions & 0 deletions bin/nodeshift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ yargs
type: 'boolean',
default: false
})
.option('resourceProfile', {
describe: 'Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored',
type: 'string'
})
.options('outputImageStream', {
describe: 'The name of the ImageStream to output to. Defaults to project name from package.json',
type: 'string'
Expand Down Expand Up @@ -182,6 +186,7 @@ function createOptions (argv) {
options.dockerImage = argv.dockerImage;
options.imageTag = argv.imageTag;
options.webApp = argv.webApp;
options.resourceProfile = argv.resourceProfile;
options.outputImageStreamName = argv.outputImageStream;
options.outputImageStreamTag = argv.outputImageStreamTag;
process.env.NODESHIFT_QUIET = argv.quiet === true;
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const cli = require('./bin/cli');
@param {string} [options.namespace.displayName] - flag to specify the project namespace display name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {boolean} [options.namespace.create] - flag to create the namespace if it does not exist. Only applicable for the build and deploy command. Must be used with namespace.name
@param {string} [options.namespace.name] - flag to specify the project namespace name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {string} [options.resourceProfile] - Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored
@param {string} [options.imageTag] - set the version to use for the ubi8/nodejs-10. Versions are ubi8/nodejs-10 tags: https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi8/nodejs-10
@param {string} [options.outputImageStream] - the name of the ImageStream to output to. Defaults to project name from package.json
@param {string} [options.outputImageTag] - The tag of the ImageStream to output to. Defaults to latest
Expand Down Expand Up @@ -49,6 +50,7 @@ function deploy (options = {}) {
@param {object} [options.namespace] -
@param {string} [options.namespace.displayName] - flag to specify the project namespace display name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {string} [options.namespace.name] - flag to specify the project namespace name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {string} [options.resourceProfile] - Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored
@param {string} [options.imageTag] - set the version to use for the ubi8/nodejs-10. Versions are ubi8/nodejs-10 tags: https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi8/nodejs-10
@param {string} [options.outputImageStream] - the name of the ImageStream to output to. Defaults to project name from package.json
@param {string} [options.outputImageTag] - The tag of the ImageStream to output to. Defaults to latest
Expand Down Expand Up @@ -76,6 +78,7 @@ function resource (options = {}) {
@param {string} [options.namespace.displayName] - flag to specify the project namespace display name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {boolean} [options.namespace.create] - flag to create the namespace if it does not exist. Only applicable for the build and deploy command. Must be used with namespace.name
@param {string} [options.namespace.name] - flag to specify the project namespace name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {string} [options.resourceProfile] - Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored
@param {string} [options.imageTag] - set the version to use for the ubi8/nodejs-10. Versions are ubi8/nodejs-10 tags: https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi8/nodejs-10
@param {string} [options.outputImageStream] - the name of the ImageStream to output to. Defaults to project name from package.json
@param {string} [options.outputImageTag] - The tag of the ImageStream to output to. Defaults to latest
Expand Down Expand Up @@ -105,6 +108,7 @@ function applyResource (options = {}) {
@param {string} [options.namespace.displayName] - flag to specify the project namespace display name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {boolean} [options.namespace.remove] - flag to remove the user created namespace. Only applicable for the undeploy command. Must be used with namespace.name
@param {string} [options.namespace.name] - flag to specify the project namespace name to build/deploy into. Overwrites any namespace settings in your OpenShift or Kubernetes configuration files
@param {string} [options.resourceProfile] - Define a subdirectory below .nodeshift/ that indicates where Openshift resources are stored
@param {string} [options.imageTag] - set the version to use for the ubi8/nodejs-10. Versions are ubi8/nodejs-10 tags: https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi8/nodejs-10
@param {string} [options.outputImageStream] - the name of the ImageStream to output to. Defaults to project name from package.json
@param {string} [options.outputImageTag] - The tag of the ImageStream to output to. Defaults to latest
Expand Down
75 changes: 46 additions & 29 deletions lib/resource-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ function loadFiles (resourceList, config) {
let loadedResource;

if (resource.ext === 'json') {
loadedResource = await loadJSON(`${config.projectLocation}/${config.nodeshiftDirectory}/${resource.filename}`);
loadedResource = await loadJSON(
config.resourceProfile
? `${config.projectLocation}/${config.nodeshiftDirectory}/${config.resourceProfile}/${resource.filename}`
: `${config.projectLocation}/${config.nodeshiftDirectory}/${resource.filename}`
);
} else {
loadedResource = await loadYaml(`${config.projectLocation}/${config.nodeshiftDirectory}/${resource.filename}`);
loadedResource = await loadYaml(
config.resourceProfile
? `${config.projectLocation}/${config.nodeshiftDirectory}/${config.resourceProfile}/${resource.filename}`
: `${config.projectLocation}/${config.nodeshiftDirectory}/${resource.filename}`
);
}

let kind;
Expand Down Expand Up @@ -136,36 +144,45 @@ function loadFiles (resourceList, config) {

function findFiles (config) {
return new Promise((resolve, reject) => {
fs.readdir(`${config.projectLocation}/${config.nodeshiftDirectory}`, (err, files) => {
// Probably can ignore, or maybe have a warning or something if the directory doesn't exist
if (err && err.code === 'ENOENT') {
logger.warning(`No ${config.nodeshiftDirectory} directory`);
return resolve([]);
}
fs.readdir(
config.resourceProfile
? `${config.projectLocation}/${config.nodeshiftDirectory}/${config.resourceProfile}`
: `${config.projectLocation}/${config.nodeshiftDirectory}`,
(err, files) => {
// Probably can ignore, or maybe have a warning or something if the directory doesn't exist
if (err && err.code === 'ENOENT') {
logger.warning(
config.resourceProfile
? `No ${config.nodeshiftDirectory}/${config.resourceProfile} directory`
: `No ${config.nodeshiftDirectory} directory`
);
return resolve([]);
}

if (err && err.code !== 'ENOENT') {
return reject(err);
}
if (err && err.code !== 'ENOENT') {
return reject(err);
}

// Map the files into an object that has the ext and fileLocation
// Then filter since we only want the .yml or .yaml or .json files
const filtered = files.map((file) => {
// this will split the filename [0] and extension [1]
const filesSplit = file.split('.');

// this will split the filename into name [0] and type [1]
const filenameSplit = filesSplit[0].split('-');
return {
ext: filesSplit[1],
name: filenameSplit[0],
type: filenameSplit[1],
filename: file
};
}).filter((file) => {
return file.ext === 'yml' || file.ext === 'yaml' || file.ext === 'json';
// Map the files into an object that has the ext and fileLocation
// Then filter since we only want the .yml or .yaml or .json files
const filtered = files.map((file) => {
// this will split the filename [0] and extension [1]
const filesSplit = file.split('.');

// this will split the filename into name [0] and type [1]
const filenameSplit = filesSplit[0].split('-');
return {
ext: filesSplit[1],
name: filenameSplit[0],
type: filenameSplit[1],
filename: file
};
}).filter((file) => {
return file.ext === 'yml' || file.ext === 'yaml' || file.ext === 'json';
});

return resolve(filtered);
});
return resolve(filtered);
});
});
}

Expand Down
98 changes: 98 additions & 0 deletions test/resource-loader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,101 @@ test('test string substitution', (t) => {
t.end();
});
});

test('test using custom resource location', (t) => {
const mockedHelper = {
yamlToJson: (file) => { return {}; }
};

const mockedfs = {
readFile: (locations, options, cb) => {
return cb(null, '{}');
},
readdir: (path, cb) => {
// test custom path
t.equals(path, `${process.cwd()}/.nodeshift/prod`, 'should be using custom resource location');
return cb(null, ['yes-svc.yml', 'yes1-route.yml', 'yes3-secret.yaml', 'deployment.json']);
}
};

const resourceLoader = proxyquire('../lib/resource-loader', {
fs: mockedfs,
'./helpers': mockedHelper
});

const config = {
projectLocation: process.cwd(),
nodeshiftDirectory: '.nodeshift',
resourceProfile: 'prod',
namespace: 'my namespace'
};

resourceLoader(config).then((resourceList) => {
t.equals(Array.isArray(resourceList), true, 'returns an array');
t.equal(resourceList.length, 4, 'should be length 4');
t.end();
});
});

test('test files at the top level with a subdirectory but no resourceProfile flag used', (t) => {
const mockedHelper = {
yamlToJson: (file) => { return {}; }
};

const mockedfs = {
readFile: (locations, options, cb) => {
return cb(null, '{}');
},
readdir: (path, cb) => {
return cb(null, ['prod', 'yes3-secret.yaml', 'deployment.json']);
}
};

const resourceLoader = proxyquire('../lib/resource-loader', {
fs: mockedfs,
'./helpers': mockedHelper
});

const config = {
projectLocation: process.cwd(),
nodeshiftDirectory: '.nodeshift',
namespace: 'my namespace'
};

resourceLoader(config).then((resourceList) => {
t.equals(Array.isArray(resourceList), true, 'returns an array');
t.equal(resourceList.length, 2, 'should be length 2');
t.end();
});
});

test('test no resource directory while using the resourceProfile flag', (t) => {
const mockedfs = {
readFile: (locations, options, cb) => { return cb(null, null); },
readdir: (path, cb) => {
// test custom resource directory
t.equals(path, `${process.cwd()}/.nodeshift/prod`, 'should be using custom resource location');
const err = new Error('no directory found');
err.code = 'ENOENT';
return cb(err, null);
}
};

const resourceLoader = proxyquire('../lib/resource-loader', {
fs: mockedfs
});

const config = {
projectLocation: process.cwd(),
nodeshiftDirectory: '.nodeshift',
resourceProfile: 'prod'
};

const result = resourceLoader(config).then((resourceList) => {
t.equals(Array.isArray(resourceList), true, 'returns an array');
t.equal(resourceList.length, 0, 'should be length zero when no directory found');
t.end();
});

t.equals(result instanceof Promise, true, 'resource loader function is a promise');
});

0 comments on commit ed269ea

Please sign in to comment.