diff --git a/CLAUDE.md b/CLAUDE.md index 7158701..997752e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -28,8 +28,10 @@ datum-cloud/enhancements#943. - Side effects are reconciled by the operator, not done in the request. Every reconcile step must be idempotent and keyed on deterministic names. - GitHub-derived fields live in `status` and are written only by - `feedback-sync`. The operator owns post `votes` and `delivery`; both use - server-side apply with their own field manager. Staff can only patch + `feedback-sync`. The operator owns post `votes`, `delivery` and + `conditions`; all use server-side apply with their own field manager. + Never apply two of them with the same manager: an apply drops fields its + manager applied before and left out. Staff can only patch `spec.hidden` on posts. - `Submission`, `SubmissionReview`, `SecurityReport` and `SecurityReportTriage` are audited at `Metadata` only (a Milo audit policy diff --git a/README.md b/README.md index 15fef5f..1b9346e 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,9 @@ webhooks. ## Status -The CRDs are defined and the operator starts. Admission webhooks and -reconcilers are tracked as sub-issues of the epic. +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. | Resource | Who writes it | Who reads it | | --- | --- | --- | @@ -50,6 +51,13 @@ operator creates them for someone else, and the Security Team logs reports for external reporters with no owner. The `reviewer` webhook records who decided on a `SubmissionReview`. +## Votes + +- **Admission** (`vote` webhook). A vote or follow needs a post that exists, isn't hidden and isn't merged. A vote also needs a verified email address. +- **`vote` reconciler.** It follows the post for each new voter, then sets `status.subscribed` so a follow the voter deletes isn't recreated. It records the voter's organizations and linked GitHub username in the vote's status; these Milo lookups are best effort. Votes on a deleted post are marked with `feedback.miloapis.com/orphaned-at` and deleted after 30 days unless the post returns. +- **`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. + ## Roles | Role | For | @@ -67,7 +75,9 @@ 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/milo/ voter lookups in Milo internal/webhook/ admission webhooks Milo calls pkg/apis/feedback/ feedback.miloapis.com/v1alpha1 types (CRDs) config/crd/ generated CRDs, installed in Milo diff --git a/cmd/feedback/operator.go b/cmd/feedback/operator.go index 00263cb..bce40f9 100644 --- a/cmd/feedback/operator.go +++ b/cmd/feedback/operator.go @@ -16,6 +16,8 @@ import ( metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" + "go.miloapis.com/feedback/internal/controller" + "go.miloapis.com/feedback/internal/milo" "go.miloapis.com/feedback/internal/version" "go.miloapis.com/feedback/internal/webhook" feedbackv1alpha1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" @@ -95,6 +97,15 @@ func runOperator(o operatorOptions) error { webhook.Register(mgr) + 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) + } + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { return err } @@ -103,7 +114,7 @@ func runOperator(o operatorOptions) error { } log.Info("Starting manager") - return mgr.Start(ctrl.SetupSignalHandler()) + return mgr.Start(ctx) } // restConfig loads kubeconfig, or the in-cluster config when it is empty. diff --git a/config/crd/bases/feedback.miloapis.com_posts.yaml b/config/crd/bases/feedback.miloapis.com_posts.yaml index 3b0af9c..345035c 100644 --- a/config/crd/bases/feedback.miloapis.com_posts.yaml +++ b/config/crd/bases/feedback.miloapis.com_posts.yaml @@ -88,6 +88,70 @@ spec: type: string type: array x-kubernetes-list-type: set + conditions: + description: |- + Conditions are written by the operator. A merged post carries + VotesMoved: whether its votes and follows reached the post at the end + of its merge chain, and if not, why. Posts are public, so messages + never say a post is hidden. + items: + description: Condition contains details for one aspect of the current + state of this API Resource. + properties: + lastTransitionTime: + description: |- + lastTransitionTime is the last time the condition transitioned from one status to another. + This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: |- + message is a human readable message indicating details about the transition. + This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: |- + observedGeneration represents the .metadata.generation that the condition was set based upon. + For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date + with respect to the current state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: |- + reason contains a programmatic identifier indicating the reason for the condition's last transition. + Producers of specific condition types may define expected values and meanings for this field, + and whether the values are considered a guaranteed API. + The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + x-kubernetes-list-map-keys: + - type + x-kubernetes-list-type: map delivery: description: Delivery tracks the post from request to release. properties: diff --git a/config/milo/rbac/feedback-operator.yaml b/config/milo/rbac/feedback-operator.yaml index ee33110..2a473b9 100644 --- a/config/milo/rbac/feedback-operator.yaml +++ b/config/milo/rbac/feedback-operator.yaml @@ -22,6 +22,14 @@ rules: - apiGroups: [feedback.miloapis.com] resources: [submissions] verbs: [delete] + # Voters' organizations and linked GitHub usernames, for staff analytics + # and so a 👍 from the same person isn't counted twice. + - apiGroups: [resourcemanager.miloapis.com] + resources: [organizationmemberships] + verbs: [list] + - apiGroups: [identity.miloapis.com] + resources: [useridentities] + verbs: [list] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding diff --git a/config/milo/webhook/kustomization.yaml b/config/milo/webhook/kustomization.yaml index 2754532..9dd1570 100644 --- a/config/milo/webhook/kustomization.yaml +++ b/config/milo/webhook/kustomization.yaml @@ -31,3 +31,19 @@ patches: - op: replace path: /webhooks/1/clientConfig/service/namespace value: feedback-system + - target: + kind: ValidatingWebhookConfiguration + name: validating-webhook-configuration + patch: |- + - 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 4156f0e..3bbc5d4 100644 --- a/config/milo/webhook/manifests.yaml +++ b/config/milo/webhook/manifests.yaml @@ -47,3 +47,29 @@ webhooks: resources: - submissionreviews sideEffects: None +--- +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingWebhookConfiguration +metadata: + name: validating-webhook-configuration +webhooks: +- admissionReviewVersions: + - v1 + clientConfig: + service: + name: webhook-service + namespace: system + path: /validate-vote + failurePolicy: Fail + name: vote.feedback.miloapis.com + rules: + - apiGroups: + - feedback.miloapis.com + apiVersions: + - v1alpha1 + operations: + - CREATE + resources: + - votes + - subscriptions + sideEffects: None diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go new file mode 100644 index 0000000..06e84f1 --- /dev/null +++ b/internal/controller/controller_test.go @@ -0,0 +1,271 @@ +package controller + +import ( + "context" + "os" + "path/filepath" + "sync" + "testing" + "time" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/config" + "sigs.k8s.io/controller-runtime/pkg/envtest" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +type fakeUsers struct{} + +func (fakeUsers) Organizations(_ context.Context, user string) ([]string, error) { + return []string{"org-of-" + user}, nil +} +func (fakeUsers) GitHubLogin(_ context.Context, user string) (string, error) { + return "gh-" + user, nil +} + +// clock is a settable time for the reconcilers. +type clock struct { + mu sync.Mutex + now time.Time +} + +func (c *clock) Now() time.Time { c.mu.Lock(); defer c.mu.Unlock(); return c.now } +func (c *clock) Set(t time.Time) { + c.mu.Lock() + c.now = t + c.mu.Unlock() +} + +// start runs the reconcilers against an envtest API server with the CRDs, +// and returns a client that reads straight from the API server. +func start(t *testing.T, now *clock) client.Client { + t.Helper() + if os.Getenv("KUBEBUILDER_ASSETS") == "" { + t.Skip("KUBEBUILDER_ASSETS not set; run with task test") + } + scheme := runtime.NewScheme() + if err := v1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + env := &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, + ErrorIfCRDPathMissing: true, + } + cfg, err := env.Start() + if err != nil { + t.Fatalf("start API server: %v", err) + } + t.Cleanup(func() { _ = env.Stop() }) + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ + Scheme: scheme, + Metrics: metricsserver.Options{BindAddress: "0"}, + // Tests start a manager per run; names repeat with -count. + Controller: config.Controller{SkipNameValidation: ptr(true)}, + }) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + if err := Setup(ctx, mgr, Options{Users: fakeUsers{}, Now: now.Now}); err != nil { + t.Fatal(err) + } + go func() { _ = mgr.Start(ctx) }() + c, err := client.New(cfg, client.Options{Scheme: scheme}) + if err != nil { + t.Fatal(err) + } + return c +} + +// eventually retries check until it returns nil or time runs out. +func eventually(t *testing.T, what string, check func() error) { + t.Helper() + deadline := time.Now().Add(15 * time.Second) + for { + err := check() + if err == nil { + return + } + if time.Now().After(deadline) { + t.Fatalf("%s: %v", what, err) + } + time.Sleep(100 * time.Millisecond) + } +} + +type errorf string + +func (e errorf) Error() string { return string(e) } + +func newVote(post, user string) *v1.Vote { + return &v1.Vote{ + ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(post, user)}, + Spec: v1.VoteSpec{PostRef: v1.PostReference{Name: post}, UserRef: v1.UserReference{Name: user}, Source: v1.ClientWebsite}, + } +} + +func TestReconcilers(t *testing.T) { + now := &clock{now: time.Now()} + c := start(t, now) + ctx := context.Background() + + post := &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "datum-cloud.enhancements.1"}} + if err := c.Create(ctx, post); err != nil { + t.Fatal(err) + } + // feedback-sync's GitHub fields: two 👍, one from u1's linked account. + post.Status.Votes = v1.PostVotes{GitHub: 2, GitHubReactions: []v1.GitHubReaction{{Login: "gh-u1"}, {Login: "someone"}}} + if err := c.Status().Update(ctx, post); err != nil { + t.Fatal(err) + } + + t.Run("a vote follows the post and records the voter", func(t *testing.T) { + if err := c.Create(ctx, newVote(post.Name, "u1")); err != nil { + t.Fatal(err) + } + eventually(t, "subscription", func() error { + var s v1.Subscription + if err := c.Get(ctx, types.NamespacedName{Name: v1.OwnedName(post.Name, "u1")}, &s); err != nil { + return err + } + if s.Spec.Reason != v1.SubscriptionReasonVoted { + return errorf("reason " + string(s.Spec.Reason)) + } + return nil + }) + eventually(t, "vote status", func() error { + var v v1.Vote + if err := c.Get(ctx, types.NamespacedName{Name: v1.OwnedName(post.Name, "u1")}, &v); err != nil { + return err + } + if !v.Status.Subscribed || v.Status.GitHubLogin != "gh-u1" || len(v.Status.Organizations) != 1 { + return errorf("status not filled in") + } + return nil + }) + }) + + t.Run("a deleted follow isn't recreated", func(t *testing.T) { + s := &v1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(post.Name, "u1")}} + if err := c.Delete(ctx, s); err != nil { + t.Fatal(err) + } + time.Sleep(time.Second) + if err := c.Get(ctx, client.ObjectKeyFromObject(s), s); !apierrors.IsNotFound(err) { + t.Errorf("follow came back: %v", err) + } + }) + + t.Run("totals count linked people once", func(t *testing.T) { + if err := c.Create(ctx, newVote(post.Name, "u2")); err != nil { + t.Fatal(err) + } + eventually(t, "totals", func() error { + var p v1.Post + if err := c.Get(ctx, client.ObjectKeyFromObject(post), &p); err != nil { + return err + } + // 2 Datum votes + 2 👍 - 1 person who did both. + v := p.Status.Votes + if v.Datum != 2 || v.Linked != 1 || v.Total != 3 || v.TrendingScore == 0 { + return errorf("votes not tallied yet") + } + // Sync's fields are untouched. + if v.GitHub != 2 || len(v.GitHubReactions) != 2 { + return errorf("sync's fields changed") + } + return nil + }) + }) + + t.Run("votes and follows move off a merged post", func(t *testing.T) { + target := &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "datum-cloud.enhancements.2"}} + if err := c.Create(ctx, target); err != nil { + t.Fatal(err) + } + // u2 already voted on the target, so keeps that vote. + if err := c.Create(ctx, newVote(target.Name, "u2")); err != nil { + t.Fatal(err) + } + var p v1.Post + if err := c.Get(ctx, client.ObjectKeyFromObject(post), &p); err != nil { + t.Fatal(err) + } + p.Status.MergedInto = target.Name + if err := c.Status().Update(ctx, &p); err != nil { + t.Fatal(err) + } + eventually(t, "moved", func() error { + var left v1.VoteList + if err := c.List(ctx, &left); err != nil { + return err + } + names := map[string]bool{} + for _, v := range left.Items { + names[v.Name] = true + } + if names[v1.OwnedName(post.Name, "u1")] || names[v1.OwnedName(post.Name, "u2")] { + return errorf("votes still on the merged post") + } + if !names[v1.OwnedName(target.Name, "u1")] || !names[v1.OwnedName(target.Name, "u2")] { + return errorf("votes not on the target") + } + return nil + }) + eventually(t, "target totals", func() error { + var tp v1.Post + if err := c.Get(ctx, client.ObjectKeyFromObject(target), &tp); err != nil { + return err + } + if tp.Status.Votes.Datum != 2 { + return errorf("target not tallied") + } + return nil + }) + }) + + t.Run("votes on a deleted post go after the retention period", func(t *testing.T) { + gone := &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "datum-cloud.enhancements.3"}} + if err := c.Create(ctx, gone); err != nil { + t.Fatal(err) + } + v := newVote(gone.Name, "u3") + if err := c.Create(ctx, v); err != nil { + t.Fatal(err) + } + if err := c.Delete(ctx, gone); err != nil { + t.Fatal(err) + } + eventually(t, "orphan marked", func() error { + if err := c.Get(ctx, client.ObjectKeyFromObject(v), v); err != nil { + return err + } + if v.Annotations[OrphanedAtAnnotation] == "" { + return errorf("not marked") + } + return nil + }) + // Past the retention period, a touch re-queues it and it goes. + now.Set(time.Now().Add(OrphanRetention + time.Hour)) + patch := client.MergeFrom(v.DeepCopy()) + v.Labels = map[string]string{"touch": "1"} + if err := c.Patch(ctx, v, patch); err != nil { + t.Fatal(err) + } + eventually(t, "orphan deleted", func() error { + err := c.Get(ctx, client.ObjectKeyFromObject(v), &v1.Vote{}) + if apierrors.IsNotFound(err) { + return nil + } + return errorf("still there") + }) + }) +} diff --git a/internal/controller/merge.go b/internal/controller/merge.go new file mode 100644 index 0000000..4717614 --- /dev/null +++ b/internal/controller/merge.go @@ -0,0 +1,259 @@ +package controller + +import ( + "context" + "fmt" + "slices" + + 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/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/event" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +const ( + // maxMergeChain is how many merges the operator follows from a merged + // 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. + MergeFieldManager = "feedback-operator-merge" +) + +// MergeReconciler moves votes and follows off a post that feedback-sync +// marked as merged into another (status.mergedInto). +// +// It follows status.mergedInto from post to post to the first one that isn't +// merged, and moves them there: each voter gets a vote on that post unless +// they already have one, then loses the old one; the same for follows. It +// never moves them onto a post the vote webhook would refuse them on. +// +// When the chain doesn't end at a post people can vote on (it reaches a post +// that doesn't exist or is hidden, loops, or is longer than maxMergeChain), +// nothing is moved or deleted: the votes and follows stay on the merged post, +// still counted there, and the post's VotesMoved condition is False with the +// reason. The reconcile succeeds rather than retrying; a change to any post in +// the chain (created, deleted, hidden or shown, merged or unmerged) queues +// every post merged into it, directly or through others, so they move as soon +// as the chain leads somewhere they can go. +type MergeReconciler struct { + client.Client +} + +// Reconcile implements reconcile.Reconciler. +func (r *MergeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var post v1.Post + if err := r.Get(ctx, req.NamespacedName, &post); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + if post.Status.MergedInto == "" { + // No longer merged; drop the condition if it has one. + return ctrl.Result{}, r.setCondition(ctx, &post, nil) + } + + target, blocked, err := r.resolve(ctx, &post) + if err != nil { + return ctrl.Result{}, err + } + if blocked != nil { + log.FromContext(ctx).Info("Leaving votes and follows on a merged post", "reason", blocked.Reason, "message", blocked.Message) + return ctrl.Result{}, r.setCondition(ctx, &post, blocked) + } + + var votes v1.VoteList + if err := r.List(ctx, &votes, client.MatchingFields{postIndex: post.Name}); err != nil { + return ctrl.Result{}, err + } + for i := range votes.Items { + old := &votes.Items[i] + moved := &v1.Vote{ + ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(target, old.Spec.UserRef.Name)}, + Spec: v1.VoteSpec{PostRef: v1.PostReference{Name: target}, UserRef: old.Spec.UserRef, Source: old.Spec.Source}, + } + if err := r.move(ctx, moved, old); err != nil { + return ctrl.Result{}, err + } + } + + var subs v1.SubscriptionList + if err := r.List(ctx, &subs, client.MatchingFields{postIndex: post.Name}); err != nil { + return ctrl.Result{}, err + } + for i := range subs.Items { + old := &subs.Items[i] + moved := &v1.Subscription{ + ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(target, old.Spec.UserRef.Name)}, + Spec: v1.SubscriptionSpec{PostRef: v1.PostReference{Name: target}, UserRef: old.Spec.UserRef, Reason: old.Spec.Reason}, + } + if err := r.move(ctx, moved, old); err != nil { + return ctrl.Result{}, err + } + } + return ctrl.Result{}, r.setCondition(ctx, &post, &metav1.Condition{ + Type: v1.PostConditionVotesMoved, + Status: metav1.ConditionTrue, + Reason: v1.PostReasonMoved, + Message: fmt.Sprintf("Votes and follows were moved to post %q.", target), + }) +} + +// resolve follows status.mergedInto from post to the first post that isn't +// merged and returns its name. When there is no such post people can vote +// on, it returns the False VotesMoved condition saying why instead. +// +// It reads posts from the cache the vote webhook also reads, so it agrees +// with the webhook about which posts take votes. If a post changes between +// the two reads, the create is refused, the reconcile fails and is retried, +// and the change itself queues the post again. +func (r *MergeReconciler) resolve(ctx context.Context, post *v1.Post) (string, *metav1.Condition, error) { + blocked := func(reason, format string, args ...any) (string, *metav1.Condition, error) { + return "", &metav1.Condition{ + Type: v1.PostConditionVotesMoved, + Status: metav1.ConditionFalse, + Reason: reason, + Message: fmt.Sprintf(format, args...) + " Votes and follows stay on this post until then.", + }, nil + } + seen := map[string]bool{post.Name: true} + name := post.Status.MergedInto + for range maxMergeChain { + if seen[name] { + return blocked(v1.PostReasonMergeCycle, "The posts this one is merged into loop back to post %q.", name) + } + seen[name] = true + var next v1.Post + err := r.Get(ctx, types.NamespacedName{Name: name}, &next) + switch { + case apierrors.IsNotFound(err) || (err == nil && next.Spec.Hidden): + // Posts are public; like the webhook, don't say which. + log.FromContext(ctx).V(1).Info("Merge target unavailable", "target", name, "hidden", err == nil) + return blocked(v1.PostReasonTargetUnavailable, "Post %q, which this one is merged into, isn't available.", name) + case err != nil: + return "", nil, err + case next.Status.MergedInto == "": + return name, nil, nil + } + name = next.Status.MergedInto + } + return blocked(v1.PostReasonMergeChainTooLong, "The posts this one is merged into are merged on more than %d times.", maxMergeChain) +} + +// move creates moved unless it exists, then deletes old. +func (r *MergeReconciler) move(ctx context.Context, moved, old client.Object) error { + if err := r.Create(ctx, moved); err != nil && !apierrors.IsAlreadyExists(err) { + return err + } + return client.IgnoreNotFound(r.Delete(ctx, old, client.Preconditions{UID: ptr(old.GetUID())})) +} + +// setCondition applies want as the post's VotesMoved condition, or removes +// the condition when want is nil. It writes nothing when the post already +// has it, so repeat reconciles of a blocked post don't touch it. +func (r *MergeReconciler) setCondition(ctx context.Context, post *v1.Post, want *metav1.Condition) error { + cur := meta.FindStatusCondition(post.Status.Conditions, v1.PostConditionVotesMoved) + status := map[string]any{} + if want == nil { + if cur == nil { + return nil + } + // Applying no conditions releases ours, which removes it. + } else { + want.ObservedGeneration = post.Generation + if cur != nil && cur.Status == want.Status && cur.Reason == want.Reason && + cur.Message == want.Message && cur.ObservedGeneration == want.ObservedGeneration { + return nil + } + conds := slices.Clone(post.Status.Conditions) + meta.SetStatusCondition(&conds, *want) // keeps lastTransitionTime unless status changes + c, err := runtime.DefaultUnstructuredConverter.ToUnstructured(meta.FindStatusCondition(conds, v1.PostConditionVotesMoved)) + if err != nil { + return err + } + status["conditions"] = []any{c} + } + u := &unstructured.Unstructured{Object: map[string]any{ + "apiVersion": v1.SchemeGroupVersion.String(), + "kind": "Post", + "metadata": map[string]any{"name": post.Name}, + "status": status, + }} + return r.Status().Apply(ctx, client.ApplyConfigurationFromUnstructured(u), client.FieldOwner(MergeFieldManager), client.ForceOwnership) +} + +func ptr[T any](v T) *T { return &v } + +// SetupWithManager registers the reconciler for merged posts, for votes and +// follows that land on one, and for changes to posts merged posts lead to. +func (r *MergeReconciler) SetupWithManager(mgr ctrl.Manager) error { + toPost := func(name func(client.Object) string) handler.EventHandler { + return handler.EnqueueRequestsFromMapFunc(func(_ context.Context, obj client.Object) []reconcile.Request { + return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: name(obj)}}} + }) + } + return ctrl.NewControllerManagedBy(mgr). + Named("merge"). + For(&v1.Post{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool { + p := obj.(*v1.Post) + return p.Status.MergedInto != "" || meta.FindStatusCondition(p.Status.Conditions, v1.PostConditionVotesMoved) != nil + }))). + Watches(&v1.Vote{}, toPost(func(o client.Object) string { return o.(*v1.Vote).Spec.PostRef.Name })). + Watches(&v1.Subscription{}, toPost(func(o client.Object) string { return o.(*v1.Subscription).Spec.PostRef.Name })). + // Whether a post takes votes depends on it existing, not being + // hidden and not being merged; only those changes affect a chain. + Watches(&v1.Post{}, handler.EnqueueRequestsFromMapFunc(r.mergedIntoPost), builder.WithPredicates(predicate.Funcs{ + CreateFunc: func(event.CreateEvent) bool { return true }, + DeleteFunc: func(event.DeleteEvent) bool { return true }, + UpdateFunc: func(e event.UpdateEvent) bool { + o, n := e.ObjectOld.(*v1.Post), e.ObjectNew.(*v1.Post) + return o.Spec.Hidden != n.Spec.Hidden || o.Status.MergedInto != n.Status.MergedInto + }, + GenericFunc: func(event.GenericEvent) bool { return false }, + })). + Complete(r) +} + +// mergedIntoPost returns the posts merged into post, directly or through +// other posts, up to maxMergeChain merges away. +func (r *MergeReconciler) mergedIntoPost(ctx context.Context, post client.Object) []reconcile.Request { + var reqs []reconcile.Request + seen := map[string]bool{post.GetName(): true} + next := []string{post.GetName()} + for range maxMergeChain { + var found []string + for _, name := range next { + var posts v1.PostList + if err := r.List(ctx, &posts, client.MatchingFields{mergedIntoIndex: name}); err != nil { + log.FromContext(ctx).Error(err, "Listing posts merged into a post failed", "post", name) + return reqs + } + for _, p := range posts.Items { + if !seen[p.Name] { + seen[p.Name] = true + found = append(found, p.Name) + reqs = append(reqs, reconcile.Request{NamespacedName: types.NamespacedName{Name: p.Name}}) + } + } + } + if len(found) == 0 { + break + } + next = found + } + return reqs +} diff --git a/internal/controller/merge_webhook_test.go b/internal/controller/merge_webhook_test.go new file mode 100644 index 0000000..409012e --- /dev/null +++ b/internal/controller/merge_webhook_test.go @@ -0,0 +1,369 @@ +package controller + +import ( + "context" + "os" + "path/filepath" + "sort" + "strings" + "sync/atomic" + "testing" + "time" + + 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/runtime" + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/rest" + "k8s.io/client-go/util/retry" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" + "sigs.k8s.io/controller-runtime/pkg/config" + "sigs.k8s.io/controller-runtime/pkg/envtest" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" + + "go.miloapis.com/feedback/internal/identity" + "go.miloapis.com/feedback/internal/webhook" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// withWebhooks is an API server with the generated admission webhooks +// installed, and one manager serving them and running the reconcilers as +// the operator, as in production. +type withWebhooks struct { + // admin reads everything and plays feedback-sync and staff. + admin client.Client + cfg *rest.Config + // denied counts the operator's creates that admission refused. + denied *atomic.Int64 +} + +// startWithWebhooks starts the reconcilers and the admission webhooks +// together, so the reconcilers' writes go through the same admission checks +// as in Milo. +func startWithWebhooks(t *testing.T) *withWebhooks { + t.Helper() + if os.Getenv("KUBEBUILDER_ASSETS") == "" { + t.Skip("KUBEBUILDER_ASSETS not set; run with task test") + } + scheme := runtime.NewScheme() + if err := v1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + env := &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, + ErrorIfCRDPathMissing: true, + WebhookInstallOptions: envtest.WebhookInstallOptions{ + Paths: []string{filepath.Join("..", "..", "config", "milo", "webhook", "manifests.yaml")}, + }, + } + cfg, err := env.Start() + if err != nil { + t.Fatalf("start API server: %v", err) + } + t.Cleanup(func() { _ = env.Stop() }) + + // The operator presents its own identity, which the owner webhook trusts + // to name a vote's owner. + operator := rest.CopyConfig(cfg) + operator.Impersonate = rest.ImpersonationConfig{UserName: identity.OperatorUser, Groups: []string{"system:masters"}} + denied := &atomic.Int64{} + o := env.WebhookInstallOptions + mgr, err := ctrl.NewManager(operator, ctrl.Options{ + Scheme: scheme, + Metrics: metricsserver.Options{BindAddress: "0"}, + Controller: config.Controller{SkipNameValidation: ptr(true)}, + WebhookServer: ctrlwebhook.NewServer(ctrlwebhook.Options{ + Host: o.LocalServingHost, Port: o.LocalServingPort, CertDir: o.LocalServingCertDir, + }), + NewClient: func(cfg *rest.Config, opts client.Options) (client.Client, error) { + c, err := client.NewWithWatch(cfg, opts) + if err != nil { + return nil, err + } + return interceptor.NewClient(c, interceptor.Funcs{ + Create: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.CreateOption) error { + err := c.Create(ctx, obj, opts...) + if apierrors.IsForbidden(err) { + denied.Add(1) + } + return err + }, + }), nil + }, + }) + if err != nil { + t.Fatal(err) + } + webhook.Register(mgr) + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + if err := Setup(ctx, mgr, Options{Users: fakeUsers{}}); err != nil { + t.Fatal(err) + } + go func() { _ = mgr.Start(ctx) }() + + check := mgr.GetWebhookServer().StartedChecker() + eventually(t, "webhook server", func() error { return check(nil) }) + + admin, err := client.New(cfg, client.Options{Scheme: scheme}) + if err != nil { + t.Fatal(err) + } + return &withWebhooks{admin: admin, cfg: cfg, denied: denied} +} + +// as returns a client acting in user's own context. +func (e *withWebhooks) as(t *testing.T, user string) client.Client { + t.Helper() + c := rest.CopyConfig(e.cfg) + c.Impersonate = rest.ImpersonationConfig{ + UserName: user + "@example.com", + Groups: []string{"system:masters"}, + Extra: map[string][]string{identity.ParentKindExtraKey: {identity.ParentKindUser}, identity.ParentNameExtraKey: {user}}, + } + cl, err := client.New(c, client.Options{Scheme: e.admin.Scheme()}) + if err != nil { + t.Fatal(err) + } + return cl +} + +// TestMergesWithAdmissionWebhooks checks merges with the admission webhooks +// in the path: the webhook refuses votes and follows on merged or hidden +// posts, so the reconciler must never try to put them there. +func TestMergesWithAdmissionWebhooks(t *testing.T) { + e := startWithWebhooks(t) + c := e.admin + ctx := context.Background() + + newPost := func(t *testing.T, name string) { + t.Helper() + if err := c.Create(ctx, &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: name}}); err != nil { + t.Fatal(err) + } + } + // vote votes as user and waits for the operator to follow the post for + // them. The webhook reads posts from the operator's cache, which catches + // up with a new post shortly. + vote := func(t *testing.T, post, user string) { + t.Helper() + as := e.as(t, user) + eventually(t, "vote on "+post, func() error { + err := as.Create(ctx, newVote(post, user)) + if apierrors.IsAlreadyExists(err) { + return nil + } + return err + }) + eventually(t, "follow of "+post, func() error { + return c.Get(ctx, types.NamespacedName{Name: v1.OwnedName(post, user)}, &v1.Subscription{}) + }) + } + updatePost := func(t *testing.T, name string, change func(*v1.Post), status bool) { + t.Helper() + err := retry.RetryOnConflict(retry.DefaultRetry, func() error { + var p v1.Post + if err := c.Get(ctx, types.NamespacedName{Name: name}, &p); err != nil { + return err + } + change(&p) + if status { + return c.Status().Update(ctx, &p) + } + return c.Update(ctx, &p) + }) + if err != nil { + t.Fatal(err) + } + } + merge := func(t *testing.T, from, into string) { + t.Helper() + updatePost(t, from, func(p *v1.Post) { p.Status.MergedInto = into }, true) + } + hide := func(t *testing.T, name string, hidden bool) { + t.Helper() + updatePost(t, name, func(p *v1.Post) { p.Spec.Hidden = hidden }, false) + } + // owners returns who has a vote and who has a follow on post. + owners := func(post string) (votes, follows string, err error) { + var vl v1.VoteList + if err := c.List(ctx, &vl); err != nil { + return "", "", err + } + var sl v1.SubscriptionList + if err := c.List(ctx, &sl); err != nil { + return "", "", err + } + var vs, ss []string + for _, v := range vl.Items { + if v.Spec.PostRef.Name == post && v.DeletionTimestamp == nil { + vs = append(vs, v.Spec.UserRef.Name) + } + } + for _, s := range sl.Items { + if s.Spec.PostRef.Name == post && s.DeletionTimestamp == nil { + ss = append(ss, s.Spec.UserRef.Name) + } + } + sort.Strings(vs) + sort.Strings(ss) + return strings.Join(vs, ","), strings.Join(ss, ","), nil + } + expectOwners := func(post, votes, follows string) func() error { + return func() error { + v, f, err := owners(post) + if err != nil { + return err + } + if v != votes || f != follows { + return errorf(post + ": votes [" + v + "] follows [" + f + "], want [" + votes + "] [" + follows + "]") + } + return nil + } + } + expectCondition := func(post string, status metav1.ConditionStatus, reason, inMessage string) func() error { + return func() error { + var p v1.Post + if err := c.Get(ctx, types.NamespacedName{Name: post}, &p); err != nil { + return err + } + cond := meta.FindStatusCondition(p.Status.Conditions, v1.PostConditionVotesMoved) + if cond == nil { + return errorf(post + ": no " + v1.PostConditionVotesMoved + " condition") + } + if cond.Status != status || cond.Reason != reason || !strings.Contains(cond.Message, inMessage) { + return errorf(post + ": condition " + string(cond.Status) + " " + cond.Reason + ": " + cond.Message) + } + return nil + } + } + // noRetries checks the operator stops trying to create anything + // admission refuses. + noRetries := func(t *testing.T) { + t.Helper() + before := e.denied.Load() + time.Sleep(2 * time.Second) + if after := e.denied.Load(); after != before { + t.Errorf("the operator kept retrying: %d creates refused in 2s", after-before) + } + } + + t.Run("votes follow a merge into a post that is already merged", func(t *testing.T) { + a, b, final := "o.r.1", "o.r.2", "o.r.3" + newPost(t, a) + newPost(t, b) + newPost(t, final) + vote(t, a, "u1") + vote(t, b, "u2") + vote(t, final, "u2") + + merge(t, b, final) + eventually(t, "b's votes on the final post", expectOwners(final, "u2", "u2")) + merge(t, a, b) + + eventually(t, "a's votes on the final post", expectOwners(final, "u1,u2", "u1,u2")) + eventually(t, "nothing left on a", expectOwners(a, "", "")) + eventually(t, "nothing left on b", expectOwners(b, "", "")) + eventually(t, "a's condition", expectCondition(a, metav1.ConditionTrue, v1.PostReasonMoved, final)) + eventually(t, "b's condition", expectCondition(b, metav1.ConditionTrue, v1.PostReasonMoved, final)) + eventually(t, "final totals", func() error { + var p v1.Post + if err := c.Get(ctx, types.NamespacedName{Name: final}, &p); err != nil { + return err + } + if p.Status.Votes.Datum != 2 { + return errorf("final post not tallied") + } + return nil + }) + }) + + t.Run("votes stay on a post merged into a hidden one until it's shown", func(t *testing.T) { + from, hidden := "o.r.10", "o.r.11" + newPost(t, from) + newPost(t, hidden) + vote(t, from, "u1") + hide(t, hidden, true) + merge(t, from, hidden) + + eventually(t, "condition", expectCondition(from, metav1.ConditionFalse, v1.PostReasonTargetUnavailable, hidden)) + noRetries(t) + if err := expectOwners(from, "u1", "u1")(); err != nil { + t.Fatal(err) + } + if err := expectOwners(hidden, "", "")(); err != nil { + t.Fatal(err) + } + + hide(t, hidden, false) + eventually(t, "votes moved once the post is shown", expectOwners(hidden, "u1", "u1")) + eventually(t, "nothing left", expectOwners(from, "", "")) + eventually(t, "condition", expectCondition(from, metav1.ConditionTrue, v1.PostReasonMoved, hidden)) + }) + + t.Run("votes stay on a post merged into one that doesn't exist yet", func(t *testing.T) { + from, missing := "o.r.20", "o.r.21" + newPost(t, from) + vote(t, from, "u1") + merge(t, from, missing) + + eventually(t, "condition", expectCondition(from, metav1.ConditionFalse, v1.PostReasonTargetUnavailable, missing)) + noRetries(t) + if err := expectOwners(from, "u1", "u1")(); err != nil { + t.Fatal(err) + } + + newPost(t, missing) + eventually(t, "votes moved once the post exists", expectOwners(missing, "u1", "u1")) + eventually(t, "condition", expectCondition(from, metav1.ConditionTrue, v1.PostReasonMoved, missing)) + }) + + t.Run("votes stay put when merges loop", func(t *testing.T) { + p, q := "o.r.30", "o.r.31" + newPost(t, p) + newPost(t, q) + vote(t, p, "u1") + vote(t, q, "u2") + updatePost(t, p, func(post *v1.Post) { post.Status.MergedInto = q }, true) + updatePost(t, q, func(post *v1.Post) { post.Status.MergedInto = p }, true) + + eventually(t, "p's condition", expectCondition(p, metav1.ConditionFalse, v1.PostReasonMergeCycle, "")) + eventually(t, "q's condition", expectCondition(q, metav1.ConditionFalse, v1.PostReasonMergeCycle, "")) + noRetries(t) + // p may have moved its votes to q before q was merged back, but + // none are lost. + pv, _, err := owners(p) + if err != nil { + t.Fatal(err) + } + qv, _, err := owners(q) + if err != nil { + t.Fatal(err) + } + all := strings.Split(pv+","+qv, ",") + sort.Strings(all) + if got := strings.Trim(strings.Join(all, ","), ","); got != "u1,u2" { + t.Errorf("votes p [%s] q [%s], want u1 and u2 once each", pv, qv) + } + + // Breaking the loop moves them, and the unmerged post loses its + // condition. + updatePost(t, q, func(post *v1.Post) { post.Status.MergedInto = "" }, true) + eventually(t, "votes moved once the loop is broken", expectOwners(q, "u1,u2", "u1,u2")) + eventually(t, "p's condition", expectCondition(p, metav1.ConditionTrue, v1.PostReasonMoved, q)) + eventually(t, "q's condition removed", func() error { + var post v1.Post + if err := c.Get(ctx, types.NamespacedName{Name: q}, &post); err != nil { + return err + } + if meta.FindStatusCondition(post.Status.Conditions, v1.PostConditionVotesMoved) != nil { + return errorf("still there") + } + return nil + }) + }) +} diff --git a/internal/controller/postvotes.go b/internal/controller/postvotes.go new file mode 100644 index 0000000..e9a732a --- /dev/null +++ b/internal/controller/postvotes.go @@ -0,0 +1,78 @@ +package controller + +import ( + "context" + "time" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// trendingRefresh is how often a post with a trending score is re-tallied, so +// the score decays as votes age. +const trendingRefresh = time.Hour + +// PostVotesReconciler keeps a post's vote totals and trending score current. +// feedback-sync owns status.votes.github and githubReactions; this owns +// total, datum, linked and trendingScore, applied with server-side apply. +type PostVotesReconciler struct { + client.Client + Options +} + +// Reconcile implements reconcile.Reconciler. +func (r *PostVotesReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var post v1.Post + if err := r.Get(ctx, req.NamespacedName, &post); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + var votes v1.VoteList + if err := r.List(ctx, &votes, client.MatchingFields{postIndex: post.Name}); err != nil { + return ctrl.Result{}, err + } + voters := make([]voter, 0, len(votes.Items)) + for _, v := range votes.Items { + if v.DeletionTimestamp == nil { + voters = append(voters, voter{at: v.CreationTimestamp.Time, login: v.Status.GitHubLogin}) + } + } + want := tally(post.Status.Votes, voters, r.now()) + cur := post.Status.Votes + if cur.Total != want.Total || cur.Datum != want.Datum || cur.Linked != want.Linked || cur.TrendingScore != want.TrendingScore { + u := &unstructured.Unstructured{Object: map[string]any{ + "apiVersion": v1.SchemeGroupVersion.String(), + "kind": "Post", + "metadata": map[string]any{"name": post.Name}, + "status": map[string]any{"votes": map[string]any{ + "total": int64(want.Total), + "datum": int64(want.Datum), + "linked": int64(want.Linked), + "trendingScore": want.TrendingScore, + }}, + }} + if err := r.Status().Apply(ctx, client.ApplyConfigurationFromUnstructured(u), client.FieldOwner(FieldManager), client.ForceOwnership); err != nil { + return ctrl.Result{}, err + } + } + if want.TrendingScore > 0 { + return ctrl.Result{RequeueAfter: trendingRefresh}, nil + } + return ctrl.Result{}, nil +} + +// SetupWithManager registers the reconciler for posts and their votes. +func (r *PostVotesReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + Named("post-votes"). + For(&v1.Post{}). + Watches(&v1.Vote{}, handler.EnqueueRequestsFromMapFunc(func(_ context.Context, obj client.Object) []reconcile.Request { + return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: obj.(*v1.Vote).Spec.PostRef.Name}}} + })). + Complete(r) +} diff --git a/internal/controller/setup.go b/internal/controller/setup.go new file mode 100644 index 0000000..d4e9a6b --- /dev/null +++ b/internal/controller/setup.go @@ -0,0 +1,74 @@ +// Package controller holds the operator's reconcilers. Each one makes +// something that follows from a request true shortly after it: a follow for +// a new vote, a post's vote totals, votes moved off a merged post. +// +// Every step is idempotent and keyed on deterministic names, so a crash or a +// retry never duplicates a vote or a follow. +package controller + +import ( + "context" + "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 + // the lookups. + Users milo.Users + // Now returns the current time; tests replace it. + Now func() time.Time +} + +func (o Options) now() time.Time { + if o.Now != nil { + return o.Now() + } + return time.Now() +} + +// Setup adds the field indexes and every reconciler to mgr. +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}, + } { + if err := r.SetupWithManager(mgr); err != nil { + return err + } + } + return nil +} diff --git a/internal/controller/tally.go b/internal/controller/tally.go new file mode 100644 index 0000000..bd48505 --- /dev/null +++ b/internal/controller/tally.go @@ -0,0 +1,70 @@ +package controller + +import ( + "math" + "strings" + "time" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// halfLife is the trending score's half-life. +const halfLife = 7 * 24 * time.Hour + +// voter is one Datum vote, as far as the tally cares. +type voter struct { + at time.Time + login string // linked GitHub username +} + +// weight is a vote's contribution to the trending score. +func weight(at, now time.Time) float64 { + age := max(now.Sub(at), 0) + return math.Pow(0.5, float64(age)/float64(halfLife)) +} + +// tally returns the operator's vote fields for a post from its Datum voters +// and the GitHub fields feedback-sync wrote (github, githubReactions): +// +// total = datum + github - linked +// trending = Σ weight(vote) + Σ weight(👍 from someone who didn't also vote) +// +// Linked people are matched by GitHub username, case-insensitively. +// Reactions without a time count towards the total but not trending. The +// score is stored multiplied by 1000. +func tally(sync v1.PostVotes, voters []voter, now time.Time) v1.PostVotes { + logins := map[string]bool{} + trending := 0.0 + for _, vt := range voters { + if vt.login != "" { + logins[strings.ToLower(vt.login)] = true + } + trending += weight(vt.at, now) + } + seen := map[string]bool{} + var linked int32 + for _, r := range sync.GitHubReactions { + l := strings.ToLower(r.Login) + if seen[l] { + continue + } + seen[l] = true + if logins[l] { + linked++ + continue + } + if !r.At.IsZero() { + trending += weight(r.At.Time, now) + } + } + // The reactions list can lag the count; never subtract more than the + // count says. + linked = min(linked, sync.GitHub) + datum := int32(len(voters)) + return v1.PostVotes{ + Datum: datum, + Linked: linked, + Total: datum + sync.GitHub - linked, + TrendingScore: int64(math.Round(trending * 1000)), + } +} diff --git a/internal/controller/tally_test.go b/internal/controller/tally_test.go new file mode 100644 index 0000000..10c789b --- /dev/null +++ b/internal/controller/tally_test.go @@ -0,0 +1,35 @@ +package controller + +import ( + "testing" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +func TestTally(t *testing.T) { + now := time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC) + week := metav1.NewTime(now.Add(-halfLife)) + voters := []voter{{at: now, login: "ada"}, {at: now.Add(-2 * halfLife)}, {at: now.Add(time.Hour)}} + + got := tally(v1.PostVotes{GitHub: 2, GitHubReactions: []v1.GitHubReaction{ + {Login: "ADA", At: week}, {Login: "ada", At: week}, // same person twice + {Login: "grace", At: week}, + {Login: "linus"}, // no time + }}, voters, now) + // The list is longer than the count (lagging sync): linked can't exceed it. + if got.Datum != 3 || got.Linked != 1 || got.Total != 4 { + t.Errorf("counts: %+v", got) + } + // 1 (now) + 0.25 (two weeks) + 1 (future clamps to now) + 0.5 (grace). + if got.TrendingScore != 2750 { + t.Errorf("trending %v, want 2750", got.TrendingScore) + } + + got = tally(v1.PostVotes{GitHub: 0, GitHubReactions: []v1.GitHubReaction{{Login: "ada", At: week}}}, voters[:1], now) + if got.Linked != 0 || got.Total != 1 { + t.Errorf("linked capped by count: %+v", got) + } +} diff --git a/internal/controller/vote.go b/internal/controller/vote.go new file mode 100644 index 0000000..2740358 --- /dev/null +++ b/internal/controller/vote.go @@ -0,0 +1,147 @@ +package controller + +import ( + "context" + "time" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + 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/builder" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/event" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +const ( + // OrphanedAtAnnotation records when a vote's post went missing. Votes on + // a post that doesn't return within OrphanRetention are deleted. + OrphanedAtAnnotation = "feedback.miloapis.com/orphaned-at" + // OrphanRetention is how long votes wait for a deleted post to return. + OrphanRetention = 30 * 24 * time.Hour + // lookupTimeout bounds the Milo lookups for one vote. + lookupTimeout = 5 * time.Second +) + +// VoteReconciler follows the post for each new voter, records the voter's +// organizations and linked GitHub username, and deletes votes whose post has +// been gone for OrphanRetention. +type VoteReconciler struct { + client.Client + Options +} + +// Reconcile implements reconcile.Reconciler. +func (r *VoteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + var vote v1.Vote + if err := r.Get(ctx, req.NamespacedName, &vote); err != nil || vote.DeletionTimestamp != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + var post v1.Post + err := r.Get(ctx, types.NamespacedName{Name: vote.Spec.PostRef.Name}, &post) + switch { + case apierrors.IsNotFound(err): + return r.orphaned(ctx, &vote) + case err != nil: + return ctrl.Result{}, err + } + if _, ok := vote.Annotations[OrphanedAtAnnotation]; ok { + patch := client.MergeFrom(vote.DeepCopy()) + delete(vote.Annotations, OrphanedAtAnnotation) + if err := r.Patch(ctx, &vote, patch); err != nil { + return ctrl.Result{}, err + } + } + if vote.Status.Subscribed || post.Status.MergedInto != "" { + // Done, or MergeReconciler moves it. + return ctrl.Result{}, nil + } + + sub := &v1.Subscription{ + ObjectMeta: metav1.ObjectMeta{Name: v1.OwnedName(post.Name, vote.Spec.UserRef.Name)}, + Spec: v1.SubscriptionSpec{PostRef: vote.Spec.PostRef, UserRef: vote.Spec.UserRef, Reason: v1.SubscriptionReasonVoted}, + } + if err := r.Create(ctx, sub); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, err + } + + vote.Status.Subscribed = true + if r.Users != nil { + r.lookup(ctx, &vote) + } + return ctrl.Result{}, r.Status().Update(ctx, &vote) +} + +// lookup records the voter's organizations and GitHub username. Failures are +// logged and the vote is recorded without them. +func (r *VoteReconciler) lookup(ctx context.Context, vote *v1.Vote) { + l := log.FromContext(ctx) + user := vote.Spec.UserRef.Name + lctx, cancel := context.WithTimeout(ctx, lookupTimeout) + defer cancel() + if orgs, err := r.Users.Organizations(lctx, user); err != nil { + l.Error(err, "Looking up the voter's organizations failed; recording the vote without them") + } else { + vote.Status.Organizations = orgs + } + if login, err := r.Users.GitHubLogin(lctx, user); err != nil { + l.Error(err, "Looking up the voter's GitHub username failed; a 👍 from them may be counted twice") + } else { + vote.Status.GitHubLogin = login + } +} + +// orphaned handles a vote whose post doesn't exist: it marks when the post +// went missing and deletes the vote once OrphanRetention has passed. +func (r *VoteReconciler) orphaned(ctx context.Context, vote *v1.Vote) (ctrl.Result, error) { + now := r.now() + at, err := time.Parse(time.RFC3339, vote.Annotations[OrphanedAtAnnotation]) + if err != nil { + patch := client.MergeFrom(vote.DeepCopy()) + if vote.Annotations == nil { + vote.Annotations = map[string]string{} + } + vote.Annotations[OrphanedAtAnnotation] = now.UTC().Format(time.RFC3339) + return ctrl.Result{RequeueAfter: OrphanRetention}, r.Patch(ctx, vote, patch) + } + if left := at.Add(OrphanRetention).Sub(now); left > 0 { + return ctrl.Result{RequeueAfter: left}, nil + } + log.FromContext(ctx).Info("Deleting a vote whose post has been gone for the retention period", "post", vote.Spec.PostRef.Name) + return ctrl.Result{}, client.IgnoreNotFound(r.Delete(ctx, vote)) +} + +// SetupWithManager registers the reconciler. Posts appearing or disappearing +// re-check their votes; other post changes don't, so busy posts don't +// re-queue every vote. +func (r *VoteReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + Named("vote"). + For(&v1.Vote{}). + Watches(&v1.Post{}, handler.EnqueueRequestsFromMapFunc(r.votesOf), builder.WithPredicates(predicate.Funcs{ + CreateFunc: func(event.CreateEvent) bool { return true }, + DeleteFunc: func(event.DeleteEvent) bool { return true }, + UpdateFunc: func(event.UpdateEvent) bool { return false }, + GenericFunc: func(event.GenericEvent) bool { return false }, + })). + Complete(r) +} + +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 { + log.FromContext(ctx).Error(err, "Listing a post's votes failed", "post", post.GetName()) + return nil + } + reqs := make([]reconcile.Request, len(votes.Items)) + for i, v := range votes.Items { + reqs[i] = reconcile.Request{NamespacedName: types.NamespacedName{Name: v.Name}} + } + return reqs +} diff --git a/internal/milo/client.go b/internal/milo/client.go new file mode 100644 index 0000000..054a01c --- /dev/null +++ b/internal/milo/client.go @@ -0,0 +1,109 @@ +// Package milo reads what the feedback service needs to know about a user +// from the Milo API: their organizations and linked GitHub username. +package milo + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "slices" + "strings" + + "k8s.io/client-go/rest" +) + +// Users looks users up. Lookups are best effort: a vote is still recorded +// when Milo can't answer. +type Users interface { + // Organizations returns the names of the organizations user belongs to, + // sorted. + Organizations(ctx context.Context, user string) ([]string, error) + // GitHubLogin returns the GitHub username linked to the user, or "". + GitHubLogin(ctx context.Context, user string) (string, error) +} + +// Client calls the Milo API with the operator's own credentials. +type Client struct { + host string + client *http.Client +} + +// NewClient returns a client for the Milo API server in cfg. +func NewClient(cfg *rest.Config) (*Client, error) { + hc, err := rest.HTTPClientFor(cfg) + if err != nil { + return nil, err + } + return &Client{host: strings.TrimSuffix(cfg.Host, "/"), client: hc}, nil +} + +type list[T any] struct { + Items []T `json:"items"` +} + +type membership struct { + Spec struct { + OrganizationRef struct { + Name string `json:"name"` + } `json:"organizationRef"` + } `json:"spec"` +} + +type userIdentity struct { + Status struct { + ProviderName string `json:"providerName"` + Username string `json:"username"` + } `json:"status"` +} + +// Organizations implements Users. +func (c *Client) Organizations(ctx context.Context, user string) ([]string, error) { + var l list[membership] + if err := c.get(ctx, "/apis/resourcemanager.miloapis.com/v1alpha1/organizationmemberships", "spec.userRef.name="+user, &l); err != nil { + return nil, err + } + var orgs []string + for _, m := range l.Items { + if n := m.Spec.OrganizationRef.Name; n != "" && !slices.Contains(orgs, n) { + orgs = append(orgs, n) + } + } + slices.Sort(orgs) + return orgs, nil +} + +// GitHubLogin implements Users. +func (c *Client) GitHubLogin(ctx context.Context, user string) (string, error) { + var l list[userIdentity] + if err := c.get(ctx, "/apis/identity.miloapis.com/v1alpha1/useridentities", "status.userUID="+user, &l); err != nil { + return "", err + } + for _, id := range l.Items { + if strings.Contains(strings.ToLower(id.Status.ProviderName), "github") && id.Status.Username != "" { + return id.Status.Username, nil + } + } + return "", nil +} + +func (c *Client) get(ctx context.Context, path, fieldSelector string, out any) error { + u := c.host + path + "?" + url.Values{"fieldSelector": {fieldSelector}}.Encode() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) + if err != nil { + return err + } + req.Header.Set("Accept", "application/json") + resp, err := c.client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + msg, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) + return fmt.Errorf("GET %s: %s: %s", path, resp.Status, strings.TrimSpace(string(msg))) + } + return json.NewDecoder(resp.Body).Decode(out) +} diff --git a/internal/webhook/envtest_test.go b/internal/webhook/envtest_test.go index 9fb3b36..a5cf410 100644 --- a/internal/webhook/envtest_test.go +++ b/internal/webhook/envtest_test.go @@ -84,10 +84,20 @@ func TestWebhooksInFrontOfAnAPIServer(t *testing.T) { } } + if err := staff.Create(ctx, &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "p1"}}); err != nil { + t.Fatal(err) + } + t.Run("the caller becomes the owner", func(t *testing.T) { v := vote("p1.u1", "") - if err := ada.Create(ctx, v); err != nil { - t.Fatal(err) + // The webhook reads posts from the operator's cache, which catches up + // with the new post shortly. + deadline := time.Now().Add(10 * time.Second) + for err := ada.Create(ctx, v); err != nil; err = ada.Create(ctx, v) { + if time.Now().After(deadline) { + t.Fatal(err) + } + time.Sleep(100 * time.Millisecond) } if v.Spec.UserRef.Name != "u1" { t.Errorf("owner = %q", v.Spec.UserRef.Name) diff --git a/internal/webhook/register.go b/internal/webhook/register.go index f55c5a0..175060b 100644 --- a/internal/webhook/register.go +++ b/internal/webhook/register.go @@ -10,4 +10,5 @@ func Register(mgr manager.Manager) { s := mgr.GetWebhookServer() 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()}}) } diff --git a/internal/webhook/vote.go b/internal/webhook/vote.go new file mode 100644 index 0000000..b7d87d3 --- /dev/null +++ b/internal/webhook/vote.go @@ -0,0 +1,56 @@ +package webhook + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + + admissionv1 "k8s.io/api/admission/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + + "go.miloapis.com/feedback/internal/identity" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +// +kubebuilder:webhook:path=/validate-vote,mutating=false,failurePolicy=fail,sideEffects=None,groups=feedback.miloapis.com,resources=votes;subscriptions,verbs=create,versions=v1alpha1,name=vote.feedback.miloapis.com,admissionReviewVersions=v1 + +// VoteCheck admits votes and follows only on posts people can see: the post +// must exist, not be hidden and not be merged into another. Votes also need +// a verified email address. +type VoteCheck struct { + // Posts reads posts. The operator's cache sees hidden ones. + Posts client.Reader +} + +var _ admission.Handler = VoteCheck{} + +// Handle implements admission.Handler. +func (c VoteCheck) Handle(ctx context.Context, req admission.Request) admission.Response { + if req.Operation != admissionv1.Create { + return admission.Allowed("") + } + if req.Resource.Resource == "votes" && identity.EmailUnverified(req.UserInfo) { + return admission.Denied("verify your email address to vote") + } + obj := &unstructured.Unstructured{} + if err := json.Unmarshal(req.Object.Raw, &obj.Object); err != nil { + return admission.Errored(http.StatusBadRequest, err) + } + name, _, _ := unstructured.NestedString(obj.Object, "spec", "postRef", "name") + var post v1.Post + err := c.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)) + case err != nil: + return admission.Errored(http.StatusInternalServerError, err) + case post.Status.MergedInto != "": + return admission.Denied(fmt.Sprintf("post %q was merged into %q; use that one", name, post.Status.MergedInto)) + } + return admission.Allowed("") +} diff --git a/internal/webhook/vote_test.go b/internal/webhook/vote_test.go new file mode 100644 index 0000000..9536bf5 --- /dev/null +++ b/internal/webhook/vote_test.go @@ -0,0 +1,61 @@ +package webhook + +import ( + "context" + "strings" + "testing" + + 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/fake" + + "go.miloapis.com/feedback/internal/identity" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +func TestVoteCheck(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}}, + &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: "merged"}, Status: v1.PostStatus{MergedInto: "open"}}, + ).Build() + c := VoteCheck{Posts: posts} + + verified := inUserContext("u1") + verified.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{"true"} + unverified := inUserContext("u1") + unverified.Extra[identity.EmailVerifiedExtraKey] = authenticationv1.ExtraValue{"false"} + + onPost := func(post string) *v1.Vote { + return &v1.Vote{Spec: v1.VoteSpec{PostRef: v1.PostReference{Name: post}, UserRef: v1.UserReference{Name: "u1"}}} + } + for _, tc := range []struct { + name, resource, post string + user authenticationv1.UserInfo + denied string + }{ + {"vote", "votes", "open", verified, ""}, + {"unverified email", "votes", "open", unverified, "verify your email"}, + {"unverified can still follow", "subscriptions", "open", unverified, ""}, + {"missing post", "votes", "nope", verified, "not found"}, + {"hidden post looks missing", "votes", "hidden", verified, "not found"}, + {"merged post", "subscriptions", "merged", verified, `merged into "open"`}, + } { + t.Run(tc.name, func(t *testing.T) { + resp := c.Handle(context.Background(), request(t, admissionv1.Create, tc.resource, tc.user, onPost(tc.post), nil)) + if tc.denied == "" { + if !resp.Allowed { + t.Errorf("denied: %v", resp.Result.Message) + } + return + } + if resp.Allowed || !strings.Contains(resp.Result.Message, tc.denied) { + t.Errorf("allowed %v, message %q; want denial containing %q", resp.Allowed, resp.Result.Message, tc.denied) + } + }) + } +} diff --git a/pkg/apis/feedback/v1alpha1/post_types.go b/pkg/apis/feedback/v1alpha1/post_types.go index 738d236..5d017ba 100644 --- a/pkg/apis/feedback/v1alpha1/post_types.go +++ b/pkg/apis/feedback/v1alpha1/post_types.go @@ -142,8 +142,37 @@ type PostStatus struct { // LastSyncedAt is when feedback-sync last wrote this post. // +optional LastSyncedAt *metav1.Time `json:"lastSyncedAt,omitempty"` + // Conditions are written by the operator. A merged post carries + // VotesMoved: whether its votes and follows reached the post at the end + // of its merge chain, and if not, why. Posts are public, so messages + // never say a post is hidden. + // +optional + // +listType=map + // +listMapKey=type + Conditions []metav1.Condition `json:"conditions,omitempty"` } +// Post conditions and their reasons. +const ( + // PostConditionVotesMoved is set on a post with status.mergedInto. True + // means its votes and follows were moved to the post at the end of the + // merge chain, named in the message. False means they stay on this post + // until the chain leads somewhere they can go; the reason says why. + PostConditionVotesMoved = "VotesMoved" + + // PostReasonMoved: the votes and follows were moved. + PostReasonMoved = "Moved" + // PostReasonTargetUnavailable: the chain ends at a post that doesn't + // exist or that people can't see. They move once it is available. + PostReasonTargetUnavailable = "TargetUnavailable" + // PostReasonMergeCycle: the chain loops back on itself. They move once + // feedback-sync breaks the loop. + PostReasonMergeCycle = "MergeCycle" + // PostReasonMergeChainTooLong: the chain is longer than the operator + // follows. They move once it is shorter. + PostReasonMergeChainTooLong = "MergeChainTooLong" +) + // GitHubIssue identifies the issue backing a post. type GitHubIssue struct { Org string `json:"org"` diff --git a/pkg/apis/feedback/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/feedback/v1alpha1/zz_generated.deepcopy.go index af5af6c..ed78436 100644 --- a/pkg/apis/feedback/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/feedback/v1alpha1/zz_generated.deepcopy.go @@ -850,6 +850,13 @@ func (in *PostStatus) DeepCopyInto(out *PostStatus) { in, out := &in.LastSyncedAt, &out.LastSyncedAt *out = (*in).DeepCopy() } + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]v1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PostStatus.