diff --git a/go.sum b/go.sum index e8254a866..ca588d7a4 100644 --- a/go.sum +++ b/go.sum @@ -532,4 +532,4 @@ sigs.k8s.io/structured-merge-diff/v4 v4.1.2 h1:Hr/htKFmJEbtMgS/UD0N+gtgctAqz81t3 sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q= -sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= +sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= \ No newline at end of file diff --git a/pkg/server/backend_manager.go b/pkg/server/backend_manager.go index 0bd44c4e1..3c3e5d380 100644 --- a/pkg/server/backend_manager.go +++ b/pkg/server/backend_manager.go @@ -25,7 +25,7 @@ import ( "time" "k8s.io/klog/v2" - client "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client" + "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client" pkgagent "sigs.k8s.io/apiserver-network-proxy/pkg/agent" "sigs.k8s.io/apiserver-network-proxy/pkg/server/metrics" "sigs.k8s.io/apiserver-network-proxy/proto/agent" @@ -155,21 +155,25 @@ type DefaultBackendStorage struct { // e.g., when associating to the DestHostBackendManager, it can only use the // identifiers of types, IPv4, IPv6 and Host. idTypes []pkgagent.IdentifierType + // managerName defines this storage belongs to what kind of backend manager. + // It is used to record some metrics. + managerName string } // NewDefaultBackendManager returns a DefaultBackendManager. func NewDefaultBackendManager() *DefaultBackendManager { return &DefaultBackendManager{ DefaultBackendStorage: NewDefaultBackendStorage( - []pkgagent.IdentifierType{pkgagent.UID})} + []pkgagent.IdentifierType{pkgagent.UID}, "DefaultBackendManager")} } // NewDefaultBackendStorage returns a DefaultBackendStorage -func NewDefaultBackendStorage(idTypes []pkgagent.IdentifierType) *DefaultBackendStorage { +func NewDefaultBackendStorage(idTypes []pkgagent.IdentifierType, managerName string) *DefaultBackendStorage { return &DefaultBackendStorage{ - backends: make(map[string][]*backend), - random: rand.New(rand.NewSource(time.Now().UnixNano())), - idTypes: idTypes, + backends: make(map[string][]*backend), + random: rand.New(rand.NewSource(time.Now().UnixNano())), + idTypes: idTypes, + managerName: managerName, } /* #nosec G404 */ } @@ -204,7 +208,7 @@ func (s *DefaultBackendStorage) AddBackend(identifier string, idType pkgagent.Id return addedBackend } s.backends[identifier] = []*backend{addedBackend} - metrics.Metrics.SetBackendCount(len(s.backends)) + metrics.Metrics.SetBackendCount(s.managerName, string(idType), len(s.backends[string(idType)])) s.agentIDs = append(s.agentIDs, identifier) if idType == pkgagent.DefaultRoute { s.defaultRouteAgentIDs = append(s.defaultRouteAgentIDs, identifier) @@ -257,7 +261,7 @@ func (s *DefaultBackendStorage) RemoveBackend(identifier string, idType pkgagent if !found { klog.V(1).InfoS("Could not find connection matching identifier to remove", "connection", conn, "identifier", identifier) } - metrics.Metrics.SetBackendCount(len(s.backends)) + metrics.Metrics.SetBackendCount(s.managerName, string(idType), len(s.backends[string(idType)])) } // NumBackends resturns the number of available backends diff --git a/pkg/server/default_route_backend_manager.go b/pkg/server/default_route_backend_manager.go index d958cff21..33b89c906 100644 --- a/pkg/server/default_route_backend_manager.go +++ b/pkg/server/default_route_backend_manager.go @@ -20,7 +20,7 @@ import ( "context" "k8s.io/klog/v2" - "sigs.k8s.io/apiserver-network-proxy/pkg/agent" + pkgagent "sigs.k8s.io/apiserver-network-proxy/pkg/agent" ) type DefaultRouteBackendManager struct { @@ -32,20 +32,21 @@ var _ BackendManager = &DefaultRouteBackendManager{} func NewDefaultRouteBackendManager() *DefaultRouteBackendManager { return &DefaultRouteBackendManager{ DefaultBackendStorage: NewDefaultBackendStorage( - []agent.IdentifierType{agent.DefaultRoute})} + []pkgagent.IdentifierType{pkgagent.DefaultRoute}, + "DefaultRouteBackendManager")} } // Backend tries to get a backend associating to the request destination host. -func (dibm *DefaultRouteBackendManager) Backend(ctx context.Context) (Backend, error) { - dibm.mu.RLock() - defer dibm.mu.RUnlock() - if len(dibm.backends) == 0 { +func (drbm *DefaultRouteBackendManager) Backend(ctx context.Context) (Backend, error) { + drbm.mu.RLock() + defer drbm.mu.RUnlock() + if len(drbm.backends) == 0 { return nil, &ErrNotFound{} } - if len(dibm.defaultRouteAgentIDs) == 0 { + if len(drbm.defaultRouteAgentIDs) == 0 { return nil, &ErrNotFound{} } - agentID := dibm.defaultRouteAgentIDs[dibm.random.Intn(len(dibm.defaultRouteAgentIDs))] + agentID := drbm.defaultRouteAgentIDs[drbm.random.Intn(len(drbm.defaultRouteAgentIDs))] klog.V(4).InfoS("Picked agent as backend", "agentID", agentID) - return dibm.backends[agentID][0], nil + return drbm.backends[agentID][0], nil } diff --git a/pkg/server/desthost_backend_manager.go b/pkg/server/desthost_backend_manager.go index 1d7dd89cd..c195eccfd 100644 --- a/pkg/server/desthost_backend_manager.go +++ b/pkg/server/desthost_backend_manager.go @@ -20,7 +20,7 @@ import ( "context" "k8s.io/klog/v2" - "sigs.k8s.io/apiserver-network-proxy/pkg/agent" + pkgagent "sigs.k8s.io/apiserver-network-proxy/pkg/agent" ) type DestHostBackendManager struct { @@ -32,22 +32,23 @@ var _ BackendManager = &DestHostBackendManager{} func NewDestHostBackendManager() *DestHostBackendManager { return &DestHostBackendManager{ DefaultBackendStorage: NewDefaultBackendStorage( - []agent.IdentifierType{agent.IPv4, agent.IPv6, agent.Host})} + []pkgagent.IdentifierType{pkgagent.IPv4, pkgagent.IPv6, pkgagent.Host}, + "DestHostBackendManager")} } // Backend tries to get a backend associating to the request destination host. -func (dibm *DestHostBackendManager) Backend(ctx context.Context) (Backend, error) { - dibm.mu.RLock() - defer dibm.mu.RUnlock() - if len(dibm.backends) == 0 { +func (dhbm *DestHostBackendManager) Backend(ctx context.Context) (Backend, error) { + dhbm.mu.RLock() + defer dhbm.mu.RUnlock() + if len(dhbm.backends) == 0 { return nil, &ErrNotFound{} } destHost := ctx.Value(destHost).(string) if destHost != "" { - bes, exist := dibm.backends[destHost] + bes, exist := dhbm.backends[destHost] if exist && len(bes) > 0 { klog.V(5).InfoS("Get the backend through the DestHostBackendManager", "destHost", destHost) - return dibm.backends[destHost][0], nil + return dhbm.backends[destHost][0], nil } } return nil, &ErrNotFound{} diff --git a/pkg/server/metrics/metrics.go b/pkg/server/metrics/metrics.go index 21770e455..577b50540 100644 --- a/pkg/server/metrics/metrics.go +++ b/pkg/server/metrics/metrics.go @@ -99,7 +99,10 @@ func newServerMetrics() *ServerMetrics { Name: "ready_backend_connections", Help: "Number of konnectivity agent connected to the proxy server", }, - []string{}, + []string{ + "manager", + "idType", + }, ) pendingDials := prometheus.NewGaugeVec( prometheus.GaugeOpts{ @@ -160,8 +163,8 @@ func (a *ServerMetrics) HTTPConnectionInc() { a.httpConnections.Inc() } func (a *ServerMetrics) HTTPConnectionDec() { a.httpConnections.Dec() } // SetBackendCount sets the number of backend connection. -func (a *ServerMetrics) SetBackendCount(count int) { - a.backend.WithLabelValues().Set(float64(count)) +func (a *ServerMetrics) SetBackendCount(managerName string, idType string, count int) { + a.backend.WithLabelValues(managerName, idType).Set(float64(count)) } // SetPendingDialCount sets the number of pending dials.