diff --git a/pkg/controller/controllername/controllername.go b/pkg/controller/controllername/controllername.go new file mode 100644 index 00000000..3210de38 --- /dev/null +++ b/pkg/controller/controllername/controllername.go @@ -0,0 +1,48 @@ +package controllername + +import ( + "strings" + "unicode" +) + +const ( + metricsNameDelimiter = "_" + loggerNameDelimiter = "-" +) + +type ControllerNamer interface { + MetricsName() string + LoggerName() string +} + +// controllerName ex. {"My","Controller", "Name"} -> MyControllerName +type controllerName []string + +func NewControllerName(name []string) controllerName { + cn := make(controllerName, len(name)) + + for i, w := range name { + cn[i] = strip(strings.ToLower(w)) + + } + return cn +} + +// Strip removes spaces and non letters +func strip(s string) string { + rr := make([]rune, 0, len(s)) + for _, r := range s { + if unicode.IsLetter(r) { + rr = append(rr, r) + } + } + return string(rr) +} + +func (c controllerName) MetricsName() string { + return strings.Join(c, metricsNameDelimiter) +} + +func (c controllerName) LoggerName() string { + return strings.Join(c, loggerNameDelimiter) +} diff --git a/pkg/controller/controllername/controllername_test.go b/pkg/controller/controllername/controllername_test.go new file mode 100644 index 00000000..ba943ef7 --- /dev/null +++ b/pkg/controller/controllername/controllername_test.go @@ -0,0 +1,79 @@ +package controllername + +import ( + "github.com/stretchr/testify/require" + "regexp" + "testing" +) + +func TestMetricsName(t *testing.T) { + + cn1 := NewControllerName([]string{"SomeFakeControllerName"}) + cn2 := NewControllerName([]string{"Some", "Controller", "Name"}) + cn3 := NewControllerName([]string{" SomeName", "Entered ", "poorly"}) + cn4 := NewControllerName([]string{"Some Spaces"}) + cn5 := NewControllerName([]string{"Too Many Spaces"}) + cn6 := NewControllerName([]string{"special!@characters"}) + + metricName1 := cn1.MetricsName() + metricName2 := cn2.MetricsName() + metricName3 := cn3.MetricsName() + metricName4 := cn4.MetricsName() + metricName5 := cn5.MetricsName() + metricName6 := cn6.MetricsName() + + require.True(t, isPrometheusBestPracticeName(metricName1)) + require.True(t, isPrometheusBestPracticeName(metricName2)) + require.True(t, isPrometheusBestPracticeName(metricName3)) + require.True(t, isPrometheusBestPracticeName(metricName4)) + require.True(t, isPrometheusBestPracticeName(metricName5)) + require.True(t, isPrometheusBestPracticeName(metricName6)) + +} + +func TestLoggerName(t *testing.T) { + cn1 := NewControllerName([]string{"SomeFakeControllerName"}) + cn2 := NewControllerName([]string{"Some", "Controller", "Name"}) + cn3 := NewControllerName([]string{" SomeName", "Entered ", "poorly"}) + cn4 := NewControllerName([]string{"Some Spaces"}) + cn5 := NewControllerName([]string{"Too Many Spaces"}) + cn6 := NewControllerName([]string{"special!@characters"}) + + loggerName1 := cn1.LoggerName() + loggerName2 := cn2.LoggerName() + loggerName3 := cn3.LoggerName() + loggerName4 := cn4.LoggerName() + loggerName5 := cn5.LoggerName() + loggerName6 := cn6.LoggerName() + + require.True(t, isBestPracticeLoggerName(loggerName1)) + require.True(t, isBestPracticeLoggerName(loggerName2)) + require.True(t, isBestPracticeLoggerName(loggerName3)) + require.True(t, isBestPracticeLoggerName(loggerName4)) + require.True(t, isBestPracticeLoggerName(loggerName5)) + require.True(t, isBestPracticeLoggerName(loggerName6)) + +} + +func TestStrip(t *testing.T) { + str := "a *&b c " + striped := strip(str) + + require.Equal(t, striped, "abc") +} + +// IsPrometheusBestPracticeName - function returns true if the name given matches best practices for prometheus name, i.e. snake_case +func isPrometheusBestPracticeName(controllerName string) bool { + pattern := "^[a-z]+(_[a-z]+)*$" + match, _ := regexp.MatchString(pattern, controllerName) + + return match +} + +// IsBestPracticeLoggerName - function returns true if the name given matches best practices for prometheus name, i.e. kebab-case +func isBestPracticeLoggerName(controllerName string) bool { + pattern := "^[a-z]+(-[a-z]+)*$" + match, _ := regexp.MatchString(pattern, controllerName) + + return match +} diff --git a/pkg/controller/testutils/testutils.go b/pkg/controller/testutils/testutils.go index 8f401247..fddfa69f 100644 --- a/pkg/controller/testutils/testutils.go +++ b/pkg/controller/testutils/testutils.go @@ -1,7 +1,6 @@ package testutils import ( - "regexp" "testing" "github.com/Azure/aks-app-routing-operator/pkg/controller/metrics" @@ -49,11 +48,3 @@ func StartTestingEnv() (*rest.Config, *envtest.Environment, error) { func CleanupTestingEnv(env *envtest.Environment) error { return env.Stop() } - -// IsPrometheusBestPracticeName - function returns true if the name given matches best practices for prometheus name, i.e. snake_case -func IsPrometheusBestPracticeName(controllerName string) bool { - pattern := "^[a-z]+(_[a-z]+)*$" - match, _ := regexp.MatchString(pattern, controllerName) - - return match -} diff --git a/pkg/controller/testutils/testutils_test.go b/pkg/controller/testutils/testutils_test.go deleted file mode 100644 index f3896262..00000000 --- a/pkg/controller/testutils/testutils_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package testutils - -import ( - "github.com/stretchr/testify/require" - "testing" -) - -func TestIsPrometheusBestPracticeName(t *testing.T) { - notSnakeCase := "obviouslyNotSnakeCase" - simpleSnakeCase := "snake_case" - complexSnakeCase := "complex_snake_case" - leadingSlash := "_leading_slash" - trailingSlash := "trailing_slash_" - - require.False(t, IsPrometheusBestPracticeName(notSnakeCase)) - require.True(t, IsPrometheusBestPracticeName(simpleSnakeCase)) - require.True(t, IsPrometheusBestPracticeName(complexSnakeCase)) - require.False(t, IsPrometheusBestPracticeName(leadingSlash)) - require.False(t, IsPrometheusBestPracticeName(trailingSlash)) -}