Skip to content

Commit

Permalink
fix the proxy server backend metric error
Browse files Browse the repository at this point in the history
  • Loading branch information
YRXING committed Dec 21, 2021
1 parent 5944ae0 commit 1a8d66f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
20 changes: 12 additions & 8 deletions pkg/server/backend_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions pkg/server/default_route_backend_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
17 changes: 9 additions & 8 deletions pkg/server/desthost_backend_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}
Expand Down
9 changes: 6 additions & 3 deletions pkg/server/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1a8d66f

Please sign in to comment.