Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
injector: Simplify check for metrics annotations (#2205)
Browse files Browse the repository at this point in the history
Signed-off-by: Delyan Raychev <delyan.raychev@microsoft.com>

Co-authored-by: Edu Serra <eduser25@gmail.com>
  • Loading branch information
draychev and eduser25 authored Dec 19, 2020
1 parent 23e0d2b commit 4defb02
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 34 deletions.
13 changes: 2 additions & 11 deletions pkg/injector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ import (
"github.com/openservicemesh/osm/pkg/constants"
)

func (wh *webhook) isMetricsEnabled(namespace string) (bool, error) {
func (wh *webhook) isMetricsEnabled(namespace string) (enabled bool, err error) {
ns := wh.kubeController.GetNamespace(namespace)
if ns == nil {
log.Error().Err(errNamespaceNotFound).Msgf("Error retrieving namespace %s", namespace)
return false, errNamespaceNotFound
}

enabled, err := isAnnotatedForMetrics(ns.Annotations)
if err != nil {
return false, err
}

return enabled, nil
}

func isAnnotatedForMetrics(annotations map[string]string) (enabled bool, err error) {
metrics, ok := annotations[constants.MetricsAnnotation]
metrics, ok := ns.Annotations[constants.MetricsAnnotation]
if !ok {
return false, nil
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/injector/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,3 @@ func TestIsMetricsEnabled(t *testing.T) {
})
}
}

func TestIsAnnotatedForMetrics(t *testing.T) {
assert := assert.New(t)

testCases := []struct {
annotations map[string]string
expectMetricsToBeEnabled bool // set to true if metrics is expected to be enabled
expectedErr bool // set to true if error is expected
}{
{map[string]string{constants.MetricsAnnotation: "enabled"}, true, false},
{map[string]string{constants.MetricsAnnotation: "disabled"}, false, false},
{nil, false, false},
{map[string]string{constants.MetricsAnnotation: "invalid"}, false, true},
}

for _, tc := range testCases {
t.Run(fmt.Sprintf("Annotation %v", tc.annotations), func(t *testing.T) {
enabled, err := isAnnotatedForMetrics(tc.annotations)
assert.Equal(enabled, tc.expectMetricsToBeEnabled)
assert.Equal(err != nil, tc.expectedErr)
})
}
}

0 comments on commit 4defb02

Please sign in to comment.