-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(aws): add support for geoproximity routing #5347
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -53,16 +53,20 @@ const ( | |||||
// providerSpecificEvaluateTargetHealth specifies whether an AWS ALIAS record | ||||||
// has the EvaluateTargetHealth field set to true. Present iff the endpoint | ||||||
// has a `providerSpecificAlias` value of `true`. | ||||||
providerSpecificEvaluateTargetHealth = "aws/evaluate-target-health" | ||||||
providerSpecificWeight = "aws/weight" | ||||||
providerSpecificRegion = "aws/region" | ||||||
providerSpecificFailover = "aws/failover" | ||||||
providerSpecificGeolocationContinentCode = "aws/geolocation-continent-code" | ||||||
providerSpecificGeolocationCountryCode = "aws/geolocation-country-code" | ||||||
providerSpecificGeolocationSubdivisionCode = "aws/geolocation-subdivision-code" | ||||||
providerSpecificMultiValueAnswer = "aws/multi-value-answer" | ||||||
providerSpecificHealthCheckID = "aws/health-check-id" | ||||||
sameZoneAlias = "same-zone" | ||||||
providerSpecificEvaluateTargetHealth = "aws/evaluate-target-health" | ||||||
providerSpecificWeight = "aws/weight" | ||||||
providerSpecificRegion = "aws/region" | ||||||
providerSpecificFailover = "aws/failover" | ||||||
providerSpecificGeolocationContinentCode = "aws/geolocation-continent-code" | ||||||
providerSpecificGeolocationCountryCode = "aws/geolocation-country-code" | ||||||
providerSpecificGeolocationSubdivisionCode = "aws/geolocation-subdivision-code" | ||||||
providerSpecificGeoProximityLocationAWSRegion = "aws/geoproximitylocation-aws-region" | ||||||
providerSpecificGeoProximityLocationBias = "aws/geoproximitylocation-bias" | ||||||
providerSpecificGeoProximityLocationCoordinates = "aws/geoproximitylocation-coordinates" | ||||||
providerSpecificGeoProximityLocationLocalZoneGroup = "aws/geoproximitylocation-local-zone-group" | ||||||
prasadkatti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
providerSpecificMultiValueAnswer = "aws/multi-value-answer" | ||||||
providerSpecificHealthCheckID = "aws/health-check-id" | ||||||
sameZoneAlias = "same-zone" | ||||||
// Currently supported up to 10 health checks or hosted zones. | ||||||
// https://docs.aws.amazon.com/Route53/latest/APIReference/API_ListTagsForResources.html#API_ListTagsForResources_RequestSyntax | ||||||
batchSize = 10 | ||||||
|
@@ -542,6 +546,8 @@ func (p *AWSProvider) records(ctx context.Context, zones map[string]*profiledZon | |||||
ep.WithProviderSpecific(providerSpecificGeolocationSubdivisionCode, *r.GeoLocation.SubdivisionCode) | ||||||
} | ||||||
} | ||||||
case r.GeoProximityLocation != nil: | ||||||
prasadkatti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
handleGeoProximityLocationRecord(&r, ep) | ||||||
default: | ||||||
// one of the above needs to be set, otherwise SetIdentifier doesn't make sense | ||||||
} | ||||||
|
@@ -560,6 +566,22 @@ func (p *AWSProvider) records(ctx context.Context, zones map[string]*profiledZon | |||||
return endpoints, nil | ||||||
} | ||||||
|
||||||
func handleGeoProximityLocationRecord(r *route53types.ResourceRecordSet, ep *endpoint.Endpoint) { | ||||||
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. Here is suggested improvements
|
||||||
if r.GeoProximityLocation.AWSRegion != nil { | ||||||
ep.WithProviderSpecific(providerSpecificGeoProximityLocationAWSRegion, aws.ToString(r.GeoProximityLocation.AWSRegion)) | ||||||
} | ||||||
if r.GeoProximityLocation.Bias != nil { | ||||||
ep.WithProviderSpecific(providerSpecificGeoProximityLocationBias, fmt.Sprintf("%d", aws.ToInt32(r.GeoProximityLocation.Bias))) | ||||||
} | ||||||
if r.GeoProximityLocation.Coordinates != nil { | ||||||
coordinates := fmt.Sprintf("%s,%s", aws.ToString(r.GeoProximityLocation.Coordinates.Latitude), aws.ToString(r.GeoProximityLocation.Coordinates.Longitude)) | ||||||
ep.WithProviderSpecific(providerSpecificGeoProximityLocationCoordinates, coordinates) | ||||||
} | ||||||
if r.GeoProximityLocation.LocalZoneGroup != nil { | ||||||
ep.WithProviderSpecific(providerSpecificGeoProximityLocationLocalZoneGroup, aws.ToString(r.GeoProximityLocation.LocalZoneGroup)) | ||||||
} | ||||||
} | ||||||
|
||||||
// Identify if old and new endpoints require DELETE/CREATE instead of UPDATE. | ||||||
func (p *AWSProvider) requiresDeleteCreate(old *endpoint.Endpoint, newE *endpoint.Endpoint) bool { | ||||||
// a change of a record type | ||||||
|
@@ -832,12 +854,31 @@ func (p *AWSProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoi | |||||
} else { | ||||||
ep.DeleteProviderSpecificProperty(providerSpecificEvaluateTargetHealth) | ||||||
} | ||||||
|
||||||
adjustGeoProximityLocationEndpoint(ep) | ||||||
} | ||||||
|
||||||
endpoints = append(endpoints, aliasCnameAaaaEndpoints...) | ||||||
return endpoints, nil | ||||||
} | ||||||
|
||||||
// if the endpoint is using geoproximity, set the bias to 0 if not set | ||||||
// this is needed to avoid unnecessary Upserts if the desired endpoint doesn't specify a bias | ||||||
func adjustGeoProximityLocationEndpoint(ep *endpoint.Endpoint) { | ||||||
if ep.SetIdentifier != "" { | ||||||
_, ok1 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationAWSRegion) | ||||||
_, ok2 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup) | ||||||
_, ok3 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationCoordinates) | ||||||
|
||||||
if ok1 || ok2 || ok3 { | ||||||
// check if ep has bias property and if not, set it to 0 | ||||||
if _, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationBias); !ok { | ||||||
ep.SetProviderSpecificProperty(providerSpecificGeoProximityLocationBias, "0") | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// newChange returns a route53 Change | ||||||
// returned Change is based on the given record by the given action, e.g. | ||||||
// action=ChangeActionCreate returns a change for creation of the record and | ||||||
|
@@ -926,6 +967,8 @@ func (p *AWSProvider) newChange(action route53types.ChangeAction, ep *endpoint.E | |||||
if useGeolocation { | ||||||
change.ResourceRecordSet.GeoLocation = geolocation | ||||||
} | ||||||
|
||||||
buildChangeForGeoProximityEndpoint(change, ep) | ||||||
} | ||||||
|
||||||
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificHealthCheckID); ok { | ||||||
|
@@ -939,6 +982,40 @@ func (p *AWSProvider) newChange(action route53types.ChangeAction, ep *endpoint.E | |||||
return change | ||||||
} | ||||||
|
||||||
func buildChangeForGeoProximityEndpoint(change *Route53Change, ep *endpoint.Endpoint) { | ||||||
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. 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
|
||||||
geoproximity := &route53types.GeoProximityLocation{} | ||||||
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 method complexity is quite high, we need to split it most likely, example func buildChangeForGeoProximityEndpoin(......)
geoproximity := &route53types.GeoProximityLocation{}
isGeoproximity := false
isGeoproximity = withGeoProximityAWSRegion(ep, geoproximity) || useGeoproximity
isGeoproximity = withGeoProximityBias(ep, geoproximity) || useGeoproximity
isGeoproximity = withGeoProximityCoordinates(ep, geoproximity) || useGeoproximity
isGeoproximity = withGeoProximityLocalZoneGroup(ep, geoproximity) || useGeoproximity
if isGeoproximity {
change.ResourceRecordSet.GeoProximityLocation = geoproximity
}
}
where
func withGeoProximityLocalZoneGroup(ep *endpoint.Endpoint, geoproximity *route53types.GeoProximityLocation) bool {
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup); ok {
geoproximity.LocalZoneGroup = aws.String(prop)
return true
}
return false
} |
||||||
useGeoproximity := false | ||||||
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationAWSRegion); ok { | ||||||
geoproximity.AWSRegion = aws.String(prop) | ||||||
useGeoproximity = true | ||||||
} | ||||||
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationBias); ok { | ||||||
bias, _ := strconv.ParseInt(prop, 10, 32) | ||||||
geoproximity.Bias = aws.Int32(int32(bias)) | ||||||
} | ||||||
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationCoordinates); ok { | ||||||
coordinates := strings.Split(prop, ",") | ||||||
if len(coordinates) == 2 { | ||||||
latitude := coordinates[0] | ||||||
longitude := coordinates[1] | ||||||
geoproximity.Coordinates = &route53types.Coordinates{ | ||||||
Latitude: aws.String(latitude), | ||||||
Longitude: aws.String(longitude), | ||||||
} | ||||||
Comment on lines
+999
to
+1004
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. Would be nice to add some validation here as well
Probably something like // validateCoordinates checks if the given latitude and longitude are valid.
func (c *Coordinates) validate(lat, lon string) error {
latitude, err := strconv.ParseFloat(lat, 64)
if err != nil || latitude < -90 || latitude > 90 {
return errors.New("invalid latitude: must be a number between -90 and 90")
}
longitude, err := strconv.ParseFloat(lon, 64)
if err != nil || longitude < -180 || longitude > 180 {
return errors.New("invalid longitude: must be a number between -180 and 180")
}
return nil
}
// Example usage
func (c *Coordinates) validateAndSet(coordinates string) (*route53types.Coordinates, error) {
parts := strings.Split(coordinates, ",")
if len(parts) != 2 {
return nil, errors.New("invalid coordinates format: expected 'latitude,longitude'")
}
lat := strings.TrimSpace(parts[0])
lon := strings.TrimSpace(parts[1])
if err := validateCoordinates(lat, lon); err != nil {
return nil, err
}
return &route53types.Coordinates{
Latitude: aws.String(lat),
Longitude: aws.String(lon),
}, nil
} |
||||||
useGeoproximity = true | ||||||
} else { | ||||||
log.Errorf("Invalid coordinates format for %s: %s; expected format 'latitude,longitude'", providerSpecificGeoProximityLocationCoordinates, prop) | ||||||
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 is debatable, but most likely should be log.Warn, to warn user so they need to change configuraiton |
||||||
} | ||||||
} | ||||||
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup); ok { | ||||||
geoproximity.LocalZoneGroup = aws.String(prop) | ||||||
useGeoproximity = true | ||||||
} | ||||||
if useGeoproximity { | ||||||
change.ResourceRecordSet.GeoProximityLocation = geoproximity | ||||||
} | ||||||
} | ||||||
|
||||||
// searches for `changes` that are contained in `queue` and returns the `changes` separated by whether they were found in the queue (`foundChanges`) or not (`notFoundChanges`) | ||||||
func findChangesInQueue(changes Route53Changes, queue Route53Changes) (foundChanges, notFoundChanges Route53Changes) { | ||||||
if queue == nil { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aws-region
not required, justregion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried to match it with the field names in the GeoProximityLocation type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aws/geoproximitylocation-aws-region
does not looks right