Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cluster): add an option to resolve mdns hostnames with dig
libnss-mdns is complicated and does not work on Alpine, which means the auto discovery could never resolve the hosts on such systems. This adds an option to resolve the hostname using dig instead, which is especially great for Docker images to use.
- Loading branch information
Showing
4 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const mockExec = jest.fn(); | ||
|
||
import { getAddrInfoDig } from './resolvers'; | ||
import { Service } from 'mdns'; | ||
|
||
jest.mock('util', () => ({ | ||
...jest.requireActual('util'), | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
promisify: () => mockExec | ||
})); | ||
|
||
describe('resolvers', () => { | ||
beforeEach(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return a single address correctly', done => { | ||
const service: Service = { | ||
addresses: undefined, | ||
flags: undefined, | ||
fullname: undefined, | ||
host: undefined, | ||
interfaceIndex: undefined, | ||
networkInterface: undefined, | ||
port: undefined, | ||
replyDomain: undefined, | ||
type: undefined | ||
}; | ||
mockExec.mockResolvedValue({ stdout: '192.168.1.20\n' }); | ||
|
||
getAddrInfoDig(service, () => { | ||
try { | ||
expect(service.addresses).toStrictEqual(['192.168.1.20']); | ||
done(); | ||
} catch (e) { | ||
done(e); | ||
} | ||
}); | ||
}); | ||
|
||
it('should resolve multiple addresses correctly', done => { | ||
const service: Service = { | ||
addresses: undefined, | ||
flags: undefined, | ||
fullname: undefined, | ||
host: undefined, | ||
interfaceIndex: undefined, | ||
networkInterface: undefined, | ||
port: undefined, | ||
replyDomain: undefined, | ||
type: undefined | ||
}; | ||
mockExec.mockResolvedValue({ stdout: '192.168.1.20\n192.168.1.21\n' }); | ||
|
||
getAddrInfoDig(service, () => { | ||
try { | ||
expect(service.addresses).toStrictEqual([ | ||
'192.168.1.20', | ||
'192.168.1.21' | ||
]); | ||
done(); | ||
} catch (e) { | ||
done(e); | ||
} | ||
}); | ||
}); | ||
|
||
it('should pass errors to the callback', done => { | ||
const service: Service = { | ||
addresses: undefined, | ||
flags: undefined, | ||
fullname: undefined, | ||
host: undefined, | ||
interfaceIndex: undefined, | ||
networkInterface: undefined, | ||
port: undefined, | ||
replyDomain: undefined, | ||
type: undefined | ||
}; | ||
mockExec.mockRejectedValue({ stdout: 'dig not found' }); | ||
|
||
getAddrInfoDig(service, e => { | ||
try { | ||
expect(e).not.toBeUndefined(); | ||
done(); | ||
} catch (e) { | ||
done(e); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Service } from 'mdns'; | ||
import * as util from 'util'; | ||
import { exec } from 'child_process'; | ||
import * as os from 'os'; | ||
|
||
const execPromise = util.promisify(exec); | ||
|
||
export async function getAddrInfoDig( | ||
service: Service, | ||
next: (e?: Error) => void | ||
): Promise<void> { | ||
try { | ||
const digOutput = await execPromise( | ||
`dig +short @224.0.0.251 -p 5353 -4 ${service.host}` | ||
); | ||
const addresses = digOutput.stdout.trim().split(os.EOL); | ||
service.addresses = (service.addresses || []).concat(addresses); | ||
next(); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} |