mirror of https://github.com/k3s-io/k3s
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 printingpull/6/head
commit
34b31c8f32
|
@ -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
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -39,6 +40,7 @@ import (
|
|||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
)
|
||||
|
||||
func TestApplyExtraArgsFail(t *testing.T) {
|
||||
|
@ -433,6 +435,65 @@ func TestApplyObject(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestApplyObjectOutput(t *testing.T) {
|
||||
initTestErrorHandler(t)
|
||||
nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
|
||||
pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
|
||||
|
||||
// Add some extra data to the post-patch object
|
||||
postPatchObj := &unstructured.Unstructured{}
|
||||
if err := json.Unmarshal(currentRC, &postPatchObj.Object); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
postPatchLabels := postPatchObj.GetLabels()
|
||||
if postPatchLabels == nil {
|
||||
postPatchLabels = map[string]string{}
|
||||
}
|
||||
postPatchLabels["post-patch"] = "value"
|
||||
postPatchObj.SetLabels(postPatchLabels)
|
||||
postPatchData, err := json.Marshal(postPatchObj)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
f, tf, _, _ := cmdtesting.NewAPIFactory()
|
||||
tf.CommandPrinter = &printers.YAMLPrinter{}
|
||||
tf.GenericPrinter = true
|
||||
tf.UnstructuredClient = &fake.RESTClient{
|
||||
APIRegistry: api.Registry,
|
||||
NegotiatedSerializer: unstructuredSerializer,
|
||||
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
||||
switch p, m := req.URL.Path, req.Method; {
|
||||
case p == pathRC && m == "GET":
|
||||
bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
|
||||
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: bodyRC}, nil
|
||||
case p == pathRC && m == "PATCH":
|
||||
validatePatchApplication(t, req)
|
||||
bodyRC := ioutil.NopCloser(bytes.NewReader(postPatchData))
|
||||
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: bodyRC}, nil
|
||||
default:
|
||||
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||
return nil, nil
|
||||
}
|
||||
}),
|
||||
}
|
||||
tf.Namespace = "test"
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
errBuf := bytes.NewBuffer([]byte{})
|
||||
|
||||
cmd := NewCmdApply(f, buf, errBuf)
|
||||
cmd.Flags().Set("filename", filenameRC)
|
||||
cmd.Flags().Set("output", "yaml")
|
||||
cmd.Run(cmd, []string{})
|
||||
|
||||
if !strings.Contains(buf.String(), "name: test-rc") {
|
||||
t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "name: test-rc")
|
||||
}
|
||||
if !strings.Contains(buf.String(), "post-patch: value") {
|
||||
t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "post-patch: value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRetry(t *testing.T) {
|
||||
initTestErrorHandler(t)
|
||||
nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
|
||||
|
|
Loading…
Reference in New Issue