Skip to content

Commit

Permalink
[CE] Test tenancies for exported-services config manager (#20678)
Browse files Browse the repository at this point in the history
Sync controller tests from ENT
  • Loading branch information
Chris S. Kim authored Feb 20, 2024
1 parent 3f3477c commit 53afd8f
Show file tree
Hide file tree
Showing 3 changed files with 707 additions and 14 deletions.
31 changes: 17 additions & 14 deletions internal/multicluster/internal/controllers/v1compat/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ package v1compat
import (
"context"
"fmt"
"slices"
"sort"

"golang.org/x/exp/maps"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/controller"
Expand All @@ -25,6 +28,7 @@ const (
controllerMetaKey = "managed-by-controller"
)

//go:generate mockery --name AggregatedConfig --inpackage --with-expecter --filename mock_AggregatedConfig.go
type AggregatedConfig interface {
Start(context.Context)
GetExportedServicesConfigEntry(context.Context, string, *acl.EnterpriseMeta) (*structs.ExportedServicesConfigEntry, error)
Expand Down Expand Up @@ -87,7 +91,9 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
entMeta.OverridePartition(req.ID.Tenancy.Partition)
existing, err := r.config.GetExportedServicesConfigEntry(ctx, req.ID.Tenancy.Partition, entMeta)
if err != nil {
rt.Logger.Error("error getting exported service config entry", "error", err)
// When we can't read the existing exported-services we purposely allow
// reconciler to continue so we can still write a new one
rt.Logger.Warn("error getting exported service config entry but continuing reconcile", "error", err)
}

if existing != nil && existing.Meta["managed-by-controller"] != ControllerName {
Expand Down Expand Up @@ -117,7 +123,6 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
},
index.IndexQueryOptions{Prefix: true},
)

if err != nil {
rt.Logger.Error("error retrieving partition exported services", "error", err)
return err
Expand All @@ -133,9 +138,8 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
},
index.IndexQueryOptions{Prefix: true},
)

if err != nil {
rt.Logger.Error("error retrieving namespace exported service", "error", err)
rt.Logger.Error("error retrieving namespace exported services", "error", err)
return err
}

Expand All @@ -149,7 +153,6 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
},
index.IndexQueryOptions{Prefix: true},
)

if err != nil {
rt.Logger.Error("error retrieving exported services", "error", err)
return err
Expand Down Expand Up @@ -243,24 +246,24 @@ func (c *exportConsumers) addConsumers(consumers []*pbmulticluster.ExportedServi
func (c *exportConsumers) configEntryConsumers() []structs.ServiceConsumer {
consumers := make([]structs.ServiceConsumer, 0, len(c.partitions)+len(c.peers)+len(c.samenessGroups))

partitions := keys(c.partitions)
sort.Strings(partitions)
partitions := maps.Keys(c.partitions)
slices.Sort(partitions)
for _, consumer := range partitions {
consumers = append(consumers, structs.ServiceConsumer{
Partition: consumer,
})
}

peers := keys(c.peers)
sort.Strings(peers)
peers := maps.Keys(c.peers)
slices.Sort(peers)
for _, consumer := range peers {
consumers = append(consumers, structs.ServiceConsumer{
Peer: consumer,
})
}

samenessGroups := keys(c.samenessGroups)
sort.Strings(samenessGroups)
samenessGroups := maps.Keys(c.samenessGroups)
slices.Sort(samenessGroups)
for _, consumer := range samenessGroups {
consumers = append(consumers, structs.ServiceConsumer{
SamenessGroup: consumer,
Expand Down Expand Up @@ -316,8 +319,8 @@ func (t *exportTracker) allExports() []structs.ExportedService {
})
}

namespaces := keys(t.namespaces)
sort.Strings(namespaces)
namespaces := maps.Keys(t.namespaces)
slices.Sort(namespaces)
for _, ns := range namespaces {
exports = append(exports, structs.ExportedService{
Name: "*",
Expand All @@ -326,7 +329,7 @@ func (t *exportTracker) allExports() []structs.ExportedService {
})
}

services := keys(t.services)
services := maps.Keys(t.services)
sort.Slice(services, func(i, j int) bool {
// the partitions must already be equal because we are only
// looking at resource exports for a single partition.
Expand Down
Loading

0 comments on commit 53afd8f

Please sign in to comment.