|
| 1 | +// Copyright 2026 The Sigstore Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package e2e |
| 16 | + |
| 17 | +import ( |
| 18 | + "os/exec" |
| 19 | + "strings" |
| 20 | + "time" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" //nolint:revive |
| 23 | + . "github.com/onsi/gomega" //nolint:revive |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + |
| 27 | + "github.com/sigstore/model-validation-operator/internal/constants" |
| 28 | + utils "github.com/sigstore/model-validation-operator/test/utils" |
| 29 | +) |
| 30 | + |
| 31 | +const continuousValidationTestBatch = "continuous-validation" |
| 32 | + |
| 33 | +var _ = Describe("ContinuousValidation lifecycle", func() { |
| 34 | + It("should inject a native sidecar that passes its probes, re-validates at the configured interval "+ |
| 35 | + "and terminates cleanly", func() { |
| 36 | + podData := utils.DefaultPodData("continuous-validation-pod", webhookTestNamespace, |
| 37 | + "continuous-validation-model", continuousValidationTestBatch) |
| 38 | + crData := utils.CRTemplateData{ModelName: podData.ModelName, Namespace: podData.Namespace} |
| 39 | + // Delete the pod before its ModelValidation so the webhook can still |
| 40 | + // admit the pod update that removes the tracking finalizer. |
| 41 | + DeferCleanup(func() { |
| 42 | + Expect(utils.KubectlDelete(podTemplate, &utils.KubectlDeleteOptions{ |
| 43 | + TemplateData: podData, IgnoreNotFound: true, Timeout: "90s", |
| 44 | + })).To(Succeed()) |
| 45 | + Expect(utils.KubectlDelete(modelValidationContinuousTemplate, &utils.KubectlDeleteOptions{ |
| 46 | + TemplateData: crData, IgnoreNotFound: true, Timeout: "60s", |
| 47 | + })).To(Succeed()) |
| 48 | + }) |
| 49 | + Expect(utils.KubectlApply(modelValidationContinuousTemplate, crData)).To(Succeed()) |
| 50 | + Expect(utils.KubectlApply(podTemplate, podData)).To(Succeed()) |
| 51 | + |
| 52 | + By("verifying the native sidecar contract") |
| 53 | + var uid types.UID |
| 54 | + Eventually(func(g Gomega) { |
| 55 | + var pod corev1.Pod |
| 56 | + g.Expect(utils.KubectlGetJSON("pod", podData.PodName, webhookTestNamespace, &pod)).To(Succeed()) |
| 57 | + g.Expect(pod.Annotations).To(HaveKeyWithValue(constants.ContinuousValidationAnnotationKey, "true")) |
| 58 | + g.Expect(pod.Spec.InitContainers).To(HaveLen(1)) |
| 59 | + sidecar := pod.Spec.InitContainers[0] |
| 60 | + g.Expect(sidecar.Name).To(Equal(constants.ModelValidationInitContainerName)) |
| 61 | + g.Expect(sidecar.RestartPolicy).To(HaveValue(Equal(corev1.ContainerRestartPolicyAlways))) |
| 62 | + g.Expect(sidecar.Args).To(ContainElement("--interval=1m")) |
| 63 | + g.Expect(sidecar.ReadinessProbe).NotTo(BeNil()) |
| 64 | + g.Expect(sidecar.LivenessProbe).NotTo(BeNil()) |
| 65 | + uid = pod.UID |
| 66 | + }, 30*time.Second, time.Second).Should(Succeed()) |
| 67 | + |
| 68 | + By("waiting for the sidecar and pod to become Ready after the initial validation") |
| 69 | + expectContinuousReadiness(podData.PodName, uid) |
| 70 | + |
| 71 | + By("waiting for a successful periodic re-validation after the one-minute interval") |
| 72 | + Eventually(func(g Gomega) { |
| 73 | + logs, err := utils.Run(exec.Command("kubectl", "logs", podData.PodName, "-n", webhookTestNamespace, |
| 74 | + "-c", constants.ModelValidationInitContainerName, "--timestamps")) |
| 75 | + g.Expect(err).NotTo(HaveOccurred()) |
| 76 | + loopStarted, ok := continuousLogTimestamp(logs, "Starting continuous validation loop", time.Time{}) |
| 77 | + g.Expect(ok).To(BeTrue(), "continuous validation loop did not start") |
| 78 | + periodic, ok := continuousLogTimestamp(logs, "Running periodic validation", loopStarted) |
| 79 | + g.Expect(ok).To(BeTrue(), "periodic validation has not run yet") |
| 80 | + _, ok = continuousLogTimestamp(logs, "Validation successful", periodic) |
| 81 | + g.Expect(ok).To(BeTrue(), "periodic validation has not succeeded yet") |
| 82 | + g.Expect(periodic.Sub(loopStarted)).To(BeNumerically("~", time.Minute, 10*time.Second), |
| 83 | + "periodic validation should follow the configured interval") |
| 84 | + }, 2*time.Minute, 5*time.Second).Should(Succeed()) |
| 85 | + |
| 86 | + By("verifying the sidecar stayed Ready without restarts and its liveness endpoint responds") |
| 87 | + expectContinuousReadiness(podData.PodName, uid) |
| 88 | + // Containers share the pod network namespace, so the workload can reach the agent. |
| 89 | + output, err := utils.Run(exec.Command("kubectl", "exec", podData.PodName, "-n", webhookTestNamespace, |
| 90 | + "-c", "test-container", "--", "wget", "-q", "-T", "5", "-O", "-", "http://127.0.0.1:8080/healthz")) |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + Expect(strings.TrimSpace(output)).To(Equal("ok")) |
| 93 | + |
| 94 | + By("deleting the pod and verifying the sidecar and tracking finalizer do not block termination") |
| 95 | + Expect(utils.KubectlDelete(podTemplate, &utils.KubectlDeleteOptions{ |
| 96 | + TemplateData: podData, Timeout: "90s", |
| 97 | + })).To(Succeed()) |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +// expectContinuousReadiness waits until the validation sidecar and the pod |
| 102 | +// report Ready, while the same pod keeps running the same sidecar container |
| 103 | +// without restarts. |
| 104 | +func expectContinuousReadiness(podName string, uid types.UID) { |
| 105 | + GinkgoHelper() |
| 106 | + Eventually(func(g Gomega) { |
| 107 | + var pod corev1.Pod |
| 108 | + g.Expect(utils.KubectlGetJSON("pod", podName, webhookTestNamespace, &pod)).To(Succeed()) |
| 109 | + g.Expect(pod.UID).To(Equal(uid), "pod must not be replaced") |
| 110 | + g.Expect(pod.Status.Phase).To(Equal(corev1.PodRunning)) |
| 111 | + var sidecar *corev1.ContainerStatus |
| 112 | + for i := range pod.Status.InitContainerStatuses { |
| 113 | + if pod.Status.InitContainerStatuses[i].Name == constants.ModelValidationInitContainerName { |
| 114 | + sidecar = &pod.Status.InitContainerStatuses[i] |
| 115 | + } |
| 116 | + } |
| 117 | + g.Expect(sidecar).NotTo(BeNil(), "validation sidecar status not reported") |
| 118 | + g.Expect(sidecar.State.Running).NotTo(BeNil(), "validation sidecar must keep running") |
| 119 | + g.Expect(sidecar.RestartCount).To(BeZero(), "validation sidecar must not restart") |
| 120 | + g.Expect(sidecar.Ready).To(BeTrue(), "validation sidecar must be Ready") |
| 121 | + g.Expect(pod.Status.Conditions).To(ContainElement(SatisfyAll( |
| 122 | + HaveField("Type", corev1.PodReady), |
| 123 | + HaveField("Status", corev1.ConditionTrue), |
| 124 | + ))) |
| 125 | + }, 3*time.Minute, 3*time.Second).Should(Succeed()) |
| 126 | +} |
| 127 | + |
| 128 | +// continuousLogTimestamp returns the timestamp of the first `kubectl logs |
| 129 | +// --timestamps` line at or after `after` that contains msg. |
| 130 | +func continuousLogTimestamp(logs, msg string, after time.Time) (time.Time, bool) { |
| 131 | + for _, line := range utils.GetNonEmptyLines(logs) { |
| 132 | + stamp, text, found := strings.Cut(line, " ") |
| 133 | + if !found || !strings.Contains(text, msg) { |
| 134 | + continue |
| 135 | + } |
| 136 | + ts, err := time.Parse(time.RFC3339Nano, stamp) |
| 137 | + if err == nil && !ts.Before(after) { |
| 138 | + return ts, true |
| 139 | + } |
| 140 | + } |
| 141 | + return time.Time{}, false |
| 142 | +} |
0 commit comments