Merge pull request #41699 from liggitt/apply-output-result

Automatic merge from submit-queue (batch tested with PRs 40746, 41699, 42108, 42174, 42093)

Output result of apply operation

Fixes #41690 

Plumbs the resulting object from patch operations back to the top level so it can be output when printing
This commit is contained in:
Kubernetes Submit Queue
2017-02-28 07:51:22 -08:00
committed by GitHub
2 changed files with 88 additions and 23 deletions

View File

@@ -308,19 +308,23 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
gracePeriod: options.GracePeriod,
}
patchBytes, err := patcher.patch(info.Object, modified, info.Source, info.Namespace, info.Name)
patchBytes, patchedObject, err := patcher.patch(info.Object, modified, info.Source, info.Namespace, info.Name)
if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("applying patch:\n%s\nto:\n%v\nfor:", patchBytes, info), info.Source, err)
}
if cmdutil.ShouldRecord(cmd, info) {
if patch, patchType, err := cmdutil.ChangeResourcePatch(info, f.Command(cmd, true)); err == nil {
if _, err = helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil {
if recordedObject, err := helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil {
glog.V(4).Infof("error recording reason: %v", err)
} else {
patchedObject = recordedObject
}
}
}
info.Refresh(patchedObject, true)
if uid, err := info.Mapping.UID(info.Object); err != nil {
return err
} else {
@@ -549,17 +553,17 @@ type patcher struct {
gracePeriod int
}
func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, namespace, name string) ([]byte, error) {
func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, namespace, name string) ([]byte, runtime.Object, error) {
// Serialize the current configuration of the object from the server.
current, err := runtime.Encode(p.encoder, obj)
if err != nil {
return nil, cmdutil.AddSourceToErr(fmt.Sprintf("serializing current configuration from:\n%v\nfor:", obj), source, err)
return nil, nil, cmdutil.AddSourceToErr(fmt.Sprintf("serializing current configuration from:\n%v\nfor:", obj), source, err)
}
// Retrieve the original configuration of the object from the annotation.
original, err := kubectl.GetOriginalConfiguration(p.mapping, obj)
if err != nil {
return nil, cmdutil.AddSourceToErr(fmt.Sprintf("retrieving original configuration from:\n%v\nfor:", obj), source, err)
return nil, nil, cmdutil.AddSourceToErr(fmt.Sprintf("retrieving original configuration from:\n%v\nfor:", obj), source, err)
}
// Create the versioned struct from the type defined in the restmapping
@@ -577,48 +581,48 @@ func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, names
patch, err = jsonmergepatch.CreateThreeWayJSONMergePatch(original, modified, current, preconditions...)
if err != nil {
if mergepatch.IsPreconditionFailed(err) {
return nil, fmt.Errorf("%s", "At least one of apiVersion, kind and name was changed")
return nil, nil, fmt.Errorf("%s", "At least one of apiVersion, kind and name was changed")
}
return nil, cmdutil.AddSourceToErr(fmt.Sprintf(createPatchErrFormat, original, modified, current), source, err)
return nil, nil, cmdutil.AddSourceToErr(fmt.Sprintf(createPatchErrFormat, original, modified, current), source, err)
}
case err != nil:
return nil, cmdutil.AddSourceToErr(fmt.Sprintf("getting instance of versioned object for %v:", p.mapping.GroupVersionKind), source, err)
return nil, nil, cmdutil.AddSourceToErr(fmt.Sprintf("getting instance of versioned object for %v:", p.mapping.GroupVersionKind), source, err)
case err == nil:
// Compute a three way strategic merge patch to send to server.
patchType = types.StrategicMergePatchType
patch, err = strategicpatch.CreateThreeWayMergePatch(original, modified, current, versionedObject, p.overwrite)
if err != nil {
return nil, cmdutil.AddSourceToErr(fmt.Sprintf(createPatchErrFormat, original, modified, current), source, err)
return nil, nil, cmdutil.AddSourceToErr(fmt.Sprintf(createPatchErrFormat, original, modified, current), source, err)
}
}
_, err = p.helper.Patch(namespace, name, patchType, patch)
return patch, err
patchedObj, err := p.helper.Patch(namespace, name, patchType, patch)
return patch, patchedObj, err
}
func (p *patcher) patch(current runtime.Object, modified []byte, source, namespace, name string) ([]byte, error) {
func (p *patcher) patch(current runtime.Object, modified []byte, source, namespace, name string) ([]byte, runtime.Object, error) {
var getErr error
patchBytes, err := p.patchSimple(current, modified, source, namespace, name)
patchBytes, patchObject, err := p.patchSimple(current, modified, source, namespace, name)
for i := 1; i <= maxPatchRetry && errors.IsConflict(err); i++ {
if i > triesBeforeBackOff {
p.backOff.Sleep(backOffPeriod)
}
current, getErr = p.helper.Get(namespace, name, false)
if getErr != nil {
return nil, getErr
return nil, nil, getErr
}
patchBytes, err = p.patchSimple(current, modified, source, namespace, name)
patchBytes, patchObject, err = p.patchSimple(current, modified, source, namespace, name)
}
if err != nil && p.force {
patchBytes, err = p.deleteAndCreate(modified, namespace, name)
patchBytes, patchObject, err = p.deleteAndCreate(modified, namespace, name)
}
return patchBytes, err
return patchBytes, patchObject, err
}
func (p *patcher) deleteAndCreate(modified []byte, namespace, name string) ([]byte, error) {
func (p *patcher) deleteAndCreate(modified []byte, namespace, name string) ([]byte, runtime.Object, error) {
err := p.delete(namespace, name)
if err != nil {
return modified, err
return modified, nil, err
}
err = wait.PollImmediate(kubectl.Interval, p.timeout, func() (bool, error) {
if _, err := p.helper.Get(namespace, name, false); !errors.IsNotFound(err) {
@@ -627,12 +631,12 @@ func (p *patcher) deleteAndCreate(modified []byte, namespace, name string) ([]by
return true, nil
})
if err != nil {
return modified, err
return modified, nil, err
}
versionedObject, _, err := p.decoder.Decode(modified, nil, nil)
if err != nil {
return modified, err
return modified, nil, err
}
_, err = p.helper.Create(namespace, true, versionedObject)
return modified, err
createdObject, err := p.helper.Create(namespace, true, versionedObject)
return modified, createdObject, err
}