Skip to content

Commit

Permalink
Fix route name failing on OpenShift (#917)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
  • Loading branch information
pavolloffay authored Feb 20, 2020
1 parent 6b81f9d commit fd64f15
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/route/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *QueryRoute) Get() *corev1.Route {
// -namespace is added to the host by OpenShift
name = util.Truncate(r.jaeger.Name, 62-len(r.jaeger.Namespace))
}

name = util.DNSName(name)
return &corev1.Route{
TypeMeta: metav1.TypeMeta{
Kind: "Route",
Expand Down
3 changes: 2 additions & 1 deletion pkg/strategy/all_in_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)

func init() {
Expand Down Expand Up @@ -110,7 +111,7 @@ func assertDeploymentsAndServicesForAllInOne(t *testing.T, name string, s S, has
ingresses := map[string]bool{}
routes := map[string]bool{}
if viper.GetString("platform") == v1.FlagPlatformOpenShift {
routes[fmt.Sprintf("%s", name)] = false
routes[fmt.Sprintf("%s", util.DNSName(name))] = false
} else {
ingresses[fmt.Sprintf("%s-query", name)] = false
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/strategy/production_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)

func init() {
Expand Down Expand Up @@ -151,7 +152,7 @@ func assertDeploymentsAndServicesForProduction(t *testing.T, name string, s S, h
ingresses := map[string]bool{}
routes := map[string]bool{}
if viper.GetString("platform") == v1.FlagPlatformOpenShift {
routes[name] = false
routes[util.DNSName(name)] = false
} else {
ingresses[fmt.Sprintf("%s-query", name)] = false
}
Expand Down
20 changes: 7 additions & 13 deletions pkg/util/dns_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,22 @@ import (
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 "-".
// If the final name starts with "-", "a" is added as prefix. Similarly, if it ends with "-", "z" is added.
// 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.
func DNSName(name string) string {
var d []rune

first := true
for _, x := range strings.ToLower(name) {
for i, x := range strings.ToLower(name) {
if regex.Match([]byte(string(x))) {
d = append(d, x)
} else {
if first {
if i == 0 || i == utf8.RuneCountInString(name)-1 {
d = append(d, 'a')
}
d = append(d, '-')

if len(d) == utf8.RuneCountInString(name) {
// we had to replace the last char, so, it's "-". DNS names can't end with dash.
d = append(d, 'z')
} else {
d = append(d, '-')
}
}

first = false
}

return string(d)
Expand Down
8 changes: 6 additions & 2 deletions pkg/util/dns_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ func TestDnsName(t *testing.T) {
{"simplest", "simplest"},
{"instance.with.dots-collector-headless", "instance-with-dots-collector-headless"},
{"TestQueryDottedServiceName.With.Dots", "testquerydottedservicename-with-dots"},
{"Service🦄", "service-z"},
{"📈Stock-Tracker", "a-stock-tracker"},
{"Service🦄", "servicea"},
{"📈Stock-Tracker", "astock-tracker"},
{"-📈Stock-Tracker", "a-stock-tracker"},
{"📈", "a"},
{"foo-", "fooa"},
{"-foo", "afoo"},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func findRoute(t *testing.T, f *framework.Framework, name string) *osv1.Route {
}

// Truncate the namespace name and use that to find the route
target := util.Truncate(name, 62-len(namespace))
target := util.DNSName(util.Truncate(name, 62-len(namespace)))
for _, r := range routeList.Items {
if strings.HasPrefix(r.Spec.Host, target) {
return &r
Expand Down

0 comments on commit fd64f15

Please sign in to comment.