Skip to content

Commit a7f4976

Browse files
authored
Merge pull request #4362 from apostasie/2025-06-remove-lock
Simplify healthcheck file handling + fix locking
2 parents 0a5cb1d + 421872f commit a7f4976

File tree

2 files changed

+19
-58
lines changed

2 files changed

+19
-58
lines changed

pkg/healthcheck/log.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,22 @@ func writeHealthLog(ctx context.Context, container containerd.Container, result
4747
return fmt.Errorf("failed to marshal health log: %w", err)
4848
}
4949

50-
// Ensure file exists before writing
51-
if err := ensureHealthLogFile(stateDir); err != nil {
52-
return err
53-
}
54-
5550
// Write the latest result to the file
5651
logPath := filepath.Join(stateDir, HealthLogFilename)
57-
return filesystem.WithAppendLock(logPath, func(file *os.File) error {
58-
if _, err := file.Seek(0, io.SeekEnd); err != nil {
52+
return filesystem.WithLock(stateDir, func() error {
53+
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0o600)
54+
if err != nil {
55+
return err
56+
}
57+
defer file.Close()
58+
if _, err = file.Seek(0, io.SeekEnd); err != nil {
5959
return fmt.Errorf("seek error: %w", err)
6060
}
61-
if _, err := file.Write(append([]byte(data), '\n')); err != nil {
61+
if _, err = file.Write(append([]byte(data), '\n')); err != nil {
6262
return fmt.Errorf("failed to write health log: %w", err)
6363
}
64-
return nil
64+
65+
return file.Sync()
6566
})
6667
}
6768

@@ -182,23 +183,6 @@ func readHealthStateFromLabels(ctx context.Context, container containerd.Contain
182183
return state, nil
183184
}
184185

185-
// ensureHealthLogFile creates the health.json file if it doesn't exist.
186-
func ensureHealthLogFile(stateDir string) error {
187-
healthLogPath := filepath.Join(stateDir, HealthLogFilename)
188-
189-
// Ensure container state directory exists
190-
if _, err := os.Stat(stateDir); os.IsNotExist(err) {
191-
return fmt.Errorf("container state directory does not exist: %s", stateDir)
192-
}
193-
194-
// Create health.json if it doesn't exist
195-
if _, err := os.Stat(healthLogPath); os.IsNotExist(err) {
196-
return filesystem.WriteFile(healthLogPath, []byte{}, 0600)
197-
}
198-
199-
return nil
200-
}
201-
202186
// getContainerStateDir returns the container's state directory from labels.
203187
func getContainerStateDir(ctx context.Context, container containerd.Container) (string, error) {
204188
info, err := container.Info(ctx)

pkg/internal/filesystem/lock.go

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ import (
3737
// If Lock returns nil, no other process will be able to place a read or write lock on the file until
3838
// this process exits, closes f, or calls Unlock on it.
3939
func Lock(path string) (file *os.File, err error) {
40-
return commonlock(path, writeLock, false)
40+
return commonlock(path, writeLock)
4141
}
4242

4343
// ReadOnlyLock places an advisory read lock on the file, blocking until it can be locked.
4444
//
4545
// If ReadOnlyLock returns nil, no other process will be able to place a write lock on
4646
// the file until this process exits, closes f, or calls Unlock on it.
4747
func ReadOnlyLock(path string) (file *os.File, err error) {
48-
return commonlock(path, readLock, false)
48+
return commonlock(path, readLock)
4949
}
5050

51-
func commonlock(path string, mode lockType, appendMode bool) (file *os.File, err error) {
51+
func commonlock(path string, mode lockType) (file *os.File, err error) {
5252
defer func() {
5353
if err != nil {
5454
err = errors.Join(ErrLockFail, err, file.Close())
@@ -65,21 +65,12 @@ func commonlock(path string, mode lockType, appendMode bool) (file *os.File, err
6565
}
6666
}
6767

68-
// For append mode, open with specific flags
69-
if appendMode {
70-
file, err = os.OpenFile(path, os.O_WRONLY, privateFilePermission)
71-
if err != nil {
72-
return nil, err
73-
}
74-
} else {
75-
// Preserve the original behavior for non-append operations
76-
file, err = os.Open(path)
77-
if errors.Is(err, os.ErrNotExist) {
78-
file, err = os.OpenFile(path, os.O_RDONLY|os.O_CREATE, privateFilePermission)
79-
}
80-
if err != nil {
81-
return nil, err
82-
}
68+
file, err = os.Open(path)
69+
if errors.Is(err, os.ErrNotExist) {
70+
file, err = os.OpenFile(path, os.O_RDONLY|os.O_CREATE, privateFilePermission)
71+
}
72+
if err != nil {
73+
return nil, err
8374
}
8475

8576
if err = platformSpecificLock(file, mode); err != nil {
@@ -131,17 +122,3 @@ func WithReadOnlyLock(path string, function func() error) (err error) {
131122

132123
return function()
133124
}
134-
135-
// WithAppendLock executes the function with a file opened in append mode
136-
func WithAppendLock(path string, function func(file *os.File) error) (err error) {
137-
file, err := commonlock(path, writeLock, true)
138-
if err != nil {
139-
return err
140-
}
141-
142-
defer func() {
143-
err = errors.Join(Unlock(file), err)
144-
}()
145-
146-
return function(file)
147-
}

0 commit comments

Comments
 (0)