-
Notifications
You must be signed in to change notification settings - Fork 3
/
resolver.go
122 lines (104 loc) · 2.94 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cloudmap
import (
"context"
"fmt"
"sync"
"time"
"google.golang.org/grpc/attributes"
"google.golang.org/grpc/grpclog"
grpcresolver "google.golang.org/grpc/resolver"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/servicediscovery"
)
type serviceDiscovery interface {
DiscoverInstances(input *servicediscovery.DiscoverInstancesInput) (*servicediscovery.DiscoverInstancesOutput, error)
}
type resolver struct {
logger grpclog.LoggerV2
cc grpcresolver.ClientConn
sd serviceDiscovery
namespace string
service string
healthStatusFilter string
maxResults int64
ctx context.Context
cancel context.CancelFunc
ticker *time.Ticker
resolveCmd chan struct{}
wg sync.WaitGroup
}
func (c *resolver) ResolveNow(grpcresolver.ResolveNowOptions) {
select {
case c.resolveCmd <- struct{}{}:
default:
}
}
func (c *resolver) lookupCloudmap() (*grpcresolver.State, error) {
output, err := c.sd.DiscoverInstances(&servicediscovery.DiscoverInstancesInput{
NamespaceName: aws.String(c.namespace),
ServiceName: aws.String(c.service),
HealthStatus: aws.String(c.healthStatusFilter),
MaxResults: aws.Int64(c.maxResults),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case servicediscovery.ErrCodeServiceNotFound:
c.logger.Errorln(servicediscovery.ErrCodeServiceNotFound, aerr.Error())
case servicediscovery.ErrCodeNamespaceNotFound:
c.logger.Errorln(servicediscovery.ErrCodeNamespaceNotFound, aerr.Error())
case servicediscovery.ErrCodeInvalidInput:
c.logger.Errorln(servicediscovery.ErrCodeInvalidInput, aerr.Error())
case servicediscovery.ErrCodeRequestLimitExceeded:
c.logger.Errorln(servicediscovery.ErrCodeRequestLimitExceeded, aerr.Error())
default:
c.logger.Errorln(aerr.Error())
}
} else {
c.logger.Errorln(err.Error())
}
return nil, err
}
addrs := make([]grpcresolver.Address, len(output.Instances))
for i, instance := range output.Instances {
addrs[i] = httpInstanceSummaryToAddr(instance)
}
return &grpcresolver.State{Addresses: addrs}, nil
}
func (c *resolver) Close() {
c.cancel()
c.ticker.Stop()
c.wg.Wait()
}
func (c *resolver) watcher() {
defer c.wg.Done()
for {
state, err := c.lookupCloudmap()
if err != nil {
c.cc.ReportError(err)
} else {
err = c.cc.UpdateState(*state)
}
if err != nil {
c.logger.Errorln(err)
// wait for next iteration
}
select {
case <-c.ctx.Done():
return
case <-c.ticker.C:
case <-c.resolveCmd:
}
}
}
func httpInstanceSummaryToAddr(s *servicediscovery.HttpInstanceSummary) grpcresolver.Address {
var attrs *attributes.Attributes
for k, v := range s.Attributes {
attrs = attrs.WithValue(k, v)
}
return grpcresolver.Address{
Addr: fmt.Sprintf("%s:%s", *s.Attributes["AWS_INSTANCE_IPV4"], *s.Attributes["AWS_INSTANCE_PORT"]),
Attributes: attrs,
}
}