Skip to content

Commit 5e06a42

Browse files
committed
Use proxy strategy as the label for proxy_server_ready_backend_connections_total metric
1 parent b12bbff commit 5e06a42

File tree

11 files changed

+247
-226
lines changed

11 files changed

+247
-226
lines changed

cmd/server/app/options/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/klog/v2"
2828

2929
"sigs.k8s.io/apiserver-network-proxy/pkg/server"
30+
"sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
3031
"sigs.k8s.io/apiserver-network-proxy/pkg/util"
3132
)
3233

@@ -314,7 +315,7 @@ func (o *ProxyRunOptions) Validate() error {
314315
if len(o.ProxyStrategies) == 0 {
315316
return fmt.Errorf("ProxyStrategies cannot be empty")
316317
}
317-
if _, err := server.ParseProxyStrategies(o.ProxyStrategies); err != nil {
318+
if _, err := proxystrategies.ParseProxyStrategies(o.ProxyStrategies); err != nil {
318319
return fmt.Errorf("invalid proxy strategies: %v", err)
319320
}
320321
if o.XfrChannelSize <= 0 {

cmd/server/app/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
4747
"sigs.k8s.io/apiserver-network-proxy/pkg/server"
4848
"sigs.k8s.io/apiserver-network-proxy/pkg/server/leases"
49+
"sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
4950
"sigs.k8s.io/apiserver-network-proxy/pkg/util"
5051
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
5152
)
@@ -134,7 +135,7 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
134135
AuthenticationAudience: o.AuthenticationAudience,
135136
}
136137
klog.V(1).Infoln("Starting frontend server for client connections.")
137-
ps, err := server.ParseProxyStrategies(o.ProxyStrategies)
138+
ps, err := proxystrategies.ParseProxyStrategies(o.ProxyStrategies)
138139
if err != nil {
139140
return err
140141
}

pkg/server/backend_manager.go

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -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,30 +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
274-
// managerName is the kind of backend manager this storage belongs to.
212+
// proxyStrategy is the proxy strategy of the backend manager this storage
213+
// belongs to.
275214
// It is used to record metrics.
276-
managerName string
215+
proxyStrategy proxystrategies.ProxyStrategy
277216
}
278217

279218
// NewDefaultBackendManager returns a DefaultBackendManager.
280219
func NewDefaultBackendManager() *DefaultBackendManager {
281220
return &DefaultBackendManager{
282221
DefaultBackendStorage: NewDefaultBackendStorage(
283-
[]header.IdentifierType{header.UID}, "DefaultBackendManager")}
222+
[]header.IdentifierType{header.UID}, proxystrategies.ProxyStrategyDefault)}
284223
}
285224

286225
// NewDefaultBackendStorage returns a DefaultBackendStorage
287-
func NewDefaultBackendStorage(idTypes []header.IdentifierType, managerName string) *DefaultBackendStorage {
226+
func NewDefaultBackendStorage(idTypes []header.IdentifierType, proxyStrategy proxystrategies.ProxyStrategy) *DefaultBackendStorage {
288227
// Set an explicit value, so that the metric is emitted even when
289228
// no agent ever successfully connects.
290229
metrics.Metrics.SetBackendCount(0)
291-
metrics.Metrics.SetTotalBackendCount(managerName, 0)
230+
metrics.Metrics.SetTotalBackendCount(proxyStrategy, 0)
292231

293232
return &DefaultBackendStorage{
294-
backends: make(map[string][]*Backend),
295-
random: rand.New(rand.NewSource(time.Now().UnixNano())), /* #nosec G404 */
296-
idTypes: idTypes,
297-
managerName: managerName,
233+
backends: make(map[string][]*Backend),
234+
random: rand.New(rand.NewSource(time.Now().UnixNano())), /* #nosec G404 */
235+
idTypes: idTypes,
236+
proxyStrategy: proxyStrategy,
298237
}
299238
}
300239

@@ -324,7 +263,7 @@ func (s *DefaultBackendStorage) addBackend(identifier string, idType header.Iden
324263
}
325264
s.backends[identifier] = []*Backend{backend}
326265
metrics.Metrics.SetBackendCount(len(s.backends))
327-
metrics.Metrics.SetTotalBackendCount(s.managerName, len(s.backends))
266+
metrics.Metrics.SetTotalBackendCount(s.proxyStrategy, len(s.backends))
328267
s.agentIDs = append(s.agentIDs, identifier)
329268
}
330269

@@ -366,7 +305,7 @@ func (s *DefaultBackendStorage) removeBackend(identifier string, idType header.I
366305
klog.V(1).InfoS("Could not find connection matching identifier to remove", "agentID", identifier, "idType", idType)
367306
}
368307
metrics.Metrics.SetBackendCount(len(s.backends))
369-
metrics.Metrics.SetTotalBackendCount(s.managerName, len(s.backends))
308+
metrics.Metrics.SetTotalBackendCount(s.proxyStrategy, len(s.backends))
370309
}
371310

372311
// NumBackends resturns the number of available backends

pkg/server/backend_manager_test.go

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ package server
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"reflect"
2322
"testing"
2423

25-
"github.com/stretchr/testify/assert"
2624
"go.uber.org/mock/gomock"
2725
"google.golang.org/grpc/metadata"
2826

@@ -385,123 +383,3 @@ func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
385383
t.Errorf("expected %v, got %v", e, a)
386384
}
387385
}
388-
389-
func TestProxyStrategy(t *testing.T) {
390-
for desc, tc := range map[string]struct {
391-
input ProxyStrategy
392-
want string
393-
wantPanic string
394-
}{
395-
"default": {
396-
input: ProxyStrategyDefault,
397-
want: "default",
398-
},
399-
"destHost": {
400-
input: ProxyStrategyDestHost,
401-
want: "destHost",
402-
},
403-
"defaultRoute": {
404-
input: ProxyStrategyDefaultRoute,
405-
want: "defaultRoute",
406-
},
407-
"unrecognized": {
408-
input: ProxyStrategy(0),
409-
wantPanic: "unhandled ProxyStrategy: 0",
410-
},
411-
} {
412-
t.Run(desc, func(t *testing.T) {
413-
if tc.wantPanic != "" {
414-
assert.PanicsWithValue(t, tc.wantPanic, func() {
415-
_ = tc.input.String()
416-
})
417-
} else {
418-
got := tc.input.String()
419-
if got != tc.want {
420-
t.Errorf("ProxyStrategy.String(): got %v, want %v", got, tc.want)
421-
}
422-
}
423-
})
424-
}
425-
}
426-
427-
func TestParseProxyStrategy(t *testing.T) {
428-
for desc, tc := range map[string]struct {
429-
input string
430-
want ProxyStrategy
431-
wantErr error
432-
}{
433-
"empty": {
434-
input: "",
435-
wantErr: fmt.Errorf("unknown proxy strategy: "),
436-
},
437-
"unrecognized": {
438-
input: "unrecognized",
439-
wantErr: fmt.Errorf("unknown proxy strategy: unrecognized"),
440-
},
441-
"default": {
442-
input: "default",
443-
want: ProxyStrategyDefault,
444-
},
445-
"destHost": {
446-
input: "destHost",
447-
want: ProxyStrategyDestHost,
448-
},
449-
"defaultRoute": {
450-
input: "defaultRoute",
451-
want: ProxyStrategyDefaultRoute,
452-
},
453-
} {
454-
t.Run(desc, func(t *testing.T) {
455-
got, err := ParseProxyStrategy(tc.input)
456-
assert.Equal(t, tc.wantErr, err, "ParseProxyStrategy(%s): got error %q, want %v", tc.input, err, tc.wantErr)
457-
if got != tc.want {
458-
t.Errorf("ParseProxyStrategy(%s): got %v, want %v", tc.input, got, tc.want)
459-
}
460-
})
461-
}
462-
}
463-
464-
func TestParseProxyStrategies(t *testing.T) {
465-
for desc, tc := range map[string]struct {
466-
input string
467-
want []ProxyStrategy
468-
wantErr error
469-
}{
470-
"empty": {
471-
input: "",
472-
wantErr: fmt.Errorf("proxy strategies cannot be empty"),
473-
},
474-
"unrecognized": {
475-
input: "unrecognized",
476-
wantErr: fmt.Errorf("unknown proxy strategy: unrecognized"),
477-
},
478-
"default": {
479-
input: "default",
480-
want: []ProxyStrategy{ProxyStrategyDefault},
481-
},
482-
"destHost": {
483-
input: "destHost",
484-
want: []ProxyStrategy{ProxyStrategyDestHost},
485-
},
486-
"defaultRoute": {
487-
input: "defaultRoute",
488-
want: []ProxyStrategy{ProxyStrategyDefaultRoute},
489-
},
490-
"duplicate": {
491-
input: "destHost,defaultRoute,defaultRoute,default",
492-
want: []ProxyStrategy{ProxyStrategyDestHost, ProxyStrategyDefaultRoute, ProxyStrategyDefaultRoute, ProxyStrategyDefault},
493-
},
494-
"multiple": {
495-
input: "destHost,defaultRoute,default",
496-
want: []ProxyStrategy{ProxyStrategyDestHost, ProxyStrategyDefaultRoute, ProxyStrategyDefault},
497-
},
498-
} {
499-
t.Run(desc, func(t *testing.T) {
500-
got, err := ParseProxyStrategies(tc.input)
501-
assert.Equal(t, tc.wantErr, err, "ParseProxyStrategies(%s): got error %q, want %v", tc.input, err, tc.wantErr)
502-
if !reflect.DeepEqual(got, tc.want) {
503-
t.Errorf("ParseProxyStrategies(%s): got %v, want %v", tc.input, got, tc.want)
504-
}
505-
})
506-
}
507-
}

pkg/server/default_route_backend_manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
"k8s.io/klog/v2"
23+
"sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
2324
"sigs.k8s.io/apiserver-network-proxy/proto/header"
2425
)
2526

@@ -32,7 +33,7 @@ var _ BackendManager = &DefaultRouteBackendManager{}
3233
func NewDefaultRouteBackendManager() *DefaultRouteBackendManager {
3334
return &DefaultRouteBackendManager{
3435
DefaultBackendStorage: NewDefaultBackendStorage(
35-
[]header.IdentifierType{header.DefaultRoute}, "DefaultRouteBackendManager")}
36+
[]header.IdentifierType{header.DefaultRoute}, proxystrategies.ProxyStrategyDefaultRoute)}
3637
}
3738

3839
// Backend tries to get a backend that advertises default route, with random selection.

pkg/server/desthost_backend_manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
"k8s.io/klog/v2"
23+
"sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
2324
"sigs.k8s.io/apiserver-network-proxy/proto/header"
2425
)
2526

@@ -32,7 +33,7 @@ var _ BackendManager = &DestHostBackendManager{}
3233
func NewDestHostBackendManager() *DestHostBackendManager {
3334
return &DestHostBackendManager{
3435
DefaultBackendStorage: NewDefaultBackendStorage(
35-
[]header.IdentifierType{header.IPv4, header.IPv6, header.Host}, "DestHostBackendManager")}
36+
[]header.IdentifierType{header.IPv4, header.IPv6, header.Host}, proxystrategies.ProxyStrategyDestHost)}
3637
}
3738

3839
func (dibm *DestHostBackendManager) AddBackend(backend *Backend) {

pkg/server/metrics/metrics.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
commonmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics"
2626
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
27+
"sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies"
2728
)
2829

2930
const (
@@ -123,7 +124,7 @@ func newServerMetrics() *ServerMetrics {
123124
Help: "Total number of konnectivity agent connected to the proxy server",
124125
},
125126
[]string{
126-
"manager",
127+
"proxy_strategy",
127128
},
128129
)
129130
pendingDials := prometheus.NewGaugeVec(
@@ -300,8 +301,8 @@ func (s *ServerMetrics) SetBackendCount(count int) {
300301
}
301302

302303
// SetTotalBackendCount sets the total number of backend connection.
303-
func (s *ServerMetrics) SetTotalBackendCount(managerName string, count int) {
304-
s.totalBackendCount.WithLabelValues(managerName).Set(float64(count))
304+
func (s *ServerMetrics) SetTotalBackendCount(proxyStrategy proxystrategies.ProxyStrategy, count int) {
305+
s.totalBackendCount.WithLabelValues(proxyStrategy.String()).Set(float64(count))
305306
}
306307

307308
// SetPendingDialCount sets the number of pending dials.

0 commit comments

Comments
 (0)