-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from wywwzjj/main
add DNS-Based Service Discovery
- Loading branch information
Showing
4 changed files
with
45 additions
and
1 deletion.
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
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,40 @@ | ||
package evaluate | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// https://github.com/kubernetes/dns/blob/master/docs/specification.md | ||
func DNSBasedServiceDiscovery() { | ||
dnsNames := []string{"any.any.svc.cluster.local.", "any.any.any.svc.cluster.local."} | ||
|
||
var results []*net.SRV | ||
for _, name := range dnsNames { | ||
_, srvs, err := net.LookupSRV("", "", name) | ||
if err != nil { | ||
fmt.Printf("error when requesting coreDNS: %s\n", err.Error()) | ||
continue | ||
} | ||
|
||
results = append(results, srvs...) | ||
} | ||
|
||
sort.Slice(results, func(i, j int) bool { | ||
switch strings.Compare(results[i].Target, results[j].Target) { | ||
case -1: | ||
return true | ||
case 0: | ||
return results[i].Port < results[j].Port | ||
case 1: | ||
return false | ||
} | ||
return false | ||
}) | ||
|
||
for _, srv := range results { | ||
fmt.Println(srv.Target, srv.Port) | ||
} | ||
} |
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