@@ -22,7 +22,6 @@ import (
2222 "io"
2323 "math/rand"
2424 "slices"
25- "strings"
2625 "sync"
2726 "time"
2827
@@ -32,72 +31,11 @@ import (
3231 commonmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics"
3332 client "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
3433 "sigs.k8s.io/apiserver-network-proxy/pkg/server/metrics"
34+ "sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
3535 "sigs.k8s.io/apiserver-network-proxy/proto/agent"
3636 "sigs.k8s.io/apiserver-network-proxy/proto/header"
3737)
3838
39- type ProxyStrategy int
40-
41- const (
42- // With this strategy the Proxy Server will randomly pick a backend from
43- // the current healthy backends to establish the tunnel over which to
44- // forward requests.
45- ProxyStrategyDefault ProxyStrategy = iota + 1
46- // With this strategy the Proxy Server will pick a backend that has the same
47- // associated host as the request.Host to establish the tunnel.
48- ProxyStrategyDestHost
49- // ProxyStrategyDefaultRoute will only forward traffic to agents that have explicity advertised
50- // they serve the default route through an agent identifier. Typically used in combination with destHost
51- ProxyStrategyDefaultRoute
52- )
53-
54- func (ps ProxyStrategy ) String () string {
55- switch ps {
56- case ProxyStrategyDefault :
57- return "default"
58- case ProxyStrategyDestHost :
59- return "destHost"
60- case ProxyStrategyDefaultRoute :
61- return "defaultRoute"
62- }
63- panic (fmt .Sprintf ("unhandled ProxyStrategy: %d" , ps ))
64- }
65-
66- func ParseProxyStrategy (s string ) (ProxyStrategy , error ) {
67- switch s {
68- case ProxyStrategyDefault .String ():
69- return ProxyStrategyDefault , nil
70- case ProxyStrategyDestHost .String ():
71- return ProxyStrategyDestHost , nil
72- case ProxyStrategyDefaultRoute .String ():
73- return ProxyStrategyDefaultRoute , nil
74- default :
75- return 0 , fmt .Errorf ("unknown proxy strategy: %s" , s )
76- }
77- }
78-
79- // GenProxyStrategiesFromStr generates the list of proxy strategies from the
80- // comma-seperated string, i.e., destHost.
81- func ParseProxyStrategies (proxyStrategies string ) ([]ProxyStrategy , error ) {
82- var result []ProxyStrategy
83-
84- strs := strings .Split (proxyStrategies , "," )
85- for _ , s := range strs {
86- if len (s ) == 0 {
87- continue
88- }
89- ps , err := ParseProxyStrategy (s )
90- if err != nil {
91- return nil , err
92- }
93- result = append (result , ps )
94- }
95- if len (result ) == 0 {
96- return nil , fmt .Errorf ("proxy strategies cannot be empty" )
97- }
98- return result , nil
99- }
100-
10139// Backend abstracts a connected Konnectivity agent.
10240//
10341// In the only currently supported case (gRPC), it wraps an
@@ -271,24 +209,31 @@ type DefaultBackendStorage struct {
271209 // e.g., when associating to the DestHostBackendManager, it can only use the
272210 // identifiers of types, IPv4, IPv6 and Host.
273211 idTypes []header.IdentifierType
212+ // proxyStrategy is the proxy strategy of the backend manager this storage
213+ // belongs to.
214+ // It is used to record metrics.
215+ proxyStrategy proxystrategies.ProxyStrategy
274216}
275217
276218// NewDefaultBackendManager returns a DefaultBackendManager.
277219func NewDefaultBackendManager () * DefaultBackendManager {
278220 return & DefaultBackendManager {
279221 DefaultBackendStorage : NewDefaultBackendStorage (
280- []header.IdentifierType {header .UID })}
222+ []header.IdentifierType {header .UID }, proxystrategies . ProxyStrategyDefault )}
281223}
282224
283225// NewDefaultBackendStorage returns a DefaultBackendStorage
284- func NewDefaultBackendStorage (idTypes []header.IdentifierType ) * DefaultBackendStorage {
226+ func NewDefaultBackendStorage (idTypes []header.IdentifierType , proxyStrategy proxystrategies. ProxyStrategy ) * DefaultBackendStorage {
285227 // Set an explicit value, so that the metric is emitted even when
286228 // no agent ever successfully connects.
287- metrics .Metrics .SetBackendCount (0 )
229+ metrics .Metrics .SetBackendCountDeprecated (0 )
230+ metrics .Metrics .SetTotalBackendCount (proxyStrategy , 0 )
231+
288232 return & DefaultBackendStorage {
289- backends : make (map [string ][]* Backend ),
290- random : rand .New (rand .NewSource (time .Now ().UnixNano ())), /* #nosec G404 */
291- idTypes : idTypes ,
233+ backends : make (map [string ][]* Backend ),
234+ random : rand .New (rand .NewSource (time .Now ().UnixNano ())), /* #nosec G404 */
235+ idTypes : idTypes ,
236+ proxyStrategy : proxyStrategy ,
292237 }
293238}
294239
@@ -317,7 +262,8 @@ func (s *DefaultBackendStorage) addBackend(identifier string, idType header.Iden
317262 return
318263 }
319264 s .backends [identifier ] = []* Backend {backend }
320- metrics .Metrics .SetBackendCount (len (s .backends ))
265+ metrics .Metrics .SetBackendCountDeprecated (len (s .backends ))
266+ metrics .Metrics .SetTotalBackendCount (s .proxyStrategy , len (s .backends ))
321267 s .agentIDs = append (s .agentIDs , identifier )
322268}
323269
@@ -358,7 +304,8 @@ func (s *DefaultBackendStorage) removeBackend(identifier string, idType header.I
358304 if ! found {
359305 klog .V (1 ).InfoS ("Could not find connection matching identifier to remove" , "agentID" , identifier , "idType" , idType )
360306 }
361- metrics .Metrics .SetBackendCount (len (s .backends ))
307+ metrics .Metrics .SetBackendCountDeprecated (len (s .backends ))
308+ metrics .Metrics .SetTotalBackendCount (s .proxyStrategy , len (s .backends ))
362309}
363310
364311// NumBackends resturns the number of available backends
0 commit comments