How to generate many Kubernetes ResourceQuota with Helm Variables?

Viewed 167

I have a OpenShift/Kubernetes new cluster and I need to create multiple ResourceQuota(s) and apply them to existing projects.

Every ResourceQuota should be applied to a given namespace and should contain a specific number of requests and limits. I would like to generate all the needed ResourceQuota YAMs and deploy them by using Helm.

I created a new custom Helm Chart with the command

helm template resourcequota

and I see Helm creating the following files:

    .
..
.helmignore
Chart.yaml
charts
templates
values.yaml

I then wrote a resourcequota.yaml in the templates folder. I would like helm to fill the ResourceQuota specs for me:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: {{ .Values.namespace }}-quota
  namespace: {{ .Values.namepace }}
spec:
  hard:
    cpu: {{ .Values.namespace.requests.cpu }}
    limits.cpu: {{ .Values.namespace.limits.cpu }}
    memory: {{ .Values.namespace.requests.memory }}
    limits.memory: {{ .Values.namespace.limits.cpu }}

The values.yaml file contains a first namespace name with sample values:

...
83 ...
84 namespace: "123-testnamespace"
85   requests:
86     cpu: "2"
87     memory: "1Gi"
88   limits:
89     cpu: "2"
90     memory: "1Gi"

I then asked Helm to locally generate a template with helm template resourcequota but it seems he doesn't know the namespace key (row 84):

Error: cannot load values.yaml: error converting YAML to JSON: yaml: line 84: did not find expected key

What am I doing wrong ? I tried to write the values.yaml from scratch but it seems like I am missing out some helm fundamentals here.

Thank you in advance

1 Answers

it seems like I am missing out some helm fundamentals here

True, As mentioned in error there is an issue at line 84

84 namespace: "123-testnamespace"
85   requests:
86     cpu: "2"
87     memory: "1Gi"
88   limits:
89     cpu: "2"
90     memory: "1Gi"

basic YAML can not create an array of value and use it as key also.

You should have to use the values.yaml like

  namespacename : "123-testnamespace"
  namespace:
    requests:
      cpu: "2"
      memory: "1Gi"
    limits:
      cpu: "2"
      memory: "1Gi"

YAML template

Using change at name & namespace values in template

apiVersion: v1
kind: ResourceQuota
metadata:
  name: {{ .Values.namespacename }}-quota
  namespace: {{ .Values.namespacename }}
spec:
  hard:
    cpu: {{ .Values.namespace.requests.cpu }}
    limits.cpu: {{ .Values.namespace.limits.cpu }}
    memory: {{ .Values.namespace.requests.memory }}
    limits.memory: {{ .Values.namespace.limits.cpu }}

Example of nested YAML

cartParams:
  - title: Test 1
    options:
      - Oh lala
      - oh lalalalala
  - title: Title test 2
    options:
      - oh lala
      - oh lala
      - oh lalala
      - oh lalalalalalalala
      - oh lalalalalalalalalala
Related