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

Commit

Permalink
Validate policy name before adding to metrics cache
Browse files Browse the repository at this point in the history
Signed-off-by: Madalina Lazar <[email protected]>
  • Loading branch information
madalazar committed Aug 9, 2023
1 parent b4daf15 commit 5e49d4d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions telemetry-aware-scheduling/pkg/cache/autoupdating.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
var (
errNull = errors.New("")
errInvalidMetricName = errors.New("invalid metric name")
errInvalidPolicyName = errors.New("invalid policy name")
)

// AutoUpdatingCache holds a map of metrics of interest with their associated NodeMetricsInfo object.
Expand Down Expand Up @@ -113,6 +114,12 @@ func (n *AutoUpdatingCache) ReadPolicy(namespace string, policyName string) (tel

// WritePolicy sends the passed object to be stored in the cache under the namespace/name.
func (n *AutoUpdatingCache) WritePolicy(namespace string, policyName string, policy telemetrypolicy.TASPolicy) error {
if len(policyName) == 0 {
klog.V(l2).ErrorS(errInvalidPolicyName, "Failed to add policy with name: "+policyName, "component", "controller")

return errInvalidPolicyName
}

n.add(fmt.Sprintf(policyPath, namespace, policyName), policy)

return nil
Expand Down
4 changes: 3 additions & 1 deletion telemetry-aware-scheduling/pkg/cache/autoupdating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ func TestNodeMetricsCache_ReadPolicy(t *testing.T) {
}{
{"existing policy", MockSelfUpdatingCache(), args{mockPolicy}, mockPolicy, false},
{"non existing policy", MockSelfUpdatingCache(), args{mockPolicy}, mockPolicy2, true},
{"empty policy name", MockSelfUpdatingCache(), args{mockInvalidPolicyName1}, mockInvalidPolicyName1, true},
{"single character policy name", MockSelfUpdatingCache(), args{mockInvalidPolicyName2}, mockInvalidPolicyName2, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
err1 := tt.n.WritePolicy(tt.args.policy.Namespace, tt.args.policy.Name, mockPolicy)
err1 := tt.n.WritePolicy(tt.args.policy.Namespace, tt.args.policy.Name, tt.args.policy)
got, err2 := tt.n.ReadPolicy(tt.want.Namespace, tt.want.Name)
if err1 != nil || err2 != nil {
if !tt.wantErr {
Expand Down
6 changes: 6 additions & 0 deletions telemetry-aware-scheduling/pkg/cache/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ var mockPolicy = telemetrypolicy.TASPolicy{
var mockPolicy2 = telemetrypolicy.TASPolicy{
ObjectMeta: v1.ObjectMeta{Name: "not-mock-policy", Namespace: "default"},
}
var mockInvalidPolicyName1 = telemetrypolicy.TASPolicy{
ObjectMeta: v1.ObjectMeta{Name: "", Namespace: "default"},
}
var mockInvalidPolicyName2 = telemetrypolicy.TASPolicy{
ObjectMeta: v1.ObjectMeta{Name: "n", Namespace: "default"},
}

// ReadMetric is a method implemented for Mock cache.
func (n MockCache) ReadMetric(string) (metrics.NodeMetricsInfo, error) {
Expand Down

0 comments on commit 5e49d4d

Please sign in to comment.