Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/distribution/reference"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -61,8 +62,9 @@ func NewPolicyInterceptorProvider(image, eventsAddress, consoleURL string, defau
}
}

// OwnImage returns the controller's own container image, pinned by digest when
// the kubelet reports one, read from its Pod status (PLAT-PGP-09.01). The
// OwnImage returns the controller's own container image, read from its Pod
// status, pinned by digest when the kubelet reports a digest for the same
// repository as the spec image (PLAT-PGP-09.01, PLAT-PGP-09.03). The
// interceptor sidecar runs this exact build.
func OwnImage(ctx context.Context, kube kubernetes.Interface, namespace, podName, container string) (string, error) {
if podName == "" {
Expand All @@ -75,6 +77,17 @@ func OwnImage(ctx context.Context, kube kubernetes.Interface, namespace, podName
return imageFromPod(pod, container)
}

// imageFromPod returns the container's image for the interceptor sidecar.
//
// 1. A "repository@sha256:..." image ID whose repository matches the spec
// image's repository pins the exact build (PLAT-PGP-09.01).
// 2. Any other image ID keeps the spec image (PLAT-PGP-09.03). This covers a
// bare digest, and a placeholder repository such as the
// "docker.io/library/import-<date>" name containerd gives an image loaded
// from an archive (Kind with the podman provider), which no registry serves.
//
// Repositories are compared after normalization, so "quay.io/x/cp:latest"
// matches "quay.io/x/cp@sha256:..." and "cp" matches "docker.io/library/cp".
func imageFromPod(pod *corev1.Pod, container string) (string, error) {
spec := ""
for _, c := range pod.Spec.Containers {
Expand All @@ -90,15 +103,31 @@ func imageFromPod(pod *corev1.Pod, container string) (string, error) {
continue
}
id := strings.TrimPrefix(st.ImageID, "docker-pullable://")
// A repository@sha256 imageID pins the exact build. A bare digest
// (locally loaded Kind images) has no repository; keep the spec image.
if at := strings.Index(id, "@sha256:"); at > 0 {
at := strings.Index(id, "@sha256:")
if at <= 0 {
continue
}
if sameRepository(id[:at], spec) {
return id, nil
}
}
return spec, nil
}

// sameRepository reports whether two image references name the same
// normalized repository. An unparsable reference never matches.
func sameRepository(a, b string) bool {
ra, err := reference.ParseNormalizedNamed(a)
if err != nil {
return false
}
rb, err := reference.ParseNormalizedNamed(b)
if err != nil {
return false
}
return ra.Name() == rb.Name()
}

// SetPolicyInterceptor enables the policy interceptor sidecar on every gateway
// this reconciler deploys. defaults is the source the sidecar values read; the
// reconciler also compares its hash with the rendered pod template, so a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ func TestSidecarUsesControllerOwnImageDigest(t *testing.T) {
}
}

// Verifies: PLAT-PGP-09.01, PLAT-PGP-09.03
func TestOwnImagePinsDigestOnlyForTheSpecRepository(t *testing.T) {
cases := []struct {
name, spec, imageID, want string
}{
{"matching registry repo gets the digest pin", "quay.io/x/cp:latest", "quay.io/x/cp@sha256:0123", "quay.io/x/cp@sha256:0123"},
{"docker-pullable prefix is stripped", "quay.io/x/cp:latest", "docker-pullable://quay.io/x/cp@sha256:0123", "quay.io/x/cp@sha256:0123"},
{"familiar and normalized names match", "cp:dev", "docker.io/library/cp@sha256:0123", "docker.io/library/cp@sha256:0123"},
{"archive-loaded import name falls back to the spec image", "localhost/hypershell-controller:dev", "docker.io/library/import-2026-11-26@sha256:9ff3", "localhost/hypershell-controller:dev"},
{"other repository falls back to the spec image", "quay.io/x/cp:latest", "quay.io/y/cp@sha256:0123", "quay.io/x/cp:latest"},
{"bare digest falls back to the spec image", "quay.io/x/cp:latest", "sha256:0123", "quay.io/x/cp:latest"},
{"empty image ID falls back to the spec image", "quay.io/x/cp:latest", "", "quay.io/x/cp:latest"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "hypershell-controller-0"},
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "controller", Image: tc.spec}}},
Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{
Name: "controller", ImageID: tc.imageID,
}}},
}
got, err := imageFromPod(pod, "controller")
if err != nil || got != tc.want {
t.Fatalf("own image = %q, %v; want %q", got, err, tc.want)
}
})
}
}

// Verifies: PLAT-PGP-08.03, CTRL-CORE-22.04
func TestSidecarDefaultsComeFromPlatformPolicySettings(t *testing.T) {
settings := &fakeSettingsClient{defaults: settingsDefaults("750m")}
Expand Down
15 changes: 14 additions & 1 deletion specs/platform/platform-gateway-policy.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ built from `components/control-plane/cmd/` and shipped in the existing
control-plane image; no new container image and no release-bundle component SHALL
be added. The controller SHALL run the sidecar using its **own** image digest,
read from its own Pod status, so the controller and the interceptor always run the
same build and no image reference needs configuration. The sidecar SHALL run as a
same build and no image reference needs configuration. The controller SHALL pin
by digest only when the reported image ID names the same repository as its
container spec image; otherwise (a bare digest, or a placeholder repository such
as the `import-<date>` name containerd gives an image loaded from an archive) it
SHALL use the spec image unchanged. The sidecar SHALL run as a
native sidecar (an init container with `restartPolicy: Always`) in each gateway
pod, so it is ready before the gateway calls `Describe`. The gateway SHALL reach
the sidecar over a `unix://` socket on a pod-local `emptyDir`. The sidecar SHALL
Expand All @@ -414,6 +418,15 @@ gateway JWKS and SHALL hold no HyperShell credentials for request evaluation.
- THEN the sidecar SHALL verify it against the local gateway JWKS before evaluating
- AND SHALL reject a call whose JWT does not verify

#### Scenario: PLAT-PGP-09.03 -- Unpullable Image ID Falls Back to the Spec Image

- GIVEN the kubelet reports a controller image ID that is a bare digest, or whose
repository differs from the controller's spec image (for example
`docker.io/library/import-2026-11-26@sha256:...` for an archive-loaded image)
- WHEN the controller renders the gateway pod
- THEN the policy interceptor sidecar SHALL use the controller's spec image
- AND SHALL NOT use the image ID's repository

---

### Requirement: PLAT-PGP-10 -- Static Bindings, Dynamic Policies
Expand Down
Loading