mirror of https://github.com/hashicorp/consul
NET-6776 - Update Routes controller to use ComputedFailoverPolicy CE (#20496)
Update Routes controller to use ComputedFailoverPolicypull/20345/head^2
parent
0c509a60a4
commit
88b8a1cc36
@ -0,0 +1,48 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package catalogv2beta1
|
||||
|
||||
import "github.com/hashicorp/consul/proto-public/pbresource"
|
||||
|
||||
// GetUnderlyingDestinations will collect FailoverDestinations from all
|
||||
// internal fields and bundle them up in one slice.
|
||||
//
|
||||
// NOTE: no deduplication occurs.
|
||||
func (x *ComputedFailoverPolicy) GetUnderlyingDestinations() []*FailoverDestination {
|
||||
if x == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
estimate := 0
|
||||
for _, pc := range x.PortConfigs {
|
||||
estimate += len(pc.Destinations)
|
||||
}
|
||||
|
||||
out := make([]*FailoverDestination, 0, estimate)
|
||||
for _, pc := range x.PortConfigs {
|
||||
out = append(out, pc.Destinations...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// GetUnderlyingDestinationRefs is like GetUnderlyingDestinations except it
|
||||
// returns a slice of References.
|
||||
//
|
||||
// NOTE: no deduplication occurs.
|
||||
func (x *ComputedFailoverPolicy) GetUnderlyingDestinationRefs() []*pbresource.Reference {
|
||||
if x == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
dests := x.GetUnderlyingDestinations()
|
||||
|
||||
out := make([]*pbresource.Reference, 0, len(dests))
|
||||
for _, dest := range dests {
|
||||
if dest.Ref != nil {
|
||||
out = append(out, dest.Ref)
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package catalogv2beta1
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/consul/proto-public/pbresource"
|
||||
)
|
||||
|
||||
func TestComputedFailoverPolicy_GetUnderlyingDestinations_AndRefs(t *testing.T) {
|
||||
type testcase struct {
|
||||
failover *ComputedFailoverPolicy
|
||||
expectDests []*FailoverDestination
|
||||
expectRefs []*pbresource.Reference
|
||||
}
|
||||
|
||||
run := func(t *testing.T, tc testcase) {
|
||||
assertSliceEquals(t, tc.expectDests, tc.failover.GetUnderlyingDestinations())
|
||||
assertSliceEquals(t, tc.expectRefs, tc.failover.GetUnderlyingDestinationRefs())
|
||||
}
|
||||
|
||||
cases := map[string]testcase{
|
||||
"nil": {},
|
||||
"kitchen sink dests": {
|
||||
failover: &ComputedFailoverPolicy{
|
||||
PortConfigs: map[string]*FailoverConfig{
|
||||
"http": {
|
||||
Destinations: []*FailoverDestination{
|
||||
newFailoverDestination("foo"),
|
||||
newFailoverDestination("bar"),
|
||||
},
|
||||
},
|
||||
"admin": {
|
||||
Destinations: []*FailoverDestination{
|
||||
newFailoverDestination("admin"),
|
||||
},
|
||||
},
|
||||
"web": {
|
||||
Destinations: []*FailoverDestination{
|
||||
newFailoverDestination("foo"), // duplicated
|
||||
newFailoverDestination("www"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expectDests: []*FailoverDestination{
|
||||
newFailoverDestination("foo"),
|
||||
newFailoverDestination("bar"),
|
||||
newFailoverDestination("admin"),
|
||||
newFailoverDestination("foo"), // duplicated
|
||||
newFailoverDestination("www"),
|
||||
},
|
||||
expectRefs: []*pbresource.Reference{
|
||||
newFailoverRef("foo"),
|
||||
newFailoverRef("bar"),
|
||||
newFailoverRef("admin"),
|
||||
newFailoverRef("foo"), // duplicated
|
||||
newFailoverRef("www"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
run(t, tc)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue