mirror of
https://github.com/rancher/steve.git
synced 2025-09-03 16:35:25 +00:00
Add debug logs and env var for user schema caches
This commit is contained in:
committed by
Silvio Moioli
parent
a80bf83f76
commit
ec2b29d9f5
@@ -5,16 +5,42 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/rancher/apiserver/pkg/builtin"
|
||||
"github.com/rancher/apiserver/pkg/types"
|
||||
"github.com/rancher/steve/pkg/accesscontrol"
|
||||
"github.com/rancher/steve/pkg/attributes"
|
||||
"github.com/sirupsen/logrus"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultExpiry = 24 * time.Hour
|
||||
)
|
||||
|
||||
var (
|
||||
schemasExpiry = defaultExpiry
|
||||
logSizeDebug = false
|
||||
)
|
||||
|
||||
func init() {
|
||||
if showSizeDebug := os.Getenv("CATTLE_LOG_CACHE_SIZE_DEBUG"); showSizeDebug == "true" {
|
||||
logSizeDebug = true
|
||||
}
|
||||
if expiry := os.Getenv("CATTLE_SCHEMAS_CACHE_EXPIRY"); expiry != "" {
|
||||
expInt, err := strconv.Atoi(expiry)
|
||||
if err != nil {
|
||||
logrus.Errorf("failed to set user schemas cache size: %v", err)
|
||||
return
|
||||
}
|
||||
schemasExpiry = time.Duration(expInt) * time.Hour
|
||||
}
|
||||
}
|
||||
|
||||
type Factory interface {
|
||||
Schemas(user user.Info) (*types.APISchemas, error)
|
||||
ByGVR(gvr schema.GroupVersionResource) string
|
||||
@@ -63,8 +89,15 @@ func (c *Collection) removeOldRecords(access *accesscontrol.AccessSet, user user
|
||||
}
|
||||
|
||||
func (c *Collection) addToCache(access *accesscontrol.AccessSet, user user.Info, schemas *types.APISchemas) {
|
||||
c.cache.Add(access.ID, schemas, 24*time.Hour)
|
||||
c.userCache.Add(user.GetName(), access.ID, 24*time.Hour)
|
||||
cacheSize := len(c.cache.Keys())
|
||||
if cacheSize >= userSchemasCacheSize {
|
||||
logrus.Debugf("user schemas cache is full. set size limit [%d], records will be evicted", userSchemasCacheSize)
|
||||
}
|
||||
if logSizeDebug {
|
||||
logrus.Debugf("current size of schemas cache [%d], access ID being added [%s]", cacheSize, access.ID)
|
||||
}
|
||||
c.cache.Add(access.ID, schemas, schemasExpiry)
|
||||
c.userCache.Add(user.GetName(), access.ID, schemasExpiry)
|
||||
}
|
||||
|
||||
// PurgeUserRecords removes a record from the backing LRU cache before expiry
|
||||
|
Reference in New Issue
Block a user