Add calls to set annotation for kubectl apply

pull/6/head
jackgr 2015-09-10 14:32:57 -07:00
parent 703a3e19aa
commit ef3f64612e
8 changed files with 83 additions and 10 deletions

View File

@ -58,8 +58,8 @@ func SetOriginalConfiguration(info *resource.Info, original []byte) error {
return nil return nil
} }
// Get the current annotations from the object. accessor := info.Mapping.MetadataAccessor
annotations, err := info.Mapping.MetadataAccessor.Annotations(info.Object) annotations, err := accessor.Annotations(info.Object)
if err != nil { if err != nil {
return err return err
} }
@ -162,3 +162,18 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool) ([]byte, error
return modified, nil return modified, nil
} }
// UpdateApplyAnnotation gets the modified configuration of the object,
// without embedding it again, and then sets it on the object as the annotation.
func UpdateApplyAnnotation(info *resource.Info) error {
modified, err := GetModifiedConfiguration(info, false)
if err != nil {
return err
}
if err := SetOriginalConfiguration(info, modified); err != nil {
return err
}
return nil
}

View File

@ -29,8 +29,8 @@ import (
"k8s.io/kubernetes/pkg/util/strategicpatch" "k8s.io/kubernetes/pkg/util/strategicpatch"
) )
// ApplyOptions stores cmd.Flag values for apply. As new fields are added, add them here instead of // ApplyOptions stores cmd.Flag values for apply. As new fields are added,
// referencing the cmd.Flags() // add them here instead of referencing the cmd.Flags()
type ApplyOptions struct { type ApplyOptions struct {
Filenames []string Filenames []string
} }

View File

@ -106,14 +106,23 @@ func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *C
if err != nil { if err != nil {
return err return err
} }
// Update the annotation used by kubectl apply
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err)
}
// Serialize the object with the annotation applied.
data, err := info.Mapping.Codec.Encode(info.Object) data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil { if err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err) return cmdutil.AddSourceToErr("creating", info.Source, err)
} }
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data) obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
if err != nil { if err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err) return cmdutil.AddSourceToErr("creating", info.Source, err)
} }
count++ count++
info.Refresh(obj, true) info.Refresh(obj, true)
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name" shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"

View File

@ -210,6 +210,11 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
return preservedFile(err, file, out) return preservedFile(err, file, out)
} }
// annotate the edited object for kubectl apply
if err := kubectl.UpdateApplyAnnotation(updates); err != nil {
return preservedFile(err, file, out)
}
visitor := resource.NewFlattenListVisitor(updates, rmap) visitor := resource.NewFlattenListVisitor(updates, rmap)
// need to make sure the original namespace wasn't changed while editing // need to make sure the original namespace wasn't changed while editing

View File

@ -214,10 +214,17 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
if cmdutil.GetFlagBool(cmd, "dry-run") { if cmdutil.GetFlagBool(cmd, "dry-run") {
fmt.Fprintln(out, "running in dry-run mode...") fmt.Fprintln(out, "running in dry-run mode...")
} else { } else {
data, err := info.Mapping.Codec.Encode(object) // Serialize the configuration into an annotation.
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return err
}
// Serialize the object with the annotation applied.
data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil { if err != nil {
return err return err
} }
object, err = resource.NewHelper(info.Client, info.Mapping).Create(namespace, false, data) object, err = resource.NewHelper(info.Client, info.Mapping).Create(namespace, false, data)
if err != nil { if err != nil {
return err return err

View File

@ -127,14 +127,23 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
if err != nil { if err != nil {
return err return err
} }
// Serialize the configuration into an annotation.
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return err
}
// Serialize the object with the annotation applied.
data, err := info.Mapping.Codec.Encode(info.Object) data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil { if err != nil {
return cmdutil.AddSourceToErr("replacing", info.Source, err) return cmdutil.AddSourceToErr("replacing", info.Source, err)
} }
obj, err := resource.NewHelper(info.Client, info.Mapping).Replace(info.Namespace, info.Name, true, data) obj, err := resource.NewHelper(info.Client, info.Mapping).Replace(info.Namespace, info.Name, true, data)
if err != nil { if err != nil {
return cmdutil.AddSourceToErr("replacing", info.Source, err) return cmdutil.AddSourceToErr("replacing", info.Source, err)
} }
info.Refresh(obj, true) info.Refresh(obj, true)
printObjectSpecificMessage(obj, out) printObjectSpecificMessage(obj, out)
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "replaced") cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "replaced")
@ -211,14 +220,23 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
if err != nil { if err != nil {
return err return err
} }
// Serialize the configuration into an annotation.
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return err
}
// Serialize the object with the annotation applied.
data, err := info.Mapping.Codec.Encode(info.Object) data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil { if err != nil {
return err return err
} }
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data) obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
if err != nil { if err != nil {
return err return err
} }
count++ count++
info.Refresh(obj, true) info.Refresh(obj, true)
printObjectSpecificMessage(obj, out) printObjectSpecificMessage(obj, out)

View File

@ -184,10 +184,23 @@ func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cob
// TODO: extract this flag to a central location, when such a location exists. // TODO: extract this flag to a central location, when such a location exists.
if !cmdutil.GetFlagBool(cmd, "dry-run") { if !cmdutil.GetFlagBool(cmd, "dry-run") {
data, err := mapping.Codec.Encode(obj) resourceMapper := &resource.Mapper{ObjectTyper: typer, RESTMapper: mapper, ClientMapper: f.ClientMapperForCommand()}
info, err := resourceMapper.InfoForObject(obj)
if err != nil { if err != nil {
return err return err
} }
// Serialize the configuration into an annotation.
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return err
}
// Serialize the object with the annotation applied.
data, err := mapping.Codec.Encode(info.Object)
if err != nil {
return err
}
obj, err = resource.NewHelper(client, mapping).Create(namespace, false, data) obj, err = resource.NewHelper(client, mapping).Create(namespace, false, data)
if err != nil { if err != nil {
return err return err

View File

@ -34,6 +34,7 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned" client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd" "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
utilerrors "k8s.io/kubernetes/pkg/util/errors" utilerrors "k8s.io/kubernetes/pkg/util/errors"
@ -404,18 +405,23 @@ func DumpReaderToFile(reader io.Reader, filename string) error {
func UpdateObject(info *resource.Info, updateFn func(runtime.Object) error) (runtime.Object, error) { func UpdateObject(info *resource.Info, updateFn func(runtime.Object) error) (runtime.Object, error) {
helper := resource.NewHelper(info.Client, info.Mapping) helper := resource.NewHelper(info.Client, info.Mapping)
err := updateFn(info.Object) if err := updateFn(info.Object); err != nil {
if err != nil {
return nil, err return nil, err
} }
// Update the annotation used by kubectl apply
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return nil, err
}
data, err := helper.Codec.Encode(info.Object) data, err := helper.Codec.Encode(info.Object)
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = helper.Replace(info.Namespace, info.Name, true, data) if _, err := helper.Replace(info.Namespace, info.Name, true, data); err != nil {
if err != nil {
return nil, err return nil, err
} }
return info.Object, nil return info.Object, nil
} }