-
Notifications
You must be signed in to change notification settings - Fork 0
/
lds.go
91 lines (80 loc) · 2.99 KB
/
lds.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
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v2
import (
"fmt"
xdsapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/gogo/protobuf/types"
"github.com/prometheus/client_golang/prometheus"
"istio.io/istio/pilot/pkg/model"
)
func (s *DiscoveryServer) pushLds(con *XdsConnection, push *model.PushContext, version string) error {
// TODO: Modify interface to take services, and config instead of making library query registry
rawListeners, err := s.generateRawListeners(con, push)
if err != nil {
return err
}
if s.DebugConfigs {
con.LDSListeners = rawListeners
}
response := ldsDiscoveryResponse(rawListeners, version)
err = con.send(response)
if err != nil {
adsLog.Warnf("LDS: Send failure %s: %v", con.ConID, err)
pushes.With(prometheus.Labels{"type": "lds_senderr"}).Add(1)
return err
}
pushes.With(prometheus.Labels{"type": "lds"}).Add(1)
adsLog.Infof("LDS: PUSH for node:%s addr:%q listeners:%d %d", con.modelNode.ID, con.PeerAddr, len(rawListeners),
response.Size())
return nil
}
func (s *DiscoveryServer) generateRawListeners(con *XdsConnection, push *model.PushContext) ([]*xdsapi.Listener, error) {
rawListeners, err := s.ConfigGenerator.BuildListeners(s.Env, con.modelNode, push)
if err != nil {
adsLog.Warnf("LDS: Failed to generate listeners for node %s: %v", con.modelNode.ID, err)
pushes.With(prometheus.Labels{"type": "lds_builderr"}).Add(1)
return nil, err
}
for _, l := range rawListeners {
if err = l.Validate(); err != nil {
retErr := fmt.Errorf("LDS: Generated invalid listener for node %v: %v", con.modelNode, err)
adsLog.Errorf("LDS: Generated invalid listener for node %s: %v, %v", con.modelNode.ID, err, l)
pushes.With(prometheus.Labels{"type": "lds_builderr"}).Add(1)
// Generating invalid listeners is a bug.
// Panic instead of trying to recover from that, since we can't
// assume anything about the state.
panic(retErr.Error())
}
}
return rawListeners, nil
}
// LdsDiscoveryResponse returns a list of listeners for the given environment and source node.
func ldsDiscoveryResponse(ls []*xdsapi.Listener, version string) *xdsapi.DiscoveryResponse {
resp := &xdsapi.DiscoveryResponse{
TypeUrl: ListenerType,
VersionInfo: version,
Nonce: nonce(),
}
for _, ll := range ls {
if ll == nil {
adsLog.Errora("Nil listener ", ll)
totalXDSInternalErrors.Add(1)
continue
}
lr, _ := types.MarshalAny(ll)
resp.Resources = append(resp.Resources, *lr)
}
return resp
}