Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite backed cache #223

Merged
merged 62 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
31b8cdb
Drop LRU caching from partition.Store
moio Feb 15, 2023
fb7d4f5
Add or filtering
rmweir Nov 1, 2023
a95c28a
Add projectsornamespacesfilter
rmweir Nov 3, 2023
e135bae
Move sql informer to lasso
rmweir Nov 17, 2023
ec100de
asd
rmweir Feb 8, 2024
7f20ee9
downgrade k8s packages
rmweir Feb 8, 2024
497dc97
fix partial match
rmweir Feb 16, 2024
5a5efd3
Add context
rmweir Mar 18, 2024
6177c6c
wip
rmweir Apr 13, 2024
1e72706
go mod
rmweir Apr 16, 2024
ff3b327
import wrangler v2 packages
rmweir Apr 16, 2024
a8483d0
import wrangler v2 packages
rmweir Apr 16, 2024
5fd2d94
fix
rmweir Apr 17, 2024
b64c242
Move type indexes to steve
rmweir Apr 24, 2024
49a95b9
Fix strict filters
rmweir Apr 24, 2024
55d3659
Add tests
rmweir May 1, 2024
b1fa554
Add tests
rmweir May 1, 2024
dfa83b9
Fix indexing
rmweir May 1, 2024
0dfe04f
wip tests
rmweir May 6, 2024
9d4b1a9
Add tests
rmweir May 8, 2024
8c19350
Add tests
rmweir May 9, 2024
f186ae5
wip tests
rmweir May 10, 2024
cdf6763
Add processor tests
rmweir May 14, 2024
c1fb6d9
Switch from Informer to Cache
rmweir May 23, 2024
e98ae90
Fix index
rmweir May 25, 2024
e6d72ed
Remove TODOs add comment
rmweir May 30, 2024
3b64ecc
Fix tests
rmweir May 30, 2024
42c45ff
go mod tidy
rmweir May 30, 2024
d8a7583
Update lasso branch- change me
rmweir May 30, 2024
0b992f2
goimports
rmweir May 30, 2024
cfdf52c
change package name to make linter happy
moio May 31, 2024
ea68d15
Fix remaining package issues
rmweir May 31, 2024
dd49955
replace lasso with merged version
moio Jun 3, 2024
ccc4153
undo renaming
moio Jun 3, 2024
1b132e4
undo cosmetic change
moio Jun 3, 2024
38d54f0
development logging line removed
moio Jun 3, 2024
67dddc2
propagate changes in https://github.com/rancher/steve/pull/161
moio Jun 3, 2024
8a43d93
delete stale empty file
moio Jun 3, 2024
4d520bb
dead code removed
moio Jun 3, 2024
40bf139
dead code removal
moio Jun 3, 2024
944fbda
uniform mock test file name to standard pattern
moio Jun 3, 2024
885c273
propagate changes from https://github.com/rancher/steve/pull/141
moio Jun 3, 2024
9fb1a89
improve doc
moio Jun 3, 2024
1733fed
Remove wrangler v1 dependency
rmweir Jun 3, 2024
5126b20
Fix apigroup
rmweir Jun 4, 2024
810adb2
Change context
rmweir Jun 4, 2024
e9e1bba
Add comments to initializeNamespaceCache
rmweir Jun 4, 2024
7d10ace
unbreak tests
moio Jun 4, 2024
39a1e11
drop dead code
moio Jun 4, 2024
b9c3a93
correct comment reference
moio Jun 4, 2024
f594b49
dead code removal
moio Jun 4, 2024
8d7a4a0
Apply suggestions from code review
moio Jun 4, 2024
ddac6ca
Apply suggestions from code review
moio Jun 4, 2024
4665897
DRY up error_wrapper.go
moio Jun 4, 2024
f621cc4
DRY up watch_refresh.go
moio Jun 4, 2024
349dd0d
DRY up unformatter.go
moio Jun 4, 2024
22bca03
DRY up partition/store.go
moio Jun 4, 2024
577a40d
DRY up server.go
moio Jun 4, 2024
06209d4
DRY up clicontext.go
moio Jun 4, 2024
7e56c0f
change prefix from alpha to sql
moio Jun 5, 2024
2df9996
add doc
moio Jun 5, 2024
2d1f4bf
Refactor according to the Func pattern
moio Jun 5, 2024
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
26 changes: 20 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/rancher/steve

go 1.22
go 1.22.0

toolchain go1.22.2

replace (
github.com/crewjam/saml => github.com/rancher/saml v0.2.0
Expand All @@ -21,6 +23,7 @@ require (
github.com/rancher/apiserver v0.0.0-20240503193545-2e1b0ddd9791
github.com/rancher/dynamiclistener v0.5.0-rc6
github.com/rancher/kubernetes-provider-detector v0.1.5
github.com/rancher/lasso v0.0.0-20240603075835-701e919d08b7
github.com/rancher/norman v0.0.0-20240503193601-9f5f6586bb5b
github.com/rancher/remotedialer v0.3.2
github.com/rancher/wrangler/v2 v2.2.0-rc6
Expand All @@ -30,14 +33,14 @@ require (
github.com/urfave/cli/v2 v2.27.1
golang.org/x/sync v0.7.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.29.3
k8s.io/api v0.30.0
k8s.io/apiextensions-apiserver v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/apimachinery v0.30.0
k8s.io/apiserver v0.29.3
k8s.io/client-go v12.0.0+incompatible
k8s.io/klog v1.0.0
k8s.io/kube-aggregator v0.29.3
k8s.io/kube-openapi v0.0.0-20240411171206-dc4e619f62f3
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
)

require (
Expand All @@ -47,6 +50,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
Expand All @@ -60,22 +64,25 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rancher/lasso v0.0.0-20240424194130-d87ec407d941 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
Expand Down Expand Up @@ -104,6 +111,13 @@ require (
k8s.io/component-base v0.29.3 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.49.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.29.10 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 // indirect
sigs.k8s.io/cli-utils v0.35.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
63 changes: 53 additions & 10 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
func run(_ *cli.Context) error {
ctx := signals.SetupSignalContext()
debugconfig.MustSetupDebug()
s, err := config.ToServer(ctx)
s, err := config.ToServer(ctx, false)
moio marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/controllers/schema/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ var (
}
)

type SchemasHandler interface {
OnSchemas(schemas *schema2.Collection) error
type SchemasHandlerFunc func(schemas *schema2.Collection) error

func (s SchemasHandlerFunc) OnSchemas(schemas *schema2.Collection) error {
return s(schemas)
}

type handler struct {
Expand All @@ -45,7 +47,7 @@ type handler struct {
cols *common.DynamicColumns
crd apiextcontrollerv1.CustomResourceDefinitionClient
ssar authorizationv1client.SelfSubjectAccessReviewInterface
handler SchemasHandler
handler SchemasHandlerFunc
}

func Register(ctx context.Context,
Expand All @@ -54,7 +56,7 @@ func Register(ctx context.Context,
crd apiextcontrollerv1.CustomResourceDefinitionController,
apiService v1.APIServiceController,
ssar authorizationv1client.SelfSubjectAccessReviewInterface,
schemasHandler SchemasHandler,
schemasHandler SchemasHandlerFunc,
schemas *schema2.Collection) {

h := &handler{
Expand Down
8 changes: 8 additions & 0 deletions pkg/resources/common/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func DefaultTemplate(clientGetter proxy.ClientGetter,
}
}

// DefaultTemplateForStore provides a default schema template which uses a provided, pre-initialized store. Primarily used when creating a Template that uses a Lasso SQL store internally.
func DefaultTemplateForStore(store types.Store, summaryCache *summarycache.SummaryCache) schema.Template {
return schema.Template{
Store: store,
Formatter: formatter(summaryCache),
}
}

func selfLink(gvr schema2.GroupVersionResource, meta metav1.Object) (prefix string) {
buf := &strings.Builder{}
if gvr.Group == "management.cattle.io" && gvr.Version == "v3" {
Expand Down
30 changes: 30 additions & 0 deletions pkg/resources/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,33 @@ func DefaultSchemaTemplates(cf *client.Factory,
},
}
}

// DefaultSchemaTemplatesForStore returns the same default templates as DefaultSchemaTemplates, only using DefaultSchemaTemplateFoStore internally to construct the templates.
func DefaultSchemaTemplatesForStore(store types.Store,
baseSchemas *types.APISchemas,
summaryCache *summarycache.SummaryCache,
discovery discovery.DiscoveryInterface) []schema.Template {

return []schema.Template{
common.DefaultTemplateForStore(store, summaryCache),
apigroups.Template(discovery),
{
ID: "configmap",
Formatter: formatters.DropHelmData,
},
{
ID: "secret",
Formatter: formatters.DropHelmData,
},
{
ID: "pod",
Formatter: formatters.Pod,
},
{
ID: "management.cattle.io.cluster",
Customize: func(apiSchema *types.APISchema) {
cluster.AddApply(baseSchemas, apiSchema)
},
},
}
}
5 changes: 3 additions & 2 deletions pkg/server/cli/clicontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ type Config struct {
}

func (c *Config) MustServer(ctx context.Context) *server.Server {
cc, err := c.ToServer(ctx)
cc, err := c.ToServer(ctx, false)
if err != nil {
panic(err)
}
return cc
}

func (c *Config) ToServer(ctx context.Context) (*server.Server, error) {
func (c *Config) ToServer(ctx context.Context, sqlCache bool) (*server.Server, error) {
var (
auth steveauth.Middleware
)
Expand All @@ -51,6 +51,7 @@ func (c *Config) ToServer(ctx context.Context) (*server.Server, error) {
return server.New(ctx, restConfig, &server.Options{
AuthMiddleware: auth,
Next: ui.New(c.UIPath),
SQLCache: sqlCache,
})
}

Expand Down
58 changes: 52 additions & 6 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"github.com/rancher/steve/pkg/schema/definitions"
"github.com/rancher/steve/pkg/server/handler"
"github.com/rancher/steve/pkg/server/router"
metricsStore "github.com/rancher/steve/pkg/stores/metrics"
"github.com/rancher/steve/pkg/stores/proxy"
"github.com/rancher/steve/pkg/stores/sqlpartition"
"github.com/rancher/steve/pkg/stores/sqlproxy"
"github.com/rancher/steve/pkg/summarycache"
"k8s.io/client-go/rest"
)
Expand Down Expand Up @@ -48,6 +52,7 @@ type Server struct {

aggregationSecretNamespace string
aggregationSecretName string
SQLCache bool
}

type Options struct {
Expand All @@ -62,6 +67,8 @@ type Options struct {
AggregationSecretName string
ClusterRegistry string
ServerVersion string
// SQLCache enables the SQLite-based lasso caching mechanism
SQLCache bool
}

func New(ctx context.Context, restConfig *rest.Config, opts *Options) (*Server, error) {
Expand All @@ -81,6 +88,8 @@ func New(ctx context.Context, restConfig *rest.Config, opts *Options) (*Server,
aggregationSecretName: opts.AggregationSecretName,
ClusterRegistry: opts.ClusterRegistry,
Version: opts.ServerVersion,
// SQLCache enables the SQLite-based lasso caching mechanism
SQLCache: opts.SQLCache,
}

if err := setup(ctx, server); err != nil {
Expand Down Expand Up @@ -147,16 +156,52 @@ func setup(ctx context.Context, server *Server) error {

summaryCache := summarycache.New(sf, ccache)
summaryCache.Start(ctx)

for _, template := range resources.DefaultSchemaTemplates(cf, server.BaseSchemas, summaryCache, asl, server.controllers.K8s.Discovery(), server.controllers.Core.Namespace().Cache()) {
sf.AddTemplate(template)
}

cols, err := common.NewDynamicColumns(server.RESTConfig)
if err != nil {
return err
}

var onSchemasHandler schemacontroller.SchemasHandlerFunc
if server.SQLCache {
s, err := sqlproxy.NewProxyStore(cols, cf, summaryCache, nil)
if err != nil {
panic(err)
}

errStore := proxy.NewErrorStore(
proxy.NewUnformatterStore(
proxy.NewWatchRefresh(
sqlpartition.NewStore(
s,
asl,
),
asl,
),
),
)
store := metricsStore.NewMetricsStore(errStore)
// end store setup code

for _, template := range resources.DefaultSchemaTemplatesForStore(store, server.BaseSchemas, summaryCache, server.controllers.K8s.Discovery()) {
sf.AddTemplate(template)
}

onSchemasHandler = func(schemas *schema.Collection) error {
if err := ccache.OnSchemas(schemas); err != nil {
return err
}
if err := s.Reset(); err != nil {
return err
}
return nil
}
} else {
for _, template := range resources.DefaultSchemaTemplates(cf, server.BaseSchemas, summaryCache, asl, server.controllers.K8s.Discovery(), server.controllers.Core.Namespace().Cache()) {
sf.AddTemplate(template)
}
onSchemasHandler = ccache.OnSchemas
}

schemas.SetupWatcher(ctx, server.BaseSchemas, asl, sf)

schemacontroller.Register(ctx,
Expand All @@ -165,7 +210,7 @@ func setup(ctx context.Context, server *Server) error {
server.controllers.CRD.CustomResourceDefinition(),
server.controllers.API.APIService(),
server.controllers.K8s.AuthorizationV1().SelfSubjectAccessReviews(),
ccache,
onSchemasHandler,
sf)

apiServer, handler, err := handler.New(server.RESTConfig, sf, server.authMiddleware, server.next, server.router)
Expand All @@ -176,6 +221,7 @@ func setup(ctx context.Context, server *Server) error {
server.APIServer = apiServer
server.Handler = handler
server.SchemaFactory = sf

return nil
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/stores/partition/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id stri
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

// ByID looks up a single object by its ID.
Expand All @@ -124,7 +124,7 @@ func (s *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

func (s *Store) listPartition(ctx context.Context, apiOp *types.APIRequest, schema *types.APISchema, partition Partition,
Expand Down Expand Up @@ -226,7 +226,7 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.AP

for _, item := range list {
item := item.DeepCopy()
result.Objects = append(result.Objects, toAPI(schema, item, nil))
result.Objects = append(result.Objects, ToAPI(schema, item, nil))
}

result.Pages = pages
Expand Down Expand Up @@ -266,7 +266,7 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

// Update updates a single object in the store.
Expand All @@ -280,7 +280,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

// Watch returns a channel of events for a list or resource.
Expand Down Expand Up @@ -310,7 +310,7 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types
return err
}
for i := range c {
response <- toAPIEvent(apiOp, schema, i)
response <- ToAPIEvent(apiOp, schema, i)
}
return nil
})
Expand All @@ -326,7 +326,7 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types
return response, nil
}

func toAPI(schema *types.APISchema, obj runtime.Object, warnings []types.Warning) types.APIObject {
func ToAPI(schema *types.APISchema, obj runtime.Object, warnings []types.Warning) types.APIObject {
if obj == nil || reflect.ValueOf(obj).IsNil() {
return types.APIObject{}
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func moveToUnderscore(obj *unstructured.Unstructured) *unstructured.Unstructured
return obj
}

func toAPIEvent(apiOp *types.APIRequest, schema *types.APISchema, event watch.Event) types.APIEvent {
func ToAPIEvent(apiOp *types.APIRequest, schema *types.APISchema, event watch.Event) types.APIEvent {
name := types.ChangeAPIEvent
switch event.Type {
case watch.Deleted:
Expand All @@ -393,7 +393,7 @@ func toAPIEvent(apiOp *types.APIRequest, schema *types.APISchema, event watch.Ev
return apiEvent
}

apiEvent.Object = toAPI(schema, event.Object, nil)
apiEvent.Object = ToAPI(schema, event.Object, nil)

m, err := meta.Accessor(event.Object)
if err != nil {
Expand Down
Loading
Loading