Skip to content

Commit

Permalink
use excludeLocalhost flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gojoy authored Aug 23, 2024
1 parent 256c4bc commit 157dc7a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type globalConfig struct {
endpoints []string
useClusterEndpoints bool
remote bool
excludeLocalhost bool

dialTimeout time.Duration
commandTimeout time.Duration
Expand Down
4 changes: 2 additions & 2 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func endpoints(gcfg globalConfig) ([]string, error) {
return endpointsFromCluster(gcfg)
}

func isLocalEp(ep string) bool {
func IsLocalEp(ep string) bool {
return strings.Contains(ep, "127.0.0.1") || strings.Contains(ep, "localhost")
}

Expand All @@ -56,7 +56,7 @@ func endpointsFromCluster(gcfg globalConfig) ([]string, error) {
if !m.GetIsLearner() {
for _, ep := range m.ClientURLs {
// filter local endpoint when set remote
if !gcfg.remote || !isLocalEp(ep) {
if !gcfg.excludeLocalhost || !IsLocalEp(ep) {
eps = append(eps, ep)
}
}
Expand Down
48 changes: 44 additions & 4 deletions endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestEndpointDedup(t *testing.T) {
}
}


func TestEndpointRemote(t *testing.T) {
oldCreateClient := createClient
t.Cleanup(func() {
Expand All @@ -127,7 +126,7 @@ func TestEndpointRemote(t *testing.T) {
name string
returnedMemberList *clientv3.MemberListResponse
expectedEndpoints []string
remote bool
excludeLocalhost bool
}{
{
"normal",
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestEndpointRemote(t *testing.T) {
for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
fakeClient.memberListResp = testcase.returnedMemberList
ep, err := endpointsFromCluster(globalConfig{endpoints: []string{"https://localhost:2379"}, remote: testcase.remote})
ep, err := endpointsFromCluster(globalConfig{endpoints: []string{"https://localhost:2379"}, excludeLocalhost: testcase.excludeLocalhost})
if err != nil {
t.Error(err)
}
Expand All @@ -176,7 +175,6 @@ func TestEndpointRemote(t *testing.T) {
}
}


type fakeClientURLClient struct {
*clientv3.Client
memberListResp *clientv3.MemberListResponse
Expand All @@ -190,3 +188,45 @@ func (f fakeClientURLClient) MemberList(ctx context.Context, opts ...clientv3.Op
func (fakeClientURLClient) Close() error {
return nil
}

func TestIsLocalEp(t *testing.T) {
testcases := []struct {
name string
ep string
desire bool
}{
{
"exclude",
"127.0.0.1:2379",
true,
},
{
"include",
"10.7.7.7:2379",
false,
},
{
"excludehttp",
"http://127.0.0.1:2379",
true,
},
{
"includehttps",
"http://10.7.7.7:2379",
false,
},
{
"includehttpsdomain",
"https://abc-0.ns1-etcd.ns1.svc.cluster.local.:2379",
false,
},
}

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
if IsLocalEp(testcase.ep) != testcase.desire {
t.Errorf("expected %v, got %v", testcase.desire, IsLocalEp(testcase.ep))
}
})
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newDefragCommand() *cobra.Command {

defragCmd.Flags().StringSliceVar(&globalCfg.endpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd endpoints")
defragCmd.Flags().BoolVar(&globalCfg.useClusterEndpoints, "cluster", false, "use all endpoints from the cluster member list")
defragCmd.Flags().BoolVar(&globalCfg.remote, "remote", true, "whether execute on remote machine")
defragCmd.Flags().BoolVar(&globalCfg.excludeLocalhost, "remote", true, "whether to exclude localhost endpoints")

defragCmd.Flags().DurationVar(&globalCfg.dialTimeout, "dial-timeout", 2*time.Second, "dial timeout for client connections")
defragCmd.Flags().DurationVar(&globalCfg.commandTimeout, "command-timeout", 30*time.Second, "command timeout (excluding dial timeout)")
Expand Down

0 comments on commit 157dc7a

Please sign in to comment.