mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 05:45:46 +00:00
11d6b0df45
xRoute resource types contain a slice of parentRefs to services that they manipulate traffic for. All xRoutes that have a parentRef to given Service will be merged together to generate a ComputedRoutes resource name-aligned with that Service. This means that a write of an xRoute with 2 parent ref pointers will cause at most 2 reconciles for ComputedRoutes. If that xRoute's list of parentRefs were ever to be reduced, or otherwise lose an item, that subsequent map event will only emit events for the current set of refs. The removed ref will not cause the generated ComputedRoutes related to that service to be re-reconciled to omit the influence of that xRoute. To combat this, we will store on the ComputedRoutes resource a BoundResources []*pbresource.Reference field with references to all resources that were used to influence the generated output. When the routes controller reconciles, it will use a bimapper to index this influence, and the dependency mappers for the xRoutes will look themselves up in that index to discover additional (former) ComputedRoutes that need to be notified as well.
146 lines
3.0 KiB
Go
146 lines
3.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package resource
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
)
|
|
|
|
func TestLessReference(t *testing.T) {
|
|
parseTenancy := func(s string) *pbresource.Tenancy {
|
|
// format is: <partition>.<peerName>.<namespace>
|
|
parts := strings.Split(s, ".")
|
|
if len(parts) != 3 {
|
|
panic("bad tenancy")
|
|
}
|
|
return &pbresource.Tenancy{
|
|
Partition: parts[0],
|
|
PeerName: parts[1],
|
|
Namespace: parts[2],
|
|
}
|
|
}
|
|
|
|
makeRef := func(s string) *pbresource.Reference {
|
|
// format is:
|
|
// - <type>/<tenancy>/<name>@<section>
|
|
// - <type>/<tenancy>/<name>
|
|
//
|
|
// type = (gvk style)
|
|
// tenancy = <partition>.<peerName>.<namespace>
|
|
|
|
parts := strings.Split(s, "/")
|
|
require.Len(t, parts, 3)
|
|
|
|
name, section, _ := strings.Cut(parts[2], "@")
|
|
|
|
return &pbresource.Reference{
|
|
Type: GVKToType(parts[0]),
|
|
Tenancy: parseTenancy(parts[1]),
|
|
Name: name,
|
|
Section: section,
|
|
}
|
|
}
|
|
|
|
var inputs []*pbresource.Reference
|
|
|
|
stringify := func(all []*pbresource.Reference) []string {
|
|
var out []string
|
|
for _, ref := range all {
|
|
out = append(out, ReferenceToString(ref))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// We generate pre-sorted data.
|
|
vals := []string{"a", "aa", "b", "bb"}
|
|
sectionVals := append([]string{""}, vals...)
|
|
for _, group := range vals {
|
|
for _, version := range vals {
|
|
for _, kind := range vals {
|
|
for _, partition := range vals {
|
|
for _, peer := range vals {
|
|
for _, namespace := range vals {
|
|
for _, name := range vals {
|
|
for _, section := range sectionVals {
|
|
if section != "" {
|
|
section = "@" + section
|
|
}
|
|
inputs = append(inputs, makeRef(
|
|
fmt.Sprintf(
|
|
"%s.%s.%s/%s.%s.%s/%s%s",
|
|
group, version, kind,
|
|
partition, peer, namespace,
|
|
name, section,
|
|
),
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
require.True(t, sort.IsSorted(sortedReferences(inputs)))
|
|
|
|
const randomTrials = 5
|
|
|
|
mixed := protoSliceClone(inputs)
|
|
for i := 0; i < randomTrials; i++ {
|
|
rand.Shuffle(len(mixed), func(i, j int) {
|
|
mixed[i], mixed[j] = mixed[j], mixed[i]
|
|
})
|
|
|
|
// We actually got a permuted list out of this.
|
|
require.NotEqual(t, stringify(inputs), stringify(mixed))
|
|
|
|
sort.Slice(mixed, func(i, j int) bool {
|
|
return LessReference(mixed[i], mixed[j])
|
|
})
|
|
|
|
// And it sorted.
|
|
require.Equal(t, stringify(inputs), stringify(mixed))
|
|
}
|
|
|
|
}
|
|
|
|
type sortedReferences []*pbresource.Reference
|
|
|
|
func (r sortedReferences) Len() int {
|
|
return len(r)
|
|
}
|
|
|
|
func (r sortedReferences) Less(i, j int) bool {
|
|
return LessReference(r[i], r[j])
|
|
}
|
|
|
|
func (r sortedReferences) Swap(i, j int) {
|
|
r[i], r[j] = r[j], r[i]
|
|
}
|
|
|
|
func protoClone[T proto.Message](v T) T {
|
|
return proto.Clone(v).(T)
|
|
}
|
|
|
|
func protoSliceClone[T proto.Message](in []T) []T {
|
|
if in == nil {
|
|
return nil
|
|
}
|
|
out := make([]T, 0, len(in))
|
|
for _, v := range in {
|
|
out = append(out, protoClone[T](v))
|
|
}
|
|
return out
|
|
}
|