Skip to content

Commit e67f889

Browse files
authored
Merge pull request kubernetes#125015 from aroradaman/proxy-localhost-nodeports-metric
Kube-Proxy: Track packets accepted on localhost nodeports
2 parents 1166d30 + 2e669de commit e67f889

File tree

7 files changed

+276
-39
lines changed

7 files changed

+276
-39
lines changed

pkg/proxy/iptables/number_generated_rules_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestNumberIptablesRules(t *testing.T) {
162162
services: 1,
163163
epPerService: 1,
164164
expectedFilterRules: 5,
165-
expectedNatRules: 17,
165+
expectedNatRules: 18,
166166
},
167167
{
168168
name: "1 Services 2 EndpointPerService - LoadBalancer",
@@ -177,7 +177,7 @@ func TestNumberIptablesRules(t *testing.T) {
177177
services: 1,
178178
epPerService: 2,
179179
expectedFilterRules: 5,
180-
expectedNatRules: 20,
180+
expectedNatRules: 21,
181181
},
182182
{
183183
name: "1 Services 10 EndpointPerService - LoadBalancer",
@@ -192,7 +192,7 @@ func TestNumberIptablesRules(t *testing.T) {
192192
services: 1,
193193
epPerService: 10,
194194
expectedFilterRules: 5,
195-
expectedNatRules: 44,
195+
expectedNatRules: 45,
196196
},
197197
{
198198
name: "10 Services 0 EndpointsPerService - LoadBalancer",
@@ -222,7 +222,7 @@ func TestNumberIptablesRules(t *testing.T) {
222222
services: 10,
223223
epPerService: 1,
224224
expectedFilterRules: 14,
225-
expectedNatRules: 125,
225+
expectedNatRules: 135,
226226
},
227227
{
228228
name: "10 Services 2 EndpointPerService - LoadBalancer",
@@ -237,7 +237,7 @@ func TestNumberIptablesRules(t *testing.T) {
237237
services: 10,
238238
epPerService: 2,
239239
expectedFilterRules: 14,
240-
expectedNatRules: 155,
240+
expectedNatRules: 165,
241241
},
242242
{
243243
name: "10 Services 10 EndpointPerService - LoadBalancer",
@@ -252,7 +252,7 @@ func TestNumberIptablesRules(t *testing.T) {
252252
services: 10,
253253
epPerService: 10,
254254
expectedFilterRules: 14,
255-
expectedNatRules: 395,
255+
expectedNatRules: 405,
256256
},
257257
}
258258

pkg/proxy/iptables/proxier.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ func NewProxier(ctx context.Context,
314314
networkInterfacer: proxyutil.RealNetwork{},
315315
conntrackTCPLiberal: conntrackTCPLiberal,
316316
logger: logger,
317-
nfAcctCounters: map[string]bool{metrics.IPTablesCTStateInvalidDroppedNFAcctCounter: false},
317+
nfAcctCounters: map[string]bool{
318+
metrics.IPTablesCTStateInvalidDroppedNFAcctCounter: false,
319+
metrics.LocalhostNodePortAcceptedNFAcctCounter: false,
320+
},
318321
}
319322

320323
burstSyncs := 2
@@ -1183,6 +1186,16 @@ func (proxier *Proxier) syncProxyRules() {
11831186
// Jump to the external destination chain. For better or for
11841187
// worse, nodeports are not subect to loadBalancerSourceRanges,
11851188
// and we can't change that.
1189+
if proxier.localhostNodePorts && proxier.ipFamily == v1.IPv4Protocol && proxier.nfAcctCounters[metrics.LocalhostNodePortAcceptedNFAcctCounter] {
1190+
natRules.Write(
1191+
"-A", string(kubeNodePortsChain),
1192+
"-m", "comment", "--comment", svcPortNameString,
1193+
"-m", protocol, "-p", protocol,
1194+
"-d", "127.0.0.0/8",
1195+
"--dport", strconv.Itoa(svcInfo.NodePort()),
1196+
"-m", "nfacct", "--nfacct-name", metrics.LocalhostNodePortAcceptedNFAcctCounter,
1197+
"-j", string(externalTrafficChain))
1198+
}
11861199
natRules.Write(
11871200
"-A", string(kubeNodePortsChain),
11881201
"-m", "comment", "--comment", svcPortNameString,

pkg/proxy/iptables/proxier_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func NewFakeProxier(ipt utiliptables.Interface) *Proxier {
113113
networkInterfacer.AddInterfaceAddr(&itf1, addrs1)
114114

115115
p := &Proxier{
116+
ipFamily: ipfamily,
116117
svcPortMap: make(proxy.ServicePortMap),
117118
serviceChanges: proxy.NewServiceChangeTracker(newServiceInfo, ipfamily, nil, nil),
118119
endpointsMap: make(proxy.EndpointsMap),
@@ -135,6 +136,10 @@ func NewFakeProxier(ipt utiliptables.Interface) *Proxier {
135136
localhostNodePorts: true,
136137
nodePortAddresses: proxyutil.NewNodePortAddresses(ipfamily, nil),
137138
networkInterfacer: networkInterfacer,
139+
nfAcctCounters: map[string]bool{
140+
metrics.IPTablesCTStateInvalidDroppedNFAcctCounter: true,
141+
metrics.LocalhostNodePortAcceptedNFAcctCounter: true,
142+
},
138143
}
139144
p.setInitialized(true)
140145
p.syncRunner = async.NewBoundedFrequencyRunner("test-sync-runner", p.syncProxyRules, 0, time.Minute, 1)
@@ -1717,7 +1722,7 @@ func TestOverallIPTablesRules(t *testing.T) {
17171722
-A KUBE-EXTERNAL-SERVICES -m comment --comment "ns2/svc2:p80 has no local endpoints" -m tcp -p tcp -d 1.2.3.4 --dport 80 -j DROP
17181723
-A KUBE-EXTERNAL-SERVICES -m comment --comment "ns2/svc2:p80 has no local endpoints" -m addrtype --dst-type LOCAL -m tcp -p tcp --dport 3001 -j DROP
17191724
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
1720-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
1725+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
17211726
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
17221727
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
17231728
-A KUBE-PROXY-FIREWALL -m comment --comment "ns5/svc5:p80 traffic not accepted by KUBE-FW-NUKIZ6OKUXPJNT4C" -m tcp -p tcp -d 5.6.7.8 --dport 80 -j DROP
@@ -1743,8 +1748,11 @@ func TestOverallIPTablesRules(t *testing.T) {
17431748
:KUBE-SVC-NUKIZ6OKUXPJNT4C - [0:0]
17441749
:KUBE-SVC-X27LE4BHSL4DOUIK - [0:0]
17451750
:KUBE-SVC-XPGD46QRK7WJZT7O - [0:0]
1751+
-A KUBE-NODEPORTS -m comment --comment ns2/svc2:p80 -m tcp -p tcp -d 127.0.0.0/8 --dport 3001 -m nfacct --nfacct-name localhost_nps_accepted_pkts -j KUBE-EXT-GNZBNJ2PO5MGZ6GT
17461752
-A KUBE-NODEPORTS -m comment --comment ns2/svc2:p80 -m tcp -p tcp --dport 3001 -j KUBE-EXT-GNZBNJ2PO5MGZ6GT
1753+
-A KUBE-NODEPORTS -m comment --comment ns3/svc3:p80 -m tcp -p tcp -d 127.0.0.0/8 --dport 3003 -m nfacct --nfacct-name localhost_nps_accepted_pkts -j KUBE-EXT-X27LE4BHSL4DOUIK
17471754
-A KUBE-NODEPORTS -m comment --comment ns3/svc3:p80 -m tcp -p tcp --dport 3003 -j KUBE-EXT-X27LE4BHSL4DOUIK
1755+
-A KUBE-NODEPORTS -m comment --comment ns5/svc5:p80 -m tcp -p tcp -d 127.0.0.0/8 --dport 3002 -m nfacct --nfacct-name localhost_nps_accepted_pkts -j KUBE-EXT-NUKIZ6OKUXPJNT4C
17481756
-A KUBE-NODEPORTS -m comment --comment ns5/svc5:p80 -m tcp -p tcp --dport 3002 -j KUBE-EXT-NUKIZ6OKUXPJNT4C
17491757
-A KUBE-SERVICES -m comment --comment "ns1/svc1:p80 cluster IP" -m tcp -p tcp -d 172.30.0.41 --dport 80 -j KUBE-SVC-XPGD46QRK7WJZT7O
17501758
-A KUBE-SERVICES -m comment --comment "ns2/svc2:p80 cluster IP" -m tcp -p tcp -d 172.30.0.42 --dport 80 -j KUBE-SVC-GNZBNJ2PO5MGZ6GT
@@ -5916,7 +5924,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
59165924
:KUBE-FORWARD - [0:0]
59175925
:KUBE-PROXY-FIREWALL - [0:0]
59185926
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
5919-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
5927+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
59205928
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
59215929
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
59225930
COMMIT
@@ -5999,7 +6007,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
59996007
:KUBE-FORWARD - [0:0]
60006008
:KUBE-PROXY-FIREWALL - [0:0]
60016009
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6002-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6010+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
60036011
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
60046012
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
60056013
COMMIT
@@ -6053,7 +6061,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
60536061
:KUBE-FORWARD - [0:0]
60546062
:KUBE-PROXY-FIREWALL - [0:0]
60556063
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6056-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6064+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
60576065
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
60586066
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
60596067
COMMIT
@@ -6117,7 +6125,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
61176125
:KUBE-PROXY-FIREWALL - [0:0]
61186126
-A KUBE-SERVICES -m comment --comment "ns4/svc4:p80 has no endpoints" -m tcp -p tcp -d 172.30.0.44 --dport 80 -j REJECT
61196127
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6120-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6128+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
61216129
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
61226130
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
61236131
COMMIT
@@ -6173,7 +6181,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
61736181
:KUBE-FORWARD - [0:0]
61746182
:KUBE-PROXY-FIREWALL - [0:0]
61756183
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6176-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6184+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
61776185
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
61786186
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
61796187
COMMIT
@@ -6230,7 +6238,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
62306238
:KUBE-FORWARD - [0:0]
62316239
:KUBE-PROXY-FIREWALL - [0:0]
62326240
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6233-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6241+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
62346242
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
62356243
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
62366244
COMMIT
@@ -6286,7 +6294,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
62866294
:KUBE-FORWARD - [0:0]
62876295
:KUBE-PROXY-FIREWALL - [0:0]
62886296
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6289-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6297+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
62906298
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
62916299
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
62926300
COMMIT
@@ -6344,7 +6352,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
63446352
:KUBE-FORWARD - [0:0]
63456353
:KUBE-PROXY-FIREWALL - [0:0]
63466354
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6347-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6355+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
63486356
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
63496357
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
63506358
COMMIT
@@ -6435,7 +6443,7 @@ func TestSyncProxyRulesRepeated(t *testing.T) {
64356443
:KUBE-FORWARD - [0:0]
64366444
:KUBE-PROXY-FIREWALL - [0:0]
64376445
-A KUBE-FIREWALL -m comment --comment "block incoming localnet connections" -d 127.0.0.0/8 ! -s 127.0.0.0/8 -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j DROP
6438-
-A KUBE-FORWARD -m conntrack --ctstate INVALID -j DROP
6446+
-A KUBE-FORWARD -m conntrack --ctstate INVALID -m nfacct --nfacct-name ct_state_invalid_dropped_pkts -j DROP
64396447
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding rules" -m mark --mark 0x4000/0x4000 -j ACCEPT
64406448
-A KUBE-FORWARD -m comment --comment "kubernetes forwarding conntrack rule" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
64416449
COMMIT

pkg/proxy/metrics/metrics.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ var (
147147
"kubeproxy_iptables_ct_state_invalid_dropped_packets_total",
148148
"packets dropped by iptables to work around conntrack problems",
149149
nil, nil, metrics.ALPHA, "")
150-
IPTablesCTStateInvalidDroppedNFAcctCounter = "ct_state_invalid_dropped_pkts"
150+
IPTablesCTStateInvalidDroppedNFAcctCounter = "ct_state_invalid_dropped_pkts"
151+
iptablesCTStateInvalidDroppedMetricCollector = newNFAcctMetricCollector(IPTablesCTStateInvalidDroppedNFAcctCounter, iptablesCTStateInvalidDroppedPacketsDescription)
151152

152153
// IPTablesRestoreFailuresTotal is the number of iptables restore failures that the proxy has
153154
// seen.
@@ -265,6 +266,15 @@ var (
265266
},
266267
[]string{"traffic_policy"},
267268
)
269+
270+
// localhostNodePortsAcceptedPacketsDescription describe the metrics for the number of packets accepted
271+
// by iptables which were destined for nodeports on loopback interface.
272+
localhostNodePortsAcceptedPacketsDescription = metrics.NewDesc(
273+
"kubeproxy_iptables_localhost_nodeports_accepted_packets_total",
274+
"Number of packets accepted on nodeports of loopback interface",
275+
nil, nil, metrics.ALPHA, "")
276+
LocalhostNodePortAcceptedNFAcctCounter = "localhost_nps_accepted_pkts"
277+
localhostNodePortsAcceptedMetricsCollector = newNFAcctMetricCollector(LocalhostNodePortAcceptedNFAcctCounter, localhostNodePortsAcceptedPacketsDescription)
268278
)
269279

270280
var registerMetricsOnce sync.Once
@@ -289,7 +299,8 @@ func RegisterMetrics(mode kubeproxyconfig.ProxyMode) {
289299

290300
switch mode {
291301
case kubeproxyconfig.ProxyModeIPTables:
292-
legacyregistry.CustomMustRegister(newCTStateInvalidPacketsCollector())
302+
legacyregistry.CustomMustRegister(iptablesCTStateInvalidDroppedMetricCollector)
303+
legacyregistry.CustomMustRegister(localhostNodePortsAcceptedMetricsCollector)
293304
legacyregistry.MustRegister(SyncFullProxyRulesLatency)
294305
legacyregistry.MustRegister(SyncPartialProxyRulesLatency)
295306
legacyregistry.MustRegister(IPTablesRestoreFailuresTotal)
@@ -315,34 +326,40 @@ func SinceInSeconds(start time.Time) float64 {
315326
return time.Since(start).Seconds()
316327
}
317328

318-
var _ metrics.StableCollector = &ctStateInvalidPacketsCollector{}
329+
var _ metrics.StableCollector = &nfacctMetricCollector{}
319330

320-
func newCTStateInvalidPacketsCollector() *ctStateInvalidPacketsCollector {
331+
func newNFAcctMetricCollector(counter string, description *metrics.Desc) *nfacctMetricCollector {
321332
client, err := nfacct.New()
322333
if err != nil {
323334
klog.ErrorS(err, "failed to initialize nfacct client")
324335
}
325-
return &ctStateInvalidPacketsCollector{client: client}
336+
return &nfacctMetricCollector{
337+
client: client,
338+
counter: counter,
339+
description: description,
340+
}
326341
}
327342

328-
type ctStateInvalidPacketsCollector struct {
343+
type nfacctMetricCollector struct {
329344
metrics.BaseStableCollector
330-
client nfacct.Interface
345+
client nfacct.Interface
346+
counter string
347+
description *metrics.Desc
331348
}
332349

333350
// DescribeWithStability implements the metrics.StableCollector interface.
334-
func (c *ctStateInvalidPacketsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
335-
ch <- iptablesCTStateInvalidDroppedPacketsDescription
351+
func (n *nfacctMetricCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
352+
ch <- n.description
336353
}
337354

338355
// CollectWithStability implements the metrics.StableCollector interface.
339-
func (c *ctStateInvalidPacketsCollector) CollectWithStability(ch chan<- metrics.Metric) {
340-
if c.client != nil {
341-
counter, err := c.client.Get(IPTablesCTStateInvalidDroppedNFAcctCounter)
356+
func (n *nfacctMetricCollector) CollectWithStability(ch chan<- metrics.Metric) {
357+
if n.client != nil {
358+
counter, err := n.client.Get(n.counter)
342359
if err != nil {
343-
klog.ErrorS(err, "failed to collect nfacct counter")
360+
klog.ErrorS(err, "failed to collect nfacct counter", "counter", n.counter)
344361
} else {
345-
metric, err := metrics.NewConstMetric(iptablesCTStateInvalidDroppedPacketsDescription, metrics.CounterValue, float64(counter.Packets))
362+
metric, err := metrics.NewConstMetric(n.description, metrics.CounterValue, float64(counter.Packets))
346363
if err != nil {
347364
klog.ErrorS(err, "failed to create constant metric")
348365
} else {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"k8s.io/component-base/metrics/testutil"
21+
)
22+
23+
// KubeProxyMetrics is metrics for kube-proxy
24+
type KubeProxyMetrics testutil.Metrics
25+
26+
// GetCounterMetricValue returns value for metric type counter.
27+
func (m *KubeProxyMetrics) GetCounterMetricValue(metricName string) float64 {
28+
return float64(testutil.Metrics(*m)[metricName][0].Value)
29+
}
30+
31+
func newKubeProxyMetricsMetrics() KubeProxyMetrics {
32+
result := testutil.NewMetrics()
33+
return KubeProxyMetrics(result)
34+
}
35+
36+
func parseKubeProxyMetrics(data string) (KubeProxyMetrics, error) {
37+
result := newKubeProxyMetricsMetrics()
38+
if err := testutil.ParseMetrics(data, (*testutil.Metrics)(&result)); err != nil {
39+
return KubeProxyMetrics{}, err
40+
}
41+
return result, nil
42+
}

0 commit comments

Comments
 (0)