-
Notifications
You must be signed in to change notification settings - Fork 2k
[v18] feat: Add bot instance cache #56163
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
Merged
nicholasmarais1158
merged 4 commits into
branch/v18
from
nicholasmarais1158/backport-55807-branch/v18
Jul 14, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7469e4a
feat: Add bot instance cache (#55807)
nicholasmarais1158 2d84a28
Merge branch 'branch/v18' into nicholasmarais1158/backport-55807-bran…
nicholasmarais1158 364035a
Merge branch 'branch/v18' into nicholasmarais1158/backport-55807-bran…
nicholasmarais1158 bb992a6
Add kind to `newStore`
nicholasmarais1158 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // 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" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/gravitational/teleport/api/defaults" | ||
| machineidv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/machineid/v1" | ||
| "github.com/gravitational/teleport/api/types" | ||
| "github.com/gravitational/teleport/api/utils/clientutils" | ||
| "github.com/gravitational/teleport/lib/services" | ||
| ) | ||
|
|
||
| type botInstanceIndex string | ||
|
|
||
| const ( | ||
| botInstanceNameIndex botInstanceIndex = "name" | ||
| ) | ||
|
|
||
| func keyForNameIndex(botInstance *machineidv1.BotInstance) string { | ||
| return makeNameIndexKey( | ||
| botInstance.GetSpec().GetBotName(), | ||
| botInstance.GetMetadata().GetName(), | ||
| ) | ||
| } | ||
|
|
||
| func makeNameIndexKey(botName string, instanceID string) string { | ||
| return botName + "/" + instanceID | ||
| } | ||
|
|
||
| func newBotInstanceCollection(upstream services.BotInstance, w types.WatchKind) (*collection[*machineidv1.BotInstance, botInstanceIndex], error) { | ||
| if upstream == nil { | ||
| return nil, trace.BadParameter("missing parameter upstream (BotInstance)") | ||
| } | ||
|
|
||
| return &collection[*machineidv1.BotInstance, botInstanceIndex]{ | ||
| store: newStore( | ||
| types.KindBotInstance, | ||
| proto.CloneOf[*machineidv1.BotInstance], | ||
| map[botInstanceIndex]func(*machineidv1.BotInstance) string{ | ||
| // Index on a combination of bot name and instance name | ||
| botInstanceNameIndex: keyForNameIndex, | ||
| }), | ||
| fetcher: func(ctx context.Context, loadSecrets bool) ([]*machineidv1.BotInstance, error) { | ||
| var out []*machineidv1.BotInstance | ||
| clientutils.IterateResources(ctx, | ||
| func(ctx context.Context, limit int, start string) ([]*machineidv1.BotInstance, string, error) { | ||
| return upstream.ListBotInstances(ctx, "", limit, start, "") | ||
| }, | ||
| func(hcc *machineidv1.BotInstance) error { | ||
| out = append(out, hcc) | ||
| return nil | ||
| }, | ||
| ) | ||
| return out, nil | ||
| }, | ||
| watch: w, | ||
| }, nil | ||
| } | ||
|
|
||
| // GetBotInstance returns the specified BotInstance resource. | ||
| func (c *Cache) GetBotInstance(ctx context.Context, botName, instanceID string) (*machineidv1.BotInstance, error) { | ||
| ctx, span := c.Tracer.Start(ctx, "cache/GetBotInstance") | ||
| defer span.End() | ||
|
|
||
| getter := genericGetter[*machineidv1.BotInstance, botInstanceIndex]{ | ||
| cache: c, | ||
| collection: c.collections.botInstances, | ||
| index: botInstanceNameIndex, | ||
| upstreamGet: func(ctx context.Context, _ string) (*machineidv1.BotInstance, error) { | ||
| return c.Config.BotInstanceService.GetBotInstance(ctx, botName, instanceID) | ||
| }, | ||
| } | ||
|
|
||
| out, err := getter.get(ctx, makeNameIndexKey(botName, instanceID)) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // ListBotInstances returns a page of BotInstance resources. | ||
| func (c *Cache) ListBotInstances(ctx context.Context, botName string, pageSize int, lastToken string, search string) ([]*machineidv1.BotInstance, string, error) { | ||
| ctx, span := c.Tracer.Start(ctx, "cache/ListBotInstances") | ||
| defer span.End() | ||
|
|
||
| lister := genericLister[*machineidv1.BotInstance, botInstanceIndex]{ | ||
| cache: c, | ||
| collection: c.collections.botInstances, | ||
| index: botInstanceNameIndex, | ||
| defaultPageSize: defaults.DefaultChunkSize, | ||
| upstreamList: func(ctx context.Context, limit int, start string) ([]*machineidv1.BotInstance, string, error) { | ||
| return c.Config.BotInstanceService.ListBotInstances(ctx, botName, limit, start, search) | ||
| }, | ||
| filter: func(b *machineidv1.BotInstance) bool { | ||
| return matchBotInstance(b, botName, search) | ||
| }, | ||
| nextToken: func(b *machineidv1.BotInstance) string { | ||
| return keyForNameIndex(b) | ||
| }, | ||
| } | ||
| out, next, err := lister.list(ctx, | ||
| pageSize, | ||
| lastToken, | ||
| ) | ||
| return out, next, trace.Wrap(err) | ||
| } | ||
|
|
||
| func matchBotInstance(b *machineidv1.BotInstance, botName string, search string) bool { | ||
| // If updating this, ensure it's consistent with the upstream search logic in `lib/services/local/bot_instance.go`. | ||
|
|
||
| if botName != "" && b.Spec.BotName != botName { | ||
| return false | ||
| } | ||
|
|
||
| if search == "" { | ||
| return true | ||
| } | ||
|
|
||
| latestHeartbeats := b.GetStatus().GetLatestHeartbeats() | ||
| heartbeat := b.Status.InitialHeartbeat // Use initial heartbeat as a fallback | ||
| if len(latestHeartbeats) > 0 { | ||
| heartbeat = latestHeartbeats[len(latestHeartbeats)-1] | ||
| } | ||
|
|
||
| values := []string{ | ||
| b.Spec.BotName, | ||
| b.Spec.InstanceId, | ||
| } | ||
|
|
||
| if heartbeat != nil { | ||
| values = append(values, heartbeat.Hostname, heartbeat.JoinMethod, heartbeat.Version, "v"+heartbeat.Version) | ||
| } | ||
|
|
||
| return slices.ContainsFunc(values, func(val string) bool { | ||
| return strings.Contains(strings.ToLower(val), strings.ToLower(search)) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/gravitational/teleport/blob/master/lib/cache/bot_instance.go#L86-L91