-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dinar Valeev <[email protected]>
- Loading branch information
Showing
5 changed files
with
110 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gateway | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/oschwald/maxminddb-golang" | ||
endpoint "sigs.k8s.io/external-dns/endpoint" | ||
) | ||
|
||
func fetchEndpointTargets(endpoints []*endpoint.Endpoint, host string, ip net.IP) (results []string, ttl endpoint.TTL) { | ||
for _, ep := range endpoints { | ||
if ep.DNSName == host { | ||
ttl = ep.RecordTTL | ||
if ep.Labels["strategy"] == "geoip" { | ||
results = extractGeo(ep, ip) | ||
if len(results) > 0 { | ||
return | ||
} | ||
} | ||
results = ep.Targets | ||
} | ||
} | ||
return | ||
} | ||
|
||
func extractGeo(endpoint *endpoint.Endpoint, clientIP net.IP) (result []string) { | ||
db, err := maxminddb.Open("geoip.mmdb") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer db.Close() | ||
|
||
clientGeo := &geo{} | ||
err = db.Lookup(clientIP, clientGeo) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
if clientGeo.DC == "" { | ||
log.Infof("empty DC %+v", clientGeo) | ||
return result | ||
} | ||
|
||
log.Infof("clientDC: %+v", clientGeo) | ||
|
||
for _, ip := range endpoint.Targets { | ||
geoData := &geo{} | ||
log.Infof("processing IP %+v", ip) | ||
err = db.Lookup(net.ParseIP(ip), geoData) | ||
if err != nil { | ||
log.Error(err) | ||
continue | ||
} | ||
|
||
log.Infof("IP info: %+v", geoData.DC) | ||
if clientGeo.DC == geoData.DC { | ||
result = append(result, ip) | ||
} | ||
} | ||
return result | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: externaldns.k8s.io/v1alpha1 | ||
kind: DNSEndpoint | ||
metadata: | ||
name: coredns-txt | ||
labels: | ||
k8gb.absa.oss/dnstype: local | ||
spec: | ||
endpoints: | ||
- dnsName: txt.example.org | ||
recordType: TXT | ||
recordTTL: 30 | ||
targets: | ||
- "foo=bar" |
This file contains 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