Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding shouldforce logic to pick each sidecar individually #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var worker = require('./lib/worker');
var worker = require("./lib/worker");

console.log('Starting up mongo-k8s-sidecar');
console.log("Starting up mongo-k8s-sidecar");

worker.init(function(err) {
worker.init(function (err) {
if (err) {
console.error('Error trying to initialize mongo-k8s-sidecar', err);
console.error("Error trying to initialize mongo-k8s-sidecar", err);
return;
}

Expand Down
155 changes: 97 additions & 58 deletions src/lib/mongo.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
var Db = require('mongodb').Db;
var MongoServer = require('mongodb').Server;
var async = require('async');
var config = require('./config');
var Db = require("mongodb").Db;
var MongoServer = require("mongodb").Server;
var async = require("async");
var config = require("./config");

var localhost = '127.0.0.1'; //Can access mongo as localhost from a sidecar
var localhost = "127.0.0.1"; //Can access mongo as localhost from a sidecar

var getDb = function(host, done) {
var getDb = function (host, done) {
//If they called without host like getDb(function(err, db) { ... });
if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
if (typeof arguments[0] === "function") {
done = arguments[0];
host = localhost;
} else {
throw new Error('getDb illegal invocation. User either getDb(\'options\', function(err, db) { ... }) OR getDb(function(err, db) { ... })');
throw new Error(
"getDb illegal invocation. User either getDb('options', function(err, db) { ... }) OR getDb(function(err, db) { ... })"
);
}
}

Expand All @@ -23,33 +25,39 @@ var getDb = function(host, done) {
mongoOptions = {
ssl: config.mongoSSLEnabled,
sslAllowInvalidCertificates: config.mongoSSLAllowInvalidCertificates,
sslAllowInvalidHostnames: config.mongoSSLAllowInvalidHostnames
}
sslAllowInvalidHostnames: config.mongoSSLAllowInvalidHostnames,
};
}

var mongoDb = new Db(config.database, new MongoServer(host, config.mongoPort, mongoOptions));
var mongoDb = new Db(
config.database,
new MongoServer(host, config.mongoPort, mongoOptions)
);

mongoDb.open(function (err, db) {
if (err) {
return done(err);
}

if(config.username) {
mongoDb.authenticate(config.username, config.password, function(err, result) {
if (err) {
return done(err);
}
if (config.username) {
mongoDb.authenticate(
config.username,
config.password,
function (err, result) {
if (err) {
return done(err);
}

return done(null, db);
});
return done(null, db);
}
);
} else {
return done(null, db);
}

});
};

var replSetGetConfig = function(db, done) {
var replSetGetConfig = function (db, done) {
db.admin().command({ replSetGetConfig: 1 }, {}, function (err, results) {
if (err) {
return done(err);
Expand All @@ -59,7 +67,7 @@ var replSetGetConfig = function(db, done) {
});
};

var replSetGetStatus = function(db, done) {
var replSetGetStatus = function (db, done) {
db.admin().command({ replSetGetStatus: {} }, {}, function (err, results) {
if (err) {
return done(err);
Expand All @@ -69,65 +77,86 @@ var replSetGetStatus = function(db, done) {
});
};

var initReplSet = function(db, hostIpAndPort, done) {
console.log('initReplSet', hostIpAndPort);
var initReplSet = function (db, hostIpAndPort, done) {
console.log("initReplSet", hostIpAndPort);

db.admin().command({ replSetInitiate: {} }, {}, function (err) {
if (err) {
return done(err);
}

//We need to hack in the fix where the host is set to the hostname which isn't reachable from other hosts
replSetGetConfig(db, function(err, rsConfig) {
replSetGetConfig(db, function (err, rsConfig) {
if (err) {
return done(err);
}

console.log('initial rsConfig is', rsConfig);
console.log("initial rsConfig is", rsConfig);
rsConfig.configsvr = config.isConfigRS;
rsConfig.members[0].host = hostIpAndPort;
async.retry({times: 20, interval: 500}, function(callback) {
replSetReconfig(db, rsConfig, false, callback);
}, function(err, results) {
if (err) {
return done(err);
async.retry(
{ times: 20, interval: 500 },
function (callback) {
replSetReconfig(db, rsConfig, false, callback);
},
function (err, results) {
if (err) {
return done(err);
}

return done();
}

return done();
});
);
});
});
};

var replSetReconfig = function(db, rsConfig, force, done) {
console.log('replSetReconfig', rsConfig);
var replSetReconfig = function (db, rsConfig, force, done) {
console.log("replSetReconfig", rsConfig);

rsConfig.version++;

db.admin().command({ replSetReconfig: rsConfig, force: force }, {}, function (err) {
if (err) {
return done(err);
}
db.admin().command(
{ replSetReconfig: rsConfig, force: force },
{},
function (err) {
if (err) {
return done(err);
}

return done();
});
return done();
}
);
};

var addNewReplSetMembers = function(db, addrToAdd, addrToRemove, shouldForce, done) {
replSetGetConfig(db, function(err, rsConfig) {
var addNewReplSetMembers = function (
db,
addrToAdd,
addrToRemove,
shouldForce,
done
) {
replSetGetConfig(db, function (err, rsConfig) {
if (err) {
return done(err);
}

removeDeadMembers(rsConfig, addrToRemove);

addNewMembers(rsConfig, addrToAdd);

replSetReconfig(db, rsConfig, shouldForce, done);
console.log("🚀 ~ file: mongo.js:152 ~ shouldForce:", shouldForce);
if (!!shouldForce) {
addNewMembers(rsConfig, addrToAdd);
replSetReconfig(db, rsConfig, shouldForce, done);
} else {
addNewMembers(rsConfig, addrToAdd, {
shouldApplyReplicaSetReconfig: true,
db,
shouldForce,
done,
});
}
});
};

var addNewMembers = function(rsConfig, addrsToAdd) {
var addNewMembers = function (rsConfig, addrsToAdd, options) {
if (!addrsToAdd || !addrsToAdd.length) return;

var memberIds = [];
Expand Down Expand Up @@ -156,7 +185,10 @@ var addNewMembers = function(rsConfig, addrsToAdd) {
for (var j in rsConfig.members) {
var member = rsConfig.members[j];
if (member.host === addrToAdd) {
console.log("Host [%s] already exists in the Replicaset. Not adding...", addrToAdd);
console.log(
"Host [%s] already exists in the Replicaset. Not adding...",
addrToAdd
);
exists = true;
break;
}
Expand All @@ -168,14 +200,22 @@ var addNewMembers = function(rsConfig, addrsToAdd) {

var cfg = {
_id: newMemberId,
host: addrToAdd
host: addrToAdd,
};

rsConfig.members.push(cfg);
if (!!options) {
replSetReconfig(
options?.db,
rsConfig,
options?.shouldForce,
options.done
);
}
}
};

var removeDeadMembers = function(rsConfig, addrsToRemove) {
var removeDeadMembers = function (rsConfig, addrsToRemove) {
if (!addrsToRemove || !addrsToRemove.length) return;

for (var i in addrsToRemove) {
Expand All @@ -190,18 +230,17 @@ var removeDeadMembers = function(rsConfig, addrsToRemove) {
}
};

var isInReplSet = function(ip, done) {
getDb(ip, function(err, db) {
var isInReplSet = function (ip, done) {
getDb(ip, function (err, db) {
if (err) {
return done(err);
}

replSetGetConfig(db, function(err, rsConfig) {
replSetGetConfig(db, function (err, rsConfig) {
db.close();
if (!err && rsConfig) {
done(null, true);
}
else {
} else {
done(null, false);
}
});
Expand All @@ -213,5 +252,5 @@ module.exports = {
replSetGetStatus: replSetGetStatus,
initReplSet: initReplSet,
addNewReplSetMembers: addNewReplSetMembers,
isInReplSet: isInReplSet
isInReplSet: isInReplSet,
};
Loading