Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| --- | --- | --- |
Expand Down Expand Up @@ -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: <name>`. 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 |
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
13 changes: 11 additions & 2 deletions cmd/feedback/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"time"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -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"
Expand All @@ -38,6 +40,8 @@ type operatorOptions struct {
leaderElectionNamespace string
webhookPort int
webhookCertDir string
submissionLimit int
submissionWindow time.Duration
}

func newOperatorCommand() *cobra.Command {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
26 changes: 1 addition & 25 deletions config/milo/webhook/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,34 +16,10 @@ 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
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
51 changes: 45 additions & 6 deletions config/milo/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
128 changes: 128 additions & 0 deletions internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
})
}
Loading
Loading