kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
10.88k stars 2.24k forks source link

Kio ByteReader changing the structure of YTT Yaml with list in it #5732

Open pauldennehy opened 1 month ago

pauldennehy commented 1 month ago

What happened?

When using Kio.Bytereader to read in a yaml with YTT data that has a list it will change the structure of that yaml. This is notice when writing the yaml to file again.

What did you expect to happen?

Structure should not change from importing and exporting.

How can we reproduce it (as minimally and precisely as possible)?

apiVersion: v1
kind: YttTemplate
metadata:
  name: ytt_array_template
ytt_header:
  header:
  #@ load("@ytt:data", "data")
  array_list1:
    -  #@data.1
    -  #@data.2
ytt_template_content:
  content:
    #!---START OF TEMPLATE--DO NOT REMOVE THIS LINE---
    desc: " The array will not work. removes all array elements, except last and will move it out of the array"
    #try string prepend
    array_list2:
      list_items:
        -  #@data.1
        -  #@data.2
        -  #@data.3
      #! END

Expected output

apiVersion: v1
kind: YttTemplate
metadata:
  name: ytt_array_template
  annotations:
    config.kubernetes.io/index: '0'
    internal.config.kubernetes.io/index: '0'
    internal.config.kubernetes.io/seqindent: 'wide'
ytt_header:
  header:
  #@ load("@ytt:data", "data")
  array_list1:
    -  #@data.1
    -  #@data.2
ytt_template_content:
  content:
    #!---START OF TEMPLATE--DO NOT REMOVE THIS LINE---
    desc: " The array will not work. removes all array elements, except last and will move it out of the array"
    #try string prepend
    array_list2:
      list_items:
        -  #@data.1
        -  #@data.2
        -  #@data.3
      #! END

Actual output

apiVersion: v1
kind: YttTemplate
metadata:
  name: ytt_array_template
  annotations:
    config.kubernetes.io/index: '0'
    internal.config.kubernetes.io/index: '0'
    internal.config.kubernetes.io/seqindent: 'wide'
ytt_header:
  header:
  #@ load("@ytt:data", "data")
  array_list1:
  -
  -
  #@data.2
ytt_template_content:
  content:
    #!---START OF TEMPLATE--DO NOT REMOVE THIS LINE---
    desc: " The array will not work. removes all array elements, except last and will move it out of the array"
    #try string prepend
    array_list2:
      list_items:
      -
      -
      -
      #@data.3
      #! END

Kustomize version

sigs.k8s.io/kustomize/kyaml v0.17.1

Operating system

Windows

ephesused commented 1 month ago

If I'm following correctly, your concern is that the yaml processing is dropping comments from list elements. Assuming you're using sigs.k8s.io/kustomize/kyaml v0.17.1 directly, would you mind posting a go test case that exhibits the behavior you're describing? Thanks.

pauldennehy commented 1 month ago

Yes dropping but also moving last comment outside of list. Correct i am using sigs.k8s.io/kustomize/kyaml v0.17.1 Code which shows input and output.

package main

import (
    "bytes"
    "fmt"
    "io"
    "os"

    "sigs.k8s.io/kustomize/kyaml/kio"
)

func ProcessYAML(reader io.Reader, writer io.Writer) error {
    data, err := io.ReadAll(reader)
    if err != nil {
        return fmt.Errorf("error reading input: %v", err)
    }

    yamlReader := kio.ByteReader{
        Reader:            bytes.NewReader(data),
        PreserveSeqIndent: true,
        WrapBareSeqNode:   true,
        DisableUnwrapping: true,
    }

    nodes, err := yamlReader.Read()
    if err != nil {
        return fmt.Errorf("error parsing YAML: %v", err)
    }

    var outputBuffer bytes.Buffer
    for _, node := range nodes {
        outputBuffer.WriteString(node.MustString() + "\n")
    }

    _, err = writer.Write(outputBuffer.Bytes())
    if err != nil {
        return fmt.Errorf("error writing output: %v", err)
    }

    return nil
}

func main() {
    inputContent := `
apiVersion: v1
kind: YttTemplate
metadata:
  name: ytt_array_template
ytt_header:
  header:
  #@ load("@ytt:data", "data")
  array_list1:
    -  #@data.1
    -  #@data.2
ytt_template_content:
  content:
    #!---START OF TEMPLATE--DO NOT REMOVE THIS LINE---
    desc: " The array will not work. removes all array elements, except last and will move it out of the array"
    #try string prepend
    array_list2:
      list_items:
        -  #@data.1
        -  #@data.2
        -  #@data.3
      #! END
`

    inputBuffer := bytes.NewBufferString(inputContent)
    if err := ProcessYAML(inputBuffer, os.Stdout); err != nil {
        fmt.Printf("%v\n", err)
        os.Exit(1)
    }
}
stormqueen1990 commented 1 month ago

This seems to be related to both #2310 and #4481 /triage accepted /lifecycle frozen