diff --git a/index.js b/index.js index a644e62..407d722 100644 --- a/index.js +++ b/index.js @@ -728,11 +728,16 @@ class K8sExecutor extends Executor { let message; let waitingReason; + let status; - pods.find(p => { - const status = hoek.reach(p, 'status.phase').toLowerCase(); + const data = pods.find(p => { + status = hoek.reach(p, 'status.phase').toLowerCase(); - waitingReason = hoek.reach(p, CONTAINER_WAITING_REASON_PATH); + if (status === 'running' || status === 'succeeded') { + message = `Successfully created pod. Pod status is: ${status}`; + + return true; + } if (status === 'failed' || status === 'unknown') { message = `Failed to create pod. Pod status is: ${status}`; @@ -740,6 +745,15 @@ class K8sExecutor extends Executor { return true; } + // waitingReason is undefined when pod is running or succeeded + waitingReason = hoek.reach(p, CONTAINER_WAITING_REASON_PATH); + + if (waitingReason === undefined) { + message = 'Build is no longer waiting.'; + + return true; + } + if ( ['CrashLoopBackOff', 'CreateContainerConfigError', 'CreateContainerError', 'StartError'].includes( waitingReason @@ -763,13 +777,19 @@ class K8sExecutor extends Executor { return message !== undefined; }); - logger.info(`BuildId:${buildId}, status:${message}`); + logger.info(`BuildId:${buildId}, status:${status}, waitingReason:${waitingReason}, message:${message}`); + // data is not undefined when desired status or waitingReason met + // hence no error needs to be thrown + if (data !== undefined) { + return message; + } + + // when condition above not met, throw error if (waitingReason === 'PodInitializing') { throw new Error('Build failed to start. Pod is still intializing.'); } - - return message; + throw new Error(`Build failed to start. Status: ${status}. Reason: ${waitingReason}.`); } /** diff --git a/test/index.test.js b/test/index.test.js index f8a8e40..3c37629 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1180,7 +1180,7 @@ describe('index', function () { items: [ { status: { - phase: 'pending' + phase: 'Running' }, spec: { nodeName: 'node1.my.k8s.cluster.com' @@ -1217,7 +1217,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please reach out to your cluster admin for help.'; @@ -1245,7 +1245,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please reach out to your cluster admin for help.'; @@ -1273,7 +1273,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please reach out to your cluster admin for help.'; @@ -1301,7 +1301,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please check if your image is valid.'; @@ -1329,7 +1329,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please check if your image is valid.'; @@ -1357,7 +1357,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please check if your image is valid.'; @@ -1385,7 +1385,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Build failed to start. Please reach out to your cluster admin for help.'; @@ -1412,7 +1412,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Failed to create pod. Pod status is: failed'; @@ -1430,7 +1430,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); const expectedMessage = 'Failed to create pod. Pod status is: failed'; requestRetryMock.withArgs(getPodsConfig).resolves(fakeGetPodsResponse); @@ -1448,7 +1448,7 @@ describe('index', function () { { state: { waiting: { - reason: 'PodIntializing', + reason: 'PodInitializing', message: 'pod is initializing' } } @@ -1458,13 +1458,13 @@ describe('index', function () { }; const expectedMessage = 'Build failed to start. Pod is still intializing.'; - fakeGetPodsResponse.body.items.push(pod); + fakeGetPodsResponse.body.items.unshift(pod); requestRetryMock.withArgs(getPodsConfig).resolves(fakeGetPodsResponse); try { await executor.verify(fakeVerifyConfig); } catch (error) { - assert.equal(expectedMessage, error); + assert.equal(expectedMessage, error.message); } }); @@ -1488,7 +1488,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod1); + fakeGetPodsResponse.body.items.unshift(pod1); const pod2 = { status: { phase: 'pending', @@ -1508,7 +1508,7 @@ describe('index', function () { } }; - fakeGetPodsResponse.body.items.push(pod2); + fakeGetPodsResponse.body.items.unshift(pod2); const expectedMessage = 'Build failed to start. Please check if your image is valid.'; @@ -1522,5 +1522,63 @@ describe('index', function () { throw new Error('should not fail'); } }); + + it('no error when waitingReason is undefined', async () => { + const pod = { + status: { + phase: 'pending', + containerStatuses: [], + metadata: { + name: 'beta_15-dsvds' + } + } + }; + + fakeGetPodsResponse.body.items.unshift(pod); + + const expectedMessage = 'Build is no longer waiting.'; + + requestRetryMock.withArgs(getPodsConfig).resolves(fakeGetPodsResponse); + + try { + const actualMessage = await executor.verify(fakeVerifyConfig); + + assert.equal(expectedMessage, actualMessage); + } catch (error) { + throw new Error('should not fail'); + } + }); + + it('error whenever unknown waitingReason is present', async () => { + const pod = { + status: { + phase: 'pending', + containerStatuses: [ + { + state: { + waiting: { + reason: 'RandomReason', + message: 'some random message' + } + } + } + ], + metadata: { + name: 'beta_15-dsvds' + } + } + }; + + fakeGetPodsResponse.body.items[0] = pod; + requestRetryMock.withArgs(getPodsConfig).resolves(fakeGetPodsResponse); + const expectedMessage = 'Build failed to start. Status: pending. Reason: RandomReason.'; + + try { + await executor.verify(fakeVerifyConfig); + throw new Error('did not fail'); + } catch (error) { + assert.equal(expectedMessage, error.message); + } + }); }); });