diff --git a/index.js b/index.js index 12c6ac1..58fe8a2 100644 --- a/index.js +++ b/index.js @@ -167,6 +167,32 @@ class K8sVMExecutor extends Executor { this.preferredNodeSelectors = hoek.reach(options, 'kubernetes.preferredNodeSelectors'); } + /** + * Update build status message + * @method updateStatusMessage + * @param {Object} config build config of the job + * @param {String} config.apiUri screwdriver base api uri + * @param {Number} config.buildId build id + * @param {String} config.statusMessage build status message + * @param {String} config.token build temporal jwt token + * @return {Promise} + */ + updateStatusMessage(config) { + const { apiUri, buildId, statusMessage, token } = config; + const statusMessageOptions = { + json: true, + method: 'PUT', + uri: `${apiUri}/v4/builds/${buildId}`, + body: { statusMessage }, + headers: { Authorization: `Bearer ${token}` }, + strictSSL: false, + maxAttempts: MAXATTEMPTS, + retryDelay: RETRYDELAY + }; + + return this.breaker.runCommand(statusMessageOptions); + } + /** * Starts a k8s build * @method start @@ -277,6 +303,15 @@ class K8sVMExecutor extends Executor { `${JSON.stringify(resp.body.status, null, 2)}`); } + if (status === 'pending') { + return this.updateStatusMessage({ + apiUri: this.ecosystem.api, + buildId: config.buildId, + statusMessage: 'Waiting for resources to be available.', + token: config.token + }); + } + return null; }); } diff --git a/package.json b/package.json index 2639c33..a496659 100644 --- a/package.json +++ b/package.json @@ -43,21 +43,21 @@ "devDependencies": { "chai": "^3.5.0", "eslint": "^4.19.1", - "eslint-config-screwdriver": "^3.0.0", + "eslint-config-screwdriver": "^3.0.1", "jenkins-mocha": "^4.0.0", "mockery": "^2.0.0", "rewire": "^3.0.2", "sinon": "^5.0.10" }, "dependencies": { - "circuit-fuses": "^4.0.0", + "circuit-fuses": "^4.0.3", "eslint-plugin-import": "^2.14.0", "hoek": "^5.0.4", "js-yaml": "^3.12.0", "lodash": "^4.17.10", "randomstring": "^1.1.5", "requestretry": "^1.13.0", - "screwdriver-executor-base": "^6.2.0", + "screwdriver-executor-base": "^6.2.1", "tinytim": "^0.1.1" } } diff --git a/test/index.test.js b/test/index.test.js index b343550..fb58106 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -374,6 +374,7 @@ describe('index', () => { describe('start', () => { let postConfig; let getConfig; + let putConfig; let fakeStartConfig; const fakeStartResponse = { @@ -432,6 +433,21 @@ describe('index', () => { retryStrategy: executor.podRetryStrategy }; + putConfig = { + uri: `${testApiUri}/v4/builds/${testBuildId}`, + method: 'PUT', + headers: { + Authorization: `Bearer ${testToken}` + }, + body: { + statusMessage: 'Waiting for resources to be available.' + }, + strictSSL: false, + maxAttempts: MAXATTEMPTS, + retryDelay: RETRYDELAY, + json: true + }; + fakeStartConfig = { annotations: {}, buildId: testBuildId, @@ -665,6 +681,52 @@ describe('index', () => { }); }); + it('update build status message when pod status is pending', () => { + const returnResponse = { + statusCode: 200, + body: { + status: { + phase: 'pending' + } + } + }; + + const returnResponseFromSDAPI = { statusCode: 200 }; + + requestRetryMock.withArgs(getConfig).yieldsAsync( + null, returnResponse, returnResponse.body); + + requestRetryMock.withArgs(putConfig).yieldsAsync( + null, returnResponseFromSDAPI); + + return executor.start(fakeStartConfig).then((resp) => { + assert.calledWith(requestRetryMock, putConfig); + assert.deepEqual(resp.statusCode, 200); + }); + }); + + it('sets error when pod status is failed', () => { + const returnResponse = { + statusCode: 200, + body: { + status: { + phase: 'failed' + } + } + }; + const returnMessage = 'Failed to create pod. Pod status is:' + + `${JSON.stringify(returnResponse.body.status, null, 2)}`; + + requestRetryMock.withArgs(getConfig).yieldsAsync( + null, returnResponse, returnResponse.body); + + return executor.start(fakeStartConfig).then(() => { + throw new Error('did not fail'); + }, (err) => { + assert.equal(err.message, returnMessage); + }); + }); + it('returns body when request responds with error in response', () => { const returnResponse = { statusCode: 500,