0

I'm new here to helm world. I want to generate manifest from my values and sample.yaml so I trying to create a template from my sample.yaml files.

Values.yaml

prodapps:
 alpha:
   name: alpha
   image: alpha.azurecr.io/alpha:latest
   imagePullPolicy: IfNotPresent
   resources:
     requests:
       memory: "10000Mi"
       cpu: "150m"
     limits:
       memory: "800Mi"
       cpu: 600m"
   ingress:
     enabled: true
     annotations:
       zap.ingress.kubernetes.io/rule-type: PathPrefixStrip
       nginx.ingress.kubernetes.io/rewrite-target: /
     path: /alpha/api
     hosts:
       - demo.jumboapps.com
 beta:
   name: beta
   disable: true
   image: beta.azurecr.io/beta:latest
   imagePullPolicy: IfNotPresent
   resources:
     requests:
       memory: "10000Mi"
       cpu: "150m"
     limits:
       memory: "800Mi"
       cpu: 600m"
   ingress:
     enabled: true
     annotations:
       zap.ingress.kubernetes.io/rule-type: PathPrefixStrip
       nginx.ingress.kubernetes.io/rewrite-target: /
     path: /beta/api
     hosts:
       - demo.jumboapps.com

Sample.yaml

apiVersion: v1
kind: Service
metadata:
 name: "{{ .Values.prodapps.alpha.name }}-svc"
 namespace: "{{ .Release.Namespace }}"
spec:
 ports:
   - port: 80
     name: "http"
 selector:
   app: "{{ .Values.prodapps.alpha.name }}"
---
apiVersion: v1
kind: Service
metadata:
 name: "{{ .Values.prodapps.beta.name }}-svc"
 namespace: "{{ .Release.Namespace }}"
spec:
 ports:
   - port: 80
     name: "http"
 selector:
   app: "{{ .Values.prodapps.beta.name }}"

My main goal is to templatize my sample.yaml.by and produce the valid Manifet file. following helm documentation. But I cant find any sensible(my understandable) syntax for looping through complex values files.

Please anyone help me out with this.

praveen
  • 179
  • 1
  • 3
  • 16
  • It doesn't really make sense to include every possible setting in Helm values; it would be simpler and clearer to directly write them in standard Kubernetes YAML files. If you need to iterate through `.Values` you can use the [text/template](https://pkg.go.dev/text/template) `range` function. From what you've shown, though, it's not totally clear what problem you're running into. – David Maze Aug 22 '21 at 14:26
  • @DavidMaze , Is there any globalised/standard procedure to construct .Values.yaml file.If yes , can you please help me out with that. – praveen Aug 23 '21 at 02:53

1 Answers1

0

You need to learn more about the usage of range and how k8s divides resources with '---' when applying resources.

You can imitate the following

Values.yaml

prodapps:
  alpha:
    name: alpha
    image: alpha.azurecr.io/alpha:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        memory: "10000Mi"
        cpu: "150m"
      limits:
        memory: "800Mi"
        cpu: 600m"
  beta:
    name: beta
    disable: true
    image: beta.azurecr.io/beta:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        memory: "10000Mi"
        cpu: "150m"
      limits:
        memory: "800Mi"
        cpu: 600m"

service.yaml

{{ if .Values.prodapps }}
{{- range $k, $v := $.Values.prodapps }}
---
apiVersion: v1
kind: Service
metadata:
  name: "{{ $v.name }}-svc"
  namespace: "{{ .Release.Namespace }}"
spec:
  ports:
    - port: 80
      name: "http"
  selector:
    app: "{{ $v.name }}"
{{- end }}
{{- end }}

deployment.yaml

{{ if .Values.prodapps }}
{{- range $k, $v := $.Values.prodapps }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ $v.name }}-dep"
  namespace: "{{ .Release.Namespace }}"
spec:
  template:
    spec:
      containers:
        - name: {{ $v.Name }}
          image: "{{ $v.image }}"
          imagePullPolicy: {{ $v.imagePullPolicy }}
          resources:
            {{- toYaml $v.resources | nindent 12 }}
{{- end }}
{{- end }}
z.x
  • 1,905
  • 7
  • 11