mirror of
https://github.com/gravitational/teleport.git
synced 2026-08-30 17:45:43 +08:00
e81212dfcc
This converts hand rolled collections of resources via ListFoo RPCs in tctl and the cache to make use of clientutils.Resources. The main driver for the change is consistency and simplifcation. Having a single place that pagination logic exists should help reduce the chances of any pagination bugs in the future. The helper also reduces the amount of code needed at a call site to aggregate resources. The guts of clientutils.IterateResources have been moved to clientutils.Resources which returns an iter.Seq2[T, error] instead of the callback based approach. This makes the api simpler and allows making use of the itertools/stream helpers. Additionally, the page size used within clientutils.Resources was made to dynamically adjust based on receiving any errors indicating that the max message size was exceeding.
101 lines
3.7 KiB
Go
101 lines
3.7 KiB
Go
// Teleport
|
|
// Copyright (C) 2025 Gravitational, Inc.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/api/types/discoveryconfig"
|
|
"github.com/gravitational/teleport/api/types/header"
|
|
"github.com/gravitational/teleport/api/utils/clientutils"
|
|
"github.com/gravitational/teleport/lib/itertools/stream"
|
|
"github.com/gravitational/teleport/lib/services"
|
|
)
|
|
|
|
type discoveryConfigIndex string
|
|
|
|
const discoveryConfigNameIndex discoveryConfigIndex = "name"
|
|
|
|
func newDiscoveryConfigCollection(upstream services.DiscoveryConfigs, w types.WatchKind) (*collection[*discoveryconfig.DiscoveryConfig, discoveryConfigIndex], error) {
|
|
if upstream == nil {
|
|
return nil, trace.BadParameter("missing parameter DiscoveryConfigs")
|
|
}
|
|
|
|
return &collection[*discoveryconfig.DiscoveryConfig, discoveryConfigIndex]{
|
|
store: newStore(
|
|
types.KindDiscoveryConfig,
|
|
(*discoveryconfig.DiscoveryConfig).Clone,
|
|
map[discoveryConfigIndex]func(*discoveryconfig.DiscoveryConfig) string{
|
|
discoveryConfigNameIndex: func(r *discoveryconfig.DiscoveryConfig) string {
|
|
return r.GetMetadata().Name
|
|
},
|
|
}),
|
|
fetcher: func(ctx context.Context, loadSecrets bool) ([]*discoveryconfig.DiscoveryConfig, error) {
|
|
out, err := stream.Collect(clientutils.Resources(ctx, upstream.ListDiscoveryConfigs))
|
|
return out, trace.Wrap(err)
|
|
},
|
|
headerTransform: func(hdr *types.ResourceHeader) *discoveryconfig.DiscoveryConfig {
|
|
return &discoveryconfig.DiscoveryConfig{
|
|
ResourceHeader: header.ResourceHeader{
|
|
Kind: hdr.Kind,
|
|
Version: hdr.Version,
|
|
Metadata: header.Metadata{
|
|
Name: hdr.Metadata.Name,
|
|
},
|
|
},
|
|
}
|
|
},
|
|
watch: w,
|
|
}, nil
|
|
}
|
|
|
|
// ListDiscoveryConfigs returns a paginated list of all DiscoveryConfig resources.
|
|
func (c *Cache) ListDiscoveryConfigs(ctx context.Context, pageSize int, pageToken string) ([]*discoveryconfig.DiscoveryConfig, string, error) {
|
|
ctx, span := c.Tracer.Start(ctx, "cache/ListDiscoveryConfigs")
|
|
defer span.End()
|
|
|
|
lister := genericLister[*discoveryconfig.DiscoveryConfig, discoveryConfigIndex]{
|
|
cache: c,
|
|
collection: c.collections.discoveryConfigs,
|
|
index: discoveryConfigNameIndex,
|
|
upstreamList: c.Config.DiscoveryConfigs.ListDiscoveryConfigs,
|
|
nextToken: func(t *discoveryconfig.DiscoveryConfig) string {
|
|
return t.GetMetadata().Name
|
|
},
|
|
}
|
|
out, next, err := lister.list(ctx, pageSize, pageToken)
|
|
return out, next, trace.Wrap(err)
|
|
}
|
|
|
|
// GetDiscoveryConfig returns the specified DiscoveryConfig resource.
|
|
func (c *Cache) GetDiscoveryConfig(ctx context.Context, name string) (*discoveryconfig.DiscoveryConfig, error) {
|
|
ctx, span := c.Tracer.Start(ctx, "cache/GetDiscoveryConfig")
|
|
defer span.End()
|
|
|
|
getter := genericGetter[*discoveryconfig.DiscoveryConfig, discoveryConfigIndex]{
|
|
cache: c,
|
|
collection: c.collections.discoveryConfigs,
|
|
index: discoveryConfigNameIndex,
|
|
upstreamGet: c.Config.DiscoveryConfigs.GetDiscoveryConfig,
|
|
}
|
|
out, err := getter.get(ctx, name)
|
|
return out, trace.Wrap(err)
|
|
}
|