Skip to content

Commit

Permalink
Create ProxyStateTemplate builder for gatewayproxy controller
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancoleman committed Dec 16, 2023
1 parent 7b35649 commit 69b0ef6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 20 deletions.
62 changes: 62 additions & 0 deletions internal/mesh/internal/controllers/gatewayproxy/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package builder

import (
"github.com/hashicorp/consul/internal/mesh/internal/types"
pbauth "github.com/hashicorp/consul/proto-public/pbauth/v2beta1"
meshv2beta1 "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/proto-public/pbmesh/v2beta1/pbproxystate"
"github.com/hashicorp/consul/proto-public/pbresource"
)

type proxyStateTemplateBuilder struct {
workload *types.DecodedWorkload
}

func NewProxyStateTemplateBuilder(workload *types.DecodedWorkload) *proxyStateTemplateBuilder {
return &proxyStateTemplateBuilder{
workload: workload,
}
}

func (b *proxyStateTemplateBuilder) identity() *pbresource.Reference {
return &pbresource.Reference{
Name: b.workload.Id.Name,
Tenancy: b.workload.Id.Tenancy,
Type: pbauth.WorkloadIdentityType,
}
}

func (b *proxyStateTemplateBuilder) listeners() []*pbproxystate.Listener {
// TODO NET-6429
return nil
}

func (b *proxyStateTemplateBuilder) clusters() map[string]*pbproxystate.Cluster {
// TODO NET-6430
return nil
}

func (b *proxyStateTemplateBuilder) endpoints() map[string]*pbproxystate.Endpoints {
// TODO NET-6431
return nil
}

func (b *proxyStateTemplateBuilder) routes() map[string]*pbproxystate.Route {
// TODO NET-6428
return nil
}

func (b *proxyStateTemplateBuilder) Build() *meshv2beta1.ProxyStateTemplate {
return &meshv2beta1.ProxyStateTemplate{
ProxyState: &meshv2beta1.ProxyState{
Identity: b.identity(),
Listeners: b.listeners(),
Clusters: b.clusters(),
Endpoints: b.endpoints(),
Routes: b.routes(),
},
RequiredEndpoints: make(map[string]*pbproxystate.EndpointRef),
RequiredLeafCertificates: make(map[string]*pbproxystate.LeafCertificateRef),
RequiredTrustBundles: make(map[string]*pbproxystate.TrustBundleRef),
}
}
37 changes: 17 additions & 20 deletions internal/mesh/internal/controllers/gatewayproxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"

"github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/mesh/internal/controllers/gatewayproxy/builder"
"github.com/hashicorp/consul/internal/mesh/internal/controllers/gatewayproxy/fetcher"
"github.com/hashicorp/consul/internal/mesh/internal/controllers/sidecarproxy/cache"
"github.com/hashicorp/consul/internal/resource"
Expand Down Expand Up @@ -41,27 +42,24 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
rt.Logger = rt.Logger.With("resource-id", req.ID, "controller", ControllerName)
rt.Logger.Trace("reconciling proxy state template")

var gatewayType *pbresource.Type
// Instantiate a data fetcher to fetch all reconciliation data.
dataFetcher := fetcher.New(rt.Client, r.cache)

// If the workload is not for a xGateway, let the sidecarproxy reconciler handle it
workloadID := resource.ReplaceType(pbcatalog.WorkloadType, req.ID)
res, err := rt.Client.Read(ctx, &pbresource.ReadRequest{Id: workloadID})
if err != nil || res.Resource == nil || res.Resource.Id == nil {
workload, err := dataFetcher.FetchWorkload(ctx, workloadID)
if err != nil {
rt.Logger.Error("error reading the associated workload", "error", err)
return err
} else {
if gatewayKind := res.Resource.Metadata["gateway-kind"]; gatewayKind == "" {
rt.Logger.Trace("workload is not a gateway; skipping reconciliation", "workload", workloadID)
return nil
}
}

// Instantiate a data fetcher to fetch all reconciliation data.
dataFetcher := fetcher.New(rt.Client, r.cache)
// If the workload is not for a xGateway, let the sidecarproxy reconciler handle it
if gatewayKind := workload.Metadata["gateway-kind"]; gatewayKind == "" {
rt.Logger.Trace("workload is not a gateway; skipping reconciliation", "workload", workloadID)
return nil
}

// Check if the gateway exists.
// TODO Switch fetch method based on gatewayType
gatewayID := resource.ReplaceType(gatewayType, req.ID)
gatewayID := resource.ReplaceType(pbmesh.MeshGatewayType, req.ID)
gateway, err := dataFetcher.FetchMeshGateway(ctx, gatewayID)
if err != nil {
rt.Logger.Error("error reading the associated gateway", "error", err)
Expand All @@ -81,14 +79,13 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
}

if proxyStateTemplate == nil {
req.ID.Uid = ""
rt.Logger.Trace("proxy state template for this gateway doesn't yet exist; generating a new one")
}

if proxyStateTemplate == nil {
req.ID.Uid = ""
}
newPST := builder.NewProxyStateTemplateBuilder(workload).Build()

proxyTemplateData, err := anypb.New(proxyStateTemplate)
proxyTemplateData, err := anypb.New(newPST)
if err != nil {
rt.Logger.Error("error creating proxy state template data", "error", err)
return err
Expand All @@ -98,9 +95,9 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c
// Write the created/updated ProxyStateTemplate with MeshGateway owner
_, err = rt.Client.Write(ctx, &pbresource.WriteRequest{
Resource: &pbresource.Resource{
Id: req.ID,
Owner: resource.ReplaceType(gatewayType, req.ID),
Data: proxyTemplateData,
Id: req.ID,
Metadata: map[string]string{"gateway-kind": workload.Metadata["gateway-kind"]},
Data: proxyTemplateData,
},
})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/consul/internal/mesh/internal/controllers/sidecarproxy/cache"
"github.com/hashicorp/consul/internal/mesh/internal/types"
"github.com/hashicorp/consul/internal/resource"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/proto-public/pbresource"
)
Expand Down Expand Up @@ -54,3 +55,18 @@ func (f *Fetcher) FetchProxyStateTemplate(ctx context.Context, id *pbresource.ID

return dec, err
}

func (f *Fetcher) FetchWorkload(ctx context.Context, id *pbresource.ID) (*types.DecodedWorkload, error) {
dec, err := resource.GetDecodedResource[*pbcatalog.Workload](ctx, f.client, id)
if err != nil {
return nil, err
} else if dec == nil {
// We also need to make sure to delete the associated proxy from cache.
// TODO f.cache.UntrackWorkload(id)
return nil, nil
}

// TODO f.cache.TrackWorkload(dec)

return dec, err
}

0 comments on commit 69b0ef6

Please sign in to comment.