Helm with values per environment

Keep one Helm chart in git and give staging and production their own replicas, resources and image tag as values on the deployments.

The situation

You have one chart for web and two environments, staging and production, that differ only in numbers: how many replicas, how much memory, which image tag. You do not want a copy of the chart per environment, and you do not want a values-production.yaml that Wardn would never read.

Wardn renders a chart the way helm template does, on its server, once per commit and set of values. The values come from three places, later on top: the chart's values.yaml, the app's default values, and the values of the app in each deployment. So one app and two deployments give two renders of one chart (Helm charts, Apps).

Repository layout

Chart.yaml
values.yaml
deployment.yaml
service.yaml

Settings

The chart

deploy/web/Chart.yaml
apiVersion: v2
name: web
version: 0.1.0
appVersion: "1.4.2"
deploy/web/values.yaml
replicaCount: 2
image:
  repository: ghcr.io/example/shop-web
  tag: 1.4.2
resources:
  requests:
    cpu: 100m
    memory: 128Mi
deploy/web/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
        - name: web
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          ports:
            - containerPort: 8080
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080

.Release.Name is the app's name, web, in every deployment. The templates name no namespace: Wardn applies them in the deployment's. Keep them free of Namespace, ClusterRole and CRD objects, which the operator refuses.

The app and two deployments

Values are set per deployment, and the web app shows how many a deployment overrides but does not edit them yet, so this part is the API. Make the app on a git source whose Path is deploy/web (Connect a source), then one deployment per environment:

api() { curl -fsS -H "Authorization: Bearer $WARDN_TOKEN" -H 'Content-Type: application/json' "$@"; }

SOURCE_ID=$(api -X POST "$WARDN_API_URL/api/v1/sources" -d '{
  "name": "shop-web", "kind": "git", "url": "https://github.com/example/shop",
  "path": "deploy/web", "credentialsRef": "github-app:51234567"
}' | jq -r .id)

APP_ID=$(api -X POST "$WARDN_API_URL/api/v1/apps" -d '{
  "name": "web", "sourceId": "'"$SOURCE_ID"'", "renderType": "helm",
  "defaultValues": {"replicaCount": 2, "image": {"tag": "1.4.2"}}
}' | jq -r .id)

# Staging: one small replica.
STAGING_DEP=$(api -X POST "$WARDN_API_URL/api/v1/deployments" -d '{
  "name": "web-staging",
  "apps": [{"appId": "'"$APP_ID"'", "values": {"replicaCount": 1, "resources": {"requests": {"cpu": "50m"}}}}],
  "targetKind": "environment", "environmentId": "'"$STAGING_ENV"'",
  "namespace": "shop", "arrival": "branch", "branch": "main"
}' | jq -r .id)

# Production: six replicas and more memory.
PROD_DEP=$(api -X POST "$WARDN_API_URL/api/v1/deployments" -d '{
  "name": "web-production",
  "apps": [{"appId": "'"$APP_ID"'", "values": {"replicaCount": 6, "resources": {"requests": {"cpu": "500m", "memory": "512Mi"}}}}],
  "targetKind": "environment", "environmentId": "'"$PROD_ENV"'",
  "namespace": "shop", "arrival": "branch", "branch": "main"
}' | jq -r .id)

Maps merge key by key and anything else, lists included, replaces what is below it. Staging therefore takes memory: 128Mi from the chart and cpu: 50m from its own values.

Values are stored in the snapshot in clear text, and only plain data is read: a second values-production.yaml in the chart does nothing. Never put a password or token in values. Keep secrets in the cluster and have the chart refer to them by name (Secret references).

Changing a value later

api -X PUT "$WARDN_API_URL/api/v1/deployments/$PROD_DEP/apps/$APP_ID" \
  -d '{"branch": "", "values": {"replicaCount": 8, "resources": {"requests": {"cpu": "500m", "memory": "512Mi"}}}}'

The call replaces both the branch and the values, so repeat what you keep. It does not render anything by itself: the next render uses the new values, and a render starts when the tracked branch moves. Push a commit, an empty one will do (git commit --allow-empty -m "Roll values"), and the snapshot records the new values and a digest of them.

What the chart may not do

Wardn does not fetch while it renders, so run helm dependency build and commit charts/ if the chart has dependencies. A chart on an OCI registry never receives a snapshot in this release, so keep the chart in git. Images are looked up anonymously to pin them to a digest; an image that needs a login cannot be pinned, and the render fails with The image ... could not be resolved to a digest. How a chart is rendered lists the rest.

Check that it worked

kubectl --context kind-staging -n shop get deploy web -o jsonpath='{.spec.replicas}'   # 1
kubectl --context kind-prod    -n shop get deploy web -o jsonpath='{.spec.replicas}'   # 6
api "$WARDN_API_URL/api/v1/deployments/$PROD_DEP" | jq '.apps[] | {name, values}'

In the web app, each deployment's app Settings tab says N overrides here, and a snapshot's page lists the settings each app ran with under Settings and secrets. Compare snapshots shows that a change came from values and not from the chart: the diff holds the one field.

If a deployment stays on its last snapshot after a push, the render failed: GET /deployments/{id} shows the app's renderStatus as pending, and Helm charts lists the messages.

Next

Related: Apps, Deployment settings, Environments.

On this page