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 4717614..6b22d38 100644 --- a/internal/controller/merge.go +++ b/internal/controller/merge.go @@ -20,6 +20,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" ) @@ -28,8 +29,6 @@ const ( // post looking for the post its votes go to. Real chains are one or two // long; this only stops a runaway walk. maxMergeChain = 10 - // mergedIntoIndex indexes posts by status.mergedInto. - mergedIntoIndex = "status.mergedInto" // MergeFieldManager applies the VotesMoved condition on posts. It is not // FieldManager, which applies status.votes: an apply drops the fields its // manager applied before and left out, so each needs its own. @@ -77,7 +76,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 { @@ -92,7 +91,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 { @@ -238,7 +237,7 @@ func (r *MergeReconciler) mergedIntoPost(ctx context.Context, post client.Object var found []string for _, name := range next { var posts v1.PostList - if err := r.List(ctx, &posts, client.MatchingFields{mergedIntoIndex: name}); err != nil { + if err := r.List(ctx, &posts, client.MatchingFields{index.MergedInto: name}); err != nil { log.FromContext(ctx).Error(err, "Listing posts merged into a post failed", "post", name) return reqs } diff --git a/internal/controller/merge_webhook_test.go b/internal/controller/merge_webhook_test.go index 409012e..50b68f6 100644 --- a/internal/controller/merge_webhook_test.go +++ b/internal/controller/merge_webhook_test.go @@ -26,6 +26,7 @@ import ( ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" "go.miloapis.com/feedback/internal/identity" + "go.miloapis.com/feedback/internal/index" "go.miloapis.com/feedback/internal/webhook" v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" ) @@ -98,9 +99,12 @@ func startWithWebhooks(t *testing.T) *withWebhooks { if err != nil { t.Fatal(err) } - webhook.Register(mgr) + webhook.Register(mgr, webhook.Options{}) 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{}}); err != nil { t.Fatal(err) } 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 d4e9a6b..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,32 +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 - } - if err := idx.IndexField(ctx, &v1.Post{}, mergedIntoIndex, func(obj client.Object) []string { - if into := obj.(*v1.Post).Status.MergedInto; into != "" { - return []string{into} - } - return nil - }); 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..2dc20de --- /dev/null +++ b/internal/index/index.go @@ -0,0 +1,55 @@ +// 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" + // MergedInto indexes posts by status.mergedInto. + MergedInto = "status.mergedInto" +) + +// 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{}, MergedInto, func(o client.Object) []string { + if into := o.(*v1.Post).Status.MergedInto; into != "" { + return []string{into} + } + return nil + }}, + {&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..b8033bc 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, Admitted: NewAdmitted(), + }}) + 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..64ffcfc --- /dev/null +++ b/internal/webhook/submission.go @@ -0,0 +1,205 @@ +package webhook + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "slices" + "sync" + "time" + + 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" + + "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. +// +// 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 + // 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 + // Admitted remembers recent admissions on this replica. Nil disables it. + Admitted *Admitted +} + +// 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][]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][]admitted{}} } + +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("") + } + 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: owner}); err != nil { + return admission.Errored(http.StatusInternalServerError, err) + } + now := c.now() + since := now.Add(-c.Window) + var recent []time.Time + cached := make(map[types.UID]bool, len(mine.Items)) + for _, s := range mine.Items { + if s.UID != "" { + cached[s.UID] = true + } + if t := s.CreationTimestamp.Time; t.After(since) { + recent = append(recent, t) + } + } + if c.Admitted != nil { + // 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) + } + } + if len(pending) == 0 { + delete(c.Admitted.byUser, owner) + } else { + c.Admitted.byUser[owner] = pending + } + } + if len(recent) < c.Limit { + // 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("") + } + 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)))) + resp.Result.Code = http.StatusTooManyRequests + resp.Result.Reason = metav1.StatusReasonTooManyRequests + return resp +} + +// 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 + } + 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() + } + 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..ac1a4f5 --- /dev/null +++ b/internal/webhook/submission_test.go @@ -0,0 +1,267 @@ +package webhook + +import ( + "context" + "fmt" + "net/http" + "strings" + "sync" + "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" + "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" + 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) + } + } +} + +// 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) + 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() + 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{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 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 f.c.Handle(context.Background(), req).Allowed { + mu.Lock() + admittedUIDs = append(admittedUIDs, uid) + mu.Unlock() + } + }) + } + wg.Wait() + 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 _, uid := range admittedUIDs { + f.store(uid, now) + } + 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 f.allowed("p21") { + t.Error("fifth submission allowed with a limit of 4") + } +} + +// 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") + } +} + +// 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") + } + 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") + } + 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 f.allowed("b") { + t.Error("a second submission allowed with a limit of 1") + } +} 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 5d017ba..5742758 100644 --- a/pkg/apis/feedback/v1alpha1/post_types.go +++ b/pkg/apis/feedback/v1alpha1/post_types.go @@ -335,6 +335,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 {