Skip to content

Commit

Permalink
feat: change the output Image Stream name and tag (#347)
Browse files Browse the repository at this point in the history
* feat: change the output Image Stream name and tag

fixes #337

* squash: fix existing tests

* squash: update docs and api docs

* squash: fix typo

* chore: update all Image Streams to ImageStream for consistency

* squash: fix test after ImageStream text updates

* squash: update to use output image tag instead of latest
  • Loading branch information
lholmquist committed Aug 19, 2019
1 parent 5c59686 commit 3faa599
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ Specify the tag of the docker image to use for the deployed application. default
#### dockerImage
Specify the s2i builder image of Node.js to use for the deployed applications. Defaults to [nodeshift/centos7-s2i-nodejs](https://hub.docker.com/r/nodeshift/centos7-s2i-nodejs)
#### outputImageStream
The name of the ImageStream to output to. Defaults to project name from package.json
#### outputImageStreamTag
The tag of the ImageStream to output to. Defaults to latest
#### quiet
suppress INFO and TRACE lines from output logs.
Expand Down Expand Up @@ -199,6 +205,9 @@ Shows the below help
--projectLocation change the default location of the project [string]
--imageTag The tag of the docker image to use for the deployed
application. [string] [default: "latest"]
--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]
--quiet suppress INFO and TRACE lines from output logs
[boolean]
--expose flag to create a default Route and expose the default
Expand Down
11 changes: 11 additions & 0 deletions bin/nodeshift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ yargs
type: 'string',
default: 'latest'
})
.options('outputImageStream', {
describe: 'The name of the ImageStream to output to. Defaults to project name from package.json',
type: 'string'
})
.options('outputImageStreamTag', {
describe: 'The tag of the ImageStream to output to.',
type: 'string',
defualt: 'latest'
})
.env('NODESHIFT')
.option('quiet', {
describe: 'suppress INFO and TRACE lines from output logs',
Expand Down Expand Up @@ -146,6 +155,8 @@ function createOptions (argv) {
options.projectLocation = argv.projectLocation;
options.dockerImage = argv.dockerImage;
options.imageTag = argv.imageTag;
options.outputImageStreamName = argv.outputImageStream;
options.outputImageStreamTag = argv.outputImageStreamTag;
process.env['NODESHIFT_QUIET'] = argv.quiet === true;
options.metadata = argv.metadata;
options.build = argv.build;
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const cli = require('./bin/cli');
@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.imageTag] - set the version to use for the nodeshift/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/nodeshift/centos7-s2i-nodejs/tags/
@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
@param {boolean} [options.quiet] - suppress INFO and TRACE lines from output logs
@param {object} [options.deploy] -
@param {number} [options.deploy.port] - flag to update the default ports on the resource files. Defaults to 8080
Expand Down Expand Up @@ -45,6 +47,8 @@ function deploy (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 {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.imageTag] - set the version to use for the nodeshift/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/nodeshift/centos7-s2i-nodejs/tags/
@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
@param {boolean} [options.quiet] - suppress INFO and TRACE lines from output logs
@param {object} [options.build] -
@param {string/boolean} [options.build.recreate] - flag to recreate a buildConfig or Imagestream. values are "buildConfig", "imageStream", true, false. Defaults to false
Expand All @@ -68,6 +72,8 @@ function resource (options = {}) {
@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.imageTag] - set the version to use for the nodeshift/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/nodeshift/centos7-s2i-nodejs/tags/
@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
@param {boolean} [options.quiet] - suppress INFO and TRACE lines from output logs
@param {object} [options.deploy] -
@param {number} [options.deploy.port] - flag to update the default ports on the resource files. Defaults to 8080
Expand All @@ -93,6 +99,8 @@ function applyResource (options = {}) {
@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.imageTag] - set the version to use for the nodeshift/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/nodeshift/centos7-s2i-nodejs/tags/
@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
@param {boolean} [options.quiet] - suppress INFO and TRACE lines from output logs
@param {boolean} [options.removeAll] - option to remove builds, buildConfigs and Imagestreams. Defaults to false
@param {object} [options.deploy] -
Expand All @@ -119,6 +127,8 @@ function undeploy (options = {}) {
@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.imageTag] - set the version to use for the nodeshift/centos7-s2i-image. Versions are docker hub tags: https://hub.docker.com/r/nodeshift/centos7-s2i-nodejs/tags/
@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
@param {boolean} [options.quiet] - suppress INFO and TRACE lines from output logs
@param {object} [options.build] -
@param {string/boolean} [options.build.recreate] - flag to recreate a buildConfig or Imagestream. values are "buildConfig", "imageStream", true, false. Defaults to false
Expand Down
3 changes: 1 addition & 2 deletions lib/build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ async function removeBuildsAndBuildConfig (config) {
async function createOrUpdateBuildConfig (config) {
// Check for a BuildConfig, create or update if necessary
const buildName = config.buildName;
const imageStreamName = config.projectName;
const outputImageStreamTag = `${imageStreamName}:latest`; // TODO: base on a tag
const outputImageStreamTag = `${config.outputImageStreamName}:${config.outputImageStreamTag}`;
// The new code under the openshift rest client, returns an error when getting a 404, so we need to catch it and check the status code
// I wonder if it is better to find all buildConfigs, then do a Array.find on the returned item list
const buildConfig = await awaitRequest(config.openshiftRestClient.apis.build.v1.ns(config.namespace.name).buildconfigs(buildName).get());
Expand Down
2 changes: 1 addition & 1 deletion lib/definitions/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = (resource, config) => {
}

const container = {
image: config.projectName,
image: config.outputImageStreamName,
name: config.projectName,
securityContext: {
privileged: false
Expand Down
2 changes: 1 addition & 1 deletion lib/definitions/deployment-config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = (resource, config) => {
from: {
kind: 'ImageStreamTag',
namespace: config.namespace.name,
name: `${config.projectName}:latest`
name: `${config.outputImageStreamName}:${config.outputImageStreamTag}`
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/image-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function createImageStream (config, options) {

// Add new metadata
imageStream.metadata = objectMetadata({
name: config.projectName,
name: config.outputImageStreamName,
namespace: config.namespace.name,
labels: {
project: config.projectName,
Expand All @@ -47,10 +47,10 @@ function createImageStream (config, options) {
}

async function createOrUpdateImageStream (config) {
const imageStream = await awaitRequest(config.openshiftRestClient.apis.image.v1.ns(config.namespace.name).imagestreams(config.projectName).get());
const imageStream = await awaitRequest(config.openshiftRestClient.apis.image.v1.ns(config.namespace.name).imagestreams(config.outputImageStreamName).get());
if (imageStream.code === 404) {
// Need to create the image stream
logger.info(`creating image stream ${config.projectName}`);
logger.info(`creating ImageStream ${config.outputImageStreamName}`);
return config.openshiftRestClient.apis.image.v1.ns(config.namespace.name).imagestreams.post({ body: createImageStream(config) });
}

Expand All @@ -59,16 +59,16 @@ async function createOrUpdateImageStream (config) {

await removeImageStream(config);

logger.info(`Re-creating image stream ${config.projectName}`);
logger.info(`Re-creating ImageStream ${config.outputImageStreamName}`);
return config.openshiftRestClient.apis.image.v1.ns(config.namespace.name).imagestreams.post({ body: createImageStream(config) });
}

logger.info(`using existing image stream ${config.projectName}`);
logger.info(`using existing image stream ${config.outputImageStreamName}`);
return imageStream;
}

function removeImageStream (config) {
logger.info(`Deleting Image Stream ${config.projectName}`);
logger.info(`Deleting ImageStream ${config.outputImageStreamName}`);

// Some default remove Body Options for the rest client, maybe at some point, these will be exposed to the user
const removeOptions = {
Expand All @@ -79,7 +79,7 @@ function removeImageStream (config) {
};

// delete image stream
return awaitRequest(config.openshiftRestClient.apis.image.v1.ns(config.namespace.name).imagestreams(config.projectName).delete({ body: removeOptions }));
return awaitRequest(config.openshiftRestClient.apis.image.v1.ns(config.namespace.name).imagestreams(config.outputImageStreamName).delete({ body: removeOptions }));
}

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions lib/nodeshift-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ async function setup (options = {}) {
throw new Error('"name" in package.json can only consist lower-case letters, numbers, and dashes. It must start with a letter and can\'t end with a -.');
}

options.outputImageStreamName = options.outputImageStreamName || projectPackage.name;
options.outputImageStreamTag = options.outputImageStreamTag || 'latest';
// Return a new object with the config, the rest client and other data.
return Object.assign({}, config, {
projectPackage,
Expand Down
2 changes: 2 additions & 0 deletions test/definitions-tests/deployment-config-spec-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ test('deployment-config-spec test', (t) => {
const resource = {};
const config = {
projectName: 'project1',
outputImageStreamName: 'project1',
outputImageStreamTag: 'latest',
namespace: {
name: 'project-namespace'
}
Expand Down
5 changes: 4 additions & 1 deletion test/image-stream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('create imageStream not found', (t) => {
let call = 0;
const config = {
projectName: 'project-name',
outputImageStreamName: 'project-name',
namespace: {
name: ''
},
Expand Down Expand Up @@ -53,7 +54,7 @@ test('create imageStream not found', (t) => {
'./common-log': () => {
return {
info: (info) => {
t.equal(info, 'creating image stream project-name', 'should have the correct info message');
t.equal(info, 'creating ImageStream project-name', 'should have the correct info message');
return info;
}
};
Expand All @@ -69,6 +70,7 @@ test('buildConfig found - no recreate', (t) => {
const config = {
buildName: 'nodejs-s2i-build',
projectName: 'project-name',
outputImageStreamName: 'project-name',
version: '1.0.0',
namespace: {
name: ''
Expand Down Expand Up @@ -117,6 +119,7 @@ test('imagestream recreate but is a buildConfig', (t) => {
},
buildName: 'nodejs-s2i-build',
projectName: 'project-name',
outputImageStreamName: 'project-name',
version: '1.0.0',
namespace: {
name: ''
Expand Down
31 changes: 31 additions & 0 deletions test/nodeshift-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ test('nodeshift-config basic setup', (t) => {
t.equal(config.projectLocation, process.cwd(), 'projectLocation prop should be cwd by default');
t.ok(config.nodeshiftDirectory, 'nodeshiftDir prop should be here');
t.equal(config.nodeshiftDirectory, '.nodeshift', 'nodeshiftDir prop should be .nodeshift by default');
t.equal(config.outputImageStreamName, 'nodeshift', 'outputImageStreamName should be the package name by default');
t.equal(config.outputImageStreamTag, 'latest', 'outputImageStreamTag should be the latest by default');
t.end();
}).catch(t.fail);

Expand Down Expand Up @@ -342,3 +344,32 @@ test('nodeshift-config options for the config loader - using namespace object fo
t.end();
});
});

test('nodeshift-config options - change outputImageStreamTag and outputImageStreamName', (t) => {
const nodeshiftConfig = proxyquire('../lib/nodeshift-config', {
'openshift-rest-client': {
config: {
fromKubeconfig: () => {
return {
namespace: 'test-namespace',
url: 'http://mock-cluster'
};
}
},
OpenshiftClient: () => {
return Promise.resolve();
}
}
});

const options = {
outputImageStreamName: 'funTimes',
outputImageStreamTag: 'notLatest'
};

nodeshiftConfig(options).then((config) => {
t.equal(config.outputImageStreamTag, options.outputImageStreamTag, 'should not be latest');
t.equal(config.outputImageStreamName, options.outputImageStreamName, 'should not be the project name');
t.end();
});
});

0 comments on commit 3faa599

Please sign in to comment.