Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
fix(1678): add configurable retryDelay and maxAttempts (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
tk3fftk authored and tkyi committed Jun 18, 2019
1 parent e691cff commit bd6ffd4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 73 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const _ = require('lodash');

const DEFAULT_BUILD_TIMEOUT = 90; // 90 minutes
const MAX_BUILD_TIMEOUT = 120; // 120 minutes
const MAXATTEMPTS = 5;
const RETRYDELAY = 3000;
const DEFAULT_MAXATTEMPTS = 5;
const DEFAULT_RETRYDELAY = 3000;
const CPU_RESOURCE = 'cpu';
const RAM_RESOURCE = 'ram';
const DISK_RESOURCE = 'disk';
Expand Down Expand Up @@ -133,12 +133,16 @@ class K8sVMExecutor extends Executor {
* @param {String} [options.launchVersion=stable] Launcher container version to use
* @param {String} [options.prefix=''] Prefix for job name
* @param {String} [options.fusebox] Options for the circuit breaker (https://github.com/screwdriver-cd/circuit-fuses)
* @param {Object} [options.requestretry] Options for the requestretry (https://github.com/FGRibreau/node-request-retry)
* @param {Number} [options.requestretry.retryDelay] Value for retryDelay option of the requestretry
* @param {Number} [options.requestretry.maxAttempts] Value for maxAttempts option of the requestretry
*/
constructor(options = {}) {
super();

this.kubernetes = options.kubernetes || {};
this.ecosystem = options.ecosystem;
this.requestretryOptions = options.requestretry || {};

if (this.kubernetes.token) {
this.token = this.kubernetes.token;
Expand All @@ -157,6 +161,8 @@ class K8sVMExecutor extends Executor {
this.maxBuildTimeout = this.kubernetes.maxBuildTimeout || MAX_BUILD_TIMEOUT;
this.podsUrl = `https://${this.host}/api/v1/namespaces/${this.jobsNamespace}/pods`;
this.breaker = new Fusebox(requestretry, options.fusebox);
this.retryDelay = this.requestretryOptions.retryDelay || DEFAULT_RETRYDELAY;
this.maxAttempts = this.requestretryOptions.maxAttempts || DEFAULT_MAXATTEMPTS;
this.maxCpu = hoek.reach(options, 'kubernetes.resources.cpu.max', { default: 12 });
this.turboCpu = hoek.reach(options, 'kubernetes.resources.cpu.turbo', { default: 12 });
this.highCpu = hoek.reach(options, 'kubernetes.resources.cpu.high', { default: 6 });
Expand Down Expand Up @@ -207,8 +213,8 @@ class K8sVMExecutor extends Executor {
uri: `${apiUri}/v4/builds/${buildId}`,
headers: { Authorization: `Bearer ${token}` },
strictSSL: false,
maxAttempts: MAXATTEMPTS,
retryDelay: RETRYDELAY,
maxAttempts: this.maxAttempts,
retryDelay: this.retryDelay,
body: {}
};

Expand Down Expand Up @@ -345,8 +351,8 @@ class K8sVMExecutor extends Executor {
method: 'GET',
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
maxAttempts: MAXATTEMPTS,
retryDelay: RETRYDELAY,
maxAttempts: this.maxAttempts,
retryDelay: this.retryDelay,
retryStrategy: this.podRetryStrategy,
json: true
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"js-yaml": "^3.12.2",
"lodash": "^4.17.11",
"randomstring": "^1.1.5",
"requestretry": "^1.13.0",
"requestretry": "^4.0.0",
"screwdriver-data-schema": "^18.43.6",
"screwdriver-executor-base": "^7.0.0",
"tinytim": "^0.1.1"
Expand Down
117 changes: 51 additions & 66 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ describe('index', () => {
}
}
};
const executorOptions = {
ecosystem: {
api: testApiUri,
store: testStoreUri
},
kubernetes: {
nodeSelectors: {},
preferredNodeSelectors: {}
},
fusebox: { retry: { minTimeout: 1 } },
prefix: 'beta_'
};

before(() => {
mockery.enable({
Expand Down Expand Up @@ -214,18 +226,7 @@ describe('index', () => {
Executor = require('../index');
/* eslint-enable global-require */

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
kubernetes: {
nodeSelectors: {},
preferredNodeSelectors: {}
},
fusebox: { retry: { minTimeout: 1 } },
prefix: 'beta_'
});
executor = new Executor(executorOptions);
});

afterEach(() => {
Expand Down Expand Up @@ -627,24 +628,17 @@ describe('index', () => {

it('sets tolerations and node affinity with appropriate node config', () => {
const spec = _.merge({}, testSpec, testPodSpec);

postConfig.body.spec = spec;

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
fusebox: { retry: { minTimeout: 1 } },
const options = _.assign({}, executorOptions, {
kubernetes: {
nodeSelectors: { key: 'value' },
token: 'api_key',
host: 'kubernetes.default',
baseImage: 'hyperctl'
},
prefix: 'beta_'
}
});

executor = new Executor(options);
postConfig.body.spec = spec;
getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
Expand All @@ -656,15 +650,7 @@ describe('index', () => {
it('sets disk toleration and node affinity when disk is HIGH', () => {
fakeStartConfig.annotations = { 'beta.screwdriver.cd/disk': 'high' };
const spec = _.merge({}, testSpecWithDisk, testPodSpec);

postConfig.body.spec = spec;

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
fusebox: { retry: { minTimeout: 1 } },
const options = _.assign({}, executorOptions, {
kubernetes: {
nodeSelectors: { key: 'value' },
token: 'api_key',
Expand All @@ -675,10 +661,11 @@ describe('index', () => {
high: 'screwdriver.cd/disk'
}
}
},
prefix: 'beta_'
}
});

executor = new Executor(options);
postConfig.body.spec = spec;
getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
Expand All @@ -688,17 +675,8 @@ describe('index', () => {
});

it('sets disk speed toleration and node affinity when diskSpeed is HIGH', () => {
fakeStartConfig.annotations = { 'screwdriver.cd/diskSpeed': 'high' };
const spec = _.merge({}, testSpecWithDiskSpeed, testPodSpec);

postConfig.body.spec = spec;

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
fusebox: { retry: { minTimeout: 1 } },
const options = _.assign({}, executorOptions, {
kubernetes: {
nodeSelectors: { key: 'value' },
token: 'api_key',
Expand All @@ -709,10 +687,12 @@ describe('index', () => {
speed: 'screwdriver.cd/diskspeed'
}
}
},
prefix: 'beta_'
}
});

executor = new Executor(options);
postConfig.body.spec = spec;
fakeStartConfig.annotations = { 'screwdriver.cd/diskSpeed': 'high' };
getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
Expand All @@ -723,21 +703,14 @@ describe('index', () => {

it('sets preferred node affinity with appropriate node config', () => {
const spec = _.merge({}, testPreferredSpec, testPodSpec);

postConfig.body.spec = spec;

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
fusebox: { retry: { minTimeout: 1 } },
prefix: 'beta_',
const options = _.assign({}, executorOptions, {
kubernetes: {
preferredNodeSelectors: { key: 'value', foo: 'bar' }
}
});

executor = new Executor(options);
postConfig.body.spec = spec;
getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
Expand All @@ -748,22 +721,15 @@ describe('index', () => {

it('sets node affinity and preferred node affinity', () => {
const spec = _.merge({}, testSpec, testPreferredSpec, testPodSpec);

postConfig.body.spec = spec;

executor = new Executor({
ecosystem: {
api: testApiUri,
store: testStoreUri
},
fusebox: { retry: { minTimeout: 1 } },
prefix: 'beta_',
const options = _.assign({}, executorOptions, {
kubernetes: {
nodeSelectors: { key: 'value' },
preferredNodeSelectors: { key: 'value', foo: 'bar' }
}
});

executor = new Executor(options);
postConfig.body.spec = spec;
getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
Expand Down Expand Up @@ -938,6 +904,25 @@ describe('index', () => {
assert.calledWith(requestRetryMock.secondCall, sinon.match(getConfig));
});
});

it('sets retryDelay and maxAttempts', () => {
const options = _.assign({}, executorOptions, {
requestretry: {
retryDelay: 1000,
maxAttempts: 1
}
});

executor = new Executor(options);
getConfig.retryDelay = 1000;
getConfig.maxAttempts = 1;
getConfig.retryStrategy = executor.podRetryStrategy;

return executor.start(fakeStartConfig).then(() => {
assert.calledWith(requestRetryMock.firstCall, postConfig);
assert.calledWith(requestRetryMock.secondCall, sinon.match(getConfig));
});
});
});

describe('setNodeSelector', () => {
Expand Down

0 comments on commit bd6ffd4

Please sign in to comment.