Skip to content

Commit

Permalink
rework cache
Browse files Browse the repository at this point in the history
  • Loading branch information
probakowski committed Oct 16, 2024
1 parent 52e7576 commit 970503d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 36 deletions.
6 changes: 0 additions & 6 deletions lib/auth/authclient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,12 +685,6 @@ type ReadWindowsDesktopAccessPoint interface {
// GetWindowsDesktops returns windows desktop hosts.
GetWindowsDesktops(ctx context.Context, filter types.WindowsDesktopFilter) ([]types.WindowsDesktop, error)

// GetDynamicWindowsDesktops returns dynamic Windows desktops.
GetDynamicWindowsDesktops(ctx context.Context) ([]types.DynamicWindowsDesktop, error)

// GetDynamicWindowsDesktop returns dynamic Windows desktop by name.
GetDynamicWindowsDesktop(ctx context.Context, name string) (types.DynamicWindowsDesktop, error)

// GetWindowsDesktopServices returns windows desktop hosts.
GetWindowsDesktopServices(ctx context.Context) ([]types.WindowsDesktopService, error)

Expand Down
29 changes: 13 additions & 16 deletions lib/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ type Cache struct {
webSessionCache types.WebSessionInterface
webTokenCache types.WebTokenInterface
windowsDesktopsCache services.WindowsDesktops
dynamicWindowsDesktopsCache services.DynamicWindowsDesktops
samlIdPServiceProvidersCache services.SAMLIdPServiceProviders //nolint:revive // Because we want this to be IdP.
userGroupsCache services.UserGroups
oktaCache services.Okta
Expand Down Expand Up @@ -693,6 +694,8 @@ type Config struct {
WebToken types.WebTokenInterface
// WindowsDesktops is a windows desktop service.
WindowsDesktops services.WindowsDesktops
// DynamicWindowsDesktops is a dynamic Windows desktop service.
DynamicWindowsDesktops services.DynamicWindowsDesktops
// SAMLIdPServiceProviders is a SAML IdP service providers service.
SAMLIdPServiceProviders services.SAMLIdPServiceProviders
// UserGroups is a user groups service.
Expand Down Expand Up @@ -996,6 +999,12 @@ func New(config Config) (*Cache, error) {
return nil, trace.Wrap(err)
}

dynamicDesktopsService, err := local.NewDynamicWindowsDesktopService(config.Backend)
if err != nil {
cancel()
return nil, trace.Wrap(err)
}

cs := &Cache{
ctx: ctx,
cancel: cancel,
Expand All @@ -1022,6 +1031,7 @@ func New(config Config) (*Cache, error) {
webSessionCache: identityService.WebSessions(),
webTokenCache: identityService.WebTokens(),
windowsDesktopsCache: local.NewWindowsDesktopService(config.Backend),
dynamicWindowsDesktopsCache: dynamicDesktopsService,
accessMontoringRuleCache: accessMonitoringRuleCache,
samlIdPServiceProvidersCache: samlIdPServiceProvidersCache,
userGroupsCache: userGroupsCache,
Expand Down Expand Up @@ -2838,30 +2848,17 @@ func (c *Cache) GetDynamicWindowsDesktop(ctx context.Context, name string) (type
return rg.reader.GetDynamicWindowsDesktop(ctx, name)
}

// GetDynamicWindowsDesktops returns all registered dynamic Windows desktop.
func (c *Cache) GetDynamicWindowsDesktops(ctx context.Context) ([]types.DynamicWindowsDesktop, error) {
ctx, span := c.Tracer.Start(ctx, "cache/GetDynamicWindowsDesktops")
defer span.End()

rg, err := readCollectionCache(c, c.collections.dynamicWindowsDesktops)
if err != nil {
return nil, trace.Wrap(err)
}
defer rg.Release()
return rg.reader.GetDynamicWindowsDesktops(ctx)
}

// ListDynamicWindowsDesktops returns all registered dynamic Windows desktop.
func (c *Cache) ListDynamicWindowsDesktops(ctx context.Context, req types.ListDynamicWindowsDesktopsRequest) (*types.ListDynamicWindowsDesktopsResponse, error) {
func (c *Cache) ListDynamicWindowsDesktops(ctx context.Context, pageSize int, nextPage string) ([]types.DynamicWindowsDesktop, string, error) {
ctx, span := c.Tracer.Start(ctx, "cache/ListDynamicWindowsDesktops")
defer span.End()

rg, err := readCollectionCache(c, c.collections.dynamicWindowsDesktops)
if err != nil {
return nil, trace.Wrap(err)
return nil, "", trace.Wrap(err)
}
defer rg.Release()
return rg.reader.ListDynamicWindowsDesktops(ctx, req)
return rg.reader.ListDynamicWindowsDesktops(ctx, pageSize, nextPage)
}

// ListSAMLIdPServiceProviders returns a paginated list of SAML IdP service provider resources.
Expand Down
27 changes: 19 additions & 8 deletions lib/cache/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cache
import (
"context"
"fmt"
"github.com/gravitational/teleport/lib/defaults"

"github.com/gravitational/trace"

Expand Down Expand Up @@ -2331,34 +2332,44 @@ var _ executor[types.WindowsDesktop, windowsDesktopsGetter] = windowsDesktopsExe
type dynamicWindowsDesktopsExecutor struct{}

func (dynamicWindowsDesktopsExecutor) getAll(ctx context.Context, cache *Cache, loadSecrets bool) ([]types.DynamicWindowsDesktop, error) {
return cache.WindowsDesktops.GetDynamicWindowsDesktops(ctx)
var desktops []types.DynamicWindowsDesktop
next := ""
for ok := true; ok; ok = next != "" {
d, n, err := cache.dynamicWindowsDesktopsCache.ListDynamicWindowsDesktops(ctx, defaults.MaxIterationLimit, next)
if err != nil {
return nil, err
}
desktops = append(desktops, d...)
next = n
}
return desktops, nil
}

func (dynamicWindowsDesktopsExecutor) upsert(ctx context.Context, cache *Cache, resource types.DynamicWindowsDesktop) error {
return cache.windowsDesktopsCache.UpsertDynamicWindowsDesktop(ctx, resource)
_, err := cache.dynamicWindowsDesktopsCache.UpsertDynamicWindowsDesktop(ctx, resource)
return err
}

func (dynamicWindowsDesktopsExecutor) deleteAll(ctx context.Context, cache *Cache) error {
return cache.windowsDesktopsCache.DeleteAllDynamicWindowsDesktops(ctx)
return cache.dynamicWindowsDesktopsCache.DeleteAllDynamicWindowsDesktops(ctx)
}

func (dynamicWindowsDesktopsExecutor) delete(ctx context.Context, cache *Cache, resource types.Resource) error {
return cache.windowsDesktopsCache.DeleteDynamicWindowsDesktop(ctx, resource.GetName())
return cache.dynamicWindowsDesktopsCache.DeleteDynamicWindowsDesktop(ctx, resource.GetName())
}

func (dynamicWindowsDesktopsExecutor) isSingleton() bool { return false }

func (dynamicWindowsDesktopsExecutor) getReader(cache *Cache, cacheOK bool) dynamicWindowsDesktopsGetter {
if cacheOK {
return cache.windowsDesktopsCache
return cache.dynamicWindowsDesktopsCache
}
return cache.Config.WindowsDesktops
return cache.Config.DynamicWindowsDesktops
}

type dynamicWindowsDesktopsGetter interface {
GetDynamicWindowsDesktop(ctx context.Context, name string) (types.DynamicWindowsDesktop, error)
GetDynamicWindowsDesktops(context.Context) ([]types.DynamicWindowsDesktop, error)
ListDynamicWindowsDesktops(ctx context.Context, req types.ListDynamicWindowsDesktopsRequest) (*types.ListDynamicWindowsDesktopsResponse, error)
ListDynamicWindowsDesktops(ctx context.Context, pageSize int, nextPage string) ([]types.DynamicWindowsDesktop, string, error)
}

var _ executor[types.DynamicWindowsDesktop, dynamicWindowsDesktopsGetter] = dynamicWindowsDesktopsExecutor{}
Expand Down
5 changes: 3 additions & 2 deletions lib/services/dynamic_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ import (
// DynamicWindowsDesktops defines an interface for managing dynamic Windows desktops.
type DynamicWindowsDesktops interface {
DynamicWindowsDesktopGetter
GetDynamicWindowsDesktop(ctx context.Context, name string) (types.DynamicWindowsDesktop, error)
CreateDynamicWindowsDesktop(context.Context, types.DynamicWindowsDesktop) (types.DynamicWindowsDesktop, error)
UpdateDynamicWindowsDesktop(context.Context, types.DynamicWindowsDesktop) (types.DynamicWindowsDesktop, error)
UpsertDynamicWindowsDesktop(context.Context, types.DynamicWindowsDesktop) (types.DynamicWindowsDesktop, error)
DeleteDynamicWindowsDesktop(ctx context.Context, name string) error
ListDynamicWindowsDesktops(ctx context.Context, pageSize int, pageToken string) ([]types.DynamicWindowsDesktop, string, error)
DeleteAllDynamicWindowsDesktops(ctx context.Context) error
}

// DynamicWindowsDesktopGetter is an interface for fetching DynamicWindowsDesktop resources.
type DynamicWindowsDesktopGetter interface {
GetDynamicWindowsDesktop(ctx context.Context, name string) (types.DynamicWindowsDesktop, error)
ListDynamicWindowsDesktops(ctx context.Context, pageSize int, pageToken string) ([]types.DynamicWindowsDesktop, string, error)
}

// MarshalDynamicWindowsDesktop marshals the DynamicWindowsDesktop resource to JSON.
Expand Down
14 changes: 13 additions & 1 deletion lib/services/local/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,19 @@ type dynamicWindowsDesktopsParser struct {
func (p *dynamicWindowsDesktopsParser) parse(event backend.Event) (types.Resource, error) {
switch event.Type {
case types.OpDelete:
return resourceHeader(event, types.KindDynamicWindowsDesktop, types.V1, 0)
name := event.Item.Key.TrimPrefix(backend.NewKey(dynamicWindowsDesktopsPrefix, "")).String()
if name == "" {
return nil, trace.NotFound("failed parsing %v", event.Item.Key.String())
}

return &types.ResourceHeader{
Kind: types.KindDynamicWindowsDesktop,
Version: types.V1,
Metadata: types.Metadata{
Name: strings.TrimPrefix(name, backend.SeparatorString),
Namespace: apidefaults.Namespace,
},
}, nil
case types.OpPut:
return services.UnmarshalDynamicWindowsDesktop(
event.Item.Value,
Expand Down
12 changes: 9 additions & 3 deletions lib/services/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,15 @@ func (p *dynamicWindowsDesktopCollector) initializationChan() <-chan struct{} {

// getResourcesAndUpdateCurrent refreshes the list of current resources.
func (p *dynamicWindowsDesktopCollector) getResourcesAndUpdateCurrent(ctx context.Context) error {
dynamicWindowsDesktops, err := p.DynamicWindowsDesktopGetter.GetDynamicWindowsDesktops(ctx)
if err != nil {
return trace.Wrap(err)
var dynamicWindowsDesktops []types.DynamicWindowsDesktop
next := ""
for ok := true; ok; ok = next != "" {
desktops, token, err := p.DynamicWindowsDesktopGetter.ListDynamicWindowsDesktops(ctx, defaults.MaxIterationLimit, next)
if err != nil {
return trace.Wrap(err)
}
dynamicWindowsDesktops = append(dynamicWindowsDesktops, desktops...)
next = token
}
newCurrent := make(map[string]types.DynamicWindowsDesktop, len(dynamicWindowsDesktops))
for _, dynamicWindowsDesktop := range dynamicWindowsDesktops {
Expand Down

0 comments on commit 970503d

Please sign in to comment.