|
| 1 | +package setup |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "sync" |
| 6 | + |
| 7 | + envoycorev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 8 | + discoveryv3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" |
| 9 | + xdsserver "github.com/envoyproxy/go-control-plane/pkg/server/v3" |
| 10 | + "google.golang.org/genproto/googleapis/rpc/status" |
| 11 | + |
| 12 | + "github.com/kgateway-dev/kgateway/v2/internal/kgateway/xds" |
| 13 | + "github.com/kgateway-dev/kgateway/v2/pkg/logging" |
| 14 | + "github.com/kgateway-dev/kgateway/v2/pkg/metrics" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + gwNameLabel = "gateway_name" |
| 19 | + gwNamespaceLabel = "gateway_namespace" |
| 20 | + typeURLLabel = "type_url" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + logger = logging.New("xds/envoy") |
| 25 | + envoyXdsSubsystem = "envoy_xds" |
| 26 | + xdsRejectsTotal = metrics.NewCounter( |
| 27 | + metrics.CounterOpts{ |
| 28 | + Subsystem: envoyXdsSubsystem, |
| 29 | + Name: "rejects_total", |
| 30 | + Help: "Total number of xDS responses rejected by envoy proxy", |
| 31 | + }, []string{gwNamespaceLabel, gwNameLabel, typeURLLabel}) |
| 32 | + xdsRejectsCurrent = metrics.NewGauge( |
| 33 | + metrics.GaugeOpts{ |
| 34 | + Subsystem: envoyXdsSubsystem, |
| 35 | + Name: "rejects_active", |
| 36 | + Help: "Number of xDS responses currently rejected by envoy proxy", |
| 37 | + }, []string{gwNamespaceLabel, gwNameLabel, typeURLLabel}) |
| 38 | +) |
| 39 | + |
| 40 | +type resourceKey struct { |
| 41 | + Namespace string |
| 42 | + Name string |
| 43 | + ResourceTypeUrl string |
| 44 | +} |
| 45 | + |
| 46 | +type resourceState struct { |
| 47 | + errors map[resourceKey]struct{} |
| 48 | +} |
| 49 | + |
| 50 | +func newResourceState() resourceState { |
| 51 | + return resourceState{ |
| 52 | + errors: make(map[resourceKey]struct{}), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +type logNackCallback struct { |
| 57 | + xdsserver.CallbackFuncs |
| 58 | + streamState map[int64]resourceState |
| 59 | + |
| 60 | + lock sync.Mutex |
| 61 | +} |
| 62 | + |
| 63 | +var _ xdsserver.Callbacks = (*logNackCallback)(nil) |
| 64 | + |
| 65 | +func newLogNackCallback() *logNackCallback { |
| 66 | + return &logNackCallback{ |
| 67 | + streamState: make(map[int64]resourceState), |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// OnStreamClosed implements server.Callbacks. |
| 72 | +func (l *logNackCallback) OnStreamClosed(streamID int64, node *envoycorev3.Node) { |
| 73 | + l.lock.Lock() |
| 74 | + streamState := l.streamState[streamID] |
| 75 | + delete(l.streamState, streamID) |
| 76 | + l.lock.Unlock() |
| 77 | + |
| 78 | + for k := range streamState.errors { |
| 79 | + l.onErrorGone(k) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// OnStreamRequest implements server.Callbacks. |
| 84 | +func (l *logNackCallback) OnStreamRequest(streamID int64, req *discoveryv3.DiscoveryRequest) error { |
| 85 | + // get gateway and typeURL from request |
| 86 | + role := req.GetNode().GetMetadata().GetFields()[xds.RoleKey].GetStringValue() |
| 87 | + parts := strings.SplitN(role, xds.KeyDelimiter, 3) |
| 88 | + if len(parts) != 3 { |
| 89 | + return nil |
| 90 | + } |
| 91 | + namespace := parts[1] |
| 92 | + name := parts[2] |
| 93 | + |
| 94 | + // note, with locality, name will include name~hash~ns |
| 95 | + if localityParts := strings.SplitN(name, xds.KeyDelimiter, 3); len(localityParts) == 3 { |
| 96 | + name = localityParts[0] |
| 97 | + } |
| 98 | + |
| 99 | + typeUrl := req.GetTypeUrl() |
| 100 | + key := resourceKey{ |
| 101 | + Namespace: namespace, |
| 102 | + Name: name, |
| 103 | + ResourceTypeUrl: strings.TrimPrefix(typeUrl, "type.googleapis.com/"), |
| 104 | + } |
| 105 | + |
| 106 | + if req.ErrorDetail != nil { |
| 107 | + if !l.handleError(streamID, key) { |
| 108 | + // Log NACK only once per resource |
| 109 | + return nil |
| 110 | + } |
| 111 | + l.onNewError(key, req.ErrorDetail) |
| 112 | + } else { |
| 113 | + errorGone := l.handleNoError(streamID, key) |
| 114 | + if errorGone { |
| 115 | + l.onErrorGone(key) |
| 116 | + } |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (l *logNackCallback) onNewError(key resourceKey, err *status.Status) { |
| 122 | + labels := toLabels(key) |
| 123 | + xdsRejectsTotal.Inc(labels...) |
| 124 | + xdsRejectsCurrent.Add(1, labels...) |
| 125 | + logger.Warn("xds error", "gateway_name", key.Name, "gateway_ns", key.Namespace, "resource", key.ResourceTypeUrl, "error", err.Message) |
| 126 | +} |
| 127 | + |
| 128 | +func (l *logNackCallback) onErrorGone(key resourceKey) { |
| 129 | + xdsRejectsCurrent.Add(-1, toLabels(key)...) |
| 130 | +} |
| 131 | + |
| 132 | +func (l *logNackCallback) handleNoError(streamID int64, key resourceKey) bool { |
| 133 | + l.lock.Lock() |
| 134 | + defer l.lock.Unlock() |
| 135 | + streamState := l.streamState[streamID] |
| 136 | + _, hadKey := streamState.errors[key] |
| 137 | + delete(streamState.errors, key) |
| 138 | + return hadKey |
| 139 | +} |
| 140 | + |
| 141 | +func (l *logNackCallback) handleError(streamID int64, key resourceKey) bool { |
| 142 | + l.lock.Lock() |
| 143 | + defer l.lock.Unlock() |
| 144 | + streamState := l.streamState[streamID] |
| 145 | + if streamState.errors == nil { |
| 146 | + streamState = newResourceState() |
| 147 | + l.streamState[streamID] = streamState |
| 148 | + } |
| 149 | + if _, exists := streamState.errors[key]; exists { |
| 150 | + return false |
| 151 | + } |
| 152 | + streamState.errors[key] = struct{}{} |
| 153 | + return true |
| 154 | +} |
| 155 | + |
| 156 | +func toLabels(key resourceKey) []metrics.Label { |
| 157 | + return []metrics.Label{ |
| 158 | + { |
| 159 | + Name: gwNamespaceLabel, |
| 160 | + Value: key.Namespace, |
| 161 | + }, |
| 162 | + { |
| 163 | + Name: gwNameLabel, |
| 164 | + Value: key.Name, |
| 165 | + }, |
| 166 | + { |
| 167 | + Name: typeURLLabel, |
| 168 | + Value: key.ResourceTypeUrl, |
| 169 | + }, |
| 170 | + } |
| 171 | +} |
0 commit comments