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

Add support for configuring pods as arbiterOnly based on labels #42

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 16 additions & 3 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ var getMongoPodLabels = function() {
return process.env.MONGO_SIDECAR_POD_LABELS || false;
};

var getMongoPodLabelsArbiter = function() {
return process.env.MONGO_SIDECAR_POD_LABELS_ARBITER || false;
};

var getMongoPodLabelCollection = function() {
var podLabels = getMongoPodLabels();
if (!podLabels) {
return parseLabelCollection(getMongoPodLabels());
};

var getMongoPodLabelCollectionArbiter = function() {
return parseLabelCollection(getMongoPodLabelsArbiter());
};

var parseLabelCollection = function(labelString) {
if (!labelString) {
return false;
}
var labels = process.env.MONGO_SIDECAR_POD_LABELS.split(',');
var labels = labelString.split(',');
for (var i in labels) {
var keyAndValue = labels[i].split('=');
labels[i] = {
Expand Down Expand Up @@ -103,6 +114,8 @@ module.exports = {
unhealthySeconds: process.env.MONGO_SIDECAR_UNHEALTHY_SECONDS || 15,
env: process.env.NODE_ENV || 'local',
mongoPodLabels: getMongoPodLabels(),
mongoPodLabelsArbiter: getMongoPodLabelsArbiter(),
mongoPodLabelCollectionArbiter: getMongoPodLabelCollectionArbiter(),
mongoPodLabelCollection: getMongoPodLabelCollection(),
k8sROServiceAddress: getk8sROServiceAddress(),
k8sMongoServiceName: getK8sMongoServiceName(),
Expand Down
3 changes: 2 additions & 1 deletion src/lib/k8s.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ var podContainsLabels = function podContainsLabels(pod, labels) {
};

module.exports = {
getMongoPods: getMongoPods
getMongoPods: getMongoPods,
podContainsLabels: podContainsLabels
};
8 changes: 7 additions & 1 deletion src/lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Db = require('mongodb').Db;
var MongoServer = require('mongodb').Server;
var async = require('async');
var config = require('./config');
var k8s = require('./k8s');

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

Expand Down Expand Up @@ -132,9 +133,14 @@ var addNewMembers = function(rsConfig, addrsToAdd) {
for (var i in addrsToAdd) {
var cfg = {
_id: ++max,
host: addrsToAdd[i]
host: addrsToAdd[i].host
};

// check if we want to add the pod as an arbiter only based on its labels
if (k8s.podContainsLabels(addrsToAdd[i].pod, config.mongoPodLabelCollectionArbiter)) {
cfg.arbiterOnly = true;
}

rsConfig.members.push(cfg);
}
};
Expand Down
7 changes: 5 additions & 2 deletions src/lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ var invalidReplicaSet = function(db, pods, done) {
var podElection = function(pods) {
//Because all the pods are going to be running this code independently, we need a way to consistently find the same
//node to kick things off, the easiest way to do that is convert their ips into longs and find the highest
pods.sort(function(a,b) {
pods
.filter(function(pod) { return !k8s.podContainsLabels(pod, config.mongoPodLabelCollectionArbiter); })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function is currently not working as intended. The caller expects the pods array it passes into the function to be sorted in-place, which the filter prohibits by creating a copy. I need to fix that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you got.

pods = pods.filter(function(pod) { return !k8s.podContainsLabels(pod, config.mongoPodLabelCollectionArbiter); });
//Now you can sort and carry on as you were before.

.sort(function(a,b) {
var aIpVal = ip.toLong(a.status.podIP);
var bIpVal = ip.toLong(b.status.podIP);
if (aIpVal < bIpVal) return -1;
Expand Down Expand Up @@ -250,7 +252,8 @@ var addrToAddLoop = function(pods, members) {
if (!podInRs) {
// If the node was not present, we prefer the stable network ID, if present.
var addrToUse = podStableNetworkAddr || podIpAddr;
addrToAdd.push(addrToUse);
var obj = { host: addrToUse, pod: pod };
addrToAdd.push(obj);
}
}
return addrToAdd;
Expand Down