From 683391522ebb7d2e6b7bf0c202233874b869a04a Mon Sep 17 00:00:00 2001 From: Dexter Yan Date: Tue, 11 Feb 2025 17:33:08 +1300 Subject: [PATCH] fix(window): improve rename file process and remove windows release (#1728) --- .github/workflows/build-test-deploy.yaml | 2 +- deploy/krew/preflight.yaml | 11 ------- deploy/krew/support-bundle.yaml | 11 ------- internal/util/util.go | 2 +- internal/util/util_test.go | 6 ---- pkg/collect/redact.go | 12 ++++++- pkg/collect/result.go | 6 +++- pkg/longhorn/util/iscsi_windows.go | 40 ------------------------ 8 files changed, 18 insertions(+), 72 deletions(-) delete mode 100644 pkg/longhorn/util/iscsi_windows.go diff --git a/.github/workflows/build-test-deploy.yaml b/.github/workflows/build-test-deploy.yaml index 13e846ea..07e38dec 100644 --- a/.github/workflows/build-test-deploy.yaml +++ b/.github/workflows/build-test-deploy.yaml @@ -187,7 +187,7 @@ jobs: strategy: matrix: goarch: [amd64, arm64] - goos: [darwin, linux, windows] + goos: [darwin, linux] include: - goarch: arm goos: linux diff --git a/deploy/krew/preflight.yaml b/deploy/krew/preflight.yaml index 5e5dad1e..f6ddb08f 100644 --- a/deploy/krew/preflight.yaml +++ b/deploy/krew/preflight.yaml @@ -60,17 +60,6 @@ spec: - from: LICENSE to: . bin: preflight - - selector: - matchLabels: - os: windows - arch: amd64 - {{addURIAndSha "https://github.com/replicatedhq/troubleshoot/releases/download/{{ .TagName }}/preflight_windows_amd64.zip" .TagName }} - files: - - from: preflight.exe - to: . - - from: LICENSE - to: . - bin: preflight.exe shortDescription: Executes application preflight tests in a cluster homepage: https://github.com/replicatedhq/troubleshoot description: | diff --git a/deploy/krew/support-bundle.yaml b/deploy/krew/support-bundle.yaml index 098eb29b..f7080b13 100644 --- a/deploy/krew/support-bundle.yaml +++ b/deploy/krew/support-bundle.yaml @@ -60,17 +60,6 @@ spec: - from: LICENSE to: . bin: support-bundle - - selector: - matchLabels: - os: windows - arch: amd64 - {{addURIAndSha "https://github.com/replicatedhq/troubleshoot/releases/download/{{ .TagName }}/support-bundle_windows_amd64.zip" .TagName }} - files: - - from: support-bundle.exe - to: . - - from: LICENSE - to: . - bin: support-bundle.exe shortDescription: Creates support bundles for off-cluster analysis homepage: https://github.com/replicatedhq/troubleshoot description: | diff --git a/internal/util/util.go b/internal/util/util.go index e10f8085..1ccb35e4 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -21,7 +21,7 @@ func HomeDir() string { if h := os.Getenv("HOME"); h != "" { return h } - return os.Getenv("USERPROFILE") // windows + return "" } func IsURL(str string) bool { diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 441134e0..ade0408f 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -21,12 +21,6 @@ func Test_HomeDir(t *testing.T) { dir: "/home/test", want: "/home/test", }, - { - name: "test windows home directory", - env: "USERPROFILE", - dir: `C:\Users\test`, - want: `C:\Users\test`, - }, } for _, tt := range tests { diff --git a/pkg/collect/redact.go b/pkg/collect/redact.go index e86cab0b..095729de 100644 --- a/pkg/collect/redact.go +++ b/pkg/collect/redact.go @@ -40,6 +40,7 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors defer func() { <-limitCh }() // free up after the function execution has run var reader io.Reader + var readerCloseFn func() error // Function to close reader if needed if data == nil { // Collected contents are in a file. Get a reader to the file. @@ -83,12 +84,13 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors errorCh <- errors.Wrap(err, "failed to get reader") return } - defer r.Close() reader = r + readerCloseFn = r.Close // Ensure we close the file later } else { // Collected contents are in memory. Get a reader to the memory buffer. reader = bytes.NewBuffer(data) + readerCloseFn = func() error { return nil } // No-op for in-memory data } // If the file is .tar, .tgz or .tar.gz, it must not be redacted. Instead it is @@ -106,6 +108,14 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors errorCh <- errors.Wrap(err, "failed to decompress file") return } + + // Ensure the reader is closed after processing + if err := readerCloseFn(); err != nil { + klog.Warningf("Failed to close reader for %s: %v", file, err) + errorCh <- errors.Wrap(err, "failed to close reader") + return + } + err = RedactResult(tmpDir, subResult, additionalRedactors) if err != nil { errorCh <- errors.Wrap(err, "failed to redact file") diff --git a/pkg/collect/result.go b/pkg/collect/result.go index 427741d5..80e76faf 100644 --- a/pkg/collect/result.go +++ b/pkg/collect/result.go @@ -188,17 +188,21 @@ func (r CollectorResult) ReplaceResult(bundlePath string, relativePath string, r return nil } + // Create a temporary file in the same directory as the target file to prevent cross-device issues tmpFile, err := os.CreateTemp("", "replace-") if err != nil { return errors.Wrap(err, "failed to create temp file") } - defer tmpFile.Close() + // Write data to the temporary file _, err = io.Copy(tmpFile, reader) if err != nil { return errors.Wrap(err, "failed to write tmp file") } + // Close the file to ensure all data is written + tmpFile.Close() + // This rename should always be in /tmp, so no cross-partition copying will happen err = os.Rename(tmpFile.Name(), filepath.Join(bundlePath, relativePath)) if err != nil { diff --git a/pkg/longhorn/util/iscsi_windows.go b/pkg/longhorn/util/iscsi_windows.go deleted file mode 100644 index d9efaefe..00000000 --- a/pkg/longhorn/util/iscsi_windows.go +++ /dev/null @@ -1,40 +0,0 @@ -//go:build windows -// +build windows - -package util - -import ( - "github.com/pkg/errors" -) - -func GetDiskInfo(directory string) (*DiskInfo, error) { - return nil, errors.Errorf("cannot get disk info of directory %v", directory) -} - -func RemoveHostDirectoryContent(directory string) error { - return errors.Errorf("failed to remove host directory %v", directory) -} - -func CopyHostDirectoryContent(src, dest string) error { - return errors.Errorf("failed to copy the content from %v to %v for the host", src, dest) -} - -func CreateDiskPathReplicaSubdirectory(path string) error { - return errors.Errorf("error creating data path %v on host", path) -} - -func ExpandFileSystem(volumeName string) error { - return errors.Errorf("error expanding filsystem on %v", volumeName) -} - -func DetectFileSystem(volumeName string) (string, error) { - return "", errors.Errorf("error detecting filsystem on %v", volumeName) -} - -func GetDiskConfig(path string) (*DiskConfig, error) { - return nil, errors.Errorf("error getting disk config from %v", path) -} - -func GenerateDiskConfig(path string) (*DiskConfig, error) { - return nil, errors.Errorf("error generating disk config from %v", path) -}