Skip to content

Commit

Permalink
improvement(core): always require a namespace and simplify env config (
Browse files Browse the repository at this point in the history
…#1900)

* improvement(core): always require a namespace and simply env config

We now always require a namespace for all environments, and make the
default simply "default". You can override the default namespace with
`environments[].defaultNamespace` as before.

This reduces code branching significantly, especially in the enterprise
UI, and simplifies our configuration as well.

BREAKING CHANGE:

The default namespace in the `kubernetes` provider is now
`<project name>.<environment namespace>` (previously it was just the
project name). Users need to override this to `"${project.name}"` if
they would like to revert to the previous default.
  • Loading branch information
edvald committed Jun 24, 2020
1 parent 841285e commit a783adc
Show file tree
Hide file tree
Showing 51 changed files with 205 additions and 327 deletions.
2 changes: 1 addition & 1 deletion docs/example-projects/using-garden-in-ci.md
Expand Up @@ -44,7 +44,7 @@ name: ci-demo-project
environments:
...
- name: preview
defaultNamespace: ${project.name}-${local.env.CIRCLE_BRANCH || local.username}
defaultNamespace: preview-${local.env.CIRCLE_BRANCH || local.username}
providers:
- name: kubernetes
environments: [preview]
Expand Down
58 changes: 11 additions & 47 deletions docs/reference/config.md
Expand Up @@ -37,27 +37,13 @@ environments:
- # The name of the environment.
name:

# Control if and how this environment should support namespaces. If set to "optional" (the default), users can
# set a namespace for the environment. This is useful for any shared environments, e.g. testing and development
# environments, where namespaces separate different users or code versions within an environment. Users then
# specify an environment with `--env <namespace>.<environment>`, e.g. `--env alice.dev` or
# `--env my-branch.testing`.
# Set the default namespace to use. This can be templated to be user-specific, or to use an environment variable
# (e.g. in CI).
#
# If set to "required", this namespace separation is enforced, and an error is thrown if a namespace is not
# specified with the `--env` parameter.
#
# If set to "disabled", an error is thrown if a namespace is specified. This makes sense for e.g. production or
# staging environments, where you don't want to split the environment between users or code versions.
#
# When specified, namespaces must be a valid DNS-style label, much like other identifiers.
namespacing: optional

# Set a default namespace to use, when `namespacing` is `required` or `optional`. This can be templated to be
# user-specific, or to use an environment variable (e.g. in CI).
#
# If this is set, users can specify `--env <environment>` and skip the namespace part, even when `namespacing` is
# `required` for the environment.
defaultNamespace:
# You can also set this to `null`, in order to require an explicit namespace to be set on usage. This may be
# advisable for shared environments, but you may also be able to achieve the desired result by templating this
# field, as mentioned above.
defaultNamespace: default

# Flag the environment as a production environment.
#
Expand Down Expand Up @@ -232,39 +218,17 @@ environments:
- name: "dev"
```

### `environments[].namespacing`

[environments](#environments) > namespacing

Control if and how this environment should support namespaces. If set to "optional" (the default), users can
set a namespace for the environment. This is useful for any shared environments, e.g. testing and development
environments, where namespaces separate different users or code versions within an environment. Users then
specify an environment with `--env <namespace>.<environment>`, e.g. `--env alice.dev` or
`--env my-branch.testing`.

If set to "required", this namespace separation is enforced, and an error is thrown if a namespace is not
specified with the `--env` parameter.

If set to "disabled", an error is thrown if a namespace is specified. This makes sense for e.g. production or
staging environments, where you don't want to split the environment between users or code versions.

When specified, namespaces must be a valid DNS-style label, much like other identifiers.

| Type | Default | Required |
| -------- | ------------ | -------- |
| `string` | `"optional"` | No |

### `environments[].defaultNamespace`

[environments](#environments) > defaultNamespace

Set a default namespace to use, when `namespacing` is `required` or `optional`. This can be templated to be user-specific, or to use an environment variable (e.g. in CI).
Set the default namespace to use. This can be templated to be user-specific, or to use an environment variable (e.g. in CI).

If this is set, users can specify `--env <environment>` and skip the namespace part, even when `namespacing` is `required` for the environment.
You can also set this to `null`, in order to require an explicit namespace to be set on usage. This may be advisable for shared environments, but you may also be able to achieve the desired result by templating this field, as mentioned above.

| Type | Required |
| -------- | -------- |
| `string` | No |
| Type | Default | Required |
| -------- | ----------- | -------- |
| `string` | `"default"` | No |

Example:

Expand Down
22 changes: 4 additions & 18 deletions docs/reference/providers/kubernetes.md
Expand Up @@ -62,9 +62,6 @@ providers:
# A default hostname to use when no hostname is explicitly configured for a service.
defaultHostname:

# Set a default username (used for namespacing within a cluster).
defaultUsername:

# Defines the strategy for deploying the project services.
# Default is "rolling update" and there is experimental support for "blue/green" deployment.
# The feature only supports modules of type `container`: other types will just deploy using the default strategy.
Expand Down Expand Up @@ -314,10 +311,9 @@ providers:
# Path to kubeconfig file to use instead of the system default. Must be a POSIX-style path.
kubeconfig:

# Specify which namespace to deploy services to. Defaults to the environment namespace, if specified and enabled,
# otherwise the project name.
# Specify which namespace to deploy services to. Defaults to `<project name>-<environment namespace>`.
#
# Note that the framework generates other namespaces as well with this name as a prefix.
# Note that the framework may generate other namespaces as well with this name as a prefix.
namespace:

# Set this to `nginx` to install/enable the NGINX ingress controller.
Expand Down Expand Up @@ -431,16 +427,6 @@ providers:
- defaultHostname: "api.mydomain.com"
```

### `providers[].defaultUsername`

[providers](#providers) > defaultUsername

Set a default username (used for namespacing within a cluster).

| Type | Required |
| -------- | -------- |
| `string` | No |

### `providers[].deploymentStrategy`

[providers](#providers) > deploymentStrategy
Expand Down Expand Up @@ -1467,9 +1453,9 @@ Path to kubeconfig file to use instead of the system default. Must be a POSIX-st

[providers](#providers) > namespace

Specify which namespace to deploy services to. Defaults to the environment namespace, if specified and enabled, otherwise the project name.
Specify which namespace to deploy services to. Defaults to `<project name>-<environment namespace>`.

Note that the framework generates other namespaces as well with this name as a prefix.
Note that the framework may generate other namespaces as well with this name as a prefix.

| Type | Required |
| -------- | -------- |
Expand Down
13 changes: 0 additions & 13 deletions docs/reference/providers/local-kubernetes.md
Expand Up @@ -58,9 +58,6 @@ providers:
# A default hostname to use when no hostname is explicitly configured for a service.
defaultHostname:

# Set a default username (used for namespacing within a cluster).
defaultUsername:

# Defines the strategy for deploying the project services.
# Default is "rolling update" and there is experimental support for "blue/green" deployment.
# The feature only supports modules of type `container`: other types will just deploy using the default strategy.
Expand Down Expand Up @@ -397,16 +394,6 @@ providers:
- defaultHostname: "api.mydomain.com"
```

### `providers[].defaultUsername`

[providers](#providers) > defaultUsername

Set a default username (used for namespacing within a cluster).

| Type | Required |
| -------- | -------- |
| `string` | No |

### `providers[].deploymentStrategy`

[providers](#providers) > deploymentStrategy
Expand Down
2 changes: 1 addition & 1 deletion examples/build-dependencies/garden.yml
Expand Up @@ -4,7 +4,7 @@ name: build-dependencies
environments:
- name: local
- name: remote
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/deployment-strategies/garden.yml
Expand Up @@ -5,7 +5,7 @@ environments:
- name: local-default
- name: local-blue-green
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
# Default deployment strategy
- name: local-kubernetes
Expand Down
2 changes: 1 addition & 1 deletion examples/disabled-configs/garden.yml
Expand Up @@ -7,9 +7,9 @@ environments:
providers:
- name: local-kubernetes
environments: [local]
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
- name: kubernetes
environments: [remote]
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
# Replace these values as appropriate
context: gke_garden-dev-200012_europe-west1-b_garden-dev-1
defaultHostname: ${local.username}-disabled-configs.dev-1.sys.garden
Expand Down
2 changes: 1 addition & 1 deletion examples/hadolint/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: demo-project
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: hadolint
- name: local-kubernetes
Expand Down
2 changes: 1 addition & 1 deletion examples/hot-reload-post-sync-command/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: hot-reload-post-sync-command
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/hot-reload/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: hot-reload
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/kaniko/garden.yml
Expand Up @@ -4,7 +4,7 @@ name: kaniko
environments:
- name: local
- name: remote
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/kubernetes-secrets/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: kubernetes-secrets
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/openfaas/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: openfaas
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/project-variables/garden.yml
Expand Up @@ -9,7 +9,7 @@ environments:
# We only want one replica of each service when developing locally
service-replicas: 1
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/remote-sources/garden.yml
Expand Up @@ -8,7 +8,7 @@ sources:
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/tasks/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: tasks
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/vote-helm/garden.yml
Expand Up @@ -3,7 +3,7 @@ name: vote-helm
environments:
- name: local
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
2 changes: 1 addition & 1 deletion examples/vote/garden.yml
Expand Up @@ -4,7 +4,7 @@ environments:
- name: local
- name: remote
- name: testing
defaultNamespace: ${project.name}-testing-${local.env.CIRCLE_BUILD_NUM || local.username}
defaultNamespace: testing-${local.env.CIRCLE_BUILD_NUM || local.username}
providers:
- name: local-kubernetes
environments: [local]
Expand Down
4 changes: 2 additions & 2 deletions garden-service/src/cli/cli.ts
Expand Up @@ -47,7 +47,7 @@ import {
checkForUpdates,
checkForStaticDir,
} from "./helpers"
import { defaultEnvironments, ProjectConfig } from "../config/project"
import { defaultEnvironments, ProjectConfig, defaultNamespace } from "../config/project"
import { ERROR_LOG_FILENAME, DEFAULT_API_VERSION, DEFAULT_GARDEN_DIR_NAME, LOGS_DIR_NAME } from "../constants"
import stringify = require("json-stringify-safe")
import { generateBasicDebugInfoReport } from "../commands/get/get-debug-info"
Expand All @@ -70,7 +70,7 @@ const GLOBAL_OPTIONS_GROUP_NAME = "Global options"

export async function makeDummyGarden(root: string, gardenOpts: GardenOpts = {}) {
const environments = gardenOpts.environmentName
? [{ name: gardenOpts.environmentName, variables: {} }]
? [{ name: gardenOpts.environmentName, defaultNamespace, variables: {} }]
: defaultEnvironments

const config: ProjectConfig = {
Expand Down

0 comments on commit a783adc

Please sign in to comment.