2023-08-01 18:39:15 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-08-01 18:39:15 +00:00
|
|
|
|
|
|
|
package bimapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/internal/controller"
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Mapper tracks bidirectional lookup for an item that contains references to
|
|
|
|
// other items. For example: an HTTPRoute has many references to Services.
|
|
|
|
//
|
|
|
|
// The primary object is called the "item" and an item has many "links".
|
|
|
|
// Tracking is done on items.
|
|
|
|
type Mapper struct {
|
|
|
|
itemType, linkType *pbresource.Type
|
|
|
|
|
|
|
|
lock sync.Mutex
|
|
|
|
itemToLink map[resource.ReferenceKey]map[resource.ReferenceKey]struct{}
|
|
|
|
linkToItem map[resource.ReferenceKey]map[resource.ReferenceKey]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a bimapper between the two required provided types.
|
|
|
|
func New(itemType, linkType *pbresource.Type) *Mapper {
|
|
|
|
if itemType == nil {
|
|
|
|
panic("itemType is required")
|
|
|
|
}
|
|
|
|
if linkType == nil {
|
|
|
|
panic("linkType is required")
|
|
|
|
}
|
|
|
|
return &Mapper{
|
|
|
|
itemType: itemType,
|
|
|
|
linkType: linkType,
|
|
|
|
itemToLink: make(map[resource.ReferenceKey]map[resource.ReferenceKey]struct{}),
|
|
|
|
linkToItem: make(map[resource.ReferenceKey]map[resource.ReferenceKey]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset clears the internal mappings.
|
|
|
|
func (m *Mapper) Reset() {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
m.itemToLink = make(map[resource.ReferenceKey]map[resource.ReferenceKey]struct{})
|
|
|
|
m.linkToItem = make(map[resource.ReferenceKey]map[resource.ReferenceKey]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the internal structures are empty.
|
|
|
|
func (m *Mapper) IsEmpty() bool {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
return len(m.itemToLink) == 0 && len(m.linkToItem) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// UntrackItem removes tracking for the provided item. The item type MUST match
|
|
|
|
// the type configured for the item.
|
2023-08-18 00:03:05 +00:00
|
|
|
func (m *Mapper) UntrackItem(item resource.ReferenceOrID) {
|
|
|
|
if !resource.EqualType(item.GetType(), m.itemType) {
|
2023-08-01 18:39:15 +00:00
|
|
|
panic(fmt.Sprintf("expected item type %q got %q",
|
|
|
|
resource.TypeToString(m.itemType),
|
2023-08-18 00:03:05 +00:00
|
|
|
resource.TypeToString(item.GetType()),
|
2023-08-01 18:39:15 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
m.untrackItem(resource.NewReferenceKey(item))
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
// UntrackLink removes tracking for the provided link. The link type MUST match
|
|
|
|
// the type configured for the link.
|
|
|
|
func (m *Mapper) UntrackLink(link resource.ReferenceOrID) {
|
|
|
|
if !resource.EqualType(link.GetType(), m.linkType) {
|
|
|
|
panic(fmt.Sprintf("expected link type %q got %q",
|
|
|
|
resource.TypeToString(m.linkType),
|
|
|
|
resource.TypeToString(link.GetType()),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
m.untrackLink(resource.NewReferenceKey(link))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mapper) untrackLink(link resource.ReferenceKey) {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
m.removeLinkLocked(link)
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:39:15 +00:00
|
|
|
func (m *Mapper) untrackItem(item resource.ReferenceKey) {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
m.removeItemLocked(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrackItem adds tracking for the provided item. The item and link types MUST
|
|
|
|
// match the types configured for the items and links.
|
2023-08-18 00:03:05 +00:00
|
|
|
func (m *Mapper) TrackItem(item resource.ReferenceOrID, links []resource.ReferenceOrID) {
|
|
|
|
if !resource.EqualType(item.GetType(), m.itemType) {
|
2023-08-01 18:39:15 +00:00
|
|
|
panic(fmt.Sprintf("expected item type %q got %q",
|
|
|
|
resource.TypeToString(m.itemType),
|
2023-08-18 00:03:05 +00:00
|
|
|
resource.TypeToString(item.GetType()),
|
2023-08-01 18:39:15 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
linksAsKeys := make([]resource.ReferenceKey, 0, len(links))
|
|
|
|
for _, link := range links {
|
2023-08-18 00:03:05 +00:00
|
|
|
if !resource.EqualType(link.GetType(), m.linkType) {
|
2023-08-01 18:39:15 +00:00
|
|
|
panic(fmt.Sprintf("expected link type %q got %q",
|
|
|
|
resource.TypeToString(m.linkType),
|
2023-08-18 00:03:05 +00:00
|
|
|
resource.TypeToString(link.GetType()),
|
2023-08-01 18:39:15 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
linksAsKeys = append(linksAsKeys, resource.NewReferenceKey(link))
|
|
|
|
}
|
|
|
|
|
|
|
|
m.trackItem(resource.NewReferenceKey(item), linksAsKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mapper) trackItem(item resource.ReferenceKey, links []resource.ReferenceKey) {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
m.removeItemLocked(item)
|
|
|
|
m.addItemLocked(item, links)
|
|
|
|
}
|
|
|
|
|
|
|
|
// you must hold the lock before calling this function
|
|
|
|
func (m *Mapper) removeItemLocked(item resource.ReferenceKey) {
|
|
|
|
for link := range m.itemToLink[item] {
|
|
|
|
delete(m.linkToItem[link], item)
|
|
|
|
if len(m.linkToItem[link]) == 0 {
|
|
|
|
delete(m.linkToItem, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(m.itemToLink, item)
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
func (m *Mapper) removeLinkLocked(link resource.ReferenceKey) {
|
|
|
|
for item := range m.linkToItem[link] {
|
|
|
|
delete(m.itemToLink[item], link)
|
|
|
|
if len(m.itemToLink[item]) == 0 {
|
|
|
|
delete(m.itemToLink, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(m.linkToItem, link)
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:39:15 +00:00
|
|
|
// you must hold the lock before calling this function
|
|
|
|
func (m *Mapper) addItemLocked(item resource.ReferenceKey, links []resource.ReferenceKey) {
|
|
|
|
if m.itemToLink[item] == nil {
|
|
|
|
m.itemToLink[item] = make(map[resource.ReferenceKey]struct{})
|
|
|
|
}
|
|
|
|
for _, link := range links {
|
|
|
|
m.itemToLink[item][link] = struct{}{}
|
|
|
|
|
|
|
|
if m.linkToItem[link] == nil {
|
|
|
|
m.linkToItem[link] = make(map[resource.ReferenceKey]struct{})
|
|
|
|
}
|
|
|
|
m.linkToItem[link][item] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinksForItem returns references to links related to the requested item.
|
2023-08-18 00:03:05 +00:00
|
|
|
// Deprecated: use LinksRefs
|
2023-08-01 18:39:15 +00:00
|
|
|
func (m *Mapper) LinksForItem(item *pbresource.ID) []*pbresource.Reference {
|
2023-08-18 00:03:05 +00:00
|
|
|
return m.LinkRefsForItem(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkRefsForItem returns references to links related to the requested item.
|
|
|
|
func (m *Mapper) LinkRefsForItem(item *pbresource.ID) []*pbresource.Reference {
|
2023-08-01 18:39:15 +00:00
|
|
|
if !resource.EqualType(item.Type, m.itemType) {
|
|
|
|
panic(fmt.Sprintf("expected item type %q got %q",
|
|
|
|
resource.TypeToString(m.itemType),
|
|
|
|
resource.TypeToString(item.Type),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2023-08-04 21:45:10 +00:00
|
|
|
links, ok := m.itemToLink[resource.NewReferenceKey(item)]
|
2023-08-01 18:39:15 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-04 21:45:10 +00:00
|
|
|
out := make([]*pbresource.Reference, 0, len(links))
|
|
|
|
for link := range links {
|
|
|
|
out = append(out, link.ToReference())
|
2023-08-01 18:39:15 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
// LinkIDsForItem returns IDs to links related to the requested item.
|
|
|
|
func (m *Mapper) LinkIDsForItem(item *pbresource.ID) []*pbresource.ID {
|
|
|
|
if !resource.EqualType(item.Type, m.itemType) {
|
|
|
|
panic(fmt.Sprintf("expected item type %q got %q",
|
|
|
|
resource.TypeToString(m.itemType),
|
|
|
|
resource.TypeToString(item.Type),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
links, ok := m.itemToLink[resource.NewReferenceKey(item)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*pbresource.ID, 0, len(links))
|
|
|
|
for l := range links {
|
|
|
|
out = append(out, l.ToID())
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:39:15 +00:00
|
|
|
// ItemsForLink returns item ids for items related to the provided link.
|
2023-08-18 00:03:05 +00:00
|
|
|
// Deprecated: use ItemIDsForLink
|
2023-08-01 18:39:15 +00:00
|
|
|
func (m *Mapper) ItemsForLink(link *pbresource.ID) []*pbresource.ID {
|
2023-08-18 00:03:05 +00:00
|
|
|
return m.ItemIDsForLink(link)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ItemIDsForLink returns item ids for items related to the provided link.
|
2023-09-12 19:56:43 +00:00
|
|
|
func (m *Mapper) ItemIDsForLink(link resource.ReferenceOrID) []*pbresource.ID {
|
|
|
|
if !resource.EqualType(link.GetType(), m.linkType) {
|
2023-08-18 00:03:05 +00:00
|
|
|
panic(fmt.Sprintf("expected link type %q got %q",
|
2023-08-01 18:39:15 +00:00
|
|
|
resource.TypeToString(m.linkType),
|
2023-09-12 19:56:43 +00:00
|
|
|
resource.TypeToString(link.GetType()),
|
2023-08-01 18:39:15 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
return m.itemIDsByLink(resource.NewReferenceKey(link))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ItemRefsForLink returns item references for items related to the provided link.
|
2023-09-12 19:56:43 +00:00
|
|
|
func (m *Mapper) ItemRefsForLink(link resource.ReferenceOrID) []*pbresource.Reference {
|
|
|
|
if !resource.EqualType(link.GetType(), m.linkType) {
|
2023-08-18 00:03:05 +00:00
|
|
|
panic(fmt.Sprintf("expected link type %q got %q",
|
|
|
|
resource.TypeToString(m.linkType),
|
2023-09-12 19:56:43 +00:00
|
|
|
resource.TypeToString(link.GetType()),
|
2023-08-18 00:03:05 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.itemRefsByLink(resource.NewReferenceKey(link))
|
2023-08-01 18:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MapLink is suitable as a DependencyMapper to map the provided link event to its item.
|
|
|
|
func (m *Mapper) MapLink(_ context.Context, _ controller.Runtime, res *pbresource.Resource) ([]controller.Request, error) {
|
|
|
|
link := res.Id
|
|
|
|
|
|
|
|
if !resource.EqualType(link.Type, m.linkType) {
|
|
|
|
return nil, fmt.Errorf("expected type %q got %q",
|
|
|
|
resource.TypeToString(m.linkType),
|
|
|
|
resource.TypeToString(link.Type),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
itemIDs := m.itemIDsByLink(resource.NewReferenceKey(link))
|
2023-08-01 18:39:15 +00:00
|
|
|
|
|
|
|
out := make([]controller.Request, 0, len(itemIDs))
|
|
|
|
for _, item := range itemIDs {
|
|
|
|
if !resource.EqualType(item.Type, m.itemType) {
|
|
|
|
return nil, fmt.Errorf("expected type %q got %q",
|
|
|
|
resource.TypeToString(m.itemType),
|
|
|
|
resource.TypeToString(item.Type),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
out = append(out, controller.Request{ID: item})
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
func (m *Mapper) itemIDsByLink(link resource.ReferenceKey) []*pbresource.ID {
|
2023-09-07 15:37:15 +00:00
|
|
|
// a lock must be held both to read item from the map and to read the
|
|
|
|
// the returned items.
|
2023-08-18 13:55:12 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
items, ok := m.linkToItem[link]
|
2023-08-01 18:39:15 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*pbresource.ID, 0, len(items))
|
|
|
|
for item := range items {
|
|
|
|
out = append(out, item.ToID())
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2023-08-18 00:03:05 +00:00
|
|
|
|
|
|
|
func (m *Mapper) itemRefsByLink(link resource.ReferenceKey) []*pbresource.Reference {
|
2023-09-07 15:37:15 +00:00
|
|
|
// a lock must be held both to read item from the map and to read the
|
|
|
|
// the returned items.
|
2023-08-18 13:55:12 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
items, ok := m.linkToItem[link]
|
2023-08-18 00:03:05 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*pbresource.Reference, 0, len(items))
|
|
|
|
for item := range items {
|
|
|
|
out = append(out, item.ToReference())
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|