Skip to content

Commit c6c2d8b

Browse files
authored
[NET-6426] Modify Reconcile Loop for Mesh Gateway Resources to Correctly Write Proxy State Template (#20085)
1 parent 3b11127 commit c6c2d8b

File tree

6 files changed

+330
-14
lines changed

6 files changed

+330
-14
lines changed

internal/mesh/internal/controllers/gatewayproxy/builder/builder.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package builder
55

66
import (
7+
"github.com/hashicorp/go-hclog"
8+
9+
"github.com/hashicorp/consul/internal/mesh/internal/controllers/gatewayproxy/fetcher"
710
"github.com/hashicorp/consul/internal/mesh/internal/types"
811
pbauth "github.com/hashicorp/consul/proto-public/pbauth/v2beta1"
912
meshv2beta1 "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
@@ -12,12 +15,22 @@ import (
1215
)
1316

1417
type proxyStateTemplateBuilder struct {
15-
workload *types.DecodedWorkload
18+
workload *types.DecodedWorkload
19+
dataFetcher *fetcher.Fetcher
20+
dc string
21+
exportedServices *types.DecodedComputedExportedServices
22+
logger hclog.Logger
23+
trustDomain string
1624
}
1725

18-
func NewProxyStateTemplateBuilder(workload *types.DecodedWorkload) *proxyStateTemplateBuilder {
26+
func NewProxyStateTemplateBuilder(workload *types.DecodedWorkload, exportedServices *types.DecodedComputedExportedServices, logger hclog.Logger, dataFetcher *fetcher.Fetcher, dc, trustDomain string) *proxyStateTemplateBuilder {
1927
return &proxyStateTemplateBuilder{
20-
workload: workload,
28+
workload: workload,
29+
dataFetcher: dataFetcher,
30+
dc: dc,
31+
exportedServices: exportedServices,
32+
logger: logger,
33+
trustDomain: trustDomain,
2134
}
2235
}
2336

internal/mesh/internal/controllers/gatewayproxy/controller.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,41 @@ import (
1313
"github.com/hashicorp/consul/internal/controller/dependency"
1414
"github.com/hashicorp/consul/internal/mesh/internal/controllers/gatewayproxy/builder"
1515
"github.com/hashicorp/consul/internal/mesh/internal/controllers/gatewayproxy/fetcher"
16+
"github.com/hashicorp/consul/internal/mesh/internal/controllers/sidecarproxy"
1617
"github.com/hashicorp/consul/internal/mesh/internal/controllers/sidecarproxy/cache"
18+
"github.com/hashicorp/consul/internal/mesh/internal/types"
1719
"github.com/hashicorp/consul/internal/resource"
1820
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
1921
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
22+
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
2023
"github.com/hashicorp/consul/proto-public/pbresource"
2124
)
2225

2326
// ControllerName is the name for this controller. It's used for logging or status keys.
24-
const ControllerName = "consul.io/gateway-proxy-controller"
27+
const ControllerName = "consul.io/gateway-proxy"
2528

2629
// Controller is responsible for triggering reconciler for watched resources
27-
func Controller(cache *cache.Cache) *controller.Controller {
30+
func Controller(cache *cache.Cache, trustDomainFetcher sidecarproxy.TrustDomainFetcher, dc string, defaultAllow bool) *controller.Controller {
2831
// TODO NET-7016 Use caching functionality in NewController being implemented at time of writing
2932
// TODO NET-7017 Add the host of other types we should watch
3033
return controller.NewController(ControllerName, pbmesh.ProxyStateTemplateType).
3134
WithWatch(pbcatalog.WorkloadType, dependency.ReplaceType(pbmesh.ProxyStateTemplateType)).
3235
WithWatch(pbmesh.ComputedProxyConfigurationType, dependency.ReplaceType(pbmesh.ProxyStateTemplateType)).
3336
WithReconciler(&reconciler{
34-
cache: cache,
37+
cache: cache,
38+
dc: dc,
39+
defaultAllow: defaultAllow,
40+
getTrustDomain: trustDomainFetcher,
3541
})
3642
}
3743

3844
// reconciler is responsible for managing the ProxyStateTemplate for all
3945
// gateway types: mesh, api (future) and terminating (future).
4046
type reconciler struct {
41-
cache *cache.Cache
47+
cache *cache.Cache
48+
dc string
49+
defaultAllow bool
50+
getTrustDomain sidecarproxy.TrustDomainFetcher
4251
}
4352

4453
// Reconcile is responsible for creating and updating the pbmesh.ProxyStateTemplate
@@ -60,9 +69,8 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
6069
}
6170

6271
if workload == nil {
63-
// If workload has been deleted, then return as ProxyStateTemplate should be cleaned up
64-
// by the garbage collector because of the owner reference.
6572
rt.Logger.Trace("workload doesn't exist; skipping reconciliation", "workload", workloadID)
73+
// Workload no longer exists, let garbage collector clean up
6674
return nil
6775
}
6876

@@ -104,7 +112,27 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
104112
rt.Logger.Trace("proxy state template for this gateway doesn't yet exist; generating a new one")
105113
}
106114

107-
newPST := builder.NewProxyStateTemplateBuilder(workload).Build()
115+
exportedServicesID := &pbresource.ID{
116+
Name: "global",
117+
Tenancy: &pbresource.Tenancy{
118+
Partition: req.ID.Tenancy.Partition,
119+
},
120+
Type: pbmulticluster.ExportedServicesType,
121+
}
122+
123+
exportedServices, err := dataFetcher.FetchExportedServices(ctx, exportedServicesID)
124+
if err != nil {
125+
rt.Logger.Error("error reading the associated exported services", "error", err)
126+
exportedServices = &types.DecodedComputedExportedServices{}
127+
}
128+
129+
trustDomain, err := r.getTrustDomain()
130+
if err != nil {
131+
rt.Logger.Error("error fetching trust domain to compute proxy state template", "error", err)
132+
return err
133+
}
134+
135+
newPST := builder.NewProxyStateTemplateBuilder(workload, exportedServices, rt.Logger, dataFetcher, r.dc, trustDomain).Build()
108136

109137
proxyTemplateData, err := anypb.New(newPST)
110138
if err != nil {

internal/mesh/internal/controllers/gatewayproxy/fetcher/data_fetcher.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/consul/internal/resource"
1212
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
1313
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
14+
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1"
1415
"github.com/hashicorp/consul/proto-public/pbresource"
1516
)
1617

@@ -34,7 +35,7 @@ func (f *Fetcher) FetchMeshGateway(ctx context.Context, id *pbresource.ID) (*typ
3435
return nil, nil
3536
}
3637

37-
return dec, err
38+
return dec, nil
3839
}
3940

4041
func (f *Fetcher) FetchProxyStateTemplate(ctx context.Context, id *pbresource.ID) (*types.DecodedProxyStateTemplate, error) {
@@ -45,7 +46,7 @@ func (f *Fetcher) FetchProxyStateTemplate(ctx context.Context, id *pbresource.ID
4546
return nil, nil
4647
}
4748

48-
return dec, err
49+
return dec, nil
4950
}
5051

5152
func (f *Fetcher) FetchWorkload(ctx context.Context, id *pbresource.ID) (*types.DecodedWorkload, error) {
@@ -56,5 +57,27 @@ func (f *Fetcher) FetchWorkload(ctx context.Context, id *pbresource.ID) (*types.
5657
return nil, nil
5758
}
5859

59-
return dec, err
60+
return dec, nil
61+
}
62+
63+
func (f *Fetcher) FetchExportedServices(ctx context.Context, id *pbresource.ID) (*types.DecodedComputedExportedServices, error) {
64+
dec, err := resource.GetDecodedResource[*pbmulticluster.ComputedExportedServices](ctx, f.client, id)
65+
if err != nil {
66+
return nil, err
67+
} else if dec == nil {
68+
return nil, nil
69+
}
70+
71+
return dec, nil
72+
}
73+
74+
func (f *Fetcher) FetchService(ctx context.Context, id *pbresource.ID) (*types.DecodedService, error) {
75+
dec, err := resource.GetDecodedResource[*pbcatalog.Service](ctx, f.client, id)
76+
if err != nil {
77+
return nil, err
78+
} else if dec == nil {
79+
return nil, nil
80+
}
81+
82+
return dec, nil
6083
}

0 commit comments

Comments
 (0)