Chuyển đến nội dung

Bài 21 · Dependencies and Hooks

Conditional Dependencies, Tags and import-values

Turn subcharts on and off with condition and tags, and pull values back out of subcharts with import-values, with a runnable stand-in that evaluates dependency switches.

The same chart is often installed in very different places. In production you point the application at a managed database; on a laptop you want a throwaway Postgres inside the cluster. Helm lets you ship both options in one chart by making dependencies optional.

condition

Add a condition to a dependency in Chart.yaml. It is a values path; the subchart is included only when the path resolves to true:

YAML
dependencies:
  - name: postgresql
    version: "^16.0.0"
    repository: "oci://registry-1.docker.io/bitnamicharts"
    condition: postgresql.enabled
YAML
# values.yaml
postgresql:
  enabled: true
externalDatabase:
  host: ""

Install with the bundled database by default, or switch it off in production:

Terminal
helm install shop ./shop --set postgresql.enabled=false --set externalDatabase.host=db.prod.internal

Details that trip people up:

  • A condition can list several paths separated by commas (a.enabled,b.enabled); Helm uses the first one that exists in the values.
  • If no listed path exists, the condition is ignored and the subchart is included. A default in values.yaml makes the behavior explicit.
  • Conditions are evaluated against the parent's final values, after all -f files and --set flags.
  • Only boolean true or false count. The string "false" from a badly quoted --set is not a boolean in older practice; use --set postgresql.enabled=false without quotes.

tags

Tags switch groups of dependencies. Give each dependency one or more tags and flip them under the top-level tags: key:

YAML
dependencies:
  - name: postgresql
    tags: [database, backend]
    version: "^16.0.0"
    repository: "oci://registry-1.docker.io/bitnamicharts"
  - name: mongodb
    tags: [database]
    version: "^16.0.0"
    repository: "oci://registry-1.docker.io/bitnamicharts"
  - name: rabbitmq
    tags: [backend]
    version: "^15.0.0"
    repository: "oci://registry-1.docker.io/bitnamicharts"
YAML
tags:
  database: true
  backend: false

The rules: a condition that resolves wins over tags; otherwise, if any tag is true the dependency is enabled, and if all existing tags are false it is disabled; with no matching tags it is enabled.

The stand-in below evaluates these rules so you can try combinations.

Python Stand-in: decide which subcharts are enabled

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how in prod the explicit postgresql.enabled: false beats the database tag default, while mongodb has no condition and no tag value, so it stays on.

When a subchart is disabled, Helm does not render it at all, so its hooks, CRDs in templates/ and RBAC objects disappear with it. Objects in the crds/ directory are different and are handled by helm install separately.

import-values

Normally data flows only downward. import-values is the controlled way to pull values up from a subchart into the parent, so the parent can reuse a setting the subchart defines instead of hard-coding it twice.

The simplest form lists keys that the subchart exports under a top-level exports: key in its values.yaml:

YAML
# subchart values.yaml
exports:
  data:
    service:
      port: 5432
YAML
# parent Chart.yaml
dependencies:
  - name: postgresql
    version: "^16.0.0"
    repository: "oci://registry-1.docker.io/bitnamicharts"
    import-values:
      - data

After import, the parent's values contain service.port: 5432. The explicit form maps any child path onto any parent path:

YAML
    import-values:
      - child: primary.service.ports.postgresql
        parent: databasePort

The parent can now write {{ .Values.databasePort }} and stays correct if the subchart changes its default. Imported values have lower priority than the parent's own values: the parent's values.yaml can still override them.

Python Stand-in: import-values copies child values into the parent

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Design advice

  • Give every optional subchart a condition, and write the default in values.yaml so helm show values documents it.
  • Use tags for environment-style bundles, such as tags.observability.
  • Prefer import-values over copying numbers between charts: one source of truth.
  • Test both states in CI: render with the subchart on and off, using the helm template workflow from lint and template debugging.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What happens when a dependency's condition path does not exist in the values?

    Missing paths are ignored, so the dependency stays enabled; set an explicit default.
  2. 2. Which takes precedence when both a condition and tags match?

    A resolved condition overrides tags.
  3. 3. What does import-values do?

    It exposes selected subchart values to the parent, which has priority over them.

Summary

  • condition switches one dependency with a values path; tags switch groups.
  • Missing condition paths leave the dependency enabled.
  • import-values pulls selected subchart values into the parent, with the parent still winning.
  • Test both the enabled and disabled rendering.

Next: Library Charts and Shared Templates

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.