| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package sotw |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"strconv" |
| 22 |
"sync/atomic" |
| 23 |
|
| 24 |
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 25 |
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" |
| 26 |
"github.com/envoyproxy/go-control-plane/pkg/cache/v3" |
| 27 |
"github.com/envoyproxy/go-control-plane/pkg/server/config" |
| 28 |
"github.com/envoyproxy/go-control-plane/pkg/server/stream/v3" |
| 29 |
) |
| 30 |
|
| 31 |
type Server interface { |
| 32 |
StreamHandler(stream stream.Stream, typeURL string) error |
| 33 |
} |
| 34 |
|
| 35 |
type Callbacks interface { |
| 36 |
|
| 37 |
|
| 38 |
OnStreamOpen(context.Context, int64, string) error |
| 39 |
|
| 40 |
OnStreamClosed(int64, *core.Node) |
| 41 |
|
| 42 |
|
| 43 |
OnStreamRequest(int64, *discovery.DiscoveryRequest) error |
| 44 |
|
| 45 |
OnStreamResponse(context.Context, int64, *discovery.DiscoveryRequest, *discovery.DiscoveryResponse) |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func NewServer(ctx context.Context, cw cache.ConfigWatcher, callbacks Callbacks, opts ...config.XDSOption) Server { |
| 50 |
s := &server{cache: cw, callbacks: callbacks, ctx: ctx, opts: config.NewOpts()} |
| 51 |
|
| 52 |
|
| 53 |
for _, opt := range opts { |
| 54 |
opt(&s.opts) |
| 55 |
} |
| 56 |
|
| 57 |
return s |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
func WithOrderedADS() config.XDSOption { |
| 63 |
return func(o *config.Opts) { |
| 64 |
o.Ordered = true |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
type server struct { |
| 69 |
cache cache.ConfigWatcher |
| 70 |
callbacks Callbacks |
| 71 |
ctx context.Context |
| 72 |
|
| 73 |
|
| 74 |
streamCount int64 |
| 75 |
|
| 76 |
|
| 77 |
opts config.Opts |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
type streamWrapper struct { |
| 84 |
stream stream.Stream |
| 85 |
ID int64 |
| 86 |
nonce int64 |
| 87 |
watches watches |
| 88 |
callbacks Callbacks |
| 89 |
|
| 90 |
node *core.Node |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
streamState stream.StreamState |
| 95 |
lastDiscoveryResponses map[string]lastDiscoveryResponse |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func (s *streamWrapper) send(resp cache.Response) (string, error) { |
| 101 |
if resp == nil { |
| 102 |
return "", errors.New("missing response") |
| 103 |
} |
| 104 |
|
| 105 |
out, err := resp.GetDiscoveryResponse() |
| 106 |
if err != nil { |
| 107 |
return "", err |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
out.Nonce = strconv.FormatInt(atomic.AddInt64(&s.nonce, 1), 10) |
| 112 |
|
| 113 |
lastResponse := lastDiscoveryResponse{ |
| 114 |
nonce: out.GetNonce(), |
| 115 |
resources: make(map[string]struct{}), |
| 116 |
} |
| 117 |
for _, r := range resp.GetRequest().GetResourceNames() { |
| 118 |
lastResponse.resources[r] = struct{}{} |
| 119 |
} |
| 120 |
s.lastDiscoveryResponses[resp.GetRequest().GetTypeUrl()] = lastResponse |
| 121 |
|
| 122 |
|
| 123 |
if s.callbacks != nil { |
| 124 |
s.callbacks.OnStreamResponse(resp.GetContext(), s.ID, resp.GetRequest(), out) |
| 125 |
} |
| 126 |
|
| 127 |
return out.GetNonce(), s.stream.Send(out) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (s *streamWrapper) shutdown() { |
| 132 |
s.watches.close() |
| 133 |
if s.callbacks != nil { |
| 134 |
s.callbacks.OnStreamClosed(s.ID, s.node) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
type lastDiscoveryResponse struct { |
| 143 |
nonce string |
| 144 |
resources map[string]struct{} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func (s *server) StreamHandler(stream stream.Stream, typeURL string) error { |
| 149 |
|
| 150 |
reqCh := make(chan *discovery.DiscoveryRequest) |
| 151 |
go func() { |
| 152 |
defer close(reqCh) |
| 153 |
for { |
| 154 |
req, err := stream.Recv() |
| 155 |
if err != nil { |
| 156 |
return |
| 157 |
} |
| 158 |
select { |
| 159 |
case reqCh <- req: |
| 160 |
case <-stream.Context().Done(): |
| 161 |
return |
| 162 |
case <-s.ctx.Done(): |
| 163 |
return |
| 164 |
} |
| 165 |
} |
| 166 |
}() |
| 167 |
|
| 168 |
return s.process(stream, reqCh, typeURL) |
| 169 |
} |