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

preserve order and comments in edit #233

Merged
merged 1 commit into from
Aug 9, 2018

Conversation

Liujingfang1
Copy link
Contributor

Fix #17

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 6, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Liujingfang1

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 6, 2018
@Liujingfang1 Liujingfang1 requested review from mengqiy and removed request for droot August 7, 2018 18:53
@Liujingfang1
Copy link
Contributor Author

This is ready for review. Please take a look.

# some descriptions for the patches
patches:
- service.yaml
- pod.yaml
Copy link
Contributor

Choose a reason for hiding this comment

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

will it preserve the comments on the items in the list ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it won't. Only the comments ahead the item is preserved.

# some descriptions for the patches
patches:
  # this is service.yaml
- service.yaml
- pod.yaml

will be

# some descriptions for the patches
patches:
- service.yaml
- pod.yaml

Copy link
Contributor

Choose a reason for hiding this comment

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

ok. Thanks.

@Liujingfang1
Copy link
Contributor Author

Here are some limitations for this change:

  • blank lines are all removed, not preserved
  • indentation might change after edit. For example
    resources:
       - r1.yam
       - r2.yaml
    
    becomes
    resources:
    - r1.yam
    - r2.yaml
    
  • order of items are adjusted
    commonLabels:
      someName: someValue
      app: bingo
      owner: alice
    
    becomes
    commonLabels:
      app: bingo
      owner: alice
      someName: someValue
    
  • comments inside one filed are removed

path string
fsys fs.FileSystem
comments []fieldComment
fields map[string]bool
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fields indicates if a field shows up in the original kustomization.yaml. We can change it to a better name.

Copy link
Contributor

@monopole monopole left a comment

Choose a reason for hiding this comment

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

clever hack!

pattern := regexp.MustCompile("^(" + strings.Join(kustomizationFields, "|") + "):")
var err error
var line []byte
for err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

can this be structured more traditionally as:

line, err := buffer.ReadBytes('\n')
for err == nil {
  doStuff()
  line, err := buffer.ReadBytes('\n')
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

var line []byte
for err == nil {
line, err = buffer.ReadBytes('\n')
if strings.HasPrefix(string(line), "#") {
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like this could be tweaked to allow the comment block to accumulate (and preserve) blank lines
might also want to allow leading blanks since it seems harmless.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can tweak it. I will update this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the tweaking. I also added a test case.

buffer := bytes.NewBuffer(content)
var comments [][]byte
incomments := false
pattern := regexp.MustCompile("^(" + strings.Join(kustomizationFields, "|") + "):")
Copy link
Contributor

Choose a reason for hiding this comment

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

this is effectively a package constant.
please declare immediately after kustomizationFields, and call it recognizedFields or something (pattern is too generic, like string or number).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good.

func (mf *kustomizationFile) getFieldComments(content []byte) error {
buffer := bytes.NewBuffer(content)
var comments [][]byte
incomments := false
Copy link
Contributor

Choose a reason for hiding this comment

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

inComments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found inComments is not needed. Removed it.

@@ -30,9 +34,18 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/types"
)

var kustomizationFields = []string{"resources", "bases", "namePrefix", "namespace", "crds", "commonLabels", "commonAnnotations", "patches", "configMapGenerator", "secretGenerator", "vars", "imageTags"}

type fieldComment struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

please called this commentedField, and add a comment to the type saying something like field has to be a recognized kustomization field, but comment can be empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added.

fsys fs.FileSystem
path string
fsys fs.FileSystem
comments []fieldComment
Copy link
Contributor

Choose a reason for hiding this comment

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

Please call this originalFields

Copy link
Contributor

Choose a reason for hiding this comment

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

or something. i.e. these fields were present in the original file, as opposed to ones added later.

path string
fsys fs.FileSystem
comments []fieldComment
fields map[string]bool
Copy link
Contributor

@monopole monopole Aug 8, 2018

Choose a reason for hiding this comment

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

isn't this redundant? i.e.

 func (mf *kustomizationFile) hasField(name string) bool {
  for _, n := range mf.originalFields {
    if n.whatever == name {
     return true
    }
  }
  return false
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can do this by a for loop. I use a map to avoid this loop :)

Copy link
Contributor

Choose a reason for hiding this comment

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

two data structures instead of one introduces the possibility that they mean different things, and can get out of sync as code changes. performance not an issue here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -100,3 +120,73 @@ func stringInSlice(str string, list []string) bool {
}
return false
}

func (mf *kustomizationFile) getFieldComments(content []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

parseCommentedFields

(its not a getter)

}
output = append(output, content...)
}
for field, found := range mf.fields {
Copy link
Contributor

Choose a reason for hiding this comment

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

this could range over kustomizationFields and call hasField

if found {
continue
}
content, err := marshalField(field, kustomization)
Copy link
Contributor

Choose a reason for hiding this comment

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

can u add a comment on what happens if the field wasn't in the original file and wasn't added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it returns an empty []byte. Will also add this comment in the PR.

@Liujingfang1
Copy link
Contributor Author

Liujingfang1 commented Aug 8, 2018

Address comments and add change to preserve blank line. Aggregate inside field comments and move them to ahead of the field.

Add unit test.

@monopole PTAL

Copy link
Contributor

@monopole monopole left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 9, 2018
@monopole
Copy link
Contributor

monopole commented Aug 9, 2018

looks great, merging

@monopole monopole merged commit 9563052 into kubernetes-sigs:master Aug 9, 2018
@monopole
Copy link
Contributor

monopole commented Aug 9, 2018

time to release? and address #236?
maybe add a deprecation message on the namespace thing

@Liujingfang1
Copy link
Contributor Author

I will add a deprecation message. Then do the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants