Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit b4daf15

Browse files
committed
Validating metric name before writing to cache
Signed-off-by: Madalina Lazar <madalina.lazar@intel.com>
1 parent 3144ce7 commit b4daf15

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

telemetry-aware-scheduling/pkg/cache/autoupdating.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ const (
2020
l2 = 2
2121
)
2222

23-
var errNull = errors.New("")
23+
var (
24+
errNull = errors.New("")
25+
errInvalidMetricName = errors.New("invalid metric name")
26+
)
2427

2528
// AutoUpdatingCache holds a map of metrics of interest with their associated NodeMetricsInfo object.
2629
type AutoUpdatingCache struct {
@@ -119,6 +122,12 @@ func (n *AutoUpdatingCache) WritePolicy(namespace string, policyName string, pol
119122
// It also increments a counter showing how many strategies are using the metric -
120123
// protecting it from deletion until there are no more associated strategies.
121124
func (n *AutoUpdatingCache) WriteMetric(metricName string, data metrics.NodeMetricsInfo) error {
125+
if len(metricName) == 0 {
126+
klog.V(l2).ErrorS(errInvalidMetricName, "Failed to write metric with metric name: "+metricName, "component", "controller")
127+
128+
return errInvalidMetricName
129+
}
130+
122131
payload := nilPayloadCheck(data)
123132
n.add(fmt.Sprintf(metricPath, metricName), payload)
124133

telemetry-aware-scheduling/pkg/cache/autoupdating_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func TestNodeMetricsCache_WriteMetric(t *testing.T) {
201201
{"false name queried", MockEmptySelfUpdatingCache(), "memory_free", args{"memoryFREE"}, true},
202202
{"number queried", MockEmptySelfUpdatingCache(), "1", args{"memoryFREE"}, true},
203203
{"add existing metric", MockEmptySelfUpdatingCache(), "dummyMetric1", args{"dummyMetric1"}, false},
204+
{"empty metric name", MockEmptySelfUpdatingCache(), "", args{""}, true},
204205
}
205206
for _, tt := range tests {
206207
tt := tt

0 commit comments

Comments
 (0)