Kubernetes yaml file not working as a helm template

Viewed 926

I know this is a repeated question however, I didn't get an answer that satisfies my query. I am trying to create a helm chart for cronjob deployment. I keep the helm chart name as cronjob-example.

Now when I run the command helm install or helm upgrade manually I can install or update the cronjob however when I try to do the same from CICD pipeline it fails with error converting file YAML to JSON on line 19 of templates/cronjob.yaml file.

cronjob.yaml file.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: {{ .Values.metadata.namespace }}
  creationTimestamp: {{ .Values.metadata.creationTimestamp }}
  name: {{ .Values.name }}
spec:
  jobTemplate:
    metadata:
      creationTimestamp: {{ .Values.metadata.creationTimestamp }}
      name: {{ .Values.name }}
    spec:
      template:
        metadata:
          creationTimestamp: {{ .Values.metadata.creationTimestamp }}
        spec:
          imagePullSecrets:
            - name: {{ .Values.image.imagePullSecrets }}
          containers:
          - image: {{ .Values.image.repository }}
            name: {{ .Chart.Name }}
            resources: {}
          restartPolicy: OnFailure
  schedule: '*/1 * * * *'
status: {}

values.yaml file

---
metadata:
  namespace: "{{K8S_NS}}"

name: "{{HELM_APP_NAME}}"
#name: "cronjob-example"

nodeLabel: agent

image:
  repository: "{{CI_REGISTRY_IMAGE}}/{{CI_COMMIT_REF_SLUG}}:{{CI_COMMIT_SHA}}.{{CI_PIPELINE_IID}}"
  pullPolicy: "Always"
  imagePullSecrets: git-image-pull-secret-cron
  creationTimestamp: null

variables:
- name: "TLS_ENV"
  value: "tst"

I am wondering why it is successful when I run it manually and why it fails through CICD pipeline. Everything works in CICD pipeline as is except the deployment stage. There it fails. All I am doing is hard coding the values I get from previous stages in cicd when I am installing/upgrading manually.

2 Answers

The issue is this location in your template.

- image: {{ .Values.image.repository }}

The substituted value for repository: "{{CI_REGISTRY_IMAGE}} /{{CI_COMMIT_REF_SLUG}}:{{CI_COMMIT_SHA}}.{{CI_PIPELINE_IID}}" results in an invalid value.

As you mentioned there is issue at line number 19 which is

- image: {{ .Values.image.repository }}

you might be giving proper value when trying locally and check what values are getting passed during the CI for

repository: "{{CI_REGISTRY_IMAGE}} /{{CI_COMMIT_REF_SLUG}}:{{CI_COMMIT_SHA}}.{{CI_PIPELINE_IID}}"

if you want to debug more you can also try

helm install --dry-run --debug or helm template --debug

It's a great way to have the server render your templates, then return the resulting manifest file.

Related