Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pauldennehy opened this issue Jul 11, 2024 · 3 comments
Open

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

pauldennehy opened this issue Jul 11, 2024 · 3 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@pauldennehy
Copy link

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

@pauldennehy pauldennehy added the kind/bug Categorizes issue or PR as related to a bug. label Jul 11, 2024
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jul 11, 2024
@ephesused
Copy link
Contributor

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
Copy link
Author

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
Copy link
Member

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

@k8s-ci-robot k8s-ci-robot added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

4 participants