mirror of https://github.com/hashicorp/consul
added computed failover controller (#20329)
* added computed failover controller * removed some uncessary changes * removed uncessary changes * minor refactor * minor refactor fmt * added copyrightpull/20334/head
parent
0abf8f8426
commit
3446eb3b1b
@ -0,0 +1,12 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
//go:build !consulent
|
||||
|
||||
package expander
|
||||
|
||||
import "github.com/hashicorp/consul/internal/catalog/internal/controllers/failover/expander/expander_ce"
|
||||
|
||||
func GetSamenessGroupExpander() *expander_ce.SamenessGroupExpander {
|
||||
return expander_ce.New()
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package expander_ce
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/consul/internal/controller"
|
||||
"github.com/hashicorp/consul/internal/controller/cache/index"
|
||||
"github.com/hashicorp/consul/internal/controller/cache/indexers"
|
||||
"github.com/hashicorp/consul/internal/resource"
|
||||
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
|
||||
"github.com/hashicorp/consul/proto-public/pbresource"
|
||||
)
|
||||
|
||||
type SamenessGroupExpander struct{}
|
||||
|
||||
func New() *SamenessGroupExpander {
|
||||
return &SamenessGroupExpander{}
|
||||
}
|
||||
|
||||
func (sgE *SamenessGroupExpander) ComputeFailoverDestinationsFromSamenessGroup(rt controller.Runtime, id *pbresource.ID, sg string, port string) ([]*pbcatalog.FailoverDestination, string, error) {
|
||||
//no - op for CE
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
const sgIndexName = "samenessGroupIndex"
|
||||
|
||||
func (sgE *SamenessGroupExpander) GetSamenessGroupIndex() *index.Index {
|
||||
return indexers.DecodedMultiIndexer(
|
||||
sgIndexName,
|
||||
index.ReferenceOrIDFromArgs,
|
||||
func(r *resource.DecodedResource[*pbcatalog.FailoverPolicy]) (bool, [][]byte, error) {
|
||||
//no - op for CE
|
||||
return false, nil, nil
|
||||
|
||||
},
|
||||
)
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package expander_ce
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/consul/internal/controller"
|
||||
"github.com/hashicorp/consul/internal/controller/cache"
|
||||
"github.com/hashicorp/consul/internal/resource"
|
||||
rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
|
||||
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
|
||||
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
|
||||
"github.com/hashicorp/consul/proto-public/pbresource"
|
||||
"github.com/hashicorp/consul/sdk/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type expanderSuite struct {
|
||||
suite.Suite
|
||||
ctx context.Context
|
||||
cache cache.Cache
|
||||
rt controller.Runtime
|
||||
tenancies []*pbresource.Tenancy
|
||||
|
||||
samenessGroupExpander *SamenessGroupExpander
|
||||
}
|
||||
|
||||
func (suite *expanderSuite) SetupTest() {
|
||||
suite.ctx = testutil.TestContext(suite.T())
|
||||
suite.tenancies = rtest.TestTenancies()
|
||||
|
||||
suite.samenessGroupExpander = New()
|
||||
|
||||
suite.cache = cache.New()
|
||||
suite.cache.AddType(pbmulticluster.SamenessGroupType)
|
||||
suite.rt = controller.Runtime{
|
||||
Cache: suite.cache,
|
||||
Logger: testutil.Logger(suite.T()),
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpander(t *testing.T) {
|
||||
suite.Run(t, new(expanderSuite))
|
||||
}
|
||||
func (suite *expanderSuite) Test_ComputeFailoverDestinationsFromSamenessGroup() {
|
||||
fpData := &pbcatalog.FailoverPolicy{
|
||||
PortConfigs: map[string]*pbcatalog.FailoverConfig{
|
||||
"http": {
|
||||
SamenessGroup: "sg1",
|
||||
},
|
||||
},
|
||||
}
|
||||
fp := rtest.Resource(pbcatalog.FailoverPolicyType, "apisvc").
|
||||
WithData(suite.T(), fpData).
|
||||
WithTenancy(resource.DefaultNamespacedTenancy()).
|
||||
Build()
|
||||
decFp, err := resource.Decode[*pbcatalog.FailoverPolicy](fp)
|
||||
require.NoError(suite.T(), err)
|
||||
dests, sg, err := suite.samenessGroupExpander.ComputeFailoverDestinationsFromSamenessGroup(suite.rt, decFp.Id, "sg1", "http")
|
||||
require.NoError(suite.T(), err)
|
||||
require.Nil(suite.T(), dests)
|
||||
require.Equal(suite.T(), "", sg)
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package expander
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/consul/internal/controller"
|
||||
"github.com/hashicorp/consul/internal/controller/cache/index"
|
||||
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
|
||||
"github.com/hashicorp/consul/proto-public/pbresource"
|
||||
)
|
||||
|
||||
// SamenessgroupExpander is used to expand sameness group for a ComputedFailover resource
|
||||
type SamenessGroupExpander interface {
|
||||
ComputeFailoverDestinationsFromSamenessGroup(rt controller.Runtime, id *pbresource.ID, sg string, port string) ([]*pbcatalog.FailoverDestination, string, error)
|
||||
GetSamenessGroupIndex() *index.Index
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
//go:build !consulent
|
||||
|
||||
package failover
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/consul/internal/controller"
|
||||
)
|
||||
|
||||
func registerEnterpriseControllerWatchers(ctrl *controller.Controller) *controller.Controller {
|
||||
return ctrl
|
||||
}
|
Loading…
Reference in new issue