forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit names and labels to 63 characters (open-telemetry#609)
* added naming formaters from jaeger project * updated naming to use name formatters * update labels to conform with kubernetes rules * fixed linting issues * added Jaeger copyright * updated Jaeger copyright
- Loading branch information
Showing
7 changed files
with
291 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Additional copyrights: | ||
// Copyright The Jaeger Authors | ||
|
||
package naming | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
var regex = regexp.MustCompile(`[a-z0-9]`) | ||
|
||
// DNSName returns a dns-safe string for the given name. | ||
// Any char that is not [a-z0-9] is replaced by "-" or "a". | ||
// Replacement character "a" is used only at the beginning or at the end of the name. | ||
// The function does not change length of the string. | ||
// source: https://github.com/jaegertracing/jaeger-operator/blob/91e3b69ee5c8761bbda9d3cf431400a73fc1112a/pkg/util/dns_name.go#L15 | ||
func DNSName(name string) string { | ||
var d []rune | ||
|
||
for i, x := range strings.ToLower(name) { | ||
if regex.Match([]byte(string(x))) { | ||
d = append(d, x) | ||
} else { | ||
if i == 0 || i == utf8.RuneCountInString(name)-1 { | ||
d = append(d, 'a') | ||
} else { | ||
d = append(d, '-') | ||
} | ||
} | ||
} | ||
|
||
return string(d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Additional copyrights: | ||
// Copyright The Jaeger Authors | ||
|
||
package naming | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDnsName(t *testing.T) { | ||
var tests = []struct { | ||
in string | ||
out string | ||
}{ | ||
{"simplest", "simplest"}, | ||
{"instance.with.dots-collector-headless", "instance-with-dots-collector-headless"}, | ||
{"TestQueryDottedServiceName.With.Dots", "testquerydottedservicename-with-dots"}, | ||
{"Service🦄", "servicea"}, | ||
{"📈Stock-Tracker", "astock-tracker"}, | ||
{"-📈Stock-Tracker", "a-stock-tracker"}, | ||
{"📈", "a"}, | ||
{"foo-", "fooa"}, | ||
{"-foo", "afoo"}, | ||
} | ||
rule, err := regexp.Compile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`) | ||
assert.NoError(t, err) | ||
|
||
for _, tt := range tests { | ||
assert.Equal(t, tt.out, DNSName(tt.in)) | ||
matched := rule.Match([]byte(tt.out)) | ||
assert.True(t, matched, "%v is not a valid name", tt.out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Additional copyrights: | ||
// Copyright The Jaeger Authors | ||
|
||
package naming | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
var regexpEndReplace, regexpBeginReplace *regexp.Regexp | ||
|
||
func init() { | ||
regexpEndReplace, _ = regexp.Compile("[^A-Za-z0-9]+$") | ||
regexpBeginReplace, _ = regexp.Compile("^[^A-Za-z0-9]+") | ||
} | ||
|
||
// Truncate will shorten the length of the instance name so that it contains at most max chars when combined with the fixed part | ||
// If the fixed part is already bigger than the max, this function is noop. | ||
// source: https://github.com/jaegertracing/jaeger-operator/blob/91e3b69ee5c8761bbda9d3cf431400a73fc1112a/pkg/util/truncate.go#L17 | ||
func Truncate(format string, max int, values ...interface{}) string { | ||
var truncated []interface{} | ||
result := fmt.Sprintf(format, values...) | ||
|
||
if excess := len(result) - max; excess > 0 { | ||
// we try to reduce the first string we find | ||
for _, value := range values { | ||
if excess == 0 { | ||
continue | ||
} | ||
|
||
if s, ok := value.(string); ok { | ||
if len(s) > excess { | ||
value = s[:len(s)-excess] | ||
excess = 0 | ||
} else { | ||
value = "" // skip this value entirely | ||
excess = excess - len(s) | ||
} | ||
} | ||
|
||
truncated = append(truncated, value) | ||
} | ||
|
||
result = fmt.Sprintf(format, truncated...) | ||
} | ||
|
||
// if at this point, the result is still bigger than max, apply a hard cap: | ||
if len(result) > max { | ||
return result[:max] | ||
} | ||
|
||
return trimNonAlphaNumeric(result) | ||
} | ||
|
||
// trimNonAlphaNumeric remove all non-alphanumeric values from start and end of the string | ||
// source: https://github.com/jaegertracing/jaeger-operator/blob/91e3b69ee5c8761bbda9d3cf431400a73fc1112a/pkg/util/truncate.go#L53 | ||
func trimNonAlphaNumeric(text string) string { | ||
newText := regexpEndReplace.ReplaceAllString(text, "") | ||
return regexpBeginReplace.ReplaceAllString(newText, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Additional copyrights: | ||
// Copyright The Jaeger Authors | ||
|
||
package naming | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTruncate(t *testing.T) { | ||
for _, tt := range []struct { | ||
format string | ||
max int | ||
values []interface{} | ||
expected string | ||
cap string | ||
}{ | ||
{ | ||
format: "%s-collector", | ||
max: 63, | ||
values: []interface{}{"simplest"}, | ||
expected: "simplest-collector", | ||
cap: "the standard case", | ||
}, | ||
{ | ||
format: "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5", | ||
max: 63, | ||
values: []interface{}{}, | ||
expected: "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85", | ||
cap: "first N case", | ||
}, | ||
{ | ||
format: "%s-collector", | ||
max: 63, | ||
values: []interface{}{"d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5"}, | ||
expected: "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11e-collector", | ||
cap: "instance + fixed within bounds", | ||
}, | ||
{ | ||
format: "%s-%s-collector", | ||
max: 63, | ||
values: []interface{}{"d0c1e62", "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5"}, | ||
expected: "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174--collector", | ||
cap: "first value gets dropped, second truncated", | ||
}, | ||
{ | ||
format: "%d-%s-collector", | ||
max: 63, | ||
values: []interface{}{42, "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5"}, | ||
expected: "42-d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96--collector", | ||
cap: "first value gets passed, second truncated", | ||
}, | ||
} { | ||
assert.Equal(t, tt.expected, Truncate(tt.format, tt.max, tt.values...)) | ||
} | ||
} | ||
|
||
func TestTrimNonAlphaNumeric(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
input: "-%$#ThisIsALabel", | ||
expected: "ThisIsALabel", | ||
}, | ||
|
||
{ | ||
input: "label-invalid--_truncated-.", | ||
expected: "label-invalid--_truncated", | ||
}, | ||
|
||
{ | ||
input: "--(label-invalid--_truncated-#.1.", | ||
expected: "label-invalid--_truncated-#.1", | ||
}, | ||
|
||
{ | ||
input: "12ValidLabel3", | ||
expected: "12ValidLabel3", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
output := trimNonAlphaNumeric(test.input) | ||
assert.Equal(t, test.expected, output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters