Skip to content

Commit

Permalink
Sanitize resource name for prometheus SLIs (#804)
Browse files Browse the repository at this point in the history
Co-authored-by: Akis Maziotis <[email protected]>
Co-authored-by: Mir Shahriar Sabuj <[email protected]>
  • Loading branch information
3 people authored Dec 22, 2022
1 parent b37df9a commit 31403ad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/provisioner/cluster_sli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package provisioner

import (
"github.com/mattermost/mattermost-cloud/internal/tools/utils"
"github.com/mattermost/mattermost-cloud/k8s"
"github.com/mattermost/mattermost-cloud/model"
log "github.com/sirupsen/logrus"
Expand All @@ -19,7 +20,7 @@ const (
)

func makeRingSLOName(group *model.GroupDTO) string {
return group.Name + "-ring-" + group.ID
return utils.SanitizeAlphaNumericString(group.Name) + "-ring-" + group.ID
}

func makeRingSLOs(group *model.GroupDTO, objective float64) slothv1.PrometheusServiceLevel {
Expand Down
18 changes: 18 additions & 0 deletions internal/tools/utils/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//

package utils

import (
"regexp"
"strings"
)

var alphaNumericCharacters = regexp.MustCompile(`[^a-z0-9]+`)

// SanitizeAlphaNumericString converts a string to a valid AlphaNumeric representation, converting all letters to
// lowercase and then removing all invalid characters.
func SanitizeAlphaNumericString(input string) string {
return alphaNumericCharacters.ReplaceAllString(strings.ToLower(input), "")
}
35 changes: 35 additions & 0 deletions internal/tools/utils/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//

package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSanitizeRFC1123String(t *testing.T) {
testCases := []struct {
Input string
Output string
}{
{
Input: "aabbcc",
Output: "aabbcc",
},
{
Input: "SpinWick-thingy-123",
Output: "spinwickthingy123",
},
{
Input: "sup3rR@nD#mS7t))((###-oopp<>../;[",
Output: "sup3rrndms7toopp",
},
}

for _, testCase := range testCases {
assert.Equal(t, testCase.Output, SanitizeAlphaNumericString(testCase.Input))
}
}

0 comments on commit 31403ad

Please sign in to comment.