From 6ba5cc760ba201fb622dd73fb72975c0461fbea9 Mon Sep 17 00:00:00 2001 From: Angel Marin Date: Thu, 26 Nov 2026 21:28:55 +0100 Subject: [PATCH] fix(control-plane): pin interceptor image by digest only for the spec repository The policy interceptor sidecar took any repo@sha256 image ID from the controller's Pod status as its image. On Kind with the podman provider the kubelet reports archive-loaded images as docker.io/library/import-@sha256, a name no registry serves, so the sidecar hit ImagePullBackOff and gateways never became ready. Use the image ID's digest pin only when its normalized repository matches the spec image's repository; otherwise keep the spec image (PLAT-PGP-09.03). Assisted-by: Claude Opus 5.5 --- .../reconciler/policy_interceptor_provider.go | 39 ++++++++++++++++--- .../policy_interceptor_provider_test.go | 30 ++++++++++++++ .../platform/platform-gateway-policy.spec.md | 15 ++++++- 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/components/control-plane/internal/reconciler/policy_interceptor_provider.go b/components/control-plane/internal/reconciler/policy_interceptor_provider.go index 8c7700a8b..79a9410cb 100644 --- a/components/control-plane/internal/reconciler/policy_interceptor_provider.go +++ b/components/control-plane/internal/reconciler/policy_interceptor_provider.go @@ -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" @@ -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 == "" { @@ -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-" 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 { @@ -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 diff --git a/components/control-plane/internal/reconciler/policy_interceptor_provider_test.go b/components/control-plane/internal/reconciler/policy_interceptor_provider_test.go index bfe7adf30..a237b3b03 100644 --- a/components/control-plane/internal/reconciler/policy_interceptor_provider_test.go +++ b/components/control-plane/internal/reconciler/policy_interceptor_provider_test.go @@ -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")} diff --git a/specs/platform/platform-gateway-policy.spec.md b/specs/platform/platform-gateway-policy.spec.md index c82f4b8fb..afa09714c 100644 --- a/specs/platform/platform-gateway-policy.spec.md +++ b/specs/platform/platform-gateway-policy.spec.md @@ -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-` 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 @@ -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