Skip to main content

Managing Chart Files

1. Reading File Content

File Type Description Usage
values.yaml Default values for the chart Used to define default configuration values that can be overridden by users
Chart.yaml Metadata about the chart Contains information such as name, version, and dependencies of the chart
templates/ Template files for Kubernetes resources Contains YAML templates that are rendered with values from values.yaml
README.md Documentation for the chart Provides instructions and usage examples for deploying the chart

Example: Reading values.yaml

image:
  repository: nginx
  tag: "1.21"
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

Example: Reading Chart.yaml

apiVersion: v2
name: my-app
description: A Helm chart for my application
type: application
version: 0.1.0
appVersion: "1.16.0"

2. Listing Files

Command Description Flags
helm list List releases -q for quiet, --namespace for namespace
helm get Get release values or manifest --values, --manifest, --namespace
helm show Show chart information values, chart, readme, values-schema

Example: List all releases

helm list -q

Example: Show chart values

helm show values my-release

3. Reading Files as Bytes

Method Description Usage
readFileBytes Reads a file and returns its content as bytes const data = readFileBytes('values.yaml');
readFileAsString Reads a file and returns its content as a string const content = readFileAsString('README.md');

Example: Reading a Values File as Bytes

const fs = require('fs');
const path = require('path');

const valuesPath = path.join(__dirname, 'values.yaml');
const fileBytes = fs.readFileSync(valuesPath);
console.log(fileBytes); // Buffer of bytes
Note: When reading files as bytes, the data is returned as a Buffer in Node.js. This is useful for binary files or when you need to process the raw bytes.

4. Using Files in ConfigMaps

Property Description Example
configMap.name Name of the ConfigMap to reference my-config
configMap.items Files to include from the ConfigMap [{key: "app.properties", path: "config/app.properties"}]

Example: Mounting ConfigMap Files

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  app.properties: |
    server.port=8080
    database.url=jdbc:postgresql://db:5432/mydb

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    spec:
      containers:
      - name: app
        image: my-app:latest
        volumeMounts:
        - name: config-volume
          mountPath: /app/config
      volumes:
      - name: config-volume
        configMap:
          name: app-config
Note: ConfigMaps can be mounted as volumes or injected as environment variables.

5. Using Files in Secrets

Property Description Usage
secret.name Name of the Kubernetes secret Used to reference the secret in templates
secret.data Map of key-value pairs to store in the secret Values are base64 encoded when stored
secret.type Type of secret (e.g., Opaque, Opaque) Defines how Kubernetes treats the secret

Example: Defining a Secret with Files

apiVersion: v1
kind: Secret
metadata:
  name: {{ .Values.secret.name }}
type: {{ .Values.secret.type }}
data:
  {{- range $key, $value := .Values.secret.data }}
  {{ $key }}: {{ $value | b64enc }}
  {{- end }}
Note: Secret values are base64 encoded when stored in Kubernetes. Helm handles this encoding automatically when using .Values.secret.data.

Example: Using Secret in Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    spec:
      containers:
        - name: app
          image: my-app:latest
          env:
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.secret.name }}
                  key: password

6. Iterating Over Files

Property Description Example
.Files Provides access to all files in the chart .Files.Get "templates/deployment.yaml"
.Files.Lines Iterates over lines in a file .Files.Lines "config.txt"
.Files.Glob Matches files using glob patterns .Files.Glob "templates/*.yaml"

Example: Reading a file line by line

{{- range .Files.Lines "config.txt" }}
{{ . }}
{{- end }}

Example: Using glob to iterate over templates

{{- range .Files.Glob "templates/*.yaml" }}
{{ .Name }}: {{ .Content }}
{{- end }}
Note: The .Files object is read-only and cannot be modified.

7. Checking File Existence

File Type Check Command Description
Chart.yaml helm template . --show-only templates/Chart.yaml Verifies the existence and content of Chart.yaml in the chart directory.
values.yaml helm template . --show-only templates/values.yaml Checks if values.yaml is included in the chart and can be rendered.
templates/ ls -R templates/ Lists all template files to confirm their presence in the chart directory.
charts/ ls -R charts/ Verifies the presence of sub-charts in the charts directory.

Example: Verifying Chart Files

helm template . --show-only templates/Chart.yaml

Example: Listing Templates

ls -R templates/

8. Encoding File Content

Encoding Type Description Usage Notes
base64 Base64 encoding for binary or non-text content data:application/octet-stream;base64,... Used for embedding binary files in templates
gzip Gzip compression for reducing file size data:application/gzip;base64,... Can be used with base64 encoding
plain Plain text encoding (default) data:text/plain;base64,... Simple content without encoding

Example: Embedding Base64 Encoded File

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  config.yaml: |
    base64: ewogICJ0ZXN0IjogInZhbHVlIgp9
Note: When embedding base64 content in templates, ensure proper escaping to avoid YAML parsing errors.

9. Including Binary Files

When working with Helm charts, you may need to include binary files (e.g., compiled binaries, images, or other non-text assets) in your chart. Helm supports including binary files through the resources directory or by embedding them directly in templates.

File Type Inclusion Method Notes
Binary files resources/ directory Place binary files in the resources/ directory to include them in the chart.
Embedded in templates base64 encoding Binary files can be embedded in templates using base64 encoding for use in Kubernetes resources.

Example: Including Binary in Resources

To include a binary file in your chart, place it inside the resources/ directory:

my-chart/
  ├── templates/
  │   └── deployment.yaml
  └── resources/
      └── my-binary

Example: Embedding Binary in Template

You can embed a binary file using base64 encoding in your template:

apiVersion: v1
kind: ConfigMap
metadata:
  name: binary-config
data:
  binary-file: |
    {{ .Files.Get "resources/my-binary" | b64enc }}

10. Organizing Static Files

Static files in Helm charts are typically used for configuration, templates, or other non-executable assets. Proper organization ensures maintainability and clarity.

File Type Description Location Usage
config.yaml Configuration file for chart values templates/ Used to define default values or override settings
README.md Documentation for the chart . Provides instructions and usage examples
values.yaml Default values for chart templates . Used by helm install to populate templates
files/ Directory for static files templates/ Used to include static files in chart resources

Example: Including Static Files in Templates

{{- $fileContent := .Files.Get "files/config.json" }}
{{- $fileContent | indent 4 }}
Note: The .Files object allows access to files within the chart directory structure.

Was this article helpful?