-
Notifications
You must be signed in to change notification settings - Fork 82
TRT-2580: Revert "OCPBUGS-78557: Fix admin context race condition in parallel tests" #1258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,73 +20,36 @@ import ( | |
| e2e "k8s.io/kubernetes/test/e2e/framework" | ||
| ) | ||
|
|
||
| // duplicateFileToPathSafe copies a file without using Gomega (safe for initialization phase). | ||
| // Returns error instead of using Expect, suitable for calling in var initialization. | ||
| func duplicateFileToPathSafe(srcPath, destPath string) error { | ||
| // DuplicateFileToPath copies a file from source path to destination path with error handling | ||
| // Parameters: | ||
| // - srcPath: path to the source file to copy | ||
| // - destPath: path where the file will be copied (created with mode 0666 if not exists, truncated if exists) | ||
| func DuplicateFileToPath(srcPath string, destPath string) { | ||
| // Validate and clean paths to prevent path traversal attacks | ||
| srcPath = filepath.Clean(srcPath) | ||
| destPath = filepath.Clean(destPath) | ||
|
|
||
| // Ensure source file exists and is readable | ||
| srcInfo, err := os.Stat(srcPath) | ||
| o.Expect(err).NotTo(o.HaveOccurred(), "Source file does not exist or is not accessible") | ||
| o.Expect(srcInfo.Mode().IsRegular()).To(o.BeTrue(), "Source path is not a regular file") | ||
|
|
||
| srcFile, err := os.Open(srcPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open source file: %w", err) | ||
| } | ||
| defer func() { _ = srcFile.Close() }() | ||
| o.Expect(err).NotTo(o.HaveOccurred()) | ||
| defer func() { | ||
| o.Expect(srcFile.Close()).NotTo(o.HaveOccurred()) | ||
| }() | ||
|
|
||
| // Create destination file with restrictive permissions (0644) | ||
| destFile, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create destination file: %w", err) | ||
| } | ||
| defer func() { _ = destFile.Close() }() | ||
|
|
||
| if _, err := io.Copy(destFile, srcFile); err != nil { | ||
| return fmt.Errorf("failed to copy file: %w", err) | ||
| } | ||
|
|
||
| return destFile.Sync() | ||
| } | ||
|
|
||
| // DuplicateFileToTemp creates a temporary duplicate of a file with a specified prefix | ||
| // Safe version for initialization phase (returns error instead of using Gomega) | ||
| func duplicateFileToTempSafe(srcPath, destPrefix string) (string, error) { | ||
| if srcPath == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| destFile, err := os.CreateTemp(os.TempDir(), destPrefix) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to create temporary file: %w", err) | ||
| } | ||
| destPath := destFile.Name() | ||
| _ = destFile.Close() | ||
|
|
||
| if err := duplicateFileToPathSafe(srcPath, destPath); err != nil { | ||
| _ = os.Remove(destPath) // best-effort cleanup on error | ||
| return "", err | ||
| } | ||
|
|
||
| return destPath, nil | ||
| } | ||
|
|
||
| // duplicateFileToTempOrEmpty creates a temporary duplicate of a file, returns empty string on error. | ||
| // Logs warning on failure. Safe for initialization phase (doesn't use Gomega). | ||
| func duplicateFileToTempOrEmpty(srcPath, destPrefix string) string { | ||
| tempPath, err := duplicateFileToTempSafe(srcPath, destPrefix) | ||
| if err != nil { | ||
| e2e.Logf("Warning: failed to create temporary admin config: %v", err) | ||
| return "" | ||
| } | ||
| return tempPath | ||
| } | ||
| o.Expect(err).NotTo(o.HaveOccurred()) | ||
| defer func() { | ||
| o.Expect(destFile.Close()).NotTo(o.HaveOccurred()) | ||
| }() | ||
|
|
||
| // DuplicateFileToPath copies a file from source path to destination path with error handling | ||
| // Parameters: | ||
| // - srcPath: path to the source file to copy | ||
| // - destPath: path where the file will be copied (created with mode 0666 if not exists, truncated if exists) | ||
| // | ||
| // Uses Gomega assertions - only call in test execution phase (It, BeforeEach, etc.) | ||
| func DuplicateFileToPath(srcPath string, destPath string) { | ||
| err := duplicateFileToPathSafe(srcPath, destPath) | ||
| o.Expect(err).NotTo(o.HaveOccurred(), "Failed to duplicate file from %s to %s", srcPath, destPath) | ||
| _, err = io.Copy(destFile, srcFile) | ||
| o.Expect(err).NotTo(o.HaveOccurred()) | ||
| o.Expect(destFile.Sync()).NotTo(o.HaveOccurred()) | ||
| } | ||
|
|
||
| // DuplicateFileToTemp creates a temporary duplicate of a file with a specified prefix | ||
|
|
@@ -96,11 +59,13 @@ func DuplicateFileToPath(srcPath string, destPath string) { | |
| // | ||
| // Returns: | ||
| // - string: path to the created temporary file | ||
| // | ||
| // Uses Gomega assertions - only call in test execution phase (It, BeforeEach, etc.) | ||
| func DuplicateFileToTemp(srcPath string, destPrefix string) string { | ||
| destPath, err := duplicateFileToTempSafe(srcPath, destPrefix) | ||
| o.Expect(err).NotTo(o.HaveOccurred(), "Failed to duplicate file %s to temp", srcPath) | ||
| destFile, err := os.CreateTemp(os.TempDir(), destPrefix) | ||
| o.Expect(err).NotTo(o.HaveOccurred(), "Failed to create temporary file") | ||
| o.Expect(destFile.Close()).NotTo(o.HaveOccurred(), "Failed to close temporary file") | ||
|
|
||
| destPath := destFile.Name() | ||
| DuplicateFileToPath(srcPath, destPath) | ||
|
Comment on lines
+63
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid close-then-reopen on temp files (TOCTOU window). Line 65 closes the temp file, and Line 68 reopens by path via Suggested fix func DuplicateFileToTemp(srcPath string, destPrefix string) string {
- destFile, err := os.CreateTemp(os.TempDir(), destPrefix)
+ destFile, err := os.CreateTemp(os.TempDir(), destPrefix)
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to create temporary file")
- o.Expect(destFile.Close()).NotTo(o.HaveOccurred(), "Failed to close temporary file")
-
- destPath := destFile.Name()
- DuplicateFileToPath(srcPath, destPath)
+ srcFile, err := os.Open(filepath.Clean(srcPath))
+ o.Expect(err).NotTo(o.HaveOccurred(), "Failed to open source file")
+ defer func() { o.Expect(srcFile.Close()).NotTo(o.HaveOccurred()) }()
+
+ _, err = io.Copy(destFile, srcFile)
+ o.Expect(err).NotTo(o.HaveOccurred(), "Failed to copy source file to temporary file")
+ o.Expect(destFile.Sync()).NotTo(o.HaveOccurred(), "Failed to sync temporary file")
+ o.Expect(destFile.Close()).NotTo(o.HaveOccurred(), "Failed to close temporary file")
+
+ destPath := destFile.Name()
return destPath
}🤖 Prompt for AI Agents |
||
| return destPath | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reintroduces shared admin-kubeconfig race risk in parallel specs.
These lines route admin and run-time kubeconfig selection back to shared
adminConfigPath/configPathsemantics. That restores the original shared mutable kubeconfig surface (the race from OCPBUGS-78557), so context-mutatingoccalls can interfere across parallel tests again.Please keep the revert if needed for the current blocker, but preserve per-test/admin-isolated kubeconfig files (or equivalent immutable context handling) in the re-landing fix so both issues are addressed together.
Also applies to: 618-619, 674-674
🤖 Prompt for AI Agents