Skip to content

Commit

Permalink
Add TaggedAddresses to service register (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
baranovpavel committed Oct 20, 2020
1 parent dd82e91 commit be0473f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ Options
* checks (Object[], optional): service checks (see `check` above)
* connect (Object, optional): specifies the [configuration](https://www.consul.io/api/agent/service.html#connect-structure) for Connect
* proxy (Object, optional): specifies the [configuration](https://www.consul.io/docs/connect/registration/service-registration.html) for a Connect proxy instance
* taggedAddresses (Object, optional): specifies a map of explicit LAN and WAN addresses for the service instance

Usage

Expand Down
28 changes: 28 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ function setIntervalContext(fn, ctx, timeout) {
ctx.once('cancel', cancel);
}

function _createTaggedAddress(src) {
var dst = {};

if (src.hasOwnProperty('address')) dst.Address = src.address;
if (src.hasOwnProperty('port')) dst.Port = src.port;

return dst;
}

function _createTaggedAddresses(src) {
var dst = {};

if (src.lan) {
dst.lan = _createTaggedAddress(normalizeKeys(src.lan));
}
if (src.wan) {
dst.wan = _createTaggedAddress(normalizeKeys(src.wan));
}

return dst;
}

/**
* Create node/server-level check object
* Corresponds to CheckType in Consul Agent Endpoint:
Expand Down Expand Up @@ -347,6 +369,12 @@ function _createService(src, isSidecar) {
dst.Proxy = _createServiceProxy(normalizeKeys(src.proxy));
}

if (src.taggedaddresses) {
dst.TaggedAddresses = _createTaggedAddresses(
normalizeKeys(src.taggedaddresses)
);
}

return dst;
}

Expand Down
42 changes: 42 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,48 @@ describe('utils', function() {
},
},
});

should(utils.createService({
taggedaddresses: {},
})).eql({
TaggedAddresses: {},
});

should(utils.createService({
taggedaddresses: {
lan: {},
wan: {},
},
})).eql({
TaggedAddresses: {
lan: {},
wan: {},
},
});

should(utils.createService({
taggedaddresses: {
lan: {
address: '127.0.0.1',
port: 8080,
},
wan: {
address: '10.0.0.1',
port: 80,
},
},
})).eql({
TaggedAddresses: {
lan: {
Address: '127.0.0.1',
Port: 8080,
},
wan: {
Address: '10.0.0.1',
Port: 80,
},
},
});
});

it('should not allow nested sidecars', function() {
Expand Down

0 comments on commit be0473f

Please sign in to comment.