Merge pull request #60990 from bmcstdio/bcustodio/kubectl-edit

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Ensure reasons end up as comments in kubectl edit.

**What this PR does / why we need it**:

This PR helps making sure that all text in the header of the file is prefixed with `#` when using `kubectl edit` to fix schema validation errors. The problem is best described with an example:

*Before:*

```yaml
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
# mycrd "example-cr" was not valid:
# * []: Invalid value: map[string]interface {}{"apiVersion":"example.com/v1alpha1", "kind":"MyCRD", "metadata":map[string]interface {}{"clusterName":"", "creationTimestamp":"2018-03-09T16:16:41Z", "name":"example-cr", "namespace":"default", "resourceVersion":"12399", "selfLink":"", "uid":"4089f5bd-23b5-11e8-a33f-42010aa40081", "generation":0}, "spec":map[string]interface {}{"size":-1}}: validation failure list:
spec.size in body should be greater than or equal to 1
#
apiVersion: example.com/v1alpha1
kind: MyCRD
```

This obviously won't be accepted as input, the user having to manually add a `#` to the line in the header.

*After:*

```yaml
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
# mycrd "example-cr" was not valid:
# * []: Invalid value: map[string]interface {}{"apiVersion":"example.com/v1alpha1", "kind":"MyCRD", "metadata":map[string]interface {}{"clusterName":"", "creationTimestamp":"2018-03-09T16:16:41Z", "name":"example-cr", "namespace":"default", "resourceVersion":"12399", "selfLink":"", "uid":"4089f5bd-23b5-11e8-a33f-42010aa40081", "generation":0}, "spec":map[string]interface {}{"size":-1}}: validation failure list:
# spec.size in body should be greater than or equal to 1
#
apiVersion: example.com/v1alpha1
kind: MyCRD
```

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
(None that I am aware of)

**Special notes for your reviewer**:
(None)

**Release note**:

```release-note
Ensure reasons end up as comments in `kubectl edit`.
```
This commit is contained in:
Kubernetes Submit Queue 2018-03-30 11:23:29 -07:00 committed by GitHub
commit a13dee2337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 4 deletions

View File

@ -41,7 +41,10 @@ go_library(
go_test(
name = "go_default_test",
srcs = ["editor_test.go"],
srcs = [
"editoptions_test.go",
"editor_test.go",
],
embed = [":go_default_library"],
)

View File

@ -657,12 +657,12 @@ func (h *editHeader) writeTo(w io.Writer, editMode EditMode) error {
for _, r := range h.reasons {
if len(r.other) > 0 {
fmt.Fprintf(w, "# %s:\n", r.head)
fmt.Fprintf(w, "# %s:\n", hashOnLineBreak(r.head))
} else {
fmt.Fprintf(w, "# %s\n", r.head)
fmt.Fprintf(w, "# %s\n", hashOnLineBreak(r.head))
}
for _, o := range r.other {
fmt.Fprintf(w, "# * %s\n", o)
fmt.Fprintf(w, "# * %s\n", hashOnLineBreak(o))
}
fmt.Fprintln(w, "#")
}
@ -738,3 +738,18 @@ func hasLines(r io.Reader) (bool, error) {
}
return false, nil
}
// hashOnLineBreak returns a string built from the provided string by inserting any necessary '#'
// characters after '\n' characters, indicating a comment.
func hashOnLineBreak(s string) string {
r := ""
for i, ch := range s {
j := i + 1
if j < len(s) && ch == '\n' && s[j] != '#' {
r += "\n# "
} else {
r += string(ch)
}
}
return r
}

View File

@ -0,0 +1,51 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package editor
import (
"testing"
)
func TestHashOnLineBreak(t *testing.T) {
tests := []struct {
original string
expected string
}{
{
original: "",
expected: "",
},
{
original: "\n",
expected: "\n",
},
{
original: "a\na\na\n",
expected: "a\n# a\n# a\n",
},
{
original: "a\n\n\na\n\n",
expected: "a\n# \n# \n# a\n# \n",
},
}
for _, test := range tests {
r := hashOnLineBreak(test.original)
if r != test.expected {
t.Errorf("expected: %s, saw: %s", test.expected, r)
}
}
}