Skip to content

Commit 7444c1e

Browse files
Ensure diff doesn't persist patches
1 parent ff809a5 commit 7444c1e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/apply/patcher.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,9 @@ func newPatcher(o *ApplyOptions, info *resource.Info) (*Patcher, error) {
8080
openapiSchema = o.OpenAPISchema
8181
}
8282

83-
helper := resource.NewHelper(info.Client, info.Mapping)
84-
if o.DryRunStrategy == cmdutil.DryRunServer {
85-
if err := o.DryRunVerifier.HasSupport(info.Mapping.GroupVersionKind); err != nil {
86-
return nil, err
87-
}
88-
helper.DryRun(true)
89-
}
9083
return &Patcher{
9184
Mapping: info.Mapping,
92-
Helper: helper,
85+
Helper: resource.NewHelper(info.Client, info.Mapping),
9386
DynamicClient: o.DynamicClient,
9487
Overwrite: o.Overwrite,
9588
BackOff: clockwork.NewRealClock(),
@@ -185,7 +178,7 @@ func (p *Patcher) patchSimple(obj runtime.Object, modified []byte, source, names
185178
}
186179
}
187180

188-
patchedObj, err := p.Helper.Patch(namespace, name, patchType, patch, nil)
181+
patchedObj, err := p.Helper.DryRun(p.ServerDryRun).Patch(namespace, name, patchType, patch, nil)
189182
return patch, patchedObj, err
190183
}
191184

@@ -230,11 +223,11 @@ func (p *Patcher) deleteAndCreate(original runtime.Object, modified []byte, name
230223
if err != nil {
231224
return modified, nil, err
232225
}
233-
createdObject, err := p.Helper.Create(namespace, true, versionedObject)
226+
createdObject, err := p.Helper.DryRun(p.ServerDryRun).Create(namespace, true, versionedObject)
234227
if err != nil {
235228
// restore the original object if we fail to create the new one
236229
// but still propagate and advertise error to user
237-
recreated, recreateErr := p.Helper.Create(namespace, true, original)
230+
recreated, recreateErr := p.Helper.DryRun(p.ServerDryRun).Create(namespace, true, original)
238231
if recreateErr != nil {
239232
err = fmt.Errorf("An error occurred force-replacing the existing object with the newly provided one:\n\n%v.\n\nAdditionally, an error occurred attempting to restore the original object:\n\n%v", err, recreateErr)
240233
} else {

staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ func (obj InfoObject) Live() runtime.Object {
312312
// Returns the "merged" object, as it would look like if applied or
313313
// created.
314314
func (obj InfoObject) Merged() (runtime.Object, error) {
315+
helper := resource.NewHelper(obj.Info.Client, obj.Info.Mapping).DryRun(true)
315316
if obj.ServerSideApply {
316317
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj.LocalObj)
317318
if err != nil {
@@ -320,9 +321,8 @@ func (obj InfoObject) Merged() (runtime.Object, error) {
320321
options := metav1.PatchOptions{
321322
Force: &obj.ForceConflicts,
322323
FieldManager: obj.FieldManager,
323-
DryRun: []string{metav1.DryRunAll},
324324
}
325-
return resource.NewHelper(obj.Info.Client, obj.Info.Mapping).Patch(
325+
return helper.Patch(
326326
obj.Info.Namespace,
327327
obj.Info.Name,
328328
types.ApplyPatchType,
@@ -334,11 +334,11 @@ func (obj InfoObject) Merged() (runtime.Object, error) {
334334
// Build the patcher, and then apply the patch with dry-run, unless the object doesn't exist, in which case we need to create it.
335335
if obj.Live() == nil {
336336
// Dry-run create if the object doesn't exist.
337-
return resource.NewHelper(obj.Info.Client, obj.Info.Mapping).CreateWithOptions(
337+
return helper.CreateWithOptions(
338338
obj.Info.Namespace,
339339
true,
340340
obj.LocalObj,
341-
&metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}},
341+
&metav1.CreateOptions{},
342342
)
343343
}
344344

@@ -361,7 +361,7 @@ func (obj InfoObject) Merged() (runtime.Object, error) {
361361
// We plan on replacing this with server-side apply when it becomes available.
362362
patcher := &apply.Patcher{
363363
Mapping: obj.Info.Mapping,
364-
Helper: resource.NewHelper(obj.Info.Client, obj.Info.Mapping),
364+
Helper: helper,
365365
Overwrite: true,
366366
BackOff: clockwork.NewRealClock(),
367367
ServerDryRun: true,

test/cmd/diff.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,34 @@ run_kubectl_diff_tests() {
3434

3535
kubectl apply -f hack/testdata/pod.yaml
3636
kube::test::get_object_assert 'pod' "{{range.items}}{{ if eq ${id_field:?} \\\"test-pod\\\" }}found{{end}}{{end}}:" 'found:'
37+
initialResourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
3738

3839
# Make sure that diffing the resource right after returns nothing (0 exit code).
3940
kubectl diff -f hack/testdata/pod.yaml
4041

42+
# Ensure diff only dry-runs and doesn't persist change
43+
resourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
44+
kube::test::if_has_string "${resourceVersion}" "${initialResourceVersion}"
45+
4146
# Make sure that:
4247
# 1. the exit code for diff is 1 because it found a difference
4348
# 2. the difference contains the changed image
4449
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml || test $? -eq 1)
4550
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
4651

52+
# Ensure diff only dry-runs and doesn't persist change
53+
resourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
54+
kube::test::if_has_string "${resourceVersion}" "${initialResourceVersion}"
55+
4756
# Test found diff with server-side apply
4857
kubectl apply -f hack/testdata/pod.yaml
4958
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml --server-side --force-conflicts || test $? -eq 1)
5059
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
5160

61+
# Ensure diff --server-side only dry-runs and doesn't persist change
62+
resourceVersion=$(kubectl get "${kube_flags[@]:?}" -f hack/testdata/pod.yaml -o go-template='{{ .metadata.resourceVersion }}')
63+
kube::test::if_has_string "${resourceVersion}" "${initialResourceVersion}"
64+
5265
# Test that we have a return code bigger than 1 if there is an error when diffing
5366
kubectl diff -f hack/testdata/invalid-pod.yaml || test $? -gt 1
5467

0 commit comments

Comments
 (0)