Matt Keeler 59cb12c798
Migrate the Endpoints controller to use the controller cache (#20241)
* Add cache resource decoding helpers

* Implement a common package for workload selection facilities. This includes:

   * Controller cache Index
   * ACL hooks
   * Dependency Mapper to go from workload to list of resources which select it
   * Dependency Mapper to go from a resource which selects workloads to all the workloads it selects.

* Update the endpoints controller to use the cache instead of custom mappers.

Co-authored-by: R.B. Boyer <4903+rboyer@users.noreply.github.com>
2024-01-18 17:52:52 -05:00

49 lines
1.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package indexers
import (
"github.com/hashicorp/consul/internal/controller/cache/index"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/proto-public/pbresource"
)
func IDIndex(name string, opts ...index.IndexOption) *index.Index {
return index.New(name, &idOrRefIndexer{getID: getResourceID}, opts...)
}
func OwnerIndex(name string, opts ...index.IndexOption) *index.Index {
return index.New(name, &idOrRefIndexer{getID: getOwnerID}, opts...)
}
func SingleIDOrRefIndex(name string, f GetSingleRefOrID, opts ...index.IndexOption) *index.Index {
return index.New(name, &idOrRefIndexer{getID: f}, opts...)
}
//go:generate mockery --name GetSingleRefOrID --with-expecter
type GetSingleRefOrID func(*pbresource.Resource) resource.ReferenceOrID
func getResourceID(r *pbresource.Resource) resource.ReferenceOrID {
return r.GetId()
}
func getOwnerID(r *pbresource.Resource) resource.ReferenceOrID {
return r.GetOwner()
}
type idOrRefIndexer struct {
getID GetSingleRefOrID
}
// FromArgs constructs a radix tree key from an ID for lookup.
func (i idOrRefIndexer) FromArgs(args ...any) ([]byte, error) {
return index.MaybePrefixReferenceOrIDFromArgs(args...)
}
// FromObject constructs a radix tree key from a Resource at write-time, or an
// ID at delete-time.
func (i idOrRefIndexer) FromResource(r *pbresource.Resource) (bool, []byte, error) {
return true, index.IndexFromRefOrID(i.getID(r)), nil
}