-
Notifications
You must be signed in to change notification settings - Fork 4.5k
update gateway-services table with endpoints #13217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9c2cd37
update gateway-services table with endpoints
dhiaayachi 98c4358
fix failing test
dhiaayachi 1734104
remove unneeded config in test
dhiaayachi cdd5543
rename "endpoint" to "destination"
dhiaayachi aad5214
more endpoint renaming to destination in tests
dhiaayachi a312202
update isDestination based on service-defaults config entry creation
dhiaayachi 2fd75dc
use a 3 state kind to be able to set the kind to unknown (when neithe…
dhiaayachi 4e361d7
set unknown state to empty to avoid modifying alot of tests
dhiaayachi f6aafd8
fix logic to set the kind correctly on CRUD
dhiaayachi db9529f
fix failing tests
dhiaayachi 3c5b85c
Merge pull request #13226 from hashicorp/egress-gtw/rename-destination
dhiaayachi 2147d77
Merge branch 'main' into egress-gtw/tgtw-gateway-services
dhiaayachi 9949f4d
add missing tests and fix service delete
dhiaayachi 35a80c5
fix failing test
dhiaayachi 1e6803f
Apply suggestions from code review
dhiaayachi 711bdc5
fix a bug with kind and add relevant test
dhiaayachi fcd4a2e
fix compile error
dhiaayachi 8a9ff79
fix failing tests
dhiaayachi 5d66a08
add kind to clone
dhiaayachi 36d52fa
fix failing tests
dhiaayachi 9ed9345
fix failing tests in catalog endpoint
dhiaayachi 042b874
fix service dump test
dhiaayachi ac6c9b4
Apply suggestions from code review
dhiaayachi 2440051
remove duplicate tests
dhiaayachi f900906
rename consts and fix kind when no destination is defined in the serv…
dhiaayachi f964e09
rename Kind to ServiceKind and change switch to use .(type)
dhiaayachi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -846,9 +846,16 @@ func ensureServiceTxn(tx WriteTxn, idx uint64, node string, preserveIndexes bool | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if svc.PeerName == "" { | ||||||||||||||||||||||||||
// Check if this service is covered by a gateway's wildcard specifier | ||||||||||||||||||||||||||
if err = checkGatewayWildcardsAndUpdate(tx, idx, svc); err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed updating gateway mapping: %s", err) | ||||||||||||||||||||||||||
// Do not associate non-typical services with gateways or consul services | ||||||||||||||||||||||||||
if svc.Kind == structs.ServiceKindTypical && svc.Service != "consul" { | ||||||||||||||||||||||||||
// Check if this service is covered by a gateway's wildcard specifier, we force the service kind to a gateway-service here as that take precedence | ||||||||||||||||||||||||||
sn := structs.ServiceName{Name: svc.Service, EnterpriseMeta: svc.EnterpriseMeta} | ||||||||||||||||||||||||||
if err = checkGatewayWildcardsAndUpdate(tx, idx, &sn, structs.GatewayservicekindService); err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed updating gateway mapping: %s", err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if err = checkGatewayAndUpdate(tx, idx, &sn, structs.GatewayservicekindService); err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed updating gateway mapping: %s", err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if err := upsertKindServiceName(tx, idx, svc.Kind, svc.CompoundServiceName()); err != nil { | ||||||||||||||||||||||||||
|
@@ -3469,6 +3476,7 @@ func terminatingConfigGatewayServices( | |||||||||||||||||||||||||
return false, nil, fmt.Errorf("unexpected config entry type: %T", conf) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// TODO(egress-tproxy): Check if it's an endpoint by quering the service and the service defaut config entry. | ||||||||||||||||||||||||||
dhiaayachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
// Check if service list matches the last known list for the config entry, if it does, skip the update | ||||||||||||||||||||||||||
_, c, err := configEntryTxn(tx, nil, conf.GetKind(), conf.GetName(), entMeta) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
|
@@ -3483,6 +3491,10 @@ func terminatingConfigGatewayServices( | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
var gatewayServices structs.GatewayServices | ||||||||||||||||||||||||||
for _, svc := range entry.Services { | ||||||||||||||||||||||||||
kind, err := GatewayServiceKind(tx, svc.Name, &svc.EnterpriseMeta) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return false, nil, fmt.Errorf("failed to query endpoints: %v", err) | ||||||||||||||||||||||||||
dhiaayachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
mapping := &structs.GatewayService{ | ||||||||||||||||||||||||||
Gateway: gateway, | ||||||||||||||||||||||||||
Service: structs.NewServiceName(svc.Name, &svc.EnterpriseMeta), | ||||||||||||||||||||||||||
|
@@ -3491,13 +3503,36 @@ func terminatingConfigGatewayServices( | |||||||||||||||||||||||||
CertFile: svc.CertFile, | ||||||||||||||||||||||||||
CAFile: svc.CAFile, | ||||||||||||||||||||||||||
SNI: svc.SNI, | ||||||||||||||||||||||||||
Kind: kind, | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
gatewayServices = append(gatewayServices, mapping) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
return false, gatewayServices, nil | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
func GatewayServiceKind(tx ReadTxn, name string, entMeta *acl.EnterpriseMeta) (structs.GatewayServiceKind, error) { | ||||||||||||||||||||||||||
serviceIter, err := tx.Get(tableServices, indexService, Query{ | ||||||||||||||||||||||||||
Value: name, | ||||||||||||||||||||||||||
EnterpriseMeta: *entMeta, | ||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return structs.GatewayservicekindService, err | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
for service := serviceIter.Next(); service != nil; service = serviceIter.Next() { | ||||||||||||||||||||||||||
return structs.GatewayservicekindUnknown, nil | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have failed some tests, I will check if we can cover it. |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
_, entry, err := configEntryTxn(tx, nil, structs.ServiceDefaults, name, entMeta) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return structs.GatewayservicekindUnknown, err | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if entry != nil { | ||||||||||||||||||||||||||
return structs.GatewayservicekindDestination, nil | ||||||||||||||||||||||||||
rboyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
return structs.GatewayservicekindUnknown, nil | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// updateGatewayNamespace is used to target all services within a namespace | ||||||||||||||||||||||||||
func updateGatewayNamespace(tx WriteTxn, idx uint64, service *structs.GatewayService, entMeta *acl.EnterpriseMeta) error { | ||||||||||||||||||||||||||
if entMeta == nil { | ||||||||||||||||||||||||||
|
@@ -3538,6 +3573,41 @@ func updateGatewayNamespace(tx WriteTxn, idx uint64, service *structs.GatewaySer | |||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
endpoints, err := tx.Get(tableConfigEntries, indexID+"_prefix", ConfigEntryKindQuery{Kind: structs.ServiceDefaults, EnterpriseMeta: *entMeta}) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed querying endpoints: %s", err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
for endpoint := endpoints.Next(); endpoint != nil; endpoint = endpoints.Next() { | ||||||||||||||||||||||||||
e := endpoint.(*structs.ServiceConfigEntry) | ||||||||||||||||||||||||||
dhiaayachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
if e.Destination == nil { | ||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
sn := structs.ServiceName{ | ||||||||||||||||||||||||||
Name: e.Name, | ||||||||||||||||||||||||||
EnterpriseMeta: e.EnterpriseMeta, | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
existing, err := tx.First(tableGatewayServices, indexID, service.Gateway, sn, service.Port) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("gateway service lookup failed: %s", err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if existing != nil { | ||||||||||||||||||||||||||
// If there's an existing service associated with this gateway then we skip it. | ||||||||||||||||||||||||||
// This means the service was specified on its own, and the service entry overrides the wildcard entry. | ||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
mapping := service.Clone() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
mapping.Service = structs.NewServiceName(e.Name, &service.Service.EnterpriseMeta) | ||||||||||||||||||||||||||
mapping.Kind = structs.GatewayservicekindDestination | ||||||||||||||||||||||||||
mapping.FromWildcard = true | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
err = updateGatewayService(tx, idx, mapping) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Also store a mapping for the wildcard so that the TLS creds can be pulled | ||||||||||||||||||||||||||
// for new services registered in its namespace | ||||||||||||||||||||||||||
|
@@ -3585,25 +3655,21 @@ func updateGatewayService(tx WriteTxn, idx uint64, mapping *structs.GatewayServi | |||||||||||||||||||||||||
// checkWildcardForGatewaysAndUpdate checks whether a service matches a | ||||||||||||||||||||||||||
// wildcard definition in gateway config entries and if so adds it the the | ||||||||||||||||||||||||||
// gateway-services table. | ||||||||||||||||||||||||||
func checkGatewayWildcardsAndUpdate(tx WriteTxn, idx uint64, svc *structs.NodeService) error { | ||||||||||||||||||||||||||
// Do not associate non-typical services with gateways or consul services | ||||||||||||||||||||||||||
if svc.Kind != structs.ServiceKindTypical || svc.Service == "consul" { | ||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
func checkGatewayWildcardsAndUpdate(tx WriteTxn, idx uint64, svc *structs.ServiceName, kind structs.GatewayServiceKind) error { | ||||||||||||||||||||||||||
sn := structs.ServiceName{Name: structs.WildcardSpecifier, EnterpriseMeta: svc.EnterpriseMeta} | ||||||||||||||||||||||||||
svcGateways, err := tx.Get(tableGatewayServices, indexService, sn) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed gateway lookup for %q: %s", svc.Service, err) | ||||||||||||||||||||||||||
return fmt.Errorf("failed gateway lookup for %q: %s", svc.Name, err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
for service := svcGateways.Next(); service != nil; service = svcGateways.Next() { | ||||||||||||||||||||||||||
if wildcardSvc, ok := service.(*structs.GatewayService); ok && wildcardSvc != nil { | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Copy the wildcard mapping and modify it | ||||||||||||||||||||||||||
gatewaySvc := wildcardSvc.Clone() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
gatewaySvc.Service = structs.NewServiceName(svc.Service, &svc.EnterpriseMeta) | ||||||||||||||||||||||||||
gatewaySvc.Service = structs.NewServiceName(svc.Name, &svc.EnterpriseMeta) | ||||||||||||||||||||||||||
gatewaySvc.FromWildcard = true | ||||||||||||||||||||||||||
gatewaySvc.Kind = kind | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if err = updateGatewayService(tx, idx, gatewaySvc); err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("Failed to associate service %q with gateway %q", gatewaySvc.Service.String(), gatewaySvc.Gateway.String()) | ||||||||||||||||||||||||||
|
@@ -3613,6 +3679,31 @@ func checkGatewayWildcardsAndUpdate(tx WriteTxn, idx uint64, svc *structs.NodeSe | |||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// checkGatewayAndUpdate checks whether a service matches a | ||||||||||||||||||||||||||
// wildcard definition in gateway config entries and if so adds it the the | ||||||||||||||||||||||||||
// gateway-services table. | ||||||||||||||||||||||||||
func checkGatewayAndUpdate(tx WriteTxn, idx uint64, svc *structs.ServiceName, kind structs.GatewayServiceKind) error { | ||||||||||||||||||||||||||
sn := structs.ServiceName{Name: svc.Name, EnterpriseMeta: svc.EnterpriseMeta} | ||||||||||||||||||||||||||
svcGateways, err := tx.First(tableGatewayServices, indexService, sn) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed gateway lookup for %q: %s", svc.Name, err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if service, ok := svcGateways.(*structs.GatewayService); ok && service != nil { | ||||||||||||||||||||||||||
// Copy the wildcard mapping and modify it | ||||||||||||||||||||||||||
gatewaySvc := service.Clone() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
gatewaySvc.Service = structs.NewServiceName(svc.Name, &svc.EnterpriseMeta) | ||||||||||||||||||||||||||
gatewaySvc.Kind = kind | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if err = updateGatewayService(tx, idx, gatewaySvc); err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("Failed to associate service %q with gateway %q", gatewaySvc.Service.String(), gatewaySvc.Gateway.String()) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
func cleanupGatewayWildcards(tx WriteTxn, idx uint64, svc *structs.ServiceNode) error { | ||||||||||||||||||||||||||
// Clean up association between service name and gateways if needed | ||||||||||||||||||||||||||
sn := structs.ServiceName{Name: svc.ServiceName, EnterpriseMeta: svc.EnterpriseMeta} | ||||||||||||||||||||||||||
|
@@ -3643,6 +3734,12 @@ func cleanupGatewayWildcards(tx WriteTxn, idx uint64, svc *structs.ServiceNode) | |||||||||||||||||||||||||
if err := deleteGatewayServiceTopologyMapping(tx, idx, m); err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed to reconcile mesh topology for gateway: %v", err) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
kind, err := GatewayServiceKind(tx, m.Service.Name, &m.Service.EnterpriseMeta) | ||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||
return fmt.Errorf("failed to reconcile mesh topology for gateway: %v", err) | ||||||||||||||||||||||||||
dhiaayachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
checkGatewayAndUpdate(tx, idx, &structs.ServiceName{Name: m.Service.Name, EnterpriseMeta: m.Service.EnterpriseMeta}, kind) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.