Skip to content

Commit

Permalink
OCPBUGS-16735: Truncate existing files when writing from inspect
Browse files Browse the repository at this point in the history
oc adm inspect generated files sometime have the leading "---"
and some time do not. This depends on the order of objects collected.
This by itself is not an issue.

However this becomes an issue when combined with multiple
invocations of oc adm inspect and collecting data to the same
directory like must-gather does.

If an object is collected multiple times then the second time
oc might overwrite the original file improperly and leave 4 bytes
of the original content behind.

This is happening when not writing the "---\n" in the second
invocation as this makes the content 4B shorter and the original
tailing 4B are left in the file intact.

This garbage confuses YAML parsers.

Signed-off-by: Martin Sivak <msivak@redhat.com>
  • Loading branch information
MarSik authored and openshift-cherrypick-robot committed Oct 10, 2023
1 parent f56bc26 commit 7b4162a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/cli/admin/inspect/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *resourceWriterReadCloser) Close() error {
type simpleFileWriter struct{}

func (f *simpleFileWriter) Write(filepath string, src fileWriterSource) error {
dest, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, 0755)
dest, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
Expand Down

0 comments on commit 7b4162a

Please sign in to comment.