Skip to content
Closed
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
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ entries. It runs as an aggregated API server under Milo, like
## Status

Early. Every resource in the design has a v1alpha1 type in
`pkg/apis/feedback/v1alpha1` and a sample in `config/samples`, but the server
only serves `FeedbackSource` so far. Storage for the rest is tracked as
`pkg/apis/feedback/v1alpha1` and a sample in `config/samples`, and the server
serves `FeedbackSource`, `Post`, `Release` and `ChangelogEntry`. Storage for the rest is tracked as
sub-issues of the epic.

| Resource | Storage | State |
| --- | --- | --- |
| `FeedbackSource` | etcd | served |
| `Post`, `PostQuery`, `Release`, `ChangelogEntry` | Postgres | planned |
| `Post`, `Release`, `ChangelogEntry` | Postgres | served |
| `PostQuery` | Postgres | planned |
| `Vote`, `Subscription` | Postgres | planned |
| `Submission`, `SecurityReport`, `SecurityAcknowledgement` | Postgres | planned |

Expand Down Expand Up @@ -59,6 +60,27 @@ runs it as an init container, and a Postgres session lock keeps replicas from
migrating at the same time. Add a migration as the next numbered file in
`migrations/`.

## Who writes what

`feedback-sync` and `feedback-controller` are recognised by username
(`--sync-users`, `--controller-users`; defaults are the `feedback-system`
service accounts). On top of Milo's IAM:

- Only `feedback-sync` creates and deletes posts, and writes releases and
changelog entries.
- Post status is split: `feedback-sync` owns the GitHub-derived fields and
delivery work items; `feedback-controller` owns the delivery stage,
history, `highestNotified` and part-shipped records; the server computes
vote counts. Fields a caller doesn't own are ignored, like spec on a
status update. Anyone else gets Forbidden on post status.
- Staff can change `spec.hidden` and metadata. Hidden posts are NotFound
outside the platform context and never listed on releases or changelog
entries.
- `Release.status.posts` and `ChangelogEntry.status.posts` are derived from
the posts table on every read.
- Votes on a deleted post are kept for `--vote-retention` (30 days) in case
the post comes back.

## Development

```bash
Expand Down
21 changes: 18 additions & 3 deletions cmd/feedback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/kube-openapi/pkg/validation/spec"

feedbackapiserver "go.miloapis.com/feedback/internal/apiserver"
"go.miloapis.com/feedback/internal/identity"
"go.miloapis.com/feedback/internal/postgres"
"go.miloapis.com/feedback/internal/version"
"go.miloapis.com/feedback/pkg/generated/openapi"
Expand Down Expand Up @@ -121,6 +122,8 @@ type serverOptions struct {
RecommendedOptions *options.RecommendedOptions
Logs *logsapi.LoggingConfiguration
Postgres postgres.Config
Identities identity.Identities
VoteRetention time.Duration
}

func newServerOptions() *serverOptions {
Expand All @@ -129,8 +132,10 @@ func newServerOptions() *serverOptions {
"/registry/feedback.miloapis.com",
feedbackapiserver.Codecs.LegacyCodec(feedbackapiserver.Scheme.PrioritizedVersionsAllGroups()...),
),
Logs: logsapi.NewLoggingConfiguration(),
Postgres: postgres.Config{MaxConns: 20, ConnectTimeout: 10 * time.Second},
Logs: logsapi.NewLoggingConfiguration(),
Postgres: postgres.Config{MaxConns: 20, ConnectTimeout: 10 * time.Second},
Identities: identity.Default,
VoteRetention: 30 * 24 * time.Hour,
}
// No admission plugins: validation lives in the registry strategies.
o.RecommendedOptions.Admission = nil
Expand All @@ -145,6 +150,12 @@ func (o *serverOptions) AddFlags(fs *pflag.FlagSet) {
"Maximum number of connections in the Postgres pool.")
fs.DurationVar(&o.Postgres.ConnectTimeout, "postgres-connect-timeout", o.Postgres.ConnectTimeout,
"Timeout for the initial Postgres connection.")
fs.StringSliceVar(&o.Identities.Sync, "sync-users", o.Identities.Sync,
"Usernames of feedback-sync, the only writer of posts, releases and changelog entries.")
fs.StringSliceVar(&o.Identities.Controller, "controller-users", o.Identities.Controller,
"Usernames of feedback-controller, which owns delivery tracking on posts.")
fs.DurationVar(&o.VoteRetention, "vote-retention", o.VoteRetention,
"How long votes on a deleted post are kept in case it returns. 0 disables the purge.")
}

func (o *serverOptions) Complete() error {
Expand Down Expand Up @@ -210,7 +221,11 @@ func (o *serverOptions) Config() (*feedbackapiserver.Config, error) {

return &feedbackapiserver.Config{
GenericConfig: genericConfig,
ExtraConfig: feedbackapiserver.ExtraConfig{Postgres: o.Postgres},
ExtraConfig: feedbackapiserver.ExtraConfig{
Postgres: o.Postgres,
Identities: o.Identities,
VoteRetention: o.VoteRetention,
},
}, nil
}

Expand Down
71 changes: 70 additions & 1 deletion internal/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package apiserver
import (
"context"
"fmt"
"time"

"github.com/jackc/pgx/v5/pgxpool"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -14,8 +15,12 @@ import (
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/klog/v2"

"go.miloapis.com/feedback/internal/identity"
"go.miloapis.com/feedback/internal/postgres"
"go.miloapis.com/feedback/internal/registry/feedbacksource"
"go.miloapis.com/feedback/internal/registry/post"
"go.miloapis.com/feedback/internal/registry/release"
"go.miloapis.com/feedback/internal/storage/pg"
"go.miloapis.com/feedback/pkg/apis/feedback/install"
feedbackv1alpha1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1"
)
Expand Down Expand Up @@ -50,6 +55,12 @@ func init() {
// ExtraConfig holds feedback-specific configuration.
type ExtraConfig struct {
Postgres postgres.Config
// Identities are the usernames of feedback-sync and feedback-controller,
// which own particular fields.
Identities identity.Identities
// VoteRetention is how long votes on a deleted post are kept in case the
// post comes back.
VoteRetention time.Duration
}

// Config combines the generic and feedback-specific configuration.
Expand Down Expand Up @@ -110,7 +121,35 @@ func (c completedConfig) New(ctx context.Context) (*FeedbackServer, error) {
v1alpha1Storage["feedbacksources"] = sourceStorage.Store
v1alpha1Storage["feedbacksources/status"] = sourceStorage.Status

// Postgres-backed resources (Post, PostQuery, Vote, Submission, ...) are
// Everything else is in Postgres.
db := &pg.DB{Pool: pool}
ids := c.ExtraConfig.Identities

posts, err := post.NewREST(db, Scheme, ids)
if err != nil {
pool.Close()
return nil, fmt.Errorf("create Post storage: %w", err)
}
v1alpha1Storage["posts"] = posts.Store
v1alpha1Storage["posts/status"] = posts.Status

releases, err := release.NewReleaseREST(db, Scheme, ids)
if err != nil {
pool.Close()
return nil, fmt.Errorf("create Release storage: %w", err)
}
v1alpha1Storage["releases"] = releases.Store
v1alpha1Storage["releases/status"] = releases.Status

changelog, err := release.NewChangelogREST(db, Scheme, ids)
if err != nil {
pool.Close()
return nil, fmt.Errorf("create ChangelogEntry storage: %w", err)
}
v1alpha1Storage["changelogentries"] = changelog.Store
v1alpha1Storage["changelogentries/status"] = changelog.Status

// Other Postgres-backed resources (PostQuery, Vote, Submission, ...) are
// added here as they are implemented. See datum-cloud/enhancements#943.

apiGroupInfo.VersionedResourcesStorageMap[feedbackv1alpha1.SchemeGroupVersion.Version] = v1alpha1Storage
Expand All @@ -119,10 +158,40 @@ func (c completedConfig) New(ctx context.Context) (*FeedbackServer, error) {
return nil, err
}

retention := c.ExtraConfig.VoteRetention
genericServer.AddPostStartHookOrDie("feedback-vote-retention", func(hc genericapiserver.PostStartHookContext) error {
go runVoteRetention(hc.Context, db, retention)
return nil
})

klog.Info("Feedback server initialized")
return s, nil
}

// runVoteRetention purges votes on long-deleted posts every hour.
func runVoteRetention(ctx context.Context, db *pg.DB, retention time.Duration) {
if retention <= 0 {
klog.Info("Vote retention purge disabled")
return
}
t := time.NewTicker(time.Hour)
defer t.Stop()
for {
n, err := post.PurgeExpiredVotes(ctx, db, retention)
switch {
case err != nil && ctx.Err() == nil:
klog.ErrorS(err, "Purging votes on deleted posts failed")
case n > 0:
klog.InfoS("Purged votes on deleted posts", "votes", n, "retention", retention)
}
select {
case <-ctx.Done():
return
case <-t.C:
}
}
}

// Run serves until ctx is cancelled, then closes the Postgres pool.
func (s *FeedbackServer) Run(ctx context.Context) error {
defer s.pool.Close()
Expand Down
68 changes: 68 additions & 0 deletions internal/identity/identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Package identity recognises the feedback service's own writers.
//
// Milo's authorization decides which verbs a caller may use on a resource.
// Some rules are about fields within a resource (only feedback-sync writes a
// post's GitHub fields, only feedback-controller writes its delivery
// tracker), which IAM can't express, so strategies check the caller here.
package identity

import (
"context"
"slices"

"k8s.io/apiserver/pkg/endpoints/request"
)

// Identities holds the usernames of the service's own components.
type Identities struct {
// Sync are feedback-sync's usernames. It is the only writer of
// GitHub-derived content.
Sync []string
// Controller are feedback-controller's usernames. It writes triage and
// delivery tracking.
Controller []string
}

// Default usernames, matching the Kubernetes service accounts in
// feedback-system. Override with flags where Milo names them differently.
var Default = Identities{
Sync: []string{"system:serviceaccount:feedback-system:feedback-sync"},
Controller: []string{"system:serviceaccount:feedback-system:feedback-controller"},
}

// Caller is who a request comes from, as far as field ownership goes.
type Caller int

const (
// Other is anyone else: staff, the website, users.
Other Caller = iota
Sync
Controller
)

func (c Caller) String() string {
switch c {
case Sync:
return "feedback-sync"
case Controller:
return "feedback-controller"
default:
return "other"
}
}

// Of returns the caller for ctx.
func (ids Identities) Of(ctx context.Context) Caller {
u, ok := request.UserFrom(ctx)
if !ok {
return Other
}
switch name := u.GetName(); {
case slices.Contains(ids.Sync, name):
return Sync
case slices.Contains(ids.Controller, name):
return Controller
default:
return Other
}
}
Loading
Loading