Skip to content

Commit

Permalink
Merge pull request #88739 from julianvmodesto/diff-server-side-field-…
Browse files Browse the repository at this point in the history
…manager

Set field manager for kubectl diff --server-side
  • Loading branch information
k8s-ci-robot authored Mar 10, 2020
2 parents 900143c + bacc2c4 commit 5877945
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
KUBECTL_EXTERNAL_DIFF environment variable can be used to select your own
diff command. By default, the "diff" command available in your path will be
run with "-u" (unified diff) and "-N" (treat absent files as empty) options.
Exit status:
0
No differences were found.
Expand Down Expand Up @@ -92,6 +92,7 @@ type DiffOptions struct {
FilenameOptions resource.FilenameOptions

ServerSideApply bool
FieldManager string
ForceConflicts bool

OpenAPISchema openapi.Resources
Expand Down Expand Up @@ -296,6 +297,7 @@ type InfoObject struct {
OpenAPI openapi.Resources
Force bool
ServerSideApply bool
FieldManager string
ForceConflicts bool
genericclioptions.IOStreams
}
Expand All @@ -316,8 +318,9 @@ func (obj InfoObject) Merged() (runtime.Object, error) {
return nil, err
}
options := metav1.PatchOptions{
Force: &obj.ForceConflicts,
DryRun: []string{metav1.DryRunAll},
Force: &obj.ForceConflicts,
FieldManager: obj.FieldManager,
DryRun: []string{metav1.DryRunAll},
}
return resource.NewHelper(obj.Info.Client, obj.Info.Mapping).Patch(
obj.Info.Namespace,
Expand Down Expand Up @@ -441,6 +444,7 @@ func (o *DiffOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
}

o.ServerSideApply = cmdutil.GetServerSideApplyFlag(cmd)
o.FieldManager = cmdutil.GetFieldManagerFlag(cmd)
o.ForceConflicts = cmdutil.GetForceConflictsFlag(cmd)
if o.ForceConflicts && !o.ServerSideApply {
return fmt.Errorf("--force-conflicts only works with --server-side")
Expand Down Expand Up @@ -529,6 +533,7 @@ func (o *DiffOptions) Run() error {
OpenAPI: o.OpenAPISchema,
Force: force,
ServerSideApply: o.ServerSideApply,
FieldManager: o.FieldManager,
ForceConflicts: o.ForceConflicts,
IOStreams: o.Diff.IOStreams,
}
Expand Down
8 changes: 8 additions & 0 deletions test/cmd/diff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ run_kubectl_diff_tests() {
# Test that it works when the live object doesn't exist
output_message=$(! kubectl diff -f hack/testdata/pod.yaml)
kube::test::if_has_string "${output_message}" 'test-pod'
# Ensure diff only dry-runs and doesn't persist change
kube::test::get_object_assert 'pod' "{{range.items}}{{ if eq ${id_field:?} \\\"test-pod\\\" }}found{{end}}{{end}}:" ':'

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

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

# Test found diff with server-side apply
kubectl apply -f hack/testdata/pod.yaml
output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml --server-side --force-conflicts || test $? -eq 1)
kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0'

# Test that we have a return code bigger than 1 if there is an error when diffing
kubectl diff -f hack/testdata/invalid-pod.yaml || test $? -gt 1

Expand Down

0 comments on commit 5877945

Please sign in to comment.