Skip to content

Commit cf06546

Browse files
Merge pull request #26877 from Honny1/build-speedup
refactor: Modularize binding build functions
2 parents 737108b + c70c0ac commit cf06546

File tree

3 files changed

+487
-241
lines changed

3 files changed

+487
-241
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package remote_build_helpers
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"github.com/sirupsen/logrus"
10+
)
11+
12+
// TempFileManager manages temporary files created during image build.
13+
// It maintains a list of created temporary files and provides cleanup functionality
14+
// to ensure proper resource management.
15+
type TempFileManager struct {
16+
files []string
17+
}
18+
19+
func NewTempFileManager() *TempFileManager {
20+
return &TempFileManager{}
21+
}
22+
23+
func (t *TempFileManager) AddFile(filename string) {
24+
t.files = append(t.files, filename)
25+
}
26+
27+
func (t *TempFileManager) Cleanup() {
28+
for _, file := range t.files {
29+
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
30+
logrus.Errorf("Failed to remove temp file %s: %v", file, err)
31+
}
32+
}
33+
t.files = t.files[:0] // Reset slice
34+
}
35+
36+
// CreateTempFileFromReader creates a temporary file in the specified destination directory
37+
// with the given pattern, and copies content from the provided reader into the file.
38+
// The created temporary file is automatically added to the manager's cleanup list.
39+
//
40+
// Parameters:
41+
// - dest: The directory where the temporary file should be created
42+
// - pattern: The pattern for naming the temporary file
43+
// - reader: The io.Reader from which to read content to write into the temporary file
44+
//
45+
// Returns:
46+
// - string: The path to the created temporary file
47+
// - error: Any error encountered during the operation
48+
func (t *TempFileManager) CreateTempFileFromReader(dest string, pattern string, reader io.Reader) (string, error) {
49+
tmpFile, err := os.CreateTemp(dest, pattern)
50+
if err != nil {
51+
return "", fmt.Errorf("creating temp file: %w", err)
52+
}
53+
defer tmpFile.Close()
54+
55+
t.AddFile(tmpFile.Name())
56+
57+
if _, err := io.Copy(tmpFile, reader); err != nil {
58+
return "", fmt.Errorf("copying stdin content: %w", err)
59+
}
60+
return tmpFile.Name(), nil
61+
}
62+
63+
// CreateTempSecret creates a temporary copy of a secret file in the specified
64+
// context directory. The original secret file is copied to a new temporary file
65+
// which is automatically added to the manager's cleanup list.
66+
//
67+
// Parameters:
68+
// - secretPath: The path to the source secret file to copy
69+
// - contextDir: The directory where the temporary secret file should be created
70+
//
71+
// Returns:
72+
// - string: The path to the created temporary secret file
73+
// - error: Any error encountered during the operation
74+
func (t *TempFileManager) CreateTempSecret(secretPath, contextDir string) (string, error) {
75+
secretFile, err := os.Open(secretPath)
76+
if err != nil {
77+
return "", fmt.Errorf("opening secret file %s: %w", secretPath, err)
78+
}
79+
defer secretFile.Close()
80+
81+
return t.CreateTempFileFromReader(contextDir, "podman-build-secret-*", secretFile)
82+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package remote_build_helpers
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestTempFileManager(t *testing.T) {
13+
manager := NewTempFileManager()
14+
15+
t.Run("CreateTempFileFromReader", func(t *testing.T) {
16+
content := "test content"
17+
r := strings.NewReader(content)
18+
19+
filename, err := manager.CreateTempFileFromReader("", "podman-build-stdin-*", r)
20+
assert.NoError(t, err)
21+
assert.FileExists(t, filename)
22+
23+
data, err := os.ReadFile(filename)
24+
assert.NoError(t, err)
25+
assert.Equal(t, content, string(data))
26+
27+
manager.Cleanup()
28+
29+
assert.NoFileExists(t, filename)
30+
})
31+
32+
t.Run("CreateTempSecret", func(t *testing.T) {
33+
tempdir := t.TempDir()
34+
secretPath := filepath.Join(tempdir, "secret.txt")
35+
36+
content := "test secret"
37+
err := os.WriteFile(secretPath, []byte(content), 0600)
38+
assert.NoError(t, err)
39+
40+
filename, err := manager.CreateTempSecret(secretPath, tempdir)
41+
assert.NoError(t, err)
42+
assert.FileExists(t, filename)
43+
44+
data, err := os.ReadFile(filename)
45+
assert.NoError(t, err)
46+
assert.Equal(t, content, string(data))
47+
48+
manager.Cleanup()
49+
50+
assert.NoFileExists(t, filename)
51+
})
52+
}

0 commit comments

Comments
 (0)