Extend helm upgrade cmd to add some field values

2/14/2022

I'm installing ingress nginx using a modified yaml file

kubectl apply -f deploy.yaml

The yaml file is just the original deploy file but with added hostPorts for the deployment:

ports:
  - name: http
    containerPort: 80
    protocol: TCP
  - name: https
    containerPort: 443
    protocol: TCP
  - name: webhook
    containerPort: 8443
    protocol: TCP

become:

ports:
  - name: http
    containerPort: 80
    protocol: TCP
    hostPort: 80 #<-- added
  - name: https
    containerPort: 443
    protocol: TCP
    hostPort: 443 #<-- added
  - name: webhook
    containerPort: 8443
    protocol: TCP
    hostPort: 8443 #<-- added

So this is working for me. But I would like to install ingress nginx using helm:

helm upgrade --install ingress-nginx ingress-nginx \
  --repo https://kubernetes.github.io/ingress-nginx \
  --namespace ingress-nginx --create-namespace

Is it possible to add the hostPort values using helm (-f values.yml)? I need to add hostPort in Deployment.spec.template.containers.ports, but I have two problems to write the correct values.yml file:

values.yml

# How to access the deployment?
spec:
  template:
    containers:
      ports: # How to add field with existing containerPort value of each element in the array?
-- user3142695
helm3
kubernetes
kubernetes-helm
nginx

2 Answers

2/15/2022

First of all, you already have hostPort in values.yaml. See following fragment:

  ## Use host ports 80 and 443
  ## Disabled by default
  hostPort:
    # -- Enable 'hostPort' or not
    enabled: false
    ports:
      # -- 'hostPort' http port
      http: 80
      # -- 'hostPort' https port
      https: 443

You should turn it on in values.yaml:

  ## Use host ports 80 and 443
  ## Disabled by default
  hostPort:
    # -- Enable 'hostPort' or not
    enabled: true

After all - as you know - you can install your ingress via helm.

helm install -f values.yaml

About webhook - see here.


You can find hostPort also in deployment file. Here is one of the templates (controller-deployment.yaml):

In this file you can find three appearances of hostPort (value controller.hostPort.enabled - responsible for enabling or disabling the hostPort).

Here:

              {{- if $.Values.controller.hostPort.enabled }}
              hostPort: {{ index $.Values.controller.hostPort.ports $key | default $value }}
              {{- end }}

and here:

          {{- range $key, $value := .Values.tcp }}
            - name: {{ $key }}-tcp
              containerPort: {{ $key }}
              protocol: TCP
              {{- if $.Values.controller.hostPort.enabled }}
              hostPort: {{ $key }}
              {{- end }}
          {{- end }}
          {{- range $key, $value := .Values.udp }}
            - name: {{ $key }}-udp
              containerPort: {{ $key }}
              protocol: UDP
              {{- if $.Values.controller.hostPort.enabled }}
              hostPort: {{ $key }}
              {{- end }}
          {{- end }}

See also: 1. ingress-nginx Documentation on Github 2. nginx Documentation 3. NGINX - Helm Charts 4. Helm Documentation

-- kkopczak
Source: StackOverflow