Skip to content

Commit

Permalink
Update the excludeLosthost feature
Browse files Browse the repository at this point in the history
1. Simplify the implementation
2. update readme

Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
  • Loading branch information
ahrtr committed Sep 14, 2024
1 parent 163fc0e commit 768ba29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Flags:
--dry-run evaluate whether or not endpoints require defragmentation, but don't actually perform it
--endpoints strings comma separated etcd endpoints (default [127.0.0.1:2379])
--etcd-storage-quota-bytes int etcd storage quota in bytes (the value passed to etcd instance by flag --quota-backend-bytes) (default 2147483648)
--exclude-localhost whether to exclude localhost endpoints (default true)
-h, --help help for etcd-defrag
--insecure-discovery accept insecure SRV records describing cluster endpoints (default true)
--insecure-skip-tls-verify skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)
Expand Down
32 changes: 12 additions & 20 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func endpoints(gcfg globalConfig) ([]string, error) {
return endpointsFromCluster(gcfg)
}

func IsLocalEndpoint(ep string) (bool, error) {

func isLocalEndpoint(ep string) (bool, error) {
if strings.HasPrefix(ep, "unix:") || strings.HasPrefix(ep, "unixs:") {
return true, nil
}

hostPort := ep
if strings.Contains(ep, "://") {
url, err := url.Parse(ep)
if err != nil {
Expand All @@ -59,31 +59,23 @@ func IsLocalEndpoint(ep string) (bool, error) {
return false, errBadScheme
}

return isLocalEndpoint(url.Host)
hostPort = url.Host
}

return isLocalEndpoint(ep)
}

func isLocalEndpoint(ep string) (bool, error) {

hostStr, _, err := net.SplitHostPort(ep)
hostname, _, err := net.SplitHostPort(hostPort)
if err != nil {
return false, err
}

// lookup localhost
addrs, err := net.LookupHost(hostStr)
if err != nil {
return false, nil
if strings.EqualFold(hostname, "localhost") {
return true, nil
}

for _, addr := range addrs {
ip := net.ParseIP(addr)
if ip != nil && ip.IsLoopback() {
return true, nil
}
ip := net.ParseIP(hostname)
if ip != nil && ip.IsLoopback() {
return true, nil
}

return false, nil
}

Expand All @@ -98,9 +90,9 @@ func endpointsFromCluster(gcfg globalConfig) ([]string, error) {
// learner member only serves Status and SerializableRead requests, just ignore it
if !m.GetIsLearner() {
for _, ep := range m.ClientURLs {
// not append local endpoint when set excludeLocalhost
// Do not append local endpoint when `--exclude-localhost` is set.
if gcfg.excludeLocalhost {
ok, err := IsLocalEndpoint(ep)
ok, err := isLocalEndpoint(ep)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestIsLocalEp(t *testing.T) {

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
if ok, err := IsLocalEndpoint(testcase.ep); err != testcase.err || ok != testcase.desire {
if ok, err := isLocalEndpoint(testcase.ep); err != testcase.err || ok != testcase.desire {
t.Errorf("expected %v, got err: %v result: %v", testcase.desire, err, ok)
}
})
Expand Down

0 comments on commit 768ba29

Please sign in to comment.