Search code examples
kuberneteskubernetes-helm

k8s: Error when creating CronJob chart


I've a question about helm validation of resources field in CronJob chart , any help welcome.

I use apiVersion batch/v1beta1

I get error when I try to retrive values from values.yaml

The error : Error: error validating "": error validating data: found `invalid field requests for v1.Container

Command I run : helm install --dry-run --debug my_chart

my CronJob chart as following:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ .Chart.Name }}
spec:
  schedule: "0 */2 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
     ...
     spec:
  containers:
    ...
     spec:
       resources:
{{ toYaml .Values.resources | indent 12 }}

Error disapierd when I change the resources configuration to

    resources:
      requests:
        cpu: 300m
        memory: 1024Mi

I don't like the last change and I'd like to follow my regular configuration guide and store values in values.yaml

cluster version are

Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.4", GitCommit:"9befc2b8928a9426501d3bf62f72849d5cbcd5a3", GitTreeState:"clean", BuildDate:"2017-11-20T05:28:34Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}

Server Version: version.Info{Major:"1", Minor:"8+", GitVersion:"v1.8.4-gke.0", GitCommit:"04502ae78d522a3d410de3710e1550cfb16dad4a", GitTreeState:"clean", BuildDate:"2017-11-27T19:19:56Z", GoVersion:"go1.8.3b4", Compiler:"gc", Platform:"linux/amd64"}

helm versions are :

Client: &version.Version{SemVer:"v2.7.0", GitCommit:"08c1144f5eb3e3b636d9775617287cc26e53dba4", GitTreeState:"clean"}

Server: &version.Version{SemVer:"v2.7.0", GitCommit:"08c1144f5eb3e3b636d9775617287cc26e53dba4", GitTreeState:"clean"}

Thanks in advance


Solution

  • TL;DR: Indent by 14 spaces (instead of 12) and remove the additional spec property in your container definition.


    Mind the correct indentation in your YAML definitions. For example, the containers property needs to be a sub-property of the CronJob's spec.jobTemplate.spec.template property (with spec.jobTemplate being the template for a Job object (or a JobTemplate), and spec.jobTemplate.spec.template then being the template for that Job's Pod object (or a PodTemplate).

    Furthermore, the Pod's containers attribute does not require an additional spec. Have a look at the API reference for the respective objects (linked above) for the exact specification of these object types.

    For a CronJob, this is how the Helm template should look like (again, indentation is important!). Also, note that in this case, the .spec.jobTemplate.spec.template.spec.resources.requests property needs to be indented by 14 spaces, and not 12.

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: {{ .Chart.Name }}
    spec:
      schedule: "0 */2 * * *"
      concurrencyPolicy: Forbid
      jobTemplate:
        spec:
          template:
            metadata:
              # ...
            spec:
              containers:
              - name: foo
                # ...
                resources:
    {{ toYaml .Values.resources | indent 14 }}
    

    Regarding the error you've received: With an indentation of 12 spaces (indent 12), Helm will create a YAML definition for your job similar to the following:

            spec:
              containers:
              - name: foo
                # ...
                resources:
                requests:
                  cpu: 300m
                  memory: 1024Mi
    

    As you can see, the requests property (intended to be a sub-property of the resources property), is now actually a property of the Container definition. However, the Container resource does not have a field called requests, resulting in the error message:

    Error: error validating "": error validating data: found `invalid field requests for v1.Container