Skip to content

Commit 39335ad

Browse files
committed
Metric to expose confiured network mtu
1 parent d7fc72f commit 39335ad

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

pkg/monitor/cluster/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func NewMonitor(log *logrus.Entry, restConfig *rest.Config, oc *api.OpenShiftClu
190190
mon.emitPrometheusAlerts, // at the end for now because it's the slowest/least reliable
191191
mon.emitCWPStatus,
192192
mon.emitClusterAuthenticationType,
193+
mon.emitNetworkMTU,
193194
}
194195

195196
return mon, nil

pkg/monitor/cluster/networkmtu.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cluster
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the Apache License 2.0.
5+
6+
import (
7+
"context"
8+
"strconv"
9+
10+
"github.com/sirupsen/logrus"
11+
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
)
14+
15+
// emitNetworkMTU collects and emits metrics related to cluster network MTU configuration
16+
func (mon *Monitor) emitNetworkMTU(ctx context.Context) error {
17+
networkConfig, err := mon.configcli.ConfigV1().Networks().Get(ctx, "cluster", metav1.GetOptions{})
18+
if err != nil {
19+
return err
20+
}
21+
22+
// Get the MTU value from network status
23+
mtuString := strconv.Itoa(networkConfig.Status.ClusterNetworkMTU)
24+
25+
// Emit metric for configured MTU size
26+
mon.emitGauge("network.mtu", 1, map[string]string{
27+
"mtu": mtuString,
28+
"network_type": networkConfig.Spec.NetworkType,
29+
})
30+
31+
if mon.hourlyRun {
32+
mon.log.WithFields(logrus.Fields{
33+
"mtu": mtuString,
34+
"network_type": networkConfig.Spec.NetworkType,
35+
}).Info("network MTU configuration")
36+
}
37+
return nil
38+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package cluster
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the Apache License 2.0.
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"go.uber.org/mock/gomock"
11+
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
14+
configv1 "github.com/openshift/api/config/v1"
15+
configfake "github.com/openshift/client-go/config/clientset/versioned/fake"
16+
17+
"github.com/Azure/ARO-RP/pkg/api"
18+
mock_metrics "github.com/Azure/ARO-RP/pkg/util/mocks/metrics"
19+
testlog "github.com/Azure/ARO-RP/test/util/log"
20+
)
21+
22+
func TestEmitNetworkMTU(t *testing.T) {
23+
ctx := context.Background()
24+
25+
tests := []struct {
26+
name string
27+
oc *api.OpenShiftCluster
28+
networkConfig *configv1.Network
29+
expectedMetric metricExpectation
30+
}{
31+
{
32+
name: "MTU 1500 cluster with OpenShiftSDN",
33+
oc: &api.OpenShiftCluster{
34+
Properties: api.OpenShiftClusterProperties{
35+
NetworkProfile: api.NetworkProfile{
36+
MTUSize: api.MTU1500,
37+
},
38+
},
39+
},
40+
networkConfig: &configv1.Network{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Name: "cluster",
43+
},
44+
Spec: configv1.NetworkSpec{
45+
NetworkType: "OpenShiftSDN",
46+
},
47+
Status: configv1.NetworkStatus{
48+
ClusterNetworkMTU: 1500,
49+
},
50+
},
51+
expectedMetric: metricExpectation{
52+
name: "network.mtu",
53+
value: 1,
54+
labels: map[string]string{
55+
"mtu": "1500",
56+
"network_type": "OpenShiftSDN",
57+
},
58+
},
59+
},
60+
{
61+
name: "MTU 1400 cluster with OVNKubernetes",
62+
oc: &api.OpenShiftCluster{
63+
Properties: api.OpenShiftClusterProperties{
64+
NetworkProfile: api.NetworkProfile{
65+
MTUSize: api.MTU1500,
66+
},
67+
},
68+
},
69+
networkConfig: &configv1.Network{
70+
ObjectMeta: metav1.ObjectMeta{
71+
Name: "cluster",
72+
},
73+
Spec: configv1.NetworkSpec{
74+
NetworkType: "OVNKubernetes",
75+
},
76+
Status: configv1.NetworkStatus{
77+
ClusterNetworkMTU: 1400,
78+
},
79+
},
80+
expectedMetric: metricExpectation{
81+
name: "network.mtu",
82+
value: 1,
83+
labels: map[string]string{
84+
"mtu": "1400",
85+
"network_type": "OVNKubernetes",
86+
},
87+
},
88+
},
89+
}
90+
91+
for _, tt := range tests {
92+
t.Run(tt.name, func(t *testing.T) {
93+
controller := gomock.NewController(t)
94+
m := mock_metrics.NewMockEmitter(controller)
95+
_, log := testlog.New()
96+
97+
// Create fake config client with network config
98+
configcli := configfake.NewSimpleClientset(tt.networkConfig)
99+
100+
mon := &Monitor{
101+
oc: tt.oc,
102+
configcli: configcli,
103+
m: m,
104+
log: log,
105+
}
106+
107+
// Set expectation for the MTU metric
108+
m.EXPECT().EmitGauge(tt.expectedMetric.name, tt.expectedMetric.value, tt.expectedMetric.labels)
109+
110+
err := mon.emitNetworkMTU(ctx)
111+
if err != nil {
112+
t.Errorf("emitNetworkMTU() error = %v", err)
113+
}
114+
})
115+
}
116+
}
117+
118+
func TestEmitNetworkMTUError(t *testing.T) {
119+
ctx := context.Background()
120+
121+
// Test case where network config is not found
122+
controller := gomock.NewController(t)
123+
m := mock_metrics.NewMockEmitter(controller)
124+
_, log := testlog.New()
125+
126+
oc := &api.OpenShiftCluster{
127+
Properties: api.OpenShiftClusterProperties{
128+
NetworkProfile: api.NetworkProfile{
129+
MTUSize: api.MTU1500,
130+
},
131+
},
132+
}
133+
134+
// Empty client with no network config
135+
configcli := configfake.NewSimpleClientset()
136+
137+
mon := &Monitor{
138+
oc: oc,
139+
configcli: configcli,
140+
m: m,
141+
log: log,
142+
}
143+
144+
err := mon.emitNetworkMTU(ctx)
145+
if err == nil {
146+
t.Error("expected error when network config is not found, got nil")
147+
}
148+
}
149+
150+
// metricExpectation represents an expected metric emission
151+
type metricExpectation struct {
152+
name string
153+
value int64
154+
labels map[string]string
155+
}

0 commit comments

Comments
 (0)