diff --git a/rds/client/client.go b/rds/client/client.go index bed36b50..c257be7b 100644 --- a/rds/client/client.go +++ b/rds/client/client.go @@ -93,10 +93,23 @@ func (client *Client) updateState(response *pb.ListResourcesResponse) { defer client.mu.Unlock() client.names = make([]string, len(response.GetResources())) - for i, res := range response.GetResources() { + oldcache := client.cache + client.cache = make(map[string]*cacheRecord, len(response.GetResources())) + + i := 0 + for _, res := range response.GetResources() { + if oldRes, ok := client.cache[res.GetName()]; ok { + client.l.Warningf("Got resource (%s) again, ignoring this instance: {%v}. Previous record: %+v.", res.GetName(), res, *oldRes) + continue + } + if oldcache[res.GetName()] != nil && res.GetIp() != oldcache[res.GetName()].ip { + client.l.Infof("Resource (%s) ip has changed: %s -> %s.", res.GetName(), oldcache[res.GetName()].ip, res.GetIp()) + } client.cache[res.GetName()] = &cacheRecord{res.GetIp(), int(res.GetPort()), res.Labels} client.names[i] = res.GetName() + i++ } + client.names = client.names[:i] } // ListEndpoints returns the list of resources. diff --git a/rds/client/client_test.go b/rds/client/client_test.go index 2c708ca6..d95314f6 100644 --- a/rds/client/client_test.go +++ b/rds/client/client_test.go @@ -61,6 +61,13 @@ var testResourcesMap = map[string][]*pb.Resource{ Port: proto.Int32(80), Labels: map[string]string{"zone": "us-central1-c"}, }, + // Duplicate resource, it should be removed from the result. + { + Name: proto.String("testR3"), + Ip: proto.String("testR3.test.com"), + Port: proto.Int32(80), + Labels: map[string]string{"zone": "us-central1-c"}, + }, }, } @@ -136,9 +143,11 @@ func TestListAndResolve(t *testing.T) { client.refreshState(time.Second) - // Test ListEndpoint() + // Test ListEndpoint(), note that we remove the duplicate resource from the + // exepected output. epList := client.ListEndpoints() - for i, res := range testResources { + expectedList := testResources[:len(testResources)-1] + for i, res := range expectedList { if epList[i].Name != res.GetName() { t.Errorf("Resource name: got=%s, want=%s", epList[i].Name, res.GetName()) } @@ -153,7 +162,7 @@ func TestListAndResolve(t *testing.T) { } // Test Resolve() - for _, res := range testResources { + for _, res := range expectedList { for _, ipVer := range []int{0, 4, 6} { t.Run(fmt.Sprintf("resolve_%s_for_IPv%d", res.GetName(), ipVer), func(t *testing.T) { expectedIP := expectedIPByVersion[res.GetName()][ipVer]