diff --git a/internal/pods/pods.go b/internal/pods/pods.go index 2ad20b2..9d5a858 100644 --- a/internal/pods/pods.go +++ b/internal/pods/pods.go @@ -94,24 +94,48 @@ func (m *Manager) hasMount(name, dest string) bool { // Used to roll out a new pod image: a mismatch triggers an idle recreate so // members pick up added tooling without losing their home volume. A blank or // unresolvable image name is treated as a match (never heal on uncertainty). +// +// It compares image IDs, not just names: setup.sh rebuilds the pod image under +// the same tag (localhost/agentbbs-pod:latest), so a name-only check never saw +// a rebuild and existing pods kept the old image forever (new tooling such as +// c0mpute never reached them). func (m *Manager) hasImage(name, image string) bool { if image == "" { return true } - out, err := exec.Command(m.engine, "container", "inspect", "-f", "{{.ImageName}}", name).Output() + out, err := exec.Command(m.engine, "container", "inspect", "-f", "{{.ImageName}}|{{.Image}}", name).Output() if err != nil { return true } - got := strings.TrimSpace(string(out)) - // Normalize: inspect may report "localhost/agentbbs-pod:latest" while m.image - // is the same; also tolerate the docker.io/library/ prefix podman adds. + gotName, gotID, _ := strings.Cut(strings.TrimSpace(string(out)), "|") + wantID := "" + if out, err := exec.Command(m.engine, "image", "inspect", "-f", "{{.Id}}", image).Output(); err == nil { + wantID = strings.TrimSpace(string(out)) + } + return sameImage(gotName, gotID, image, wantID) +} + +// sameImage decides whether a container (gotName, gotID) runs the wanted image +// (wantName, wantID). Names must match after normalization; when both IDs are +// known they must match too. An unknown ID is not a mismatch. +func sameImage(gotName, gotID, wantName, wantID string) bool { + // Normalize: inspect may report "localhost/agentbbs-pod:latest" while the + // wanted name is the same; also tolerate the docker.io/library/ prefix + // podman adds. norm := func(s string) string { s = strings.TrimPrefix(s, "docker.io/library/") s = strings.TrimPrefix(s, "docker.io/") s = strings.TrimPrefix(s, "localhost/") return s } - return norm(got) == norm(image) + if norm(gotName) != norm(wantName) { + return false + } + id := func(s string) string { return strings.TrimPrefix(strings.TrimSpace(s), "sha256:") } + if id(gotID) == "" || id(wantID) == "" { + return true + } + return id(gotID) == id(wantID) } // agentDir is the host directory bind-mounted into a pod at /run/agentbbs-agent, diff --git a/internal/pods/pods_test.go b/internal/pods/pods_test.go index 2422d0a..a91beff 100644 --- a/internal/pods/pods_test.go +++ b/internal/pods/pods_test.go @@ -35,3 +35,21 @@ func TestPublicHTMLMountCreatesAndMaps(t *testing.T) { t.Fatalf("expected %q to be a created directory, err=%v", wantHost, err) } } + +func TestSameImage(t *testing.T) { + cases := []struct { + gotName, gotID, wantName, wantID string + want bool + }{ + {"localhost/agentbbs-pod:latest", "62a86171b60a", "localhost/agentbbs-pod:latest", "dc18f4df01f5", false}, // rebuilt under the same tag + {"localhost/agentbbs-pod:latest", "dc18f4df01f5", "agentbbs-pod:latest", "sha256:dc18f4df01f5", true}, + {"docker.io/library/ubuntu:24.04", "abc", "localhost/agentbbs-pod:latest", "abc", false}, // different image + {"localhost/agentbbs-pod:latest", "", "localhost/agentbbs-pod:latest", "dc18", true}, // unknown id: no heal + {"localhost/agentbbs-pod:latest", "dc18", "localhost/agentbbs-pod:latest", "", true}, + } + for _, c := range cases { + if got := sameImage(c.gotName, c.gotID, c.wantName, c.wantID); got != c.want { + t.Errorf("sameImage(%q,%q,%q,%q) = %v, want %v", c.gotName, c.gotID, c.wantName, c.wantID, got, c.want) + } + } +}