Skip to content

Commit

Permalink
Convert dynamicdesktopwatcher to use genericwatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy committed Oct 28, 2024
1 parent 0318ad6 commit ca70101
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/services/readonly/readonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,22 @@ type Server interface {
// GetAWSAccountID returns the AWS Account ID if this node comes from an EC2 instance.
GetAWSAccountID() string
}

// DynamicWindowsDesktop represents a Windows desktop host that is automatically discovered by Windows Desktop Service.
type DynamicWindowsDesktop interface {
// ResourceWithLabels provides common resource methods.
ResourceWithLabels
// GetAddr returns the network address of this host.
GetAddr() string
// GetDomain returns the ActiveDirectory domain of this host.
GetDomain() string
// NonAD checks whether this is a standalone host that
// is not joined to an Active Directory domain.
NonAD() bool
// GetScreenSize returns the desired size of the screen to use for sessions
// to this host. Returns (0, 0) if no screen size is set, which means to
// use the size passed by the client over TDP.
GetScreenSize() (width, height uint32)
// Copy returns a copy of this dynamic Windows desktop
Copy() *types.DynamicWindowsDesktopV1
}
48 changes: 48 additions & 0 deletions lib/services/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,54 @@ func NewKubeClusterWatcher(ctx context.Context, cfg KubeClusterWatcherConfig) (*
return w, trace.Wrap(err)
}

type DynamicWindowsDesktopGetter interface {
ListDynamicWindowsDesktops(ctx context.Context, pageSize int, pageToken string) ([]types.DynamicWindowsDesktop, string, error)
}

// DynamicWindowsDesktopWatcherConfig is a DynamicWindowsDesktopWatcher configuration.
type DynamicWindowsDesktopWatcherConfig struct {
// DynamicWindowsDesktopGetter is responsible for fetching DynamicWindowsDesktop resources.
DynamicWindowsDesktopGetter
// DynamicWindowsDesktopsC receives up-to-date list of all DynamicWindowsDesktop resources.
DynamicWindowsDesktopsC chan []types.DynamicWindowsDesktop
// ResourceWatcherConfig is the resource watcher configuration.
ResourceWatcherConfig
}

// NewDynamicWindowsDesktopWatcher returns a new instance of DynamicWindowsDesktopWatcher.
func NewDynamicWindowsDesktopWatcher(ctx context.Context, cfg DynamicWindowsDesktopWatcherConfig) (*GenericWatcher[types.DynamicWindowsDesktop, readonly.DynamicWindowsDesktop], error) {
if cfg.DynamicWindowsDesktopGetter == nil {
return nil, trace.BadParameter("KubernetesClusterGetter must be provided")
}

w, err := NewGenericResourceWatcher(ctx, GenericWatcherConfig[types.DynamicWindowsDesktop, readonly.DynamicWindowsDesktop]{
ResourceWatcherConfig: cfg.ResourceWatcherConfig,
ResourceKind: types.KindDynamicWindowsDesktop,
ResourceGetter: func(ctx context.Context) ([]types.DynamicWindowsDesktop, error) {
var desktops []types.DynamicWindowsDesktop
next := ""
for {
d, token, err := cfg.DynamicWindowsDesktopGetter.ListDynamicWindowsDesktops(ctx, defaults.MaxIterationLimit, next)
if err != nil {
return nil, err
}
desktops = append(desktops, d...)
if token == "" {
break
}
next = token
}
return desktops, nil
},
ResourceKey: types.DynamicWindowsDesktop.GetName,
ResourcesC: cfg.DynamicWindowsDesktopsC,
CloneFunc: func(resource types.DynamicWindowsDesktop) types.DynamicWindowsDesktop {
return resource.Copy()
},
})
return w, trace.Wrap(err)
}

// GenericWatcherConfig is a generic resource watcher configuration.
type GenericWatcherConfig[T any, R any] struct {
// ResourceGetter is used to directly fetch the current set of resources.
Expand Down
5 changes: 3 additions & 2 deletions lib/srv/desktop/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/gravitational/teleport/lib/auth/windows"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/services/readonly"
"github.com/gravitational/teleport/lib/utils"
)

Expand Down Expand Up @@ -310,7 +311,7 @@ func (s *WindowsService) ldapEntryToWindowsDesktop(

// startDynamicReconciler starts resource watcher and reconciler that registers/unregisters Windows desktops
// according to the up-to-date list of dynamic Windows desktops resources.
func (s *WindowsService) startDynamicReconciler(ctx context.Context) (*services.DynamicWindowsDesktopWatcher, error) {
func (s *WindowsService) startDynamicReconciler(ctx context.Context) (*services.GenericWatcher[types.DynamicWindowsDesktop, readonly.DynamicWindowsDesktop], error) {
if len(s.cfg.ResourceMatchers) == 0 {
s.cfg.Logger.DebugContext(ctx, "Not starting dynamic desktop resource watcher.")
return nil, nil
Expand Down Expand Up @@ -353,7 +354,7 @@ func (s *WindowsService) startDynamicReconciler(ctx context.Context) (*services.
defer watcher.Close()
for {
select {
case desktops := <-watcher.DynamicWindowsDesktopsC:
case desktops := <-watcher.ResourcesC:
newResources = make(map[string]types.WindowsDesktop)
for _, dynamicDesktop := range desktops {
desktop, err := s.toWindowsDesktop(dynamicDesktop)
Expand Down

0 comments on commit ca70101

Please sign in to comment.