Skip to content

Commit

Permalink
Ensure w-k can poll/sync with correct api URL (#432)
Browse files Browse the repository at this point in the history
* Ensure w-k can poll/sync with correct api URL

* additional logging of sent resources

* improve logging of RemoteResources

* tweak variable naming
  • Loading branch information
carrolp authored Jan 3, 2023
1 parent e65c416 commit a7ec8be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/razeedash/DelayedSendArray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
* Copyright 2019, 2022 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,8 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var validUrl = require('valid-url');
var requestretry = require('requestretry');
const objectPath = require('object-path');
const validUrl = require('valid-url');
const requestretry = require('requestretry');
const HttpAgent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent;
const log = require('../bunyan-api').createLogger('DelayedSendArray');
Expand Down Expand Up @@ -68,7 +69,7 @@ module.exports = class DelayedSendArray {
if (o.constructor === {}.constructor) {
this.sendObject.push(o);
} else {
let type = typeof o;
const type = typeof o;
throw `Type ${type} not supported.`;
}
}
Expand All @@ -94,18 +95,24 @@ module.exports = class DelayedSendArray {
clearTimeout(this.flushTimeout);
this.flushTimeout = undefined;
if (this.sendObject.length > 0) {
let outBoundArray = this.sendObject;
const outBoundArray = this.sendObject;
this.sendObject = [];
let httpMethod = 'POST';
let res = this.httpCall(httpMethod, outBoundArray);
const httpMethod = 'POST';
const res = this.httpCall(httpMethod, outBoundArray);
if (this._trackSendPromises) {
this._sendPromises.push(res);
}
}
}

async httpCall(httpMethod, data, options = {}) {
let url = `${this.url}/clusters/${this._clusterID}/${options.endpoint || 'resources'}`;
const url = `${this.url}/clusters/${this._clusterID}/${options.endpoint || 'resources'}`;

// Log how many resources are being sent, and list any RemoteResources by selfLink
const dArr = Array.isArray(data) ? data : [data];
const remoteResourceSelfLinks = dArr.filter( d => objectPath.get( d, 'object.kind', '' ) == 'RemoteResource' ).map( d => objectPath.get( d, 'object.metadata.annotations.selfLink', 'no-selfLink' ) );
log.info(`${httpMethod} ${dArr.length} resource(s) to ${url} starting. RemoteResource(s): ${remoteResourceSelfLinks.join( ', ' )}`);

return requestretry({
url: url,
method: httpMethod,
Expand All @@ -122,11 +129,10 @@ module.exports = class DelayedSendArray {
retryStrategy: options.retryStrategy || requestretry.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}).then(function (response) {
if (response.statusCode >= 200 && response.statusCode < 300) {
let numSent = Array.isArray(data) ? data.length : 1;
log.info(`${httpMethod} ${numSent} resource(s) to ${url} successful. StatusCode: ${response.statusCode}`);
log.info(`${httpMethod} ${dArr.length} resource(s) to ${url} successful. StatusCode: ${response.statusCode}`);
return response;
} else {
log.error(`${httpMethod} to ${url} failed: ${response.statusCode}`);
log.error(`${httpMethod} ${dArr.length} resource(s) to ${url} failed. StatusCode: ${response.statusCode}`);
return response;
}
}).catch(err => {
Expand Down
3 changes: 2 additions & 1 deletion src/razeedash/Sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ var objectPath = require('object-path');

const DelayedSendArray = require('./DelayedSendArray');
const log = require('../bunyan-api').createLogger('Sender');
const Config = require('../Config');

module.exports = class RazeedashSender {

constructor(clusterID) {
this._dsa = new DelayedSendArray(process.env.RAZEEDASH_URL || 'http://localhost:3000/api/v2', clusterID, undefined, true);
this._dsa = new DelayedSendArray(Config.razeedashUrl || 'http://localhost:3000/api/v2', clusterID, undefined, true);
this._sentSelflinks = {};
}

Expand Down

0 comments on commit a7ec8be

Please sign in to comment.