2023-06-06 21:09:48 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
package selectiontracker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-10 23:34:53 +00:00
|
|
|
"fmt"
|
2023-06-06 21:09:48 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
"github.com/hashicorp/consul/internal/controller"
|
|
|
|
"github.com/hashicorp/consul/internal/radix"
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/lib/stringslice"
|
2023-09-22 16:51:15 +00:00
|
|
|
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
|
2023-06-06 21:09:48 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WorkloadSelectionTracker struct {
|
|
|
|
lock sync.Mutex
|
2023-10-10 23:34:53 +00:00
|
|
|
prefixes *radix.Tree[[]*pbresource.ID]
|
|
|
|
exact *radix.Tree[[]*pbresource.ID]
|
2023-06-06 21:09:48 +00:00
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
// workloadSelectors contains a map keyed on resource references with values
|
2023-06-06 21:09:48 +00:00
|
|
|
// being the selector that resource is currently associated with. This map
|
|
|
|
// is kept mainly to make tracking removal operations more efficient.
|
|
|
|
// Generally any operation that could take advantage of knowing where
|
|
|
|
// in the trees the resource id is referenced can use this to prevent
|
|
|
|
// needing to search the whole tree.
|
2023-10-10 23:34:53 +00:00
|
|
|
workloadSelectors map[resource.ReferenceKey]*pbcatalog.WorkloadSelector
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *WorkloadSelectionTracker {
|
|
|
|
return &WorkloadSelectionTracker{
|
2023-10-10 23:34:53 +00:00
|
|
|
prefixes: radix.New[[]*pbresource.ID](),
|
|
|
|
exact: radix.New[[]*pbresource.ID](),
|
|
|
|
workloadSelectors: make(map[resource.ReferenceKey]*pbcatalog.WorkloadSelector),
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapWorkload will return a slice of controller.Requests with 1 resource for
|
|
|
|
// each resource that selects the specified Workload resource.
|
|
|
|
func (t *WorkloadSelectionTracker) MapWorkload(_ context.Context, _ controller.Runtime, res *pbresource.Resource) ([]controller.Request, error) {
|
2023-10-10 23:34:53 +00:00
|
|
|
resIds := t.GetIDsForWorkload(res.Id)
|
|
|
|
|
|
|
|
return controller.MakeRequests(nil, resIds), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WorkloadSelectionTracker) GetIDsForWorkload(id *pbresource.ID) []*pbresource.ID {
|
2023-06-06 21:09:48 +00:00
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
var result []*pbresource.ID
|
|
|
|
|
|
|
|
workloadTreeKey := treePathFromNameOrPrefix(id.GetTenancy(), id.GetName())
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
// gather the list of all resources that select the specified workload using a prefix match
|
2023-10-10 23:34:53 +00:00
|
|
|
t.prefixes.WalkPath(workloadTreeKey, func(path string, ids []*pbresource.ID) bool {
|
|
|
|
result = append(result, ids...)
|
2023-06-06 21:09:48 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
// gather the list of all resources that select the specified workload using an exact match
|
2023-10-10 23:34:53 +00:00
|
|
|
exactReqs, _ := t.exact.Get(workloadTreeKey)
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
// return the combined list of all resources that select the specified workload
|
2023-10-10 23:34:53 +00:00
|
|
|
return append(result, exactReqs...)
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrackIDForSelector will associate workloads matching the specified workload
|
|
|
|
// selector with the given resource id.
|
|
|
|
func (t *WorkloadSelectionTracker) TrackIDForSelector(id *pbresource.ID, selector *pbcatalog.WorkloadSelector) {
|
2023-12-22 16:32:40 +00:00
|
|
|
if selector == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
ref := resource.NewReferenceKey(id)
|
|
|
|
if previousSelector, found := t.workloadSelectors[ref]; found {
|
2023-06-06 21:09:48 +00:00
|
|
|
if stringslice.Equal(previousSelector.Names, selector.Names) &&
|
|
|
|
stringslice.Equal(previousSelector.Prefixes, selector.Prefixes) {
|
|
|
|
// the selector is unchanged so do nothing
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Potentially we could detect differences and do more minimal work. However
|
|
|
|
// users are not expected to alter workload selectors often and therefore
|
|
|
|
// not optimizing this further is probably fine. Therefore we are going
|
|
|
|
// to wipe all tracking of the id and reinsert things.
|
|
|
|
t.untrackID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop over all the exact matching rules and associate those workload names
|
|
|
|
// with the given resource id
|
|
|
|
for _, name := range selector.GetNames() {
|
2023-10-10 23:34:53 +00:00
|
|
|
key := treePathFromNameOrPrefix(id.GetTenancy(), name)
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
// lookup any resource id associations for the given workload name
|
2023-10-10 23:34:53 +00:00
|
|
|
leaf, _ := t.exact.Get(key)
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
// append the ID to the existing request list
|
2023-10-10 23:34:53 +00:00
|
|
|
t.exact.Insert(key, append(leaf, id))
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loop over all the prefix matching rules and associate those prefixes
|
|
|
|
// with the given resource id.
|
|
|
|
for _, prefix := range selector.GetPrefixes() {
|
2023-10-10 23:34:53 +00:00
|
|
|
key := treePathFromNameOrPrefix(id.GetTenancy(), prefix)
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
// lookup any resource id associations for the given workload name prefix
|
2023-10-10 23:34:53 +00:00
|
|
|
leaf, _ := t.prefixes.Get(key)
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
// append the new resource ID to the existing request list
|
2023-10-10 23:34:53 +00:00
|
|
|
t.prefixes.Insert(key, append(leaf, id))
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
t.workloadSelectors[ref] = selector
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UntrackID causes the tracker to stop tracking the given resource ID
|
|
|
|
func (t *WorkloadSelectionTracker) UntrackID(id *pbresource.ID) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
t.untrackID(id)
|
|
|
|
}
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
// GetSelector returns the currently stored selector for the given ID.
|
|
|
|
func (t *WorkloadSelectionTracker) GetSelector(id *pbresource.ID) *pbcatalog.WorkloadSelector {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
return t.workloadSelectors[resource.NewReferenceKey(id)]
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:09:48 +00:00
|
|
|
// untrackID should be called to stop tracking a resource ID.
|
|
|
|
// This method assumes the lock is already held. Besides modifying
|
|
|
|
// the prefix & name trees to not reference this ID, it will also
|
|
|
|
// delete any corresponding entry within the workloadSelectors map
|
|
|
|
func (t *WorkloadSelectionTracker) untrackID(id *pbresource.ID) {
|
2023-10-10 23:34:53 +00:00
|
|
|
ref := resource.NewReferenceKey(id)
|
|
|
|
selector, found := t.workloadSelectors[ref]
|
2023-06-06 21:09:48 +00:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
exactTreePaths := make([]string, len(selector.GetNames()))
|
|
|
|
for i, name := range selector.GetNames() {
|
|
|
|
exactTreePaths[i] = treePathFromNameOrPrefix(id.GetTenancy(), name)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixTreePaths := make([]string, len(selector.GetPrefixes()))
|
|
|
|
for i, prefix := range selector.GetPrefixes() {
|
|
|
|
prefixTreePaths[i] = treePathFromNameOrPrefix(id.GetTenancy(), prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeIDFromTreeAtPaths(t.exact, id, exactTreePaths)
|
|
|
|
removeIDFromTreeAtPaths(t.prefixes, id, prefixTreePaths)
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
// If we don't do this deletion then reinsertion of the id for
|
|
|
|
// tracking in the future could prevent selection criteria from
|
|
|
|
// being properly inserted into the radix trees.
|
2023-10-10 23:34:53 +00:00
|
|
|
delete(t.workloadSelectors, ref)
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeIDFromTree will remove the given resource ID from all leaf nodes in the radix tree.
|
2023-10-10 23:34:53 +00:00
|
|
|
func removeIDFromTreeAtPaths(t *radix.Tree[[]*pbresource.ID], id *pbresource.ID, paths []string) {
|
2023-06-06 21:09:48 +00:00
|
|
|
for _, path := range paths {
|
2023-10-10 23:34:53 +00:00
|
|
|
ids, _ := t.Get(path)
|
2023-06-06 21:09:48 +00:00
|
|
|
|
|
|
|
foundIdx := -1
|
2023-10-10 23:34:53 +00:00
|
|
|
for idx, resID := range ids {
|
|
|
|
if resource.EqualID(resID, id) {
|
2023-06-06 21:09:48 +00:00
|
|
|
foundIdx = idx
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundIdx != -1 {
|
2023-10-10 23:34:53 +00:00
|
|
|
l := len(ids)
|
|
|
|
|
|
|
|
if foundIdx == l-1 {
|
|
|
|
ids = ids[:foundIdx]
|
2023-06-06 21:09:48 +00:00
|
|
|
} else {
|
2023-10-10 23:34:53 +00:00
|
|
|
ids = slices.Delete(ids, foundIdx, foundIdx+1)
|
2023-06-06 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 23:34:53 +00:00
|
|
|
if len(ids) > 0 {
|
|
|
|
t.Insert(path, ids)
|
2023-06-06 21:09:48 +00:00
|
|
|
} else {
|
|
|
|
t.Delete(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-10 23:34:53 +00:00
|
|
|
|
|
|
|
// treePathFromNameOrPrefix computes radix tree key from the resource tenancy and a selector name or prefix.
|
|
|
|
// The keys will be computed in the following form:
|
|
|
|
// <partition>/<peer>/<namespace>/<name or prefix>.
|
|
|
|
func treePathFromNameOrPrefix(tenancy *pbresource.Tenancy, nameOrPrefix string) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%s/%s",
|
|
|
|
tenancy.GetPartition(),
|
|
|
|
tenancy.GetPeerName(),
|
|
|
|
tenancy.GetNamespace(),
|
|
|
|
nameOrPrefix)
|
|
|
|
}
|