Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 8 additions & 37 deletions health.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
package main

// File-based healthcheck for distroless containers, delegating to
// github.com/cplieger/health.
// github.com/cplieger/health. The library handles degraded mode (a
// read-only marker dir) internally, so this app keeps only thin wrappers.

import (
"fmt"
"os"
"path/filepath"
import "github.com/cplieger/health"

"github.com/cplieger/health"
)

// healthMarkerPath is the default marker location.
// healthMarkerPath is the marker location, sourced from the library.
const healthMarkerPath = health.DefaultPath

// healthMarker wraps *health.Marker to preserve the internal API used
// healthMarker aliases *health.Marker to preserve the internal API used
// by main.go and tests.
type healthMarker struct {
*health.Marker
degraded bool
}
type healthMarker = health.Marker

// newHealthMarker constructs a marker and detects degraded mode.
// newHealthMarker constructs a marker for path.
func newHealthMarker(path string) *healthMarker {
m := health.NewMarker(path)
degraded := probeHealthDir(path) != nil
return &healthMarker{Marker: m, degraded: degraded}
return health.NewMarker(path)
}

// runProbe delegates to health.RunProbe (calls os.Exit).
Expand All @@ -37,22 +27,3 @@ func runProbe(path string) {
func probeCheck(path string) int {
return health.ProbeCheck(path)
}

// probeHealthDir verifies the marker's parent directory is writable by
// creating and deleting a temp file.
func probeHealthDir(path string) error {
dir := filepath.Dir(path)
f, err := os.CreateTemp(dir, ".health-probe-*")
if err != nil {
return err
}
name := f.Name()
if closeErr := f.Close(); closeErr != nil {
_ = os.Remove(name)
return fmt.Errorf("close probe: %w", closeErr)
}
if rmErr := os.Remove(name); rmErr != nil {
return fmt.Errorf("remove probe: %w", rmErr)
}
return nil
}
28 changes: 15 additions & 13 deletions health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"path/filepath"
"testing"

"github.com/cplieger/health"
"pgregory.net/rapid"
)

Expand Down Expand Up @@ -52,15 +53,16 @@
}

path := filepath.Join(dir, ".healthy")
m := newHealthMarker(path)

if !m.degraded {
// Some environments (root, permissive filesystems like Windows
// or containers) allow writes through 0500; skip rather than
// fail in those cases.
// Some environments (root, permissive filesystems like Windows
// or containers) allow writes through 0500; skip rather than
// fail in those cases.
if health.ProbeDir(path) == nil {

Check failure on line 60 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 60 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 60 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 60 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 60 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir
t.Skip("test environment bypasses directory mode; skipping")
}

m := newHealthMarker(path)

m.Set(true)
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatalf("degraded marker should never create file: %v", err)
Expand Down Expand Up @@ -119,12 +121,12 @@
})
}

// TestProbeHealthDir_Writable confirms the probe succeeds on a normal
// TestProbeDir_Writable confirms the probe succeeds on a normal
// writable temp dir and leaves no artifact behind.
func TestProbeHealthDir_Writable(t *testing.T) {
func TestProbeDir_Writable(t *testing.T) {
dir := t.TempDir()
if err := probeHealthDir(filepath.Join(dir, ".healthy")); err != nil {
t.Fatalf("probeHealthDir on writable dir: %v", err)
if err := health.ProbeDir(filepath.Join(dir, ".healthy")); err != nil {

Check failure on line 128 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 128 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 128 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 128 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 128 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir
t.Fatalf("health.ProbeDir on writable dir: %v", err)
}

entries, err := os.ReadDir(dir)
Expand All @@ -136,10 +138,10 @@
}
}

// TestProbeHealthDir_NonExistent confirms a missing parent directory is
// TestProbeDir_NonExistent confirms a missing parent directory is
// reported as an error rather than masked.
func TestProbeHealthDir_NonExistent(t *testing.T) {
err := probeHealthDir(filepath.Join(t.TempDir(), "nope", ".healthy"))
func TestProbeDir_NonExistent(t *testing.T) {
err := health.ProbeDir(filepath.Join(t.TempDir(), "nope", ".healthy"))

Check failure on line 144 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 144 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 144 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 144 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 144 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir
if err == nil {
t.Fatal("expected error for non-existent parent dir")
}
Expand Down Expand Up @@ -178,7 +180,7 @@
path := filepath.Join(dir, ".healthy")

// If the OS ignores directory mode (root, Windows), skip.
if probeHealthDir(path) == nil {
if health.ProbeDir(path) == nil {

Check failure on line 183 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 183 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 183 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 183 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir

Check failure on line 183 in health_test.go

View workflow job for this annotation

GitHub Actions / ci / go / validate

undefined: health.ProbeDir (typecheck)
t.Skip("test environment bypasses directory mode; skipping")
}

Expand Down
Loading