Skip to content

Commit 1519d8c

Browse files
committed
address review feedback
This commit addresses the review feedback by making the following changes: - use a more object-oriented approach for geoProximity handling - change log levels to warnings instead of errors - add more test cases for geoProximity
1 parent eb200f8 commit 1519d8c

File tree

2 files changed

+419
-50
lines changed

2 files changed

+419
-50
lines changed

provider/aws/aws.go

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ const (
6969
sameZoneAlias = "same-zone"
7070
// Currently supported up to 10 health checks or hosted zones.
7171
// https://docs.aws.amazon.com/Route53/latest/APIReference/API_ListTagsForResources.html#API_ListTagsForResources_RequestSyntax
72-
batchSize = 10
72+
batchSize = 10
73+
minLatitude = -90.0
74+
maxLatitude = 90.0
75+
minLongitude = -180.0
76+
maxLongitude = 180.0
7377
)
7478

7579
// see elb: https://docs.aws.amazon.com/general/latest/gr/elb.html
@@ -235,6 +239,12 @@ type profiledZone struct {
235239
zone *route53types.HostedZone
236240
}
237241

242+
type geoProximity struct {
243+
location *route53types.GeoProximityLocation
244+
endpoint *endpoint.Endpoint
245+
isSet bool
246+
}
247+
238248
func (cs Route53Changes) Route53Changes() []route53types.Change {
239249
var ret []route53types.Change
240250
for _, c := range cs {
@@ -868,16 +878,17 @@ func (p *AWSProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoi
868878
// if the endpoint is using geoproximity, set the bias to 0 if not set
869879
// this is needed to avoid unnecessary Upserts if the desired endpoint doesn't specify a bias
870880
func adjustGeoProximityLocationEndpoint(ep *endpoint.Endpoint) {
871-
if ep.SetIdentifier != "" {
872-
_, ok1 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationAWSRegion)
873-
_, ok2 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup)
874-
_, ok3 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationCoordinates)
875-
876-
if ok1 || ok2 || ok3 {
877-
// check if ep has bias property and if not, set it to 0
878-
if _, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationBias); !ok {
879-
ep.SetProviderSpecificProperty(providerSpecificGeoProximityLocationBias, "0")
880-
}
881+
if ep.SetIdentifier == "" {
882+
return
883+
}
884+
_, ok1 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationAWSRegion)
885+
_, ok2 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup)
886+
_, ok3 := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationCoordinates)
887+
888+
if ok1 || ok2 || ok3 {
889+
// check if ep has bias property and if not, set it to 0
890+
if _, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationBias); !ok {
891+
ep.SetProviderSpecificProperty(providerSpecificGeoProximityLocationBias, "0")
881892
}
882893
}
883894
}
@@ -985,78 +996,99 @@ func (p *AWSProvider) newChange(action route53types.ChangeAction, ep *endpoint.E
985996
return change
986997
}
987998

988-
func withChangeForGeoProximityEndpoint(change *Route53Change, ep *endpoint.Endpoint) {
989-
geoproximity := &route53types.GeoProximityLocation{}
990-
isGeoproximity := false
999+
func newGeoProximity(ep *endpoint.Endpoint) *geoProximity {
1000+
return &geoProximity{
1001+
location: &route53types.GeoProximityLocation{},
1002+
endpoint: ep,
1003+
isSet: false,
1004+
}
1005+
}
9911006

992-
isGeoproximity = withGeoProximityAWSRegion(ep, geoproximity)
993-
isGeoproximity = isGeoproximity || withGeoProximityCoordinates(ep, geoproximity)
994-
isGeoproximity = isGeoproximity || withGeoProximityLocalZoneGroup(ep, geoproximity)
995-
withGeoProximityBias(ep, geoproximity)
1007+
func (gp *geoProximity) withAWSRegion() *geoProximity {
1008+
if prop, ok := gp.endpoint.GetProviderSpecificProperty(providerSpecificGeoProximityLocationAWSRegion); ok {
1009+
gp.location.AWSRegion = aws.String(prop)
1010+
gp.isSet = true
1011+
}
1012+
return gp
1013+
}
9961014

997-
if isGeoproximity {
998-
change.ResourceRecordSet.GeoProximityLocation = geoproximity
1015+
// add a method to set the local zone group for the geoproximity location
1016+
func (gp *geoProximity) withLocalZoneGroup() *geoProximity {
1017+
if prop, ok := gp.endpoint.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup); ok {
1018+
gp.location.LocalZoneGroup = aws.String(prop)
1019+
gp.isSet = true
9991020
}
1021+
return gp
10001022
}
10011023

1002-
func withGeoProximityAWSRegion(ep *endpoint.Endpoint, geoproximity *route53types.GeoProximityLocation) bool {
1003-
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationAWSRegion); ok {
1004-
geoproximity.AWSRegion = aws.String(prop)
1005-
return true
1024+
// add a method to set the bias for the geoproximity location
1025+
func (gp *geoProximity) withBias() *geoProximity {
1026+
if prop, ok := gp.endpoint.GetProviderSpecificProperty(providerSpecificGeoProximityLocationBias); ok {
1027+
bias, err := strconv.ParseInt(prop, 10, 32)
1028+
if err != nil {
1029+
log.Warnf("Failed parsing value of %s: %s: %v; using bias of 0", providerSpecificGeoProximityLocationBias, prop, err)
1030+
bias = 0
1031+
}
1032+
gp.location.Bias = aws.Int32(int32(bias))
1033+
gp.isSet = true
10061034
}
1007-
return false
1035+
return gp
10081036
}
10091037

10101038
// validateCoordinates checks if the given latitude and longitude are valid.
10111039
func validateCoordinates(lat, long string) error {
10121040
latitude, err := strconv.ParseFloat(lat, 64)
1013-
if err != nil || latitude < -90 || latitude > 90 {
1014-
return errors.New("invalid latitude: must be a number between -90 and 90")
1041+
if err != nil || latitude < minLatitude || latitude > maxLatitude {
1042+
return fmt.Errorf("invalid latitude: must be a number between %f and %f", minLatitude, maxLatitude)
10151043
}
10161044

10171045
longitude, err := strconv.ParseFloat(long, 64)
1018-
if err != nil || longitude < -180 || longitude > 180 {
1019-
return errors.New("invalid longitude: must be a number between -180 and 180")
1046+
if err != nil || longitude < minLongitude || longitude > maxLongitude {
1047+
return fmt.Errorf("invalid longitude: must be a number between %f and %f", minLongitude, maxLongitude)
10201048
}
10211049

10221050
return nil
10231051
}
10241052

1025-
func withGeoProximityCoordinates(ep *endpoint.Endpoint, geoproximity *route53types.GeoProximityLocation) bool {
1026-
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationCoordinates); ok {
1053+
func (gp *geoProximity) withCoordinates() *geoProximity {
1054+
if prop, ok := gp.endpoint.GetProviderSpecificProperty(providerSpecificGeoProximityLocationCoordinates); ok {
10271055
coordinates := strings.Split(prop, ",")
10281056
if len(coordinates) == 2 {
10291057
latitude := coordinates[0]
10301058
longitude := coordinates[1]
10311059
if err := validateCoordinates(latitude, longitude); err != nil {
1032-
log.Errorf("Invalid coordinates %s for name=%s setIdentifier=%s; %v", prop, ep.DNSName, ep.SetIdentifier, err)
1033-
return false
1034-
}
1035-
geoproximity.Coordinates = &route53types.Coordinates{
1036-
Latitude: aws.String(latitude),
1037-
Longitude: aws.String(longitude),
1060+
log.Warnf("Invalid coordinates %s for name=%s setIdentifier=%s; %v", prop, gp.endpoint.DNSName, gp.endpoint.SetIdentifier, err)
1061+
} else {
1062+
gp.location.Coordinates = &route53types.Coordinates{
1063+
Latitude: aws.String(latitude),
1064+
Longitude: aws.String(longitude),
1065+
}
1066+
gp.isSet = true
10381067
}
1039-
return true
10401068
} else {
1041-
log.Errorf("Invalid coordinates format for %s: %s; expected format 'latitude,longitude'", providerSpecificGeoProximityLocationCoordinates, prop)
1069+
log.Warnf("Invalid coordinates format for %s: %s; expected format 'latitude,longitude'", providerSpecificGeoProximityLocationCoordinates, prop)
10421070
}
10431071
}
1044-
return false
1072+
return gp
10451073
}
10461074

1047-
func withGeoProximityLocalZoneGroup(ep *endpoint.Endpoint, geoproximity *route53types.GeoProximityLocation) bool {
1048-
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationLocalZoneGroup); ok {
1049-
geoproximity.LocalZoneGroup = aws.String(prop)
1050-
return true
1075+
func (gp *geoProximity) build() *route53types.GeoProximityLocation {
1076+
if gp.isSet {
1077+
return gp.location
10511078
}
1052-
return false
1079+
return nil
10531080
}
10541081

1055-
func withGeoProximityBias(ep *endpoint.Endpoint, geoproximity *route53types.GeoProximityLocation) {
1056-
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGeoProximityLocationBias); ok {
1057-
bias, _ := strconv.ParseInt(prop, 10, 32)
1058-
geoproximity.Bias = aws.Int32(int32(bias))
1059-
}
1082+
func withChangeForGeoProximityEndpoint(change *Route53Change, ep *endpoint.Endpoint) {
1083+
1084+
geoProx := newGeoProximity(ep).
1085+
withAWSRegion().
1086+
withCoordinates().
1087+
withLocalZoneGroup().
1088+
withBias()
1089+
1090+
change.ResourceRecordSet.GeoProximityLocation = geoProx.build()
1091+
10601092
}
10611093

10621094
// 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`)

0 commit comments

Comments
 (0)