Skip to content

Commit

Permalink
feat: configurable liveness probes for local mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vvagaytsev committed May 17, 2022
1 parent d74b25c commit 307cd18
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
10 changes: 10 additions & 0 deletions core/src/plugins/container/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ export const containerDevModeSchema = () =>

export interface ContainerLocalModeSpec {
localAppPort: number
enableLivenessProbe: boolean
command?: string[]
containerName?: string
}
Expand All @@ -318,6 +319,12 @@ export const containerLocalModeSchema = () =>
.description("The command to run the local application (optional)."),
containerName: joi.string().optional().description("The k8s name of the remote container (optional)."),
localAppPort: joi.number().description("The working port of the local application."),
enableLivenessProbe: joi
.boolean()
.default(true)
.description(
"Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to false to disable liveness probes."
),
}).description(dedent`
Specifies necessary configuration details of the local application which will replace a target remote service in the k8s cluster.
Expand All @@ -329,6 +336,9 @@ export const containerLocalModeSchema = () =>
Local mode is enabled by setting the \`--local-mode\` option on the \`garden deploy\` command.
The liveness probes are enabled by default.
Those can be disabled by setting \`services[].localMode.enableLivenessProbe: false\`.
See the [Local Mode guide](${localModeGuideLink}) for more information.
`)

Expand Down
12 changes: 9 additions & 3 deletions core/src/plugins/kubernetes/local-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,20 @@ function patchOriginalServiceSpec(

/**
* Patches the main container by adding localMode-specific settings like ports, environment variables,
* docker image name and readiness probe settings.
* docker image name and health-check settings.
*
* @param mainContainer the main container object to be patched
* @param proxyContainerName the target container name
* @param localModeEnvVars the list of localMode-specific environment variables
* @param localModePorts the list of localMode-specific ports (e.g. ssh port for tunnel setup)
* @param localModeSpec the local mode configuration spec
*/
function patchMainContainer(
mainContainer: V1Container,
proxyContainerName: string,
localModeEnvVars: PrimitiveMap,
localModePorts: ServicePortSpec[]
localModePorts: ServicePortSpec[],
localModeSpec: ContainerLocalModeSpec
) {
mainContainer.name = proxyContainerName
mainContainer.image = reverseProxyImageName
Expand All @@ -367,6 +369,10 @@ function patchMainContainer(
containerPort: port.containerPort,
})
}

if (!localModeSpec.enableLivenessProbe) {
delete mainContainer.livenessProbe
}
}

/**
Expand Down Expand Up @@ -405,7 +411,7 @@ export async function configureLocalMode(configParams: ConfigureLocalModeParams)
const localModePorts = prepareLocalModePorts(configParams)

patchOriginalServiceSpec(service.spec, localModeEnvVars, localModePorts)
patchMainContainer(mainContainer, proxyContainerName, localModeEnvVars, localModePorts)
patchMainContainer(mainContainer, proxyContainerName, localModeEnvVars, localModePorts, localModeSpec)

// todo: check if anything else should be configured here
}
Expand Down
23 changes: 23 additions & 0 deletions core/test/integ/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,29 @@ describe("kubernetes container deployment handlers", () => {
})
})

it("should delete liveness probes in local mode when livenessProbeEnabled=false", async () => {
const service = graph.getService("local-mode")
service.spec.localMode.enableLivenessProbe = false
const namespace = provider.config.namespace!.name!

const resource = await createWorkloadManifest({
api,
provider,
service,
runtimeContext: emptyRuntimeContext,
namespace,
enableDevMode: false,
enableHotReload: false,
enableLocalMode: true, // <----
log: garden.log,
production: false,
blueGreen: false,
})

const appContainerSpec = resource.spec.template?.spec?.containers.find((c) => c.name === "local-mode")
expect(appContainerSpec!.livenessProbe).to.be.undefined
})

it("should remove readiness probes when in local mode", async () => {
const service = graph.getService("local-mode")
const namespace = provider.config.namespace!.name!
Expand Down
13 changes: 12 additions & 1 deletion docs/guides/running-service-in-local-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ inspect the `known_hosts` file and clean it up manually.
## Configuration

To configure a service for local mode, add `localMode` to your module/service configuration to specify your target
services:
services.

### Health-checks

The readiness probes are disabled for all services running in local mode. This has been done because of some technical
reasons. The k8s cluster readiness checks are applied to a proxy container which sends the traffic to the local service.
When a readiness probe happens, the target local service and the relevant port forward are not ready yet.

The _local mode_ supports liveness probes. The liveness probles have intentionally longer initial delays, just to make
sure that the local services and port forward have enough time to start. The liveness probes are enabled by default, but
can be disabled if necessary. See the example below.

### Configuring local mode for `container` modules

Expand All @@ -86,6 +96,7 @@ services:
args: [ npm, start ]
localMode:
localAppPort: 8090 # The port of the local service, will be used for port-forward setup
enableLivenessProbe: false # Optional, `true` is the default value. Set it to `false` if you need to disable liveness probes for the local service.
command: [ npm, run, serve ] # Starts the local service which will replace the target one in the k8s cluster
containerName: "node-service" # Optional. The name of the target k8s service. It will be inferred automatically if this option is not defined.
...
Expand Down
20 changes: 20 additions & 0 deletions docs/reference/module-types/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ services:
#
# Local mode is enabled by setting the `--local-mode` option on the `garden deploy` command.
#
# The liveness probes are enabled by default.
# Those can be disabled by setting `services[].localMode.enableLivenessProbe: false`.
#
# See the [Local Mode guide](https://docs.garden.io/guides/running-service-in-local-mode.md) for more information.
localMode:
# The command to run the local application (optional).
Expand All @@ -323,6 +326,10 @@ services:
# The working port of the local application.
localAppPort:

# Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to
# false to disable liveness probes.
enableLivenessProbe: true

# List of ingress endpoints that the service exposes.
ingresses:
- # Annotations to attach to the ingress (Note: May not be applicable to all providers)
Expand Down Expand Up @@ -1399,6 +1406,9 @@ The `command` should not depend on the current service or module path.

Local mode is enabled by setting the `--local-mode` option on the `garden deploy` command.

The liveness probes are enabled by default.
Those can be disabled by setting `services[].localMode.enableLivenessProbe: false`.

See the [Local Mode guide](https://docs.garden.io/guides/running-service-in-local-mode.md) for more information.

| Type | Required |
Expand Down Expand Up @@ -1435,6 +1445,16 @@ The working port of the local application.
| -------- | -------- |
| `number` | No |

### `services[].localMode.enableLivenessProbe`

[services](#services) > [localMode](#serviceslocalmode) > enableLivenessProbe

Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to false to disable liveness probes.

| Type | Default | Required |
| --------- | ------- | -------- |
| `boolean` | `true` | No |

### `services[].ingresses[]`

[services](#services) > ingresses
Expand Down
20 changes: 20 additions & 0 deletions docs/reference/module-types/jib-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ services:
#
# Local mode is enabled by setting the `--local-mode` option on the `garden deploy` command.
#
# The liveness probes are enabled by default.
# Those can be disabled by setting `services[].localMode.enableLivenessProbe: false`.
#
# See the [Local Mode guide](https://docs.garden.io/guides/running-service-in-local-mode.md) for more information.
localMode:
# The command to run the local application (optional).
Expand All @@ -344,6 +347,10 @@ services:
# The working port of the local application.
localAppPort:

# Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to
# false to disable liveness probes.
enableLivenessProbe: true

# List of ingress endpoints that the service exposes.
ingresses:
- # Annotations to attach to the ingress (Note: May not be applicable to all providers)
Expand Down Expand Up @@ -1470,6 +1477,9 @@ The `command` should not depend on the current service or module path.

Local mode is enabled by setting the `--local-mode` option on the `garden deploy` command.

The liveness probes are enabled by default.
Those can be disabled by setting `services[].localMode.enableLivenessProbe: false`.

See the [Local Mode guide](https://docs.garden.io/guides/running-service-in-local-mode.md) for more information.

| Type | Required |
Expand Down Expand Up @@ -1506,6 +1516,16 @@ The working port of the local application.
| -------- | -------- |
| `number` | No |

### `services[].localMode.enableLivenessProbe`

[services](#services) > [localMode](#serviceslocalmode) > enableLivenessProbe

Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to false to disable liveness probes.

| Type | Default | Required |
| --------- | ------- | -------- |
| `boolean` | `true` | No |

### `services[].ingresses[]`

[services](#services) > ingresses
Expand Down
20 changes: 20 additions & 0 deletions docs/reference/module-types/maven-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ services:
#
# Local mode is enabled by setting the `--local-mode` option on the `garden deploy` command.
#
# The liveness probes are enabled by default.
# Those can be disabled by setting `services[].localMode.enableLivenessProbe: false`.
#
# See the [Local Mode guide](https://docs.garden.io/guides/running-service-in-local-mode.md) for more information.
localMode:
# The command to run the local application (optional).
Expand All @@ -323,6 +326,10 @@ services:
# The working port of the local application.
localAppPort:

# Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to
# false to disable liveness probes.
enableLivenessProbe: true

# List of ingress endpoints that the service exposes.
ingresses:
- # Annotations to attach to the ingress (Note: May not be applicable to all providers)
Expand Down Expand Up @@ -1409,6 +1416,9 @@ The `command` should not depend on the current service or module path.

Local mode is enabled by setting the `--local-mode` option on the `garden deploy` command.

The liveness probes are enabled by default.
Those can be disabled by setting `services[].localMode.enableLivenessProbe: false`.

See the [Local Mode guide](https://docs.garden.io/guides/running-service-in-local-mode.md) for more information.

| Type | Required |
Expand Down Expand Up @@ -1445,6 +1455,16 @@ The working port of the local application.
| -------- | -------- |
| `number` | No |

### `services[].localMode.enableLivenessProbe`

[services](#services) > [localMode](#serviceslocalmode) > enableLivenessProbe

Enable liveness probes for the local service (over the proxy container) if true. True by default. Set it to false to disable liveness probes.

| Type | Default | Required |
| --------- | ------- | -------- |
| `boolean` | `true` | No |

### `services[].ingresses[]`

[services](#services) > ingresses
Expand Down

0 comments on commit 307cd18

Please sign in to comment.