Merge pull request #66725 from juanvallejo/jvallejo/update-patch-return-code-logic

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

update exit code to 0 if patch not needed

**Release note**:
```release-note
The `kubectl patch` command no longer exits with exit code 1 when a redundant patch results in a no-op
```

The specific logic in the `patch` command that exited with code 1, was only doing so when there was no diff between an existing object and its patched counterpart. (In case of errors, we just return those, which eventually ends up exiting with code 1 anyway). This patch removes this block, as we should not be treating patch no-ops as errors.

Fixes https://github.com/kubernetes/kubernetes/issues/58212

cc @soltysh
pull/8/head
Kubernetes Submit Queue 2018-08-01 03:31:35 -07:00 committed by GitHub
commit 007bf90e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 9 deletions

View File

@ -245,14 +245,8 @@ func (o *PatchOptions) RunPatch() error {
if err != nil {
return err
}
printer.PrintObj(info.Object, o.Out)
// if object was not successfully patched, exit with error code 1
if !didPatch {
return cmdutil.ErrExit
}
return nil
return printer.PrintObj(info.Object, o.Out)
}
count++

View File

@ -435,8 +435,12 @@ run_pod_tests() {
## Patch can modify a local object
kubectl patch --local -f pkg/kubectl/validation/testdata/v1/validPod.yaml --patch='{"spec": {"restartPolicy":"Never"}}' -o yaml | grep -q "Never"
## Patch fails with error message "not patched" and exit code 1
output_message=$(! kubectl patch "${kube_flags[@]}" pod valid-pod -p='{"spec":{"replicas":7}}' 2>&1)
## Patch fails with type restore error and exit code 1
output_message=$(! kubectl patch "${kube_flags[@]}" pod valid-pod -p='{"metadata":{"labels":"invalid"}}' 2>&1)
kube::test::if_has_string "${output_message}" 'cannot restore map from string'
## Patch exits with error message "not patched" and exit code 0 when no-op occurs
output_message=$(kubectl patch "${kube_flags[@]}" pod valid-pod -p='{"metadata":{"labels":{"name":"valid-pod"}}}' 2>&1)
kube::test::if_has_string "${output_message}" 'not patched'
## Patch pod can change image