Skip to content

raw-configuration: fix invalid resources issued from comment-only YAML fragments#46

Open
LeonoreMangold wants to merge 3 commits into
AvistoTelecom:mainfrom
LeonoreMangold:fix-comment-only-fragments
Open

raw-configuration: fix invalid resources issued from comment-only YAML fragments#46
LeonoreMangold wants to merge 3 commits into
AvistoTelecom:mainfrom
LeonoreMangold:fix-comment-only-fragments

Conversation

@LeonoreMangold

@LeonoreMangold LeonoreMangold commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Example of the error:

templates:
  - |
    # Create my priority class
    ---
    apiVersion: scheduling.k8s.io/v1
    kind: PriorityClass
    metadata:
      name: test
    value: 1
    preemptionPolicy: Never
    globalDefault: false
    description: "my first priority class"

Here the previous version of the chart would generate two Kubernetes manifests, the first one resulting from the comment part ("# Create my priority class") before the first object separator "---".
The first document only contained the labels appended by the chart, something like that:

---
metadata:
  labels:
    app: raw
    chart: raw-1.0.2
    heritage: Helm
    release: raw

But this is an invalid Kubernetes manifest since it does not contain mandatory fields like apiVersion or kind, so applying the Helm release will result in an error.

This kind of use case with a comment being placed before the first separator "---" can occur when using a range loop that needs to be documented. Like that:

{{- range $key, $value:= .Values.priorityClasses }}
# An explanatory comment about what's coming below
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: {{ $key }}
value: {{ $value.priority }}
preemptionPolicy: Never
globalDefault: false
description: {{ $value.desc }}
{{- end }}

{{/* Match "---" line separator without leading indentation and with potential trailing whitespaces or comment before the end of line */}}
{{- range regexSplit "(?m)^---\\s*(#.*)?$" $kubeResources -1 }}
{{- if . | trim }}
{{ $document := . | trim | fromYaml }}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: converting to a yaml document removes the comments and only outputs the "real" manifest content. This should be the most reliable base to test for emptiness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant