Skip to content

Commit

Permalink
feat(1961): Use screwdriver-request package (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Aug 20, 2021
1 parent ef7f29c commit fb29974
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 122 deletions.
84 changes: 50 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs');
const hoek = require('@hapi/hoek');
const path = require('path');
const randomstring = require('randomstring');
const requestretry = require('requestretry');
const request = require('screwdriver-request');
const handlebars = require('handlebars');
const yaml = require('js-yaml');
const _ = require('lodash');
Expand Down Expand Up @@ -246,7 +246,7 @@ class K8sExecutor extends Executor {
this.automountServiceAccountToken = this.kubernetes.automountServiceAccountToken === 'true' || false;
this.terminationGracePeriodSeconds = this.kubernetes.terminationGracePeriodSeconds || 30;
this.podsUrl = `https://${this.host}/api/v1/namespaces/${this.jobsNamespace}/pods`;
this.breaker = new Fusebox(requestretry, options.fusebox);
this.breaker = new Fusebox(request, 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 });
Expand Down Expand Up @@ -276,8 +276,8 @@ class K8sExecutor extends Executor {
this.annotations = hoek.reach(options, 'kubernetes.annotations');
this.privileged = hoek.reach(options, 'kubernetes.privileged', { default: false });
this.secrets = hoek.reach(options, 'kubernetes.buildSecrets', { default: {} });
this.scheduleStatusRetryStrategy = (err, response, body) => {
const conditions = hoek.reach(body, 'status.conditions');
this.scheduleStatusRetryStrategy = (response, retryWithMergedOptions) => {
const conditions = hoek.reach(response, 'body.status.conditions');
let scheduled = false;

if (conditions) {
Expand All @@ -286,12 +286,20 @@ class K8sExecutor extends Executor {
scheduled = String(scheduledStatus) === 'True';
}

return err || !scheduled;
if (!scheduled) {
retryWithMergedOptions({});
}

return response;
};
this.pendingStatusRetryStrategy = (err, response, body) => {
const status = hoek.reach(body, 'status.phase');
this.pendingStatusRetryStrategy = (response, retryWithMergedOptions) => {
const status = hoek.reach(response, 'body.status.phase');

return err || !status || status.toLowerCase() === 'pending';
if (!status || status.toLowerCase() === 'pending') {
retryWithMergedOptions({});
}

return response;
};
}

Expand All @@ -309,22 +317,23 @@ class K8sExecutor extends Executor {
updateBuild(config) {
const { apiUri, buildId, statusMessage, token, stats } = config;
const options = {
json: true,
method: 'PUT',
uri: `${apiUri}/v4/builds/${buildId}`,
url: `${apiUri}/v4/builds/${buildId}`,
headers: { Authorization: `Bearer ${token}` },
strictSSL: false,
maxAttempts: this.maxAttempts,
retryDelay: this.retryDelay,
body: {}
https: { rejectUnauthorized: false },
retry: {
limit: this.maxAttempts,
calculateDelay: ({ computedValue }) => (computedValue ? this.retryDelay : 0)
},
json: {}
};

if (statusMessage) {
options.body.statusMessage = statusMessage;
options.json.statusMessage = statusMessage;
}

if (stats) {
options.body.stats = stats;
options.json.stats = stats;
}

return this.breaker.runCommand(options);
Expand All @@ -347,13 +356,13 @@ class K8sExecutor extends Executor {
const { buildId, token } = config;
const podConfig = this.createPodConfig(config);
const options = {
uri: this.podsUrl,
url: this.podsUrl,
method: 'POST',
json: podConfig,
headers: {
Authorization: `Bearer ${this.token}`
},
strictSSL: false
https: { rejectUnauthorized: false }
};

try {
Expand Down Expand Up @@ -398,15 +407,19 @@ class K8sExecutor extends Executor {
logger.info(`Get pod status for ${podName} and buildId: ${buildId}`);

const statusOptions = {
uri: `${this.podsUrl}/${podName}/status`,
url: `${this.podsUrl}/${podName}/status`,
method: 'GET',
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
maxAttempts: this.maxAttempts,
retryDelay: this.retryDelay,
retryStrategy: this.scheduleStatusRetryStrategy,
json: true
https: { rejectUnauthorized: false },
retry: {
limit: this.maxAttempts,
calculateDelay: ({ computedValue }) => (computedValue ? this.retryDelay : 0)
},
hooks: {
afterResponse: [this.scheduleStatusRetryStrategy]
}
};

const resp = await this.breaker.runCommand(statusOptions);

logger.debug(`Build ${buildId} pod response: ${JSON.stringify(resp.body)}`);
Expand Down Expand Up @@ -603,15 +616,15 @@ class K8sExecutor extends Executor {
*/
async _stop(config) {
const options = {
uri: this.podsUrl,
url: this.podsUrl,
method: 'DELETE',
qs: {
searchParams: {
labelSelector: `sdbuild=${this.prefix}${config.buildId}`
},
headers: {
Authorization: `Bearer ${this.token}`
},
strictSSL: false
https: { rejectUnauthorized: false }
};

try {
Expand Down Expand Up @@ -698,15 +711,18 @@ class K8sExecutor extends Executor {
logger.info(`Get pod status for and buildId: ${buildId}`);

const statusOptions = {
uri: this.podsUrl,
url: this.podsUrl,
method: 'GET',
headers: { Authorization: `Bearer ${this.token}` },
strictSSL: false,
maxAttempts: this.maxAttempts,
retryDelay: this.retryDelay,
retryStrategy: this.pendingStatusRetryStrategy,
json: true,
qs: {
https: { rejectUnauthorized: false },
retry: {
limit: this.maxAttempts,
calculateDelay: ({ computedValue }) => (computedValue ? this.retryDelay : 0)
},
hooks: {
afterResponse: [this.pendingStatusRetryStrategy]
},
searchParams: {
labelSelector: `sdbuild=${this.prefix}${buildId}`
}
};
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
},
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^7.7.0",
"eslint-config-screwdriver": "^5.0.4",
"mocha": "^8.2.1",
"eslint": "^7.32.0",
"eslint-config-screwdriver": "^5.0.7",
"mocha": "^8.4.0",
"mocha-multi-reporters": "^1.5.1",
"mocha-sonarqube-reporter": "^1.0.2",
"mockery": "^2.0.0",
Expand All @@ -51,18 +51,19 @@
"sinon": "^5.0.10"
},
"dependencies": {
"@hapi/hoek": "^9.0.4",
"circuit-fuses": "^4.0.3",
"handlebars": "^4.7.6",
"js-yaml": "^3.11.0",
"@hapi/hoek": "^9.2.0",
"circuit-fuses": "^4.1.0",
"handlebars": "^4.7.7",
"js-yaml": "^3.14.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.20",
"lodash": "^4.17.21",
"node-gyp": "^7.1.2",
"randomstring": "^1.1.5",
"randomstring": "^1.2.1",
"request": "^2.88.0",
"requestretry": "^4.0.0",
"screwdriver-executor-base": "^8.0.0",
"screwdriver-logger": "^1.0.1",
"screwdriver-executor-base": "^8.4.0",
"screwdriver-logger": "^1.1.0",
"screwdriver-request": "^1.0.2",
"tinytim": "^0.1.1"
}
}
Loading

0 comments on commit fb29974

Please sign in to comment.