Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand All @@ -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.

Expand Down
7 changes: 4 additions & 3 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {
requireModule,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand
showHelpForCommand,
isKubernetes
} = require('./util')

let Fastify = null
Expand Down Expand Up @@ -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
)
}
}
Expand Down Expand Up @@ -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 })
Expand Down
21 changes: 21 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 7 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }