diff --git a/README.md b/README.md index ce7955bc..de6f9492 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ You can pass the following options via CLI arguments. You can also use `--config | Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` | | Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` | | Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` | -| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker) | | `--debug-host` | `FASTIFY_DEBUG_HOST` | +| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` | | Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` | | Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` | | Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` | @@ -175,9 +175,9 @@ By default `--ignore-watch` flag is set to ignore `node_modules build dist .git #### Containerization -When deploying to a Docker, and potentially other, containers, it is advisable to set a fastify address of `0.0.0.0` because these containers do not default to exposing mapped ports to localhost. +When deploying to a Docker container, and potentially other, containers, it is advisable to set a fastify address of `0.0.0.0` because these containers do not default to exposing mapped ports to localhost. -For containers built and run specifically by the Docker Daemon, fastify-cli is able to detect that the server process is running within a Docker container and the `0.0.0.0` listen address is set automatically. +For containers built and run specifically by the Docker Daemon or inside a Kubernetes cluster, fastify-cli is able to detect that the server process is running within a container and the `0.0.0.0` listen address is set automatically. Other containerization tools (eg. Buildah and Podman) are not detected automatically, so the `0.0.0.0` listen address must be set explicitly with either the `--address` flag or the `FASTIFY_ADDRESS` environment variable. diff --git a/start.js b/start.js index 9d0116bd..610f37a9 100755 --- a/start.js +++ b/start.js @@ -17,7 +17,8 @@ const { requireModule, requireFastifyForModule, requireServerPluginFromPath, - showHelpForCommand + showHelpForCommand, + isKubernetes } = require('./util') let Fastify = null @@ -125,7 +126,7 @@ async function runFastify (args, additionalOptions, serverOptions) { } else { require('inspector').open( opts.debugPort, - opts.debugHost || isDocker() ? listenAddressDocker : undefined + opts.debugHost || isDocker() || isKubernetes() ? listenAddressDocker : undefined ) } } @@ -165,7 +166,7 @@ async function runFastify (args, additionalOptions, serverOptions) { await fastify.listen({ port: opts.port, host: opts.address }) } else if (opts.socket) { await fastify.listen({ path: opts.socket }) - } else if (isDocker()) { + } else if (isDocker() || isKubernetes()) { await fastify.listen({ port: opts.port, host: listenAddressDocker }) } else { await fastify.listen({ port: opts.port }) diff --git a/test/start.test.js b/test/start.test.js index 0994c167..4cabb039 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -535,6 +535,27 @@ test('should start the server listening on 0.0.0.0 when running in docker', asyn t.pass('server closed') }) +test('should start the server listening on 0.0.0.0 when running in kubernetes', async t => { + t.plan(2) + const isKubernetes = sinon.stub() + isKubernetes.returns(true) + + const start = proxyquire('../start', { + './util': { + ...require('../util'), + isKubernetes + } + }) + + const argv = ['-p', getPort(), './examples/plugin.js'] + const fastify = await start.start(argv) + + t.equal(fastify.server.address().address, '0.0.0.0') + + await fastify.close() + t.pass('server closed') +}) + test('should start the server with watch options that the child process restart when directory changed', { skip: process.platform === 'win32' }, async (t) => { t.plan(3) const tmpjs = path.resolve(baseFilename + '.js') diff --git a/util.js b/util.js index ca695a61..fcf06de5 100644 --- a/util.js +++ b/util.js @@ -98,4 +98,10 @@ function showHelpForCommand (commandName) { } } -module.exports = { exit, requireModule, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath } +function isKubernetes () { + // Detection based on https://kubernetes.io/docs/reference/kubectl/#in-cluster-authentication-and-namespace-overrides + return process.env.KUBERNETES_SERVICE_HOST !== undefined || + fs.existsSync('/run/secrets/kubernetes.io/serviceaccount/token') +} + +module.exports = { isKubernetes, exit, requireModule, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath }