Skip to content

Commit

Permalink
Merge pull request cravall#28 from cvallance/ocd-changes-to-pr-25
Browse files Browse the repository at this point in the history
Just a couple of optimizations and style changes to PR cravall#25.
  • Loading branch information
cravall authored Apr 3, 2017
2 parents 36b78a0 + e4061a2 commit 3b1eae7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
47 changes: 30 additions & 17 deletions src/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var dns = require('dns');

var getMongoPodLabels = function() {
return process.env.MONGO_SIDECAR_POD_LABELS || false;
};
Expand Down Expand Up @@ -31,25 +33,36 @@ var getK8sClusterDomain = function() {
var domain = process.env.KUBERNETES_CLUSTER_DOMAIN || "cluster.local";
verifyCorrectnessOfDomain(domain);
return domain;
};

/**
* Calls a reverse DNS lookup to ensure that the given custom domain name matches the actual one.
* Raises a console warning if that is not the case.
* @param clusterDomain the domain to verify.
*/
function verifyCorrectnessOfDomain(clusterDomain) {
var dns = require('dns');
if(clusterDomain && dns.getServers() && dns.getServers().length > 0) {
// In the case that we can resolve the DNS servers, we get the first and try to retrieve its host.
dns.reverse(dns.getServers()[0], function(err, host) {
if(err || host.length < 1 || !host[0].endsWith(clusterDomain)) {
console.warn("Possibly wrong cluster domain name! Detected '%s' but expected similar to: %s", clusterDomain, host);
} else {
console.log("The cluster domain '%s' was successfully verified.", clusterDomain)
}
});
}
/**
* Calls a reverse DNS lookup to ensure that the given custom domain name matches the actual one.
* Raises a console warning if that is not the case.
* @param clusterDomain the domain to verify.
*/
var verifyCorrectnessOfDomain = function(clusterDomain) {
if (!clusterDomain) {
return;
}

var servers = dns.getServers();
if (!servers || !servers.length) {
console.log("dns.getServers() didn't return any results when verifying the cluster domain '%s'.", clusterDomain);
return;
}

// In the case that we can resolve the DNS servers, we get the first and try to retrieve its host.
dns.reverse(servers[0], function(err, host) {
if (err) {
console.warn("Error occurred trying to verify the cluster domain '%s'", clusterDomain);
}
else if (host.length < 1 || !host[0].endsWith(clusterDomain)) {
console.warn("Possibly wrong cluster domain name! Detected '%s' but expected similar to '%s'", clusterDomain, host);
}
else {
console.log("The cluster domain '%s' was successfully verified.", clusterDomain);
}
});
};

/**
Expand Down
37 changes: 18 additions & 19 deletions src/lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ var primaryWork = function(db, pods, members, shouldForce, done) {
}

if (addrToAdd.length || addrToRemove.length) {
console.log('Addresses to add: ', addrToAdd);
console.log('Addresses to remove:', addrToRemove);
console.log('Addresses to add: ', addrToAdd);
console.log('Addresses to remove: ', addrToRemove);

mongo.addNewReplSetMembers(db, addrToAdd, addrToRemove, shouldForce, done);
return;
Expand Down Expand Up @@ -190,7 +190,7 @@ var notInReplicaSet = function(db, pods, done) {
var primary = pods[0]; // After the sort election, the 0-th pod should be the primary.
var primaryStableNetworkAddressAndPort = getPodStableNetworkAddressAndPort(primary);
// Prefer the stable network ID over the pod IP, if present.
var primaryAddressAndPort = primaryStableNetworkAddressAndPort ? primaryStableNetworkAddressAndPort : hostIpAndPort;
var primaryAddressAndPort = primaryStableNetworkAddressAndPort || hostIpAndPort;
mongo.initReplSet(db, primaryAddressAndPort, done);
return;
}
Expand Down Expand Up @@ -234,12 +234,12 @@ var addrToAddLoop = function(pods, members) {
}

var podIpAddr = getPodIpAddressAndPort(pod);
var podStableNetworkAdd = getPodStableNetworkAddressAndPort(pod);
var podStableNetworkAddr = getPodStableNetworkAddressAndPort(pod);
var podInRs = false;

for (var j in members) {
var member = members[j];
if ((podIpAddr && member.name === podIpAddr) || (podStableNetworkAdd && member.name === podStableNetworkAdd)) {
if (member.name === podIpAddr || member.name === podStableNetworkAddr) {
/* If we have the pod's ip or the stable network address already in the config, no need to read it. Checks both the pod IP and the
* stable network ID - we don't want any duplicates - either one of the two is sufficient to consider the node present. */
podInRs = true;
Expand All @@ -249,7 +249,7 @@ var addrToAddLoop = function(pods, members) {

if (!podInRs) {
// If the node was not present, we prefer the stable network ID, if present.
var addrToUse = podStableNetworkAdd ? podStableNetworkAdd : podIpAddr;
var addrToUse = podStableNetworkAddr || podIpAddr;
addrToAdd.push(addrToUse);
}
}
Expand All @@ -258,15 +258,15 @@ var addrToAddLoop = function(pods, members) {

/**
* @param pod this is the Kubernetes pod, containing the info.
* @returns podIp the pod's IP address with the default port of 27017 (retrieved from the config) attached at the end. Example
* @returns string - podIp the pod's IP address with the port from config attached at the end. Example
* WWW.XXX.YYY.ZZZ:27017. It returns undefined, if the data is insufficient to retrieve the IP address.
*/
var getPodIpAddressAndPort = function(pod) {
var podIpAddress = undefined;
if (pod && pod.status && pod.status.podIP) {
podIpAddress = pod.status.podIP + ":" + config.mongoPort;
if (!pod || !pod.status || !pod.status.podIP) {
return;
}
return podIpAddress;

return pod.status.podIP + ":" + config.mongoPort;
};

/**
Expand All @@ -275,17 +275,16 @@ var getPodIpAddressAndPort = function(pod) {
* <a href="https://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/#stable-network-id">Stateful Set documentation</a>
* for more details. If those are not set, then simply the pod's IP is returned.
* @param pod the Kubernetes pod, containing the information from the k8s client.
* @returns stableNetworkAddress the k8s MongoDB stable network address, or undefined.
* @returns string the k8s MongoDB stable network address, or undefined.
*/
var getPodStableNetworkAddressAndPort = function(pod) {
var podStableNetworkAddress = undefined;
if (config.k8sMongoServiceName && pod && pod.metadata && pod.metadata.name && pod.metadata.namespace) {
var clusterDomain = config.k8sClusterDomain;
var mongoPort = config.mongoPort;
podStableNetworkAddress = pod.metadata.name + "." + config.k8sMongoServiceName + "." + pod.metadata.namespace + ".svc." +
clusterDomain + ":" + mongoPort;
if (!config.k8sMongoServiceName || !pod || !pod.metadata || !pod.metadata.name || !pod.metadata.namespace) {
return;
}
return podStableNetworkAddress;

var clusterDomain = config.k8sClusterDomain;
var mongoPort = config.mongoPort;
return pod.metadata.name + "." + config.k8sMongoServiceName + "." + pod.metadata.namespace + ".svc." + clusterDomain + ":" + mongoPort;
};

module.exports = {
Expand Down

0 comments on commit 3b1eae7

Please sign in to comment.