Skip to content

Commit 5877945

Browse files
authored
Merge pull request kubernetes#88739 from julianvmodesto/diff-server-side-field-manager
Set field manager for kubectl diff --server-side
2 parents 900143c + bacc2c4 commit 5877945

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var (
5757
KUBECTL_EXTERNAL_DIFF environment variable can be used to select your own
5858
diff command. By default, the "diff" command available in your path will be
5959
run with "-u" (unified diff) and "-N" (treat absent files as empty) options.
60-
60+
6161
Exit status:
6262
0
6363
No differences were found.
@@ -92,6 +92,7 @@ type DiffOptions struct {
9292
FilenameOptions resource.FilenameOptions
9393

9494
ServerSideApply bool
95+
FieldManager string
9596
ForceConflicts bool
9697

9798
OpenAPISchema openapi.Resources
@@ -296,6 +297,7 @@ type InfoObject struct {
296297
OpenAPI openapi.Resources
297298
Force bool
298299
ServerSideApply bool
300+
FieldManager string
299301
ForceConflicts bool
300302
genericclioptions.IOStreams
301303
}
@@ -316,8 +318,9 @@ func (obj InfoObject) Merged() (runtime.Object, error) {
316318
return nil, err
317319
}
318320
options := metav1.PatchOptions{
319-
Force: &obj.ForceConflicts,
320-
DryRun: []string{metav1.DryRunAll},
321+
Force: &obj.ForceConflicts,
322+
FieldManager: obj.FieldManager,
323+
DryRun: []string{metav1.DryRunAll},
321324
}
322325
return resource.NewHelper(obj.Info.Client, obj.Info.Mapping).Patch(
323326
obj.Info.Namespace,
@@ -441,6 +444,7 @@ func (o *DiffOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
441444
}
442445

443446
o.ServerSideApply = cmdutil.GetServerSideApplyFlag(cmd)
447+
o.FieldManager = cmdutil.GetFieldManagerFlag(cmd)
444448
o.ForceConflicts = cmdutil.GetForceConflictsFlag(cmd)
445449
if o.ForceConflicts && !o.ServerSideApply {
446450
return fmt.Errorf("--force-conflicts only works with --server-side")
@@ -529,6 +533,7 @@ func (o *DiffOptions) Run() error {
529533
OpenAPI: o.OpenAPISchema,
530534
Force: force,
531535
ServerSideApply: o.ServerSideApply,
536+
FieldManager: o.FieldManager,
532537
ForceConflicts: o.ForceConflicts,
533538
IOStreams: o.Diff.IOStreams,
534539
}

test/cmd/diff.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ run_kubectl_diff_tests() {
2929
# Test that it works when the live object doesn't exist
3030
output_message=$(! kubectl diff -f hack/testdata/pod.yaml)
3131
kube::test::if_has_string "${output_message}" 'test-pod'
32+
# Ensure diff only dry-runs and doesn't persist change
33+
kube::test::get_object_assert 'pod' "{{range.items}}{{ if eq ${id_field:?} \\\"test-pod\\\" }}found{{end}}{{end}}:" ':'
3234

3335
kubectl apply -f hack/testdata/pod.yaml
36+
kube::test::get_object_assert 'pod' "{{range.items}}{{ if eq ${id_field:?} \\\"test-pod\\\" }}found{{end}}{{end}}:" 'found:'
3437

3538
# Make sure that diffing the resource right after returns nothing (0 exit code).
3639
kubectl diff -f hack/testdata/pod.yaml
@@ -41,6 +44,11 @@ run_kubectl_diff_tests() {
4144
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml || test $? -eq 1)
4245
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
4346

47+
# Test found diff with server-side apply
48+
kubectl apply -f hack/testdata/pod.yaml
49+
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml --server-side --force-conflicts || test $? -eq 1)
50+
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'
51+
4452
# Test that we have a return code bigger than 1 if there is an error when diffing
4553
kubectl diff -f hack/testdata/invalid-pod.yaml || test $? -gt 1
4654

0 commit comments

Comments
 (0)