Merge pull request #15980 from janetkuo/kubectl-edit-updateapplyannotation

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot 2015-10-24 02:48:22 -07:00
commit 6d10b76b11
3 changed files with 173 additions and 121 deletions

View File

@ -910,6 +910,26 @@ __EOF__
kube::test::get_object_assert 'rc mock2' "{{${labels_field}.status}}" 'replaced'
fi
fi
# Command: kubectl edit multiple resources
temp_editor="${KUBE_TEMP}/tmp-editor.sh"
echo -e '#!/bin/bash\nsed -i "s/status\:\ replaced/status\:\ edited/g" $@' > "${temp_editor}"
chmod +x "${temp_editor}"
EDITOR="${temp_editor}" kubectl edit "${kube_flags[@]}" -f "${file}"
# Post-condition: mock service (and mock2) and mock rc (and mock2) are edited
if [ "$has_svc" = true ]; then
kube::test::get_object_assert 'services mock' "{{${labels_field}.status}}" 'edited'
if [ "$two_svcs" = true ]; then
kube::test::get_object_assert 'services mock2' "{{${labels_field}.status}}" 'edited'
fi
fi
if [ "$has_rc" = true ]; then
kube::test::get_object_assert 'rc mock' "{{${labels_field}.status}}" 'edited'
if [ "$two_rcs" = true ]; then
kube::test::get_object_assert 'rc mock2' "{{${labels_field}.status}}" 'edited'
fi
fi
# cleaning
rm "${temp_editor}"
# Command
# We need to set --overwrite, because otherwise, if the first attempt to run "kubectl label"
# fails on some, but not all, of the resources, retries will fail because it tries to modify

View File

@ -402,6 +402,10 @@ func (a genericAccessor) Annotations() map[string]string {
}
func (a genericAccessor) SetAnnotations(annotations map[string]string) {
if a.annotations == nil {
emptyAnnotations := make(map[string]string)
a.annotations = &emptyAnnotations
}
*a.annotations = annotations
}

View File

@ -145,11 +145,12 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
defaultVersion := cmdutil.OutputVersion(cmd, clientConfig.Version)
results := editResults{}
for {
obj, err := resource.AsVersionedObject(infos, false, defaultVersion)
objs, err := resource.AsVersionedObjects(infos, defaultVersion)
if err != nil {
return preservedFile(err, results.file, out)
}
// if input object is a list, traverse and edit each item one at a time
for _, obj := range objs {
// TODO: add an annotating YAML printer that can print inline comments on each field,
// including descriptions or validation errors
@ -176,19 +177,19 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
}
glog.V(4).Infof("User edited:\n%s", string(edited))
fmt.Printf("User edited:\n%s", string(edited))
lines, err := hasLines(bytes.NewBuffer(edited))
if err != nil {
return preservedFile(err, file, out)
}
if bytes.Equal(original, edited) {
// Compare content without comments
if bytes.Equal(stripComments(original), stripComments(edited)) {
if len(results.edit) > 0 {
preservedFile(nil, file, out)
} else {
os.Remove(file)
}
fmt.Fprintln(out, "Edit cancelled, no changes made.")
return nil
continue
}
if !lines {
if len(results.edit) > 0 {
@ -197,7 +198,7 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
os.Remove(file)
}
fmt.Fprintln(out, "Edit cancelled, saved file was empty.")
return nil
continue
}
results = editResults{
@ -207,7 +208,7 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
// parse the edited file
updates, err := rmap.InfoForData(edited, "edited-file")
if err != nil {
return preservedFile(err, file, out)
return fmt.Errorf("The edited file had a syntax error: %v", err)
}
// annotate the edited object for kubectl apply
@ -278,6 +279,9 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
} else {
fmt.Fprintf(out, "The edits you made on deleted resources have been saved to %q\n", file)
}
}
}
if len(results.edit) == 0 {
return nil
}
@ -395,3 +399,27 @@ func hasLines(r io.Reader) (bool, error) {
}
return false, nil
}
// stripComments will transform a YAML file into JSON, thus dropping any comments
// in it. Note that if the given file has a syntax error, the transformation will
// fail and we will manually drop all comments from the file.
func stripComments(file []byte) []byte {
stripped, err := yaml.ToJSON(file)
if err != nil {
stripped = manualStrip(file)
}
return stripped
}
// manualStrip is used for dropping comments from a YAML file
func manualStrip(file []byte) []byte {
stripped := []byte{}
for _, line := range bytes.Split(file, []byte("\n")) {
if bytes.HasPrefix(bytes.TrimSpace(line), []byte("#")) {
continue
}
stripped = append(stripped, line...)
stripped = append(stripped, '\n')
}
return stripped
}