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

46 lines
1.4 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package workloadselector
import (
"context"
"github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/controller/dependency"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/proto-public/pbresource"
)
// MapSelectorToWorkloads will use the "id" index on watched Workload type to find all current
// workloads selected by the resource.
func MapSelectorToWorkloads[T WorkloadSelecting](_ context.Context, rt controller.Runtime, r *pbresource.Resource) ([]controller.Request, error) {
res, err := resource.Decode[T](r)
if err != nil {
return nil, err
}
workloads, err := GetWorkloadsWithSelector[T](rt.Cache, res)
if err != nil {
return nil, err
}
reqs := make([]controller.Request, len(workloads))
for i, workload := range workloads {
reqs[i] = controller.Request{
ID: workload.Id,
}
}
return reqs, nil
}
// MapWorkloadsToSelectors returns a DependencyMapper that will use the specified index to map a workload
// to resources that select it.
//
// This mapper can only be used on watches for the Workload type and works in conjunction with the Index
// created by this package.
func MapWorkloadsToSelectors(indexType *pbresource.Type, indexName string) controller.DependencyMapper {
return dependency.CacheParentsMapper(indexType, indexName)
}