1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-27 19:05:09 +00:00
steve/pkg/stores/proxy/watch_refresh.go
Silvio Moioli 7a84620e8b
SQLite backed cache (#223)
This uses SQLite-backed informers provided by Lasso with https://github.com/rancher/lasso/pull/65 to implement Steve API (/v1/) functionality.

This new functionality is available behind a feature flag to be specified at Steve startup

See https://confluence.suse.com/pages/viewpage.action?pageId=1359086083 

Co-authored-by: Ricardo Weir <ricardo.weir@suse.com>
Co-authored-by: Michael Bolot <michael.bolot@suse.com>
Co-authored-by: Silvio Moioli <silvio@moioli.net>
Signed-off-by: Silvio Moioli <silvio@moioli.net>
2024-06-05 16:17:12 +02:00

56 lines
1.3 KiB
Go

package proxy
import (
"context"
"time"
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/steve/pkg/accesscontrol"
"k8s.io/apiserver/pkg/endpoints/request"
)
// WatchRefresh implements types.Store with awareness of changes to the requester's access.
type WatchRefresh struct {
types.Store
asl accesscontrol.AccessSetLookup
}
// NewWatchRefresh returns a new store with awareness of changes to the requester's access.
func NewWatchRefresh(s types.Store, asl accesscontrol.AccessSetLookup) *WatchRefresh {
return &WatchRefresh{
Store: s,
asl: asl,
}
}
// Watch performs a watch request which halts if the user's access level changes.
func (w *WatchRefresh) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types.WatchRequest) (chan types.APIEvent, error) {
user, ok := request.UserFrom(apiOp.Context())
if !ok {
return w.Store.Watch(apiOp, schema, wr)
}
as := w.asl.AccessFor(user)
ctx, cancel := context.WithCancel(apiOp.Context())
apiOp = apiOp.WithContext(ctx)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(2 * time.Second):
}
newAs := w.asl.AccessFor(user)
if as.ID != newAs.ID {
// RBAC changed
cancel()
return
}
}
}()
return w.Store.Watch(apiOp, schema, wr)
}