mirror of https://github.com/k3s-io/k3s
Merge pull request #14626 from jackgr/apply_annotation
Add calls to set annotation for kubectl applypull/6/head
commit
be3fc9007a
|
@ -58,8 +58,8 @@ func SetOriginalConfiguration(info *resource.Info, original []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Get the current annotations from the object.
|
||||
annotations, err := info.Mapping.MetadataAccessor.Annotations(info.Object)
|
||||
accessor := info.Mapping.MetadataAccessor
|
||||
annotations, err := accessor.Annotations(info.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -162,3 +162,18 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool) ([]byte, error
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -29,8 +29,8 @@ import (
|
|||
"k8s.io/kubernetes/pkg/util/strategicpatch"
|
||||
)
|
||||
|
||||
// ApplyOptions stores cmd.Flag values for apply. As new fields are added, add them here instead of
|
||||
// referencing the cmd.Flags()
|
||||
// ApplyOptions stores cmd.Flag values for apply. As new fields are added,
|
||||
// add them here instead of referencing the cmd.Flags()
|
||||
type ApplyOptions struct {
|
||||
Filenames []string
|
||||
}
|
||||
|
|
|
@ -106,14 +106,23 @@ func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *C
|
|||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
||||
}
|
||||
|
||||
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
|
||||
if err != nil {
|
||||
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
||||
}
|
||||
|
||||
count++
|
||||
info.Refresh(obj, true)
|
||||
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
|
|
|
@ -210,6 +210,11 @@ func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
|
|||
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)
|
||||
|
||||
// need to make sure the original namespace wasn't changed while editing
|
||||
|
|
|
@ -214,10 +214,17 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
|||
if cmdutil.GetFlagBool(cmd, "dry-run") {
|
||||
fmt.Fprintln(out, "running in dry-run mode...")
|
||||
} 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 {
|
||||
return err
|
||||
}
|
||||
|
||||
object, err = resource.NewHelper(info.Client, info.Mapping).Create(namespace, false, data)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -127,14 +127,23 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
|
|||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||
}
|
||||
|
||||
obj, err := resource.NewHelper(info.Client, info.Mapping).Replace(info.Namespace, info.Name, true, data)
|
||||
if err != nil {
|
||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||
}
|
||||
|
||||
info.Refresh(obj, true)
|
||||
printObjectSpecificMessage(obj, out)
|
||||
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 {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
count++
|
||||
info.Refresh(obj, true)
|
||||
printObjectSpecificMessage(obj, out)
|
||||
|
|
|
@ -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.
|
||||
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 {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -34,6 +34,7 @@ import (
|
|||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
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) {
|
||||
helper := resource.NewHelper(info.Client, info.Mapping)
|
||||
|
||||
err := updateFn(info.Object)
|
||||
if err != nil {
|
||||
if err := updateFn(info.Object); err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = helper.Replace(info.Namespace, info.Name, true, data)
|
||||
if err != nil {
|
||||
if _, err := helper.Replace(info.Namespace, info.Name, true, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return info.Object, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue