Append a Hostname During helm upgrade

7/9/2019

In our CI pipeline, we've switched to use convention-based hostnames (primarily for dynamic dev environments based on PR). This new functionality requires us to respect legacy hostnames that are set in the Helm Charts since other services point to those old hostnames.

We want to append or prepend our convention-based hostname to the existing hostname list, without overwriting any of the values.

We've found a workaround, for now; but, are hoping to find a cleaner solution. The workaround allows us to --set service.hostnames[10]="k8s-myapp-prod.website.com", where the index of 10 is high enough to not collide with hostnames in the chart.

We only ever expect 2 or 3 hostnames to be here, so the solution works, it ignores that there isn't more than one other hostname. I'm more concerned with a future update that checks that there's only one hostname in the list and throws an index out of bounds error.

Our command looks like this:

helm upgrade --install \
  --namespace "myapp" \
  --values "./values-prod.yaml" \
  --set service.hostnames[10]="k8s-myapp-prod.website.com" \
  "myapp-prod" ./

Any thoughts on making this cleaner, or some kind of other magic we can use?

Here's a copy of our values.yaml file:

image:
  repository: dockerhub.com/myorg

stack:
  environment: prod

service:
  ingress:
    class: nginx
  hostnames:
    - legacy-url-myapp-prod.website.com
  port: 80
  healthcheck: /heartbeat

resources:
  memory:
    request: "512Mi"
    limit: "512Mi"
  cpu:
    request: "500m"
    limit: "500m"

autoscaling:
  enabled: true
  minReplicas: 2
  maxReplicas: 6
  cpu: 50

environment:
  DEPLOY_ENV: prod

spec:
  strategy:
    type: RollingUpdate
-- jamesbar2
kubernetes
kubernetes-helm

1 Answer

7/9/2019

If you are adding ingress hostnames using an array in your values file, and looping through them with range in the helm template, then you can dynamically find the next array index to use on the command line. You can use jq and yq to count the existing hostnames in the values file. NB: script below has no error checking.

hostcount=$(yq r -j values-prod.yaml service | jq -r '.hostnames | length')

# ((hostcount++)) # edit: length is 1-based, array index is 0-based; hostcount should be the next index as is

helm upgrade --install \
  --namespace "myapp" \
  --values "./values-prod.yaml" \
  --set service.hostnames[$hostcount]="k8s-myapp-prod.website.com" \
  "myapp-prod" ./
-- Jeremy Gaither
Source: StackOverflow