From faa30f9b6716a6506074f1a058c8760a6eb6b113 Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 13:18:27 -0500 Subject: [PATCH 1/4] feat: admit submissions and carry out review decisions Submissions need a verified email and are limited per user (5 per 10 minutes by default, counted from the cache). Each submission gets a SubmissionReview owned by it. The review reconciler carries out Rejected, Merged and Approved decisions: merges and approvals give the submitter a follow and a vote, and approvals link the submission once feedback-sync syncs a post annotated with its name. Merged decisions must name a visible, unmerged post. Field indexes move to a shared package, and generation now points the webhook config at the operator's Service. --- README.md | 15 +- Taskfile.yaml | 3 + cmd/feedback/operator.go | 13 +- config/milo/webhook/kustomization.yaml | 26 +-- config/milo/webhook/manifests.yaml | 51 +++++- internal/controller/controller_test.go | 128 +++++++++++++++ internal/controller/merge.go | 5 +- internal/controller/postvotes.go | 3 +- internal/controller/setup.go | 21 +-- internal/controller/submission.go | 179 +++++++++++++++++++++ internal/controller/vote.go | 3 +- internal/index/index.go | 47 ++++++ internal/webhook/envtest_test.go | 6 +- internal/webhook/register.go | 22 ++- internal/webhook/submission.go | 122 ++++++++++++++ internal/webhook/submission_test.go | 95 +++++++++++ internal/webhook/vote.go | 8 +- pkg/apis/feedback/v1alpha1/crd_test.go | 2 +- pkg/apis/feedback/v1alpha1/digest_types.go | 2 +- pkg/apis/feedback/v1alpha1/post_types.go | 5 + 20 files changed, 693 insertions(+), 63 deletions(-) create mode 100644 internal/controller/submission.go create mode 100644 internal/index/index.go create mode 100644 internal/webhook/submission.go create mode 100644 internal/webhook/submission_test.go diff --git a/README.md b/README.md index 1b9346e..d1730d7 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ webhooks. ## Status The CRDs are defined. The operator sets owners and reviewers in admission, -and reconciles votes and follows. Submissions, security reports and -attachments are tracked as sub-issues of the epic. +and reconciles votes, follows and submission reviews. Attachments, triage, +security reports and digests are tracked as sub-issues of the epic. | Resource | Who writes it | Who reads it | | --- | --- | --- | @@ -58,6 +58,16 @@ decided on a `SubmissionReview`. - **`post-votes` reconciler.** It counts a post's votes from the cache (indexed by `spec.postRef.name`) and applies `status.votes.{total,datum,linked,trendingScore}` with server-side apply as `feedback-operator`. `github` and `githubReactions` belong to `feedback-sync`. `total = datum + github - linked`, where `linked` counts people whose 👍 comes from the GitHub account linked to their vote. Posts with a trending score are re-tallied hourly as votes age. - **`merge` reconciler.** When `status.mergedInto` is set, it gives each voter and follower the same on the target post, unless they already have one, and deletes the old one. +## Submissions + +- **Admission** (`submission` webhook): the sender's email must be verified, and each user can send at most `--submission-limit` (default 5) per `--submission-window` (default 10 minutes), counted from the cache. A `Merged` review decision must name a post that exists, isn't hidden and isn't merged (`submissionreview` webhook). +- **`submission` reconciler.** It creates each submission's `SubmissionReview`, owned by the submission so Milo's garbage collector deletes it with the submission, and sets the phase to `PendingReview`. +- **`submission-review` reconciler.** It carries out the staff decision: + - `Rejected` copies the reason to the submission. + - `Merged` has the submitter follow (`reason: merged`) and vote on the post. + - `Approved` waits until feedback-sync creates the GitHub issue and syncs a post annotated `feedback.miloapis.com/submission: `. It then links the submission to that post and has the submitter follow (`reason: submitted`) and vote. + - The `Applied` condition and `status.applied` record when the decision has been fully carried out. + ## Roles | Role | For | @@ -77,6 +87,7 @@ client certificates and get Kubernetes RBAC in Milo (`config/milo/rbac`). cmd/feedback/ operator and version commands internal/controller/ reconcilers internal/identity/ who a request comes from, from Milo's user info +internal/index/ cache field indexes shared by reconcilers and webhooks internal/milo/ voter lookups in Milo internal/webhook/ admission webhooks Milo calls pkg/apis/feedback/ feedback.miloapis.com/v1alpha1 types (CRDs) diff --git a/Taskfile.yaml b/Taskfile.yaml index 5d2cf08..46b584d 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -43,6 +43,9 @@ tasks: - "{{.TOOL_DIR}}/controller-gen object:headerFile=hack/boilerplate.go.txt paths=./pkg/apis/..." - "{{.TOOL_DIR}}/controller-gen crd paths=./pkg/apis/... output:crd:artifacts:config=config/crd/bases" - "{{.TOOL_DIR}}/controller-gen webhook paths=./internal/webhook/... output:webhook:artifacts:config=config/milo/webhook" + # controller-gen always names the kubebuilder placeholder Service. + - | + perl -pi -e 's/^( name: )webhook-service$/${1}feedback-operator-webhook/; s/^( namespace: )system$/${1}feedback-system/' config/milo/webhook/manifests.yaml install-tools: desc: Install code generators into bin/ diff --git a/cmd/feedback/operator.go b/cmd/feedback/operator.go index bce40f9..8911666 100644 --- a/cmd/feedback/operator.go +++ b/cmd/feedback/operator.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "time" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/runtime" @@ -17,6 +18,7 @@ import ( ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" "go.miloapis.com/feedback/internal/controller" + "go.miloapis.com/feedback/internal/index" "go.miloapis.com/feedback/internal/milo" "go.miloapis.com/feedback/internal/version" "go.miloapis.com/feedback/internal/webhook" @@ -38,6 +40,8 @@ type operatorOptions struct { leaderElectionNamespace string webhookPort int webhookCertDir string + submissionLimit int + submissionWindow time.Duration } func newOperatorCommand() *cobra.Command { @@ -59,6 +63,8 @@ func newOperatorCommand() *cobra.Command { f.StringVar(&o.leaderElectionNamespace, "leader-election-namespace", "feedback-system", "Namespace of the leader election lease.") f.IntVar(&o.webhookPort, "webhook-port", 9443, "Port the admission webhook server listens on.") f.StringVar(&o.webhookCertDir, "webhook-cert-dir", "/etc/kubernetes/pki/webhook", "Directory holding tls.crt and tls.key for the webhook server.") + f.IntVar(&o.submissionLimit, "submission-limit", 5, "Submissions a user may send per --submission-window; 0 disables the limit.") + f.DurationVar(&o.submissionWindow, "submission-window", 10*time.Minute, "Window for --submission-limit.") goFlags := flag.NewFlagSet("zap", flag.ExitOnError) zapOpts.BindFlags(goFlags) f.AddGoFlagSet(goFlags) @@ -95,13 +101,16 @@ func runOperator(o operatorOptions) error { return fmt.Errorf("creating manager: %w", err) } - webhook.Register(mgr) + ctx := ctrl.SetupSignalHandler() + if err := index.Add(ctx, mgr); err != nil { + return fmt.Errorf("adding indexes: %w", err) + } + webhook.Register(mgr, webhook.Options{SubmissionLimit: o.submissionLimit, SubmissionWindow: o.submissionWindow}) users, err := milo.NewClient(miloConfig) if err != nil { return fmt.Errorf("creating Milo user client: %w", err) } - ctx := ctrl.SetupSignalHandler() if err := controller.Setup(ctx, mgr, controller.Options{Users: users}); err != nil { return fmt.Errorf("setting up controllers: %w", err) } diff --git a/config/milo/webhook/kustomization.yaml b/config/milo/webhook/kustomization.yaml index 9dd1570..1aa8909 100644 --- a/config/milo/webhook/kustomization.yaml +++ b/config/milo/webhook/kustomization.yaml @@ -1,5 +1,5 @@ # The admission webhooks Milo calls for feedback resources. manifests.yaml is -# generated by `task generate`; the patches point it at the operator's +# generated by `task generate`, which also points it at the operator's # Service. Milo trusts certificates from the datum-control-plane issuer, so # there is no caBundle. apiVersion: kustomize.config.k8s.io/v1alpha1 @@ -16,21 +16,6 @@ patches: - op: replace path: /metadata/name value: feedback.miloapis.com - - target: - kind: MutatingWebhookConfiguration - patch: |- - - op: replace - path: /webhooks/0/clientConfig/service/name - value: feedback-operator-webhook - - op: replace - path: /webhooks/0/clientConfig/service/namespace - value: feedback-system - - op: replace - path: /webhooks/1/clientConfig/service/name - value: feedback-operator-webhook - - op: replace - path: /webhooks/1/clientConfig/service/namespace - value: feedback-system - target: kind: ValidatingWebhookConfiguration name: validating-webhook-configuration @@ -38,12 +23,3 @@ patches: - op: replace path: /metadata/name value: feedback.miloapis.com - - target: - kind: ValidatingWebhookConfiguration - patch: |- - - op: replace - path: /webhooks/0/clientConfig/service/name - value: feedback-operator-webhook - - op: replace - path: /webhooks/0/clientConfig/service/namespace - value: feedback-system diff --git a/config/milo/webhook/manifests.yaml b/config/milo/webhook/manifests.yaml index 3bbc5d4..3968506 100644 --- a/config/milo/webhook/manifests.yaml +++ b/config/milo/webhook/manifests.yaml @@ -8,8 +8,8 @@ webhooks: - v1 clientConfig: service: - name: webhook-service - namespace: system + name: feedback-operator-webhook + namespace: feedback-system path: /mutate-owner failurePolicy: Fail name: owner.feedback.miloapis.com @@ -31,8 +31,8 @@ webhooks: - v1 clientConfig: service: - name: webhook-service - namespace: system + name: feedback-operator-webhook + namespace: feedback-system path: /mutate-submissionreview failurePolicy: Fail name: reviewer.feedback.miloapis.com @@ -57,8 +57,47 @@ webhooks: - v1 clientConfig: service: - name: webhook-service - namespace: system + name: feedback-operator-webhook + namespace: feedback-system + path: /validate-submission + failurePolicy: Fail + name: submission.feedback.miloapis.com + rules: + - apiGroups: + - feedback.miloapis.com + apiVersions: + - v1alpha1 + operations: + - CREATE + resources: + - submissions + sideEffects: None +- admissionReviewVersions: + - v1 + clientConfig: + service: + name: feedback-operator-webhook + namespace: feedback-system + path: /validate-submissionreview + failurePolicy: Fail + name: submissionreview.feedback.miloapis.com + rules: + - apiGroups: + - feedback.miloapis.com + apiVersions: + - v1alpha1 + operations: + - CREATE + - UPDATE + resources: + - submissionreviews + sideEffects: None +- admissionReviewVersions: + - v1 + clientConfig: + service: + name: feedback-operator-webhook + namespace: feedback-system path: /validate-vote failurePolicy: Fail name: vote.feedback.miloapis.com diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go index 06e84f1..54ac3ad 100644 --- a/internal/controller/controller_test.go +++ b/internal/controller/controller_test.go @@ -18,6 +18,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + "go.miloapis.com/feedback/internal/index" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -74,6 +75,9 @@ func start(t *testing.T, now *clock) client.Client { } ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) + if err := index.Add(ctx, mgr); err != nil { + t.Fatal(err) + } if err := Setup(ctx, mgr, Options{Users: fakeUsers{}, Now: now.Now}); err != nil { t.Fatal(err) } @@ -269,3 +273,127 @@ func TestReconcilers(t *testing.T) { }) }) } + +func TestSubmissionReviews(t *testing.T) { + c := start(t, &clock{now: time.Now()}) + ctx := context.Background() + + submit := func(name, user string) *v1.Submission { + s := &v1.Submission{ + ObjectMeta: metav1.ObjectMeta{Name: name}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: user}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b", + Context: v1.SubmissionContext{Client: v1.ClientPortal}}, + } + if err := c.Create(ctx, s); err != nil { + t.Fatal(err) + } + return s + } + decide := func(name string, spec v1.SubmissionReviewSpec) { + eventually(t, "review for "+name, func() error { + var r v1.SubmissionReview + if err := c.Get(ctx, types.NamespacedName{Name: name}, &r); err != nil { + return err + } + r.Spec = spec + return c.Update(ctx, &r) + }) + } + phase := func(name string, want v1.SubmissionPhase, post string) { + eventually(t, name+" "+string(want), func() error { + var s v1.Submission + if err := c.Get(ctx, types.NamespacedName{Name: name}, &s); err != nil { + return err + } + got := "" + if s.Status.PostRef != nil { + got = s.Status.PostRef.Name + } + if s.Status.Phase != want || got != post { + return errorf(string(s.Status.Phase) + " " + got) + } + return nil + }) + } + has := func(obj client.Object) error { return c.Get(ctx, client.ObjectKeyFromObject(obj), obj) } + + t.Run("new submissions wait for review", func(t *testing.T) { + submit("sub-new", "u1") + phase("sub-new", v1.SubmissionPhasePendingReview, "") + }) + + t.Run("rejected", func(t *testing.T) { + submit("sub-rej", "u1") + decide("sub-rej", v1.SubmissionReviewSpec{Decision: v1.ReviewDecisionRejected, Reason: "Out of scope"}) + phase("sub-rej", v1.SubmissionPhaseRejected, "") + }) + + t.Run("merged into a post", func(t *testing.T) { + if err := c.Create(ctx, &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "a.b.10"}}); err != nil { + t.Fatal(err) + } + submit("sub-merge", "u5") + decide("sub-merge", v1.SubmissionReviewSpec{Decision: v1.ReviewDecisionMerged, MergedInto: "a.b.10"}) + phase("sub-merge", v1.SubmissionPhaseMerged, "a.b.10") + eventually(t, "follow and vote", func() error { + s := &v1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName("a.b.10", "u5")}} + if err := has(s); err != nil { + return err + } + if s.Spec.Reason != v1.SubscriptionReasonMerged { + return errorf("reason " + string(s.Spec.Reason)) + } + v := &v1.Vote{ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName("a.b.10", "u5")}} + if err := has(v); err != nil { + return err + } + if v.Spec.Source != v1.ClientPortal { + return errorf("source " + string(v.Spec.Source)) + } + return nil + }) + }) + + t.Run("approved, then linked when its post syncs", func(t *testing.T) { + submit("sub-ok", "u6") + decide("sub-ok", v1.SubmissionReviewSpec{Decision: v1.ReviewDecisionApproved, PublicTitle: "T", PublicBody: "B"}) + phase("sub-ok", v1.SubmissionPhaseApproved, "") + eventually(t, "waiting condition", func() error { + var r v1.SubmissionReview + if err := c.Get(ctx, types.NamespacedName{Name: "sub-ok"}, &r); err != nil { + return err + } + if r.Status.Applied || len(r.Status.Conditions) == 0 || r.Status.Conditions[0].Reason != "WaitingForPost" { + return errorf("not waiting") + } + return nil + }) + // feedback-sync creates the issue and syncs its post. + post := &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "datum-cloud.enhancements.99", + Annotations: map[string]string{v1.FromSubmissionAnnotation: "sub-ok"}}} + if err := c.Create(ctx, post); err != nil { + t.Fatal(err) + } + phase("sub-ok", v1.SubmissionPhaseApproved, post.Name) + eventually(t, "applied", func() error { + var r v1.SubmissionReview + if err := c.Get(ctx, types.NamespacedName{Name: "sub-ok"}, &r); err != nil { + return err + } + if !r.Status.Applied { + return errorf("not applied") + } + return has(&v1.Vote{ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(post.Name, "u6")}}) + }) + }) + + t.Run("the review goes with its submission", func(t *testing.T) { + var r v1.SubmissionReview + if err := c.Get(ctx, types.NamespacedName{Name: "sub-new"}, &r); err != nil { + t.Fatal(err) + } + if len(r.OwnerReferences) != 1 || r.OwnerReferences[0].Name != "sub-new" { + t.Errorf("owner references %v", r.OwnerReferences) + } + }) +} diff --git a/internal/controller/merge.go b/internal/controller/merge.go index 743b326..108b22c 100644 --- a/internal/controller/merge.go +++ b/internal/controller/merge.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "go.miloapis.com/feedback/internal/index" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -36,7 +37,7 @@ func (r *MergeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl } var votes v1.VoteList - if err := r.List(ctx, &votes, client.MatchingFields{postIndex: post.Name}); err != nil { + if err := r.List(ctx, &votes, client.MatchingFields{index.Post: post.Name}); err != nil { return ctrl.Result{}, err } for i := range votes.Items { @@ -51,7 +52,7 @@ func (r *MergeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl } var subs v1.SubscriptionList - if err := r.List(ctx, &subs, client.MatchingFields{postIndex: post.Name}); err != nil { + if err := r.List(ctx, &subs, client.MatchingFields{index.Post: post.Name}); err != nil { return ctrl.Result{}, err } for i := range subs.Items { diff --git a/internal/controller/postvotes.go b/internal/controller/postvotes.go index e9a732a..ee145e2 100644 --- a/internal/controller/postvotes.go +++ b/internal/controller/postvotes.go @@ -11,6 +11,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "go.miloapis.com/feedback/internal/index" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -33,7 +34,7 @@ func (r *PostVotesReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return ctrl.Result{}, client.IgnoreNotFound(err) } var votes v1.VoteList - if err := r.List(ctx, &votes, client.MatchingFields{postIndex: post.Name}); err != nil { + if err := r.List(ctx, &votes, client.MatchingFields{index.Post: post.Name}); err != nil { return ctrl.Result{}, err } voters := make([]voter, 0, len(votes.Items)) diff --git a/internal/controller/setup.go b/internal/controller/setup.go index 3063a0b..d3d15e7 100644 --- a/internal/controller/setup.go +++ b/internal/controller/setup.go @@ -11,18 +11,13 @@ import ( "time" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" "go.miloapis.com/feedback/internal/milo" - v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) // FieldManager is the operator's server-side apply field manager. const FieldManager = "feedback-operator" -// postIndex indexes votes and subscriptions by spec.postRef.name. -const postIndex = "spec.postRef.name" - // Options configures the reconcilers. type Options struct { // Users looks up voters' organizations and GitHub usernames. Nil skips @@ -39,24 +34,16 @@ func (o Options) now() time.Time { return time.Now() } -// Setup adds the field indexes and every reconciler to mgr. +// Setup adds every reconciler to mgr. The indexes in package index must be +// added first. func Setup(ctx context.Context, mgr ctrl.Manager, o Options) error { - idx := mgr.GetFieldIndexer() - if err := idx.IndexField(ctx, &v1.Vote{}, postIndex, func(obj client.Object) []string { - return []string{obj.(*v1.Vote).Spec.PostRef.Name} - }); err != nil { - return err - } - if err := idx.IndexField(ctx, &v1.Subscription{}, postIndex, func(obj client.Object) []string { - return []string{obj.(*v1.Subscription).Spec.PostRef.Name} - }); err != nil { - return err - } c := mgr.GetClient() for _, r := range []interface{ SetupWithManager(ctrl.Manager) error }{ &PostVotesReconciler{Client: c, Options: o}, &VoteReconciler{Client: c, Options: o}, &MergeReconciler{Client: c}, + &SubmissionReconciler{Client: c}, + &ReviewReconciler{Client: c}, } { if err := r.SetupWithManager(mgr); err != nil { return err diff --git a/internal/controller/submission.go b/internal/controller/submission.go new file mode 100644 index 0000000..a451487 --- /dev/null +++ b/internal/controller/submission.go @@ -0,0 +1,179 @@ +package controller + +import ( + "context" + "fmt" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + "go.miloapis.com/feedback/internal/index" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// ConditionApplied is the SubmissionReview condition that says whether the +// decision has been carried out. +const ConditionApplied = "Applied" + +// SubmissionReconciler gives every new submission its SubmissionReview, owned +// by the submission so it goes with it, and starts it in PendingReview. +type SubmissionReconciler struct { + client.Client +} + +// Reconcile implements reconcile.Reconciler. +func (r *SubmissionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var sub v1.Submission + if err := r.Get(ctx, req.NamespacedName, &sub); err != nil || sub.DeletionTimestamp != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + review := &v1.SubmissionReview{ObjectMeta: metav1.ObjectMeta{Name: sub.Name}} + if err := controllerutil.SetOwnerReference(&sub, review, r.Scheme()); err != nil { + return ctrl.Result{}, err + } + if err := r.Create(ctx, review); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, err + } + if sub.Status.Phase == "" { + sub.Status.Phase = v1.SubmissionPhasePendingReview + return ctrl.Result{}, r.Status().Update(ctx, &sub) + } + return ctrl.Result{}, nil +} + +// SetupWithManager registers the reconciler. +func (r *SubmissionReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr).Named("submission").For(&v1.Submission{}).Complete(r) +} + +// ReviewReconciler carries out staff decisions on SubmissionReviews: +// +// - Rejected: the submission becomes Rejected with the reason. +// - Merged: the submitter follows (reason merged) and votes on the post, +// and the submission becomes Merged into it. +// - Approved: the submission becomes Approved. feedback-sync creates the +// GitHub issue and annotates the post it syncs with the submission's +// name; then the submitter follows (reason submitted) and votes, and the +// submission links to the post. +// +// MoveToSecurity is carried out by the security report reconciler. +// status.applied is set once a decision is fully carried out. +type ReviewReconciler struct { + client.Client +} + +// Reconcile implements reconcile.Reconciler. +func (r *ReviewReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var review v1.SubmissionReview + if err := r.Get(ctx, req.NamespacedName, &review); err != nil || review.DeletionTimestamp != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + d := review.Spec.Decision + if d == "" || d == v1.ReviewDecisionMoveToSecurity || review.Status.Applied { + return ctrl.Result{}, nil + } + var sub v1.Submission + if err := r.Get(ctx, req.NamespacedName, &sub); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + var status v1.SubmissionStatus + pending := "" + switch d { + case v1.ReviewDecisionRejected: + status = v1.SubmissionStatus{Phase: v1.SubmissionPhaseRejected, Reason: review.Spec.Reason} + case v1.ReviewDecisionMerged: + post := review.Spec.MergedInto + if err := grant(ctx, r.Client, &sub, post, v1.SubscriptionReasonMerged); err != nil { + return ctrl.Result{}, err + } + status = v1.SubmissionStatus{Phase: v1.SubmissionPhaseMerged, PostRef: &v1.PostReference{Name: post}} + case v1.ReviewDecisionApproved: + status = v1.SubmissionStatus{Phase: v1.SubmissionPhaseApproved} + var posts v1.PostList + if err := r.List(ctx, &posts, client.MatchingFields{index.FromSubmission: sub.Name}); err != nil { + return ctrl.Result{}, err + } + if len(posts.Items) == 0 { + pending = "Waiting for feedback-sync to create the issue and sync its post." + break + } + post := posts.Items[0].Name + if err := grant(ctx, r.Client, &sub, post, v1.SubscriptionReasonSubmitted); err != nil { + return ctrl.Result{}, err + } + status.PostRef = &v1.PostReference{Name: post} + default: + return ctrl.Result{}, nil + } + + if sub.Status.Phase != status.Phase || sub.Status.Reason != status.Reason || !samePost(sub.Status.PostRef, status.PostRef) { + sub.Status = status + if err := r.Status().Update(ctx, &sub); err != nil { + return ctrl.Result{}, err + } + } + cond := metav1.Condition{Type: ConditionApplied, Status: metav1.ConditionTrue, Reason: string(d), ObservedGeneration: review.Generation} + if pending != "" { + cond.Status, cond.Reason, cond.Message = metav1.ConditionFalse, "WaitingForPost", pending + } + review.Status.Applied = pending == "" + meta.SetStatusCondition(&review.Status.Conditions, cond) + return ctrl.Result{}, r.Status().Update(ctx, &review) +} + +func samePost(a, b *v1.PostReference) bool { + if a == nil || b == nil { + return a == b + } + return a.Name == b.Name +} + +// grant has the submitter follow and vote on post. Existing ones are kept. A +// vote the admission webhook refuses (the post was hidden or merged since) is +// skipped: the decision still stands. +func grant(ctx context.Context, c client.Client, sub *v1.Submission, post string, reason v1.SubscriptionReason) error { + user := sub.Spec.UserRef + follow := &v1.Subscription{ + ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(post, user.Name)}, + Spec: v1.SubscriptionSpec{PostRef: v1.PostReference{Name: post}, UserRef: user, Reason: reason}, + } + vote := &v1.Vote{ + ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(post, user.Name)}, + Spec: v1.VoteSpec{PostRef: v1.PostReference{Name: post}, UserRef: user, Source: sub.Spec.Context.Client}, + } + for _, obj := range []client.Object{follow, vote} { + err := c.Create(ctx, obj) + switch { + case err == nil || apierrors.IsAlreadyExists(err): + case apierrors.IsForbidden(err) || apierrors.IsInvalid(err): + log.FromContext(ctx).Info("Not giving the submitter a follow or vote", "post", post, "err", err.Error()) + default: + return fmt.Errorf("giving the submitter a %T on %s: %w", obj, post, err) + } + } + return nil +} + +// SetupWithManager registers the reconciler for reviews, their submissions, +// and posts synced from an approved submission. +func (r *ReviewReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + Named("submission-review"). + For(&v1.SubmissionReview{}). + Watches(&v1.Post{}, handler.EnqueueRequestsFromMapFunc(func(_ context.Context, obj client.Object) []reconcile.Request { + if s := obj.GetAnnotations()[v1.FromSubmissionAnnotation]; s != "" { + return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: s}}} + } + return nil + })). + Complete(r) +} diff --git a/internal/controller/vote.go b/internal/controller/vote.go index 2740358..a9a9a52 100644 --- a/internal/controller/vote.go +++ b/internal/controller/vote.go @@ -16,6 +16,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "go.miloapis.com/feedback/internal/index" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -135,7 +136,7 @@ func (r *VoteReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *VoteReconciler) votesOf(ctx context.Context, post client.Object) []reconcile.Request { var votes v1.VoteList - if err := r.List(ctx, &votes, client.MatchingFields{postIndex: post.GetName()}); err != nil { + if err := r.List(ctx, &votes, client.MatchingFields{index.Post: post.GetName()}); err != nil { log.FromContext(ctx).Error(err, "Listing a post's votes failed", "post", post.GetName()) return nil } diff --git a/internal/index/index.go b/internal/index/index.go new file mode 100644 index 0000000..82becfe --- /dev/null +++ b/internal/index/index.go @@ -0,0 +1,47 @@ +// Package index registers the cache field indexes the reconcilers and +// webhooks share. +package index + +import ( + "context" + + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/manager" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +const ( + // Post indexes votes and subscriptions by spec.postRef.name. + Post = "spec.postRef.name" + // Owner indexes user-owned objects by spec.userRef.name. + Owner = "spec.userRef.name" + // FromSubmission indexes posts by the submission they were created from + // (v1alpha1.FromSubmissionAnnotation). + FromSubmission = "metadata.annotations.fromSubmission" +) + +// Add registers every index with mgr. Call it once, before mgr starts. +func Add(ctx context.Context, mgr manager.Manager) error { + idx := mgr.GetFieldIndexer() + for _, i := range []struct { + obj client.Object + field string + fn client.IndexerFunc + }{ + {&v1.Vote{}, Post, func(o client.Object) []string { return []string{o.(*v1.Vote).Spec.PostRef.Name} }}, + {&v1.Subscription{}, Post, func(o client.Object) []string { return []string{o.(*v1.Subscription).Spec.PostRef.Name} }}, + {&v1.Submission{}, Owner, func(o client.Object) []string { return []string{o.(*v1.Submission).Spec.UserRef.Name} }}, + {&v1.Post{}, FromSubmission, func(o client.Object) []string { + if s := o.GetAnnotations()[v1.FromSubmissionAnnotation]; s != "" { + return []string{s} + } + return nil + }}, + } { + if err := idx.IndexField(ctx, i.obj, i.field, i.fn); err != nil { + return err + } + } + return nil +} diff --git a/internal/webhook/envtest_test.go b/internal/webhook/envtest_test.go index a5cf410..8330794 100644 --- a/internal/webhook/envtest_test.go +++ b/internal/webhook/envtest_test.go @@ -19,6 +19,7 @@ import ( ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" "go.miloapis.com/feedback/internal/identity" + "go.miloapis.com/feedback/internal/index" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -59,7 +60,10 @@ func TestWebhooksInFrontOfAnAPIServer(t *testing.T) { if err != nil { t.Fatal(err) } - Register(mgr) + if err := index.Add(context.Background(), mgr); err != nil { + t.Fatal(err) + } + Register(mgr, Options{}) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) go func() { _ = mgr.Start(ctx) }() diff --git a/internal/webhook/register.go b/internal/webhook/register.go index 175060b..4914b1e 100644 --- a/internal/webhook/register.go +++ b/internal/webhook/register.go @@ -1,14 +1,30 @@ package webhook import ( + "time" + "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/webhook" ) -// Register adds every admission webhook to mgr's webhook server. -func Register(mgr manager.Manager) { +// Options configures the webhooks. +type Options struct { + // SubmissionLimit submissions per user per SubmissionWindow. Zero + // disables the limit. + SubmissionLimit int + SubmissionWindow time.Duration +} + +// Register adds every admission webhook to mgr's webhook server. The indexes +// in package index must be added too. +func Register(mgr manager.Manager, o Options) { s := mgr.GetWebhookServer() + c := mgr.GetClient() s.Register("/mutate-owner", &webhook.Admission{Handler: Owner{}}) s.Register("/mutate-submissionreview", &webhook.Admission{Handler: Reviewer{}}) - s.Register("/validate-vote", &webhook.Admission{Handler: VoteCheck{Posts: mgr.GetClient()}}) + s.Register("/validate-vote", &webhook.Admission{Handler: VoteCheck{Posts: c}}) + s.Register("/validate-submission", &webhook.Admission{Handler: SubmissionCheck{ + Submissions: c, Limit: o.SubmissionLimit, Window: o.SubmissionWindow, + }}) + s.Register("/validate-submissionreview", &webhook.Admission{Handler: ReviewCheck{Posts: c}}) } diff --git a/internal/webhook/submission.go b/internal/webhook/submission.go new file mode 100644 index 0000000..92f1bdc --- /dev/null +++ b/internal/webhook/submission.go @@ -0,0 +1,122 @@ +package webhook + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "time" + + admissionv1 "k8s.io/api/admission/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/duration" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + + "go.miloapis.com/feedback/internal/identity" + "go.miloapis.com/feedback/internal/index" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// +kubebuilder:webhook:path=/validate-submission,mutating=false,failurePolicy=fail,sideEffects=None,groups=feedback.miloapis.com,resources=submissions,verbs=create,versions=v1alpha1,name=submission.feedback.miloapis.com,admissionReviewVersions=v1 + +// SubmissionCheck admits new submissions from people with a verified email, +// up to Limit per Window each. +type SubmissionCheck struct { + // Submissions reads submissions, indexed by owner. + Submissions client.Reader + // Limit submissions per user per Window. Zero disables the limit. + Limit int + Window time.Duration + // Now returns the current time; tests replace it. + Now func() time.Time + // Attachments reports whether attachments can be accepted. + Attachments bool +} + +var _ admission.Handler = SubmissionCheck{} + +// Handle implements admission.Handler. +func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admission.Response { + if req.Operation != admissionv1.Create { + return admission.Allowed("") + } + if identity.EmailUnverified(req.UserInfo) { + return admission.Denied("verify your email address to send feedback") + } + sub := &v1.Submission{} + if err := json.Unmarshal(req.Object.Raw, sub); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + if len(sub.Spec.Attachments) > 0 && !c.Attachments { + return admission.Denied("attachments aren't available yet; send the feedback without them") + } + if c.Limit <= 0 { + return admission.Allowed("") + } + var mine v1.SubmissionList + if err := c.Submissions.List(ctx, &mine, client.MatchingFields{index.Owner: sub.Spec.UserRef.Name}); err != nil { + return admission.Errored(http.StatusInternalServerError, err) + } + now := c.now() + since := now.Add(-c.Window) + var recent []metav1.Time + for _, s := range mine.Items { + if s.CreationTimestamp.Time.After(since) { + recent = append(recent, s.CreationTimestamp) + } + } + if len(recent) < c.Limit { + return admission.Allowed("") + } + oldest := recent[0] + for _, t := range recent { + if t.Before(&oldest) { + oldest = t + } + } + wait := oldest.Add(c.Window).Sub(now).Round(time.Second) + resp := admission.Denied(fmt.Sprintf("You can send %d pieces of feedback every %s; try again in %s.", + c.Limit, duration.HumanDuration(c.Window), duration.HumanDuration(max(wait, time.Second)))) + resp.Result.Code = http.StatusTooManyRequests + resp.Result.Reason = metav1.StatusReasonTooManyRequests + return resp +} + +func (c SubmissionCheck) now() time.Time { + if c.Now != nil { + return c.Now() + } + return time.Now() +} + +// +kubebuilder:webhook:path=/validate-submissionreview,mutating=false,failurePolicy=fail,sideEffects=None,groups=feedback.miloapis.com,resources=submissionreviews,verbs=create;update,versions=v1alpha1,name=submissionreview.feedback.miloapis.com,admissionReviewVersions=v1 + +// ReviewCheck admits a Merged decision only into a post people can see: it +// must exist, not be hidden and not be merged itself. +type ReviewCheck struct { + Posts client.Reader +} + +var _ admission.Handler = ReviewCheck{} + +// Handle implements admission.Handler. +func (c ReviewCheck) Handle(ctx context.Context, req admission.Request) admission.Response { + review := &v1.SubmissionReview{} + if err := json.Unmarshal(req.Object.Raw, review); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + if review.Spec.Decision != v1.ReviewDecisionMerged { + return admission.Allowed("") + } + if req.Operation == admissionv1.Update { + old := &v1.SubmissionReview{} + if err := json.Unmarshal(req.OldObject.Raw, old); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + if old.Spec.Decision != "" { + return admission.Allowed("") + } + } + return checkPost(ctx, c.Posts, review.Spec.MergedInto) +} diff --git a/internal/webhook/submission_test.go b/internal/webhook/submission_test.go new file mode 100644 index 0000000..a815168 --- /dev/null +++ b/internal/webhook/submission_test.go @@ -0,0 +1,95 @@ +package webhook + +import ( + "context" + "net/http" + "strings" + "testing" + "time" + + admissionv1 "k8s.io/api/admission/v1" + authenticationv1 "k8s.io/api/authentication/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + "go.miloapis.com/feedback/internal/identity" + "go.miloapis.com/feedback/internal/index" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +func TestSubmissionCheck(t *testing.T) { + now := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) + scheme := runtime.NewScheme() + _ = v1.AddToScheme(scheme) + sent := func(name, user string, ago time.Duration) *v1.Submission { + return &v1.Submission{ObjectMeta: metav1.ObjectMeta{Name: name, CreationTimestamp: metav1.NewTime(now.Add(-ago))}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: user}}} + } + subs := fake.NewClientBuilder().WithScheme(scheme). + WithIndex(&v1.Submission{}, index.Owner, func(o client.Object) []string { return []string{o.(*v1.Submission).Spec.UserRef.Name} }). + WithObjects(sent("a", "u1", time.Minute), sent("b", "u1", 2*time.Minute), sent("c", "u1", time.Hour), sent("d", "u2", time.Minute)). + Build() + c := SubmissionCheck{Submissions: subs, Limit: 2, Window: 10 * time.Minute, Now: func() time.Time { return now }} + + user := func(id, verified string) authenticationv1.UserInfo { + u := inUserContext(id) + u.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{verified} + return u + } + newSub := func(owner string, attachments int) *v1.Submission { + s := &v1.Submission{Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: owner}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}} + for range attachments { + s.Spec.Attachments = append(s.Spec.Attachments, v1.AttachmentReference{UploadRef: "x"}) + } + return s + } + + resp := c.Handle(context.Background(), request(t, admissionv1.Create, "submissions", user("u1", "true"), newSub("u1", 0), nil)) + if resp.Allowed || resp.Result.Code != http.StatusTooManyRequests || !strings.Contains(resp.Result.Message, "try again in 8m") { + t.Errorf("third in ten minutes: allowed %v, %d %q", resp.Allowed, resp.Result.Code, resp.Result.Message) + } + if resp := c.Handle(context.Background(), request(t, admissionv1.Create, "submissions", user("u2", "true"), newSub("u2", 0), nil)); !resp.Allowed { + t.Errorf("second in ten minutes denied: %v", resp.Result.Message) + } + if resp := c.Handle(context.Background(), request(t, admissionv1.Create, "submissions", user("u2", "false"), newSub("u2", 0), nil)); resp.Allowed { + t.Error("unverified email allowed") + } + if resp := c.Handle(context.Background(), request(t, admissionv1.Create, "submissions", user("u2", "true"), newSub("u2", 1), nil)); resp.Allowed { + t.Error("attachments allowed without uploads") + } + c.Limit = 0 + if resp := c.Handle(context.Background(), request(t, admissionv1.Create, "submissions", user("u1", "true"), newSub("u1", 0), nil)); !resp.Allowed { + t.Error("limit 0 still limits") + } +} + +func TestReviewCheck(t *testing.T) { + scheme := runtime.NewScheme() + _ = v1.AddToScheme(scheme) + posts := fake.NewClientBuilder().WithScheme(scheme).WithObjects( + &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "open"}}, + &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "hidden"}, Spec: v1.PostSpec{Hidden: true}}, + ).Build() + c := ReviewCheck{Posts: posts} + staff := authenticationv1.UserInfo{Username: "staff@datum.net"} + review := func(d v1.ReviewDecision, into string) *v1.SubmissionReview { + return &v1.SubmissionReview{Spec: v1.SubmissionReviewSpec{Decision: d, MergedInto: into, Reason: "r"}} + } + for _, tc := range []struct { + name string + obj *v1.SubmissionReview + allowed bool + }{ + {"merge into a visible post", review(v1.ReviewDecisionMerged, "open"), true}, + {"merge into a hidden post", review(v1.ReviewDecisionMerged, "hidden"), false}, + {"merge into a missing post", review(v1.ReviewDecisionMerged, "nope"), false}, + {"reject", review(v1.ReviewDecisionRejected, ""), true}, + } { + resp := c.Handle(context.Background(), request(t, admissionv1.Update, "submissionreviews", staff, tc.obj, review("", ""))) + if resp.Allowed != tc.allowed { + t.Errorf("%s: allowed %v (%v)", tc.name, resp.Allowed, resp.Result.Message) + } + } +} diff --git a/internal/webhook/vote.go b/internal/webhook/vote.go index b7d87d3..6ca6294 100644 --- a/internal/webhook/vote.go +++ b/internal/webhook/vote.go @@ -42,8 +42,14 @@ func (c VoteCheck) Handle(ctx context.Context, req admission.Request) admission. return admission.Errored(http.StatusBadRequest, err) } name, _, _ := unstructured.NestedString(obj.Object, "spec", "postRef", "name") + return checkPost(ctx, c.Posts, name) +} + +// checkPost admits a reference to a post that exists, isn't hidden and isn't +// merged into another. +func checkPost(ctx context.Context, posts client.Reader, name string) admission.Response { var post v1.Post - err := c.Posts.Get(ctx, types.NamespacedName{Name: name}, &post) + err := posts.Get(ctx, types.NamespacedName{Name: name}, &post) switch { case apierrors.IsNotFound(err) || (err == nil && post.Spec.Hidden): return admission.Denied(fmt.Sprintf("post %q not found", name)) diff --git a/pkg/apis/feedback/v1alpha1/crd_test.go b/pkg/apis/feedback/v1alpha1/crd_test.go index 52ef683..a551bbe 100644 --- a/pkg/apis/feedback/v1alpha1/crd_test.go +++ b/pkg/apis/feedback/v1alpha1/crd_test.go @@ -138,7 +138,7 @@ func TestCRDs(t *testing.T) { t.Run("submissions can't change", func(t *testing.T) { s := &v1.Submission{ ObjectMeta: metav1.ObjectMeta{Name: "sub-2"}, - Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}, } if err := c.Create(ctx, s); err != nil { t.Fatal(err) diff --git a/pkg/apis/feedback/v1alpha1/digest_types.go b/pkg/apis/feedback/v1alpha1/digest_types.go index f267a9c..78f2021 100644 --- a/pkg/apis/feedback/v1alpha1/digest_types.go +++ b/pkg/apis/feedback/v1alpha1/digest_types.go @@ -60,7 +60,7 @@ type DigestItem struct { Submission string `json:"submission,omitempty"` // Changelog is the ChangelogEntry for part-shipped and released. // +optional - Changelog string `json:"changelog,omitempty"` + Changelog string `json:"changelog,omitempty"` At metav1.Time `json:"at"` } diff --git a/pkg/apis/feedback/v1alpha1/post_types.go b/pkg/apis/feedback/v1alpha1/post_types.go index 738d236..36374a7 100644 --- a/pkg/apis/feedback/v1alpha1/post_types.go +++ b/pkg/apis/feedback/v1alpha1/post_types.go @@ -306,6 +306,11 @@ const ( CategoryLabelPrefix = "category.feedback.miloapis.com/" ) +// FromSubmissionAnnotation is set by feedback-sync on a post it created from +// an approved submission, naming the submission. The operator uses it to link +// the submission to its post and to give the submitter a vote. +const FromSubmissionAnnotation = "feedback.miloapis.com/submission" + // PostName is the name of the Post for a GitHub issue: // "..", lower-cased. func PostName(org, repository string, number int32) string { From 9c615b3c19a18b76fad8a0487e4725483b568c80 Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 16:37:33 -0500 Subject: [PATCH 2/4] fix: hold the submission limit against parallel requests The limit counted submissions in the operator's cache, so parallel requests could all read the same count and be admitted. Each replica now also remembers what it admitted until the cache shows it, deciding one request at a time, so a burst to one replica gets at most the limit. Document that the limit is still soft across replicas (replicas x limit per window at worst), and why that's acceptable: submissions are never published without staff review. --- internal/webhook/register.go | 2 +- internal/webhook/submission.go | 62 ++++++++++++++++++++++++++--- internal/webhook/submission_test.go | 54 +++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 6 deletions(-) diff --git a/internal/webhook/register.go b/internal/webhook/register.go index 4914b1e..b8033bc 100644 --- a/internal/webhook/register.go +++ b/internal/webhook/register.go @@ -24,7 +24,7 @@ func Register(mgr manager.Manager, o Options) { s.Register("/mutate-submissionreview", &webhook.Admission{Handler: Reviewer{}}) s.Register("/validate-vote", &webhook.Admission{Handler: VoteCheck{Posts: c}}) s.Register("/validate-submission", &webhook.Admission{Handler: SubmissionCheck{ - Submissions: c, Limit: o.SubmissionLimit, Window: o.SubmissionWindow, + Submissions: c, Limit: o.SubmissionLimit, Window: o.SubmissionWindow, Admitted: NewAdmitted(), }}) s.Register("/validate-submissionreview", &webhook.Admission{Handler: ReviewCheck{Posts: c}}) } diff --git a/internal/webhook/submission.go b/internal/webhook/submission.go index 92f1bdc..7a064b4 100644 --- a/internal/webhook/submission.go +++ b/internal/webhook/submission.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "sync" "time" admissionv1 "k8s.io/api/admission/v1" @@ -22,6 +23,17 @@ import ( // SubmissionCheck admits new submissions from people with a verified email, // up to Limit per Window each. +// +// The limit is soft. It counts the user's submissions in the operator's +// cache, which lags creation slightly, and each operator replica keeps its +// own count. Admitted (when set) closes the gap within one replica by +// remembering what this replica admitted that the cache hasn't seen yet, so +// a burst of parallel requests to one replica gets at most Limit through. +// Requests spread across replicas can still get Limit through each, so a +// determined user can send replicas × Limit per Window. That is acceptable +// here: submissions are never published without a staff review, so the +// limit protects the review queue rather than anything public. A strict +// limit would need a shared counter written on every submission. type SubmissionCheck struct { // Submissions reads submissions, indexed by owner. Submissions client.Reader @@ -32,8 +44,19 @@ type SubmissionCheck struct { Now func() time.Time // Attachments reports whether attachments can be accepted. Attachments bool + // Admitted remembers recent admissions on this replica. Nil disables it. + Admitted *Admitted } +// Admitted records when this replica admitted each user's submissions. +type Admitted struct { + mu sync.Mutex + byUser map[string][]time.Time +} + +// NewAdmitted returns an empty record. +func NewAdmitted() *Admitted { return &Admitted{byUser: map[string][]time.Time{}} } + var _ admission.Handler = SubmissionCheck{} // Handle implements admission.Handler. @@ -54,24 +77,53 @@ func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admi if c.Limit <= 0 { return admission.Allowed("") } + owner := sub.Spec.UserRef.Name + if c.Admitted != nil { + // Decide one request at a time, so parallel requests see each + // other's admissions. + c.Admitted.mu.Lock() + defer c.Admitted.mu.Unlock() + } var mine v1.SubmissionList - if err := c.Submissions.List(ctx, &mine, client.MatchingFields{index.Owner: sub.Spec.UserRef.Name}); err != nil { + if err := c.Submissions.List(ctx, &mine, client.MatchingFields{index.Owner: owner}); err != nil { return admission.Errored(http.StatusInternalServerError, err) } now := c.now() since := now.Add(-c.Window) - var recent []metav1.Time + var recent []time.Time + var newest time.Time for _, s := range mine.Items { - if s.CreationTimestamp.Time.After(since) { - recent = append(recent, s.CreationTimestamp) + t := s.CreationTimestamp.Time + if t.After(since) { + recent = append(recent, t) } + if t.After(newest) { + newest = t + } + } + if c.Admitted != nil { + // A submission is created after it is admitted, so an admission + // older than the newest submission in the cache has been counted. + var keep []time.Time + for _, t := range c.Admitted.byUser[owner] { + if t.After(since) { + keep = append(keep, t) + if t.After(newest) { + recent = append(recent, t) + } + } + } + c.Admitted.byUser[owner] = keep } if len(recent) < c.Limit { + if c.Admitted != nil { + c.Admitted.byUser[owner] = append(c.Admitted.byUser[owner], now) + } return admission.Allowed("") } oldest := recent[0] for _, t := range recent { - if t.Before(&oldest) { + if t.Before(oldest) { oldest = t } } diff --git a/internal/webhook/submission_test.go b/internal/webhook/submission_test.go index a815168..c088b6b 100644 --- a/internal/webhook/submission_test.go +++ b/internal/webhook/submission_test.go @@ -2,8 +2,11 @@ package webhook import ( "context" + "fmt" "net/http" "strings" + "sync" + "sync/atomic" "testing" "time" @@ -93,3 +96,54 @@ func TestReviewCheck(t *testing.T) { } } } + +// TestSubmissionLimitHoldsForParallelRequests checks that requests racing the +// cache on one replica can't all get through: the cache shows none of them. +func TestSubmissionLimitHoldsForParallelRequests(t *testing.T) { + now := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) + scheme := runtime.NewScheme() + _ = v1.AddToScheme(scheme) + subs := fake.NewClientBuilder().WithScheme(scheme). + WithIndex(&v1.Submission{}, index.Owner, func(o client.Object) []string { return []string{o.(*v1.Submission).Spec.UserRef.Name} }). + Build() + c := SubmissionCheck{Submissions: subs, Limit: 3, Window: 10 * time.Minute, Now: func() time.Time { return now }, Admitted: NewAdmitted()} + u := inUserContext("u1") + u.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{"true"} + sub := &v1.Submission{Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}} + req := request(t, admissionv1.Create, "submissions", u, sub, nil) + + var wg sync.WaitGroup + var allowed atomic.Int32 + for range 20 { + wg.Go(func() { + if c.Handle(context.Background(), req).Allowed { + allowed.Add(1) + } + }) + } + wg.Wait() + if got := allowed.Load(); got != 3 { + t.Errorf("%d of 20 parallel submissions allowed, want 3", got) + } + + // Once the cache shows the admitted submissions, they aren't counted + // twice. + for i := range 3 { + _ = subs.Create(context.Background(), &v1.Submission{ + ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("s%d", i), CreationTimestamp: metav1.NewTime(now.Add(time.Second))}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}, + }) + } + var cached v1.SubmissionList + if err := subs.List(context.Background(), &cached); err != nil || len(cached.Items) != 3 || !cached.Items[0].CreationTimestamp.Equal(&metav1.Time{Time: now.Add(time.Second)}) { + t.Fatalf("cache fixture: %v %+v", err, cached.Items) + } + c.Limit = 4 + c.Now = func() time.Time { return now.Add(2 * time.Second) } + if !c.Handle(context.Background(), req).Allowed { + t.Error("fourth submission denied with a limit of 4") + } + if c.Handle(context.Background(), req).Allowed { + t.Error("fifth submission allowed with a limit of 4") + } +} From b93190a0d0908379b4eac33e743e6ded44abefb1 Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 17:30:48 -0500 Subject: [PATCH 3/4] fix: don't count a submission twice in the second it was admitted creationTimestamp has whole seconds, so a submission created in the same second as its admission looked older than the admission, and both counted towards the limit: users got 429 after 3 or 4 of 5. Count instead how many of this replica's admissions the cache already shows since the oldest one, rounded down to the second. --- internal/webhook/submission.go | 56 ++++++++++++++++++----------- internal/webhook/submission_test.go | 37 +++++++++++++++++++ 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/internal/webhook/submission.go b/internal/webhook/submission.go index 7a064b4..f354590 100644 --- a/internal/webhook/submission.go +++ b/internal/webhook/submission.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "slices" "sync" "time" @@ -48,7 +49,8 @@ type SubmissionCheck struct { Admitted *Admitted } -// Admitted records when this replica admitted each user's submissions. +// Admitted records when this replica admitted each user's submissions, so +// SubmissionCheck can count the ones its cache doesn't show yet. type Admitted struct { mu sync.Mutex byUser map[string][]time.Time @@ -90,30 +92,23 @@ func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admi } now := c.now() since := now.Add(-c.Window) - var recent []time.Time - var newest time.Time + var recent, cached []time.Time for _, s := range mine.Items { t := s.CreationTimestamp.Time if t.After(since) { recent = append(recent, t) - } - if t.After(newest) { - newest = t + cached = append(cached, t) } } if c.Admitted != nil { - // A submission is created after it is admitted, so an admission - // older than the newest submission in the cache has been counted. - var keep []time.Time + var local []time.Time for _, t := range c.Admitted.byUser[owner] { if t.After(since) { - keep = append(keep, t) - if t.After(newest) { - recent = append(recent, t) - } + local = append(local, t) } } - c.Admitted.byUser[owner] = keep + c.Admitted.byUser[owner] = local + recent = append(recent, unseen(local, cached)...) } if len(recent) < c.Limit { if c.Admitted != nil { @@ -121,12 +116,7 @@ func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admi } return admission.Allowed("") } - oldest := recent[0] - for _, t := range recent { - if t.Before(oldest) { - oldest = t - } - } + oldest := slices.MinFunc(recent, func(a, b time.Time) int { return a.Compare(b) }) wait := oldest.Add(c.Window).Sub(now).Round(time.Second) resp := admission.Denied(fmt.Sprintf("You can send %d pieces of feedback every %s; try again in %s.", c.Limit, duration.HumanDuration(c.Window), duration.HumanDuration(max(wait, time.Second)))) @@ -135,6 +125,32 @@ func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admi return resp } +// unseen returns the admissions in local, oldest first, that the cache +// doesn't show yet. Every submission this replica admitted is created after +// its admission, so the cached submissions created since the oldest +// admission account for that many of them. creationTimestamp has whole +// seconds, so the comparison rounds the admission time down: an admission +// and its submission in the same second must not both count. A submission +// another replica created in that time also accounts for one, which errs +// towards admitting; the limit is soft anyway. +func unseen(local, cached []time.Time) []time.Time { + if len(local) == 0 { + return nil + } + slices.SortFunc(local, func(a, b time.Time) int { return a.Compare(b) }) + from := local[0].Truncate(time.Second) + seen := 0 + for _, t := range cached { + if !t.Before(from) { + seen++ + } + } + if seen >= len(local) { + return nil + } + return local[seen:] +} + func (c SubmissionCheck) now() time.Time { if c.Now != nil { return c.Now() diff --git a/internal/webhook/submission_test.go b/internal/webhook/submission_test.go index c088b6b..28fcf44 100644 --- a/internal/webhook/submission_test.go +++ b/internal/webhook/submission_test.go @@ -147,3 +147,40 @@ func TestSubmissionLimitHoldsForParallelRequests(t *testing.T) { t.Error("fifth submission allowed with a limit of 4") } } + +// TestSubmissionLimitCountsSameSecondOnce covers an admission and its +// submission in the same second: creationTimestamp has whole seconds, so the +// submission looks older than its own admission, and it must not count +// twice. +func TestSubmissionLimitCountsSameSecondOnce(t *testing.T) { + second := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) + scheme := runtime.NewScheme() + _ = v1.AddToScheme(scheme) + subs := fake.NewClientBuilder().WithScheme(scheme). + WithIndex(&v1.Submission{}, index.Owner, func(o client.Object) []string { return []string{o.(*v1.Submission).Spec.UserRef.Name} }). + Build() + now := second.Add(500 * time.Millisecond) + c := SubmissionCheck{Submissions: subs, Limit: 2, Window: 10 * time.Minute, Now: func() time.Time { return now }, Admitted: NewAdmitted()} + u := inUserContext("u1") + u.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{"true"} + req := request(t, admissionv1.Create, "submissions", u, + &v1.Submission{Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}}, nil) + + if !c.Handle(context.Background(), req).Allowed { + t.Fatal("first submission denied") + } + // The API server stores it with the second the admission happened in. + if err := subs.Create(context.Background(), &v1.Submission{ + ObjectMeta: metav1.ObjectMeta{Name: "s1", CreationTimestamp: metav1.NewTime(second)}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}, + }); err != nil { + t.Fatal(err) + } + now = second.Add(700 * time.Millisecond) + if !c.Handle(context.Background(), req).Allowed { + t.Error("second submission denied with a limit of 2: the first counted twice") + } + if c.Handle(context.Background(), req).Allowed { + t.Error("third submission allowed with a limit of 2") + } +} From 56a86c69d527ab9e1c912a6f1ee690c013456bdb Mon Sep 17 00:00:00 2001 From: Matt Jenkinson Date: Tue, 17 Nov 2026 23:16:14 -0500 Subject: [PATCH 4/4] fix: match pending admissions to cached submissions by UID The submission limit matched the admissions it remembered to cached submissions by time. creationTimestamp is set before admission and has whole seconds, so an admission at 12:00:01.020 for a submission stamped 12:00:00 counted twice, and the next submission was refused early. The webhook validates after the API server sets the UID, so pending admissions are now matched to cached submissions by UID. Also: - Dry runs are limited but not recorded, so they don't use up the limit. - An admission the cache never shows (the create failed later) stops counting after a minute instead of the whole window. - The same create admitted twice counts once. Tests cover a second boundary, dry runs, failed creates and repeated admissions. The second-boundary, dry-run and repeated-admission tests fail without this change. --- internal/webhook/submission.go | 93 +++++++------ internal/webhook/submission_test.go | 195 ++++++++++++++++++++-------- 2 files changed, 192 insertions(+), 96 deletions(-) diff --git a/internal/webhook/submission.go b/internal/webhook/submission.go index f354590..64ffcfc 100644 --- a/internal/webhook/submission.go +++ b/internal/webhook/submission.go @@ -11,6 +11,7 @@ import ( admissionv1 "k8s.io/api/admission/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/duration" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" @@ -49,15 +50,28 @@ type SubmissionCheck struct { Admitted *Admitted } -// Admitted records when this replica admitted each user's submissions, so -// SubmissionCheck can count the ones its cache doesn't show yet. +// Admitted records the submissions this replica admitted that its cache +// didn't show yet, so SubmissionCheck can count them. type Admitted struct { mu sync.Mutex - byUser map[string][]time.Time + byUser map[string][]admitted } +// admitted is one submission this replica admitted: its UID, which the API +// server sets before validating admission, and when it was admitted. +type admitted struct { + uid types.UID + at time.Time +} + +// PendingTTL is how long an admission the cache doesn't show is still +// counted. A create can fail after admission (another webhook or a +// conflict), and its submission never appears; this stops it from counting +// for a whole window. The cache normally catches up in well under a second. +const PendingTTL = time.Minute + // NewAdmitted returns an empty record. -func NewAdmitted() *Admitted { return &Admitted{byUser: map[string][]time.Time{}} } +func NewAdmitted() *Admitted { return &Admitted{byUser: map[string][]admitted{}} } var _ admission.Handler = SubmissionCheck{} @@ -92,27 +106,43 @@ func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admi } now := c.now() since := now.Add(-c.Window) - var recent, cached []time.Time + var recent []time.Time + cached := make(map[types.UID]bool, len(mine.Items)) for _, s := range mine.Items { - t := s.CreationTimestamp.Time - if t.After(since) { + if s.UID != "" { + cached[s.UID] = true + } + if t := s.CreationTimestamp.Time; t.After(since) { recent = append(recent, t) - cached = append(cached, t) } } if c.Admitted != nil { - var local []time.Time - for _, t := range c.Admitted.byUser[owner] { - if t.After(since) { - local = append(local, t) + // The cache counts the submissions it shows; this replica's record + // adds the ones it admitted that the cache doesn't show yet. The two + // are matched by UID, never by time: creationTimestamp has whole + // seconds and is set before admission, so it can fall in an earlier + // second than the admission. + var pending []admitted + for _, a := range c.Admitted.byUser[owner] { + if cached[a.uid] || !a.at.After(since) || now.Sub(a.at) >= PendingTTL { + continue + } + pending = append(pending, a) + // The same create admitted again doesn't count against itself. + if a.uid == "" || a.uid != sub.UID { + recent = append(recent, a.at) } } - c.Admitted.byUser[owner] = local - recent = append(recent, unseen(local, cached)...) + if len(pending) == 0 { + delete(c.Admitted.byUser, owner) + } else { + c.Admitted.byUser[owner] = pending + } } if len(recent) < c.Limit { - if c.Admitted != nil { - c.Admitted.byUser[owner] = append(c.Admitted.byUser[owner], now) + // A dry run creates nothing, so it doesn't use up the limit. + if c.Admitted != nil && !isDryRun(req) && !c.Admitted.has(owner, sub.UID) { + c.Admitted.byUser[owner] = append(c.Admitted.byUser[owner], admitted{uid: sub.UID, at: now}) } return admission.Allowed("") } @@ -125,32 +155,17 @@ func (c SubmissionCheck) Handle(ctx context.Context, req admission.Request) admi return resp } -// unseen returns the admissions in local, oldest first, that the cache -// doesn't show yet. Every submission this replica admitted is created after -// its admission, so the cached submissions created since the oldest -// admission account for that many of them. creationTimestamp has whole -// seconds, so the comparison rounds the admission time down: an admission -// and its submission in the same second must not both count. A submission -// another replica created in that time also accounts for one, which errs -// towards admitting; the limit is soft anyway. -func unseen(local, cached []time.Time) []time.Time { - if len(local) == 0 { - return nil - } - slices.SortFunc(local, func(a, b time.Time) int { return a.Compare(b) }) - from := local[0].Truncate(time.Second) - seen := 0 - for _, t := range cached { - if !t.Before(from) { - seen++ - } +// has reports whether uid is already recorded for owner, so a repeated +// admission of the same create counts once. Callers hold a.mu. +func (a *Admitted) has(owner string, uid types.UID) bool { + if uid == "" { + return false } - if seen >= len(local) { - return nil - } - return local[seen:] + return slices.ContainsFunc(a.byUser[owner], func(x admitted) bool { return x.uid == uid }) } +func isDryRun(req admission.Request) bool { return req.DryRun != nil && *req.DryRun } + func (c SubmissionCheck) now() time.Time { if c.Now != nil { return c.Now() diff --git a/internal/webhook/submission_test.go b/internal/webhook/submission_test.go index 28fcf44..ac1a4f5 100644 --- a/internal/webhook/submission_test.go +++ b/internal/webhook/submission_test.go @@ -6,7 +6,6 @@ import ( "net/http" "strings" "sync" - "sync/atomic" "testing" "time" @@ -14,8 +13,11 @@ import ( authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "go.miloapis.com/feedback/internal/identity" "go.miloapis.com/feedback/internal/index" @@ -97,90 +99,169 @@ func TestReviewCheck(t *testing.T) { } } -// TestSubmissionLimitHoldsForParallelRequests checks that requests racing the -// cache on one replica can't all get through: the cache shows none of them. -func TestSubmissionLimitHoldsForParallelRequests(t *testing.T) { - now := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) +// limitFixture is a SubmissionCheck with an empty cache and a record of +// admissions, and a way to make requests from u1. +type limitFixture struct { + t *testing.T + subs client.Client + c SubmissionCheck + now time.Time +} + +func newLimitFixture(t *testing.T, limit int, now time.Time) *limitFixture { scheme := runtime.NewScheme() _ = v1.AddToScheme(scheme) - subs := fake.NewClientBuilder().WithScheme(scheme). + f := &limitFixture{t: t, now: now} + f.subs = fake.NewClientBuilder().WithScheme(scheme). WithIndex(&v1.Submission{}, index.Owner, func(o client.Object) []string { return []string{o.(*v1.Submission).Spec.UserRef.Name} }). Build() - c := SubmissionCheck{Submissions: subs, Limit: 3, Window: 10 * time.Minute, Now: func() time.Time { return now }, Admitted: NewAdmitted()} + f.c = SubmissionCheck{Submissions: f.subs, Limit: limit, Window: 10 * time.Minute, Now: func() time.Time { return f.now }, Admitted: NewAdmitted()} + return f +} + +// req is a create of a submission with uid, as the API server sends it to +// validating admission: the UID is already set. +func (f *limitFixture) req(uid string, dryRun bool) admission.Request { u := inUserContext("u1") u.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{"true"} - sub := &v1.Submission{Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}} - req := request(t, admissionv1.Create, "submissions", u, sub, nil) + sub := &v1.Submission{ObjectMeta: metav1.ObjectMeta{UID: types.UID(uid)}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}} + r := request(f.t, admissionv1.Create, "submissions", u, sub, nil) + if dryRun { + r.DryRun = ptr.To(true) + } + return r +} + +func (f *limitFixture) allowed(uid string) bool { + return f.c.Handle(context.Background(), f.req(uid, false)).Allowed +} + +// store puts a submission in the cache, as the API server stored it. +func (f *limitFixture) store(uid string, created time.Time) { + f.t.Helper() + if err := f.subs.Create(context.Background(), &v1.Submission{ + ObjectMeta: metav1.ObjectMeta{Name: "s-" + uid, UID: types.UID(uid), CreationTimestamp: metav1.NewTime(created)}, + Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}, + }); err != nil { + f.t.Fatal(err) + } +} + +// TestSubmissionLimitHoldsForParallelRequests checks that requests racing the +// cache on one replica can't all get through: the cache shows none of them. +func TestSubmissionLimitHoldsForParallelRequests(t *testing.T) { + now := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) + f := newLimitFixture(t, 3, now) var wg sync.WaitGroup - var allowed atomic.Int32 - for range 20 { + var mu sync.Mutex + var admittedUIDs []string + for i := range 20 { + uid := fmt.Sprintf("p%d", i) + req := f.req(uid, false) wg.Go(func() { - if c.Handle(context.Background(), req).Allowed { - allowed.Add(1) + if f.c.Handle(context.Background(), req).Allowed { + mu.Lock() + admittedUIDs = append(admittedUIDs, uid) + mu.Unlock() } }) } wg.Wait() - if got := allowed.Load(); got != 3 { - t.Errorf("%d of 20 parallel submissions allowed, want 3", got) + if len(admittedUIDs) != 3 { + t.Fatalf("%d of 20 parallel submissions allowed, want 3", len(admittedUIDs)) } // Once the cache shows the admitted submissions, they aren't counted // twice. - for i := range 3 { - _ = subs.Create(context.Background(), &v1.Submission{ - ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("s%d", i), CreationTimestamp: metav1.NewTime(now.Add(time.Second))}, - Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}, - }) + for _, uid := range admittedUIDs { + f.store(uid, now) } - var cached v1.SubmissionList - if err := subs.List(context.Background(), &cached); err != nil || len(cached.Items) != 3 || !cached.Items[0].CreationTimestamp.Equal(&metav1.Time{Time: now.Add(time.Second)}) { - t.Fatalf("cache fixture: %v %+v", err, cached.Items) - } - c.Limit = 4 - c.Now = func() time.Time { return now.Add(2 * time.Second) } - if !c.Handle(context.Background(), req).Allowed { + f.c.Limit = 4 + f.now = now.Add(2 * time.Second) + if !f.allowed("p20") { t.Error("fourth submission denied with a limit of 4") } - if c.Handle(context.Background(), req).Allowed { + if f.allowed("p21") { t.Error("fifth submission allowed with a limit of 4") } } -// TestSubmissionLimitCountsSameSecondOnce covers an admission and its -// submission in the same second: creationTimestamp has whole seconds, so the -// submission looks older than its own admission, and it must not count -// twice. -func TestSubmissionLimitCountsSameSecondOnce(t *testing.T) { - second := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) - scheme := runtime.NewScheme() - _ = v1.AddToScheme(scheme) - subs := fake.NewClientBuilder().WithScheme(scheme). - WithIndex(&v1.Submission{}, index.Owner, func(o client.Object) []string { return []string{o.(*v1.Submission).Spec.UserRef.Name} }). - Build() - now := second.Add(500 * time.Millisecond) - c := SubmissionCheck{Submissions: subs, Limit: 2, Window: 10 * time.Minute, Now: func() time.Time { return now }, Admitted: NewAdmitted()} - u := inUserContext("u1") - u.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{"true"} - req := request(t, admissionv1.Create, "submissions", u, - &v1.Submission{Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}, Kind: v1.SubmissionKindIdea, Title: "t", Body: "b"}}, nil) +// TestSubmissionLimitCountsEachSubmissionOnce covers a submission whose +// creationTimestamp is in an earlier second than its admission: the API +// server sets it before calling admission, and it has whole seconds. The +// cached submission and the record of its admission must count once. +func TestSubmissionLimitCountsEachSubmissionOnce(t *testing.T) { + for _, tc := range []struct { + name string + admitted time.Time + created time.Time + }{ + {"same second", time.Date(2026, 11, 17, 12, 0, 0, 500e6, time.UTC), time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC)}, + {"across a second boundary", time.Date(2026, 11, 17, 12, 0, 1, 20e6, time.UTC), time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC)}, + } { + t.Run(tc.name, func(t *testing.T) { + f := newLimitFixture(t, 2, tc.admitted) + if !f.allowed("a") { + t.Fatal("first submission denied") + } + f.store("a", tc.created) + f.now = tc.admitted.Add(200 * time.Millisecond) + if !f.allowed("b") { + t.Error("second submission denied with a limit of 2: the first counted twice") + } + if f.allowed("c") { + t.Error("third submission allowed with a limit of 2") + } + }) + } +} + +// TestSubmissionLimitIgnoresDryRuns checks a dry run is limited like a real +// create but doesn't use up the limit, since it creates nothing. +func TestSubmissionLimitIgnoresDryRuns(t *testing.T) { + f := newLimitFixture(t, 1, time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC)) + for range 3 { + if !f.c.Handle(context.Background(), f.req("dry", true)).Allowed { + t.Fatal("dry run denied") + } + } + if !f.allowed("a") { + t.Fatal("first real submission denied after dry runs") + } + if f.c.Handle(context.Background(), f.req("dry2", true)).Allowed { + t.Error("dry run over the limit allowed") + } +} - if !c.Handle(context.Background(), req).Allowed { +// TestSubmissionLimitForgetsFailedCreates covers a create that fails after +// admission: its submission never reaches the cache, and it stops counting +// after PendingTTL rather than for the whole window. +func TestSubmissionLimitForgetsFailedCreates(t *testing.T) { + now := time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC) + f := newLimitFixture(t, 1, now) + if !f.allowed("failed") { t.Fatal("first submission denied") } - // The API server stores it with the second the admission happened in. - if err := subs.Create(context.Background(), &v1.Submission{ - ObjectMeta: metav1.ObjectMeta{Name: "s1", CreationTimestamp: metav1.NewTime(second)}, - Spec: v1.SubmissionSpec{UserRef: v1.UserReference{Name: "u1"}}, - }); err != nil { - t.Fatal(err) + f.now = now.Add(PendingTTL - time.Second) + if f.allowed("b") { + t.Error("second submission allowed while the first may still be on its way") } - now = second.Add(700 * time.Millisecond) - if !c.Handle(context.Background(), req).Allowed { - t.Error("second submission denied with a limit of 2: the first counted twice") + f.now = now.Add(PendingTTL) + if !f.allowed("b") { + t.Error("second submission denied after the first never appeared") + } +} + +// TestSubmissionLimitCountsRepeatedAdmissionOnce covers the API server +// sending the same create to admission twice: it's one submission. +func TestSubmissionLimitCountsRepeatedAdmissionOnce(t *testing.T) { + f := newLimitFixture(t, 1, time.Date(2026, 11, 17, 12, 0, 0, 0, time.UTC)) + if !f.allowed("a") || !f.allowed("a") { + t.Fatal("the same create denied the second time") } - if c.Handle(context.Background(), req).Allowed { - t.Error("third submission allowed with a limit of 2") + if f.allowed("b") { + t.Error("a second submission allowed with a limit of 1") } }