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

Commit

Permalink
fixed linking schemas as service
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Aug 18, 2017
1 parent 56ed119 commit e42ff51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
24 changes: 17 additions & 7 deletions pkg/converter/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

const (
fqdnTemplate = "%s.%s.%s" // app.service.project
hostPortPattern = "([a-z]+[a-z0-9._-]*)(:[0-9]+)?" // sloppy appName conform
hostPortPattern = `([a-z]+[a-z0-9._-]*)(:[0-9]+)?` // sloppy appName conform
schemePattern = `^(\w+)(:\/\/)+`
)

var hostPortRegex *regexp.Regexp = regexp.MustCompile(hostPortPattern)
var schemeRegex *regexp.Regexp = regexp.MustCompile(schemePattern)

type DependencyError struct {
errStr string
Expand Down Expand Up @@ -54,12 +56,11 @@ func (l *Linker) Resolve(cf *ComposeFile, sf *SloppyFile) error {
for _, link := range l.links {
for key, val := range link.app.App.EnvVars {
app := link.app.App
matches := l.FindServiceString(key, val)
if matches == nil {
match := l.FindServiceString(key, val)
if match == "" {
continue
}

match := matches[1]
targetLink := l.GetByApp(match)

if targetLink == nil {
Expand Down Expand Up @@ -139,12 +140,21 @@ func (l *Linker) buildLinks(sf *SloppyFile) {
// Primary match would be a <host:port> one.
// To also support service linking without a port the
// environment key name requires to contain the `HOST` string.
func (l *Linker) FindServiceString(key string, val string) []string {
func (l *Linker) FindServiceString(key string, val string) string {
if strings.Index(val, ":") != -1 ||
strings.Contains(key, "HOST") {
return hostPortRegex.FindStringSubmatch(val)
matches := hostPortRegex.FindAllStringSubmatch(val, -1)
for _, subMatch := range matches {
schemeMatches := schemeRegex.FindStringSubmatch(val)
// skipping schemes as service
if schemeMatches != nil && subMatch[1] == schemeMatches[1] {
continue
}

return subMatch[1]
}
}
return nil
return ""
}

func (l *Linker) formatDependency(in string) (out string) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/converter/linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ func TestLinker_FindService(t *testing.T) {
"SHORT": {"s:80", true},
"FOO_BAR": {"another.foo:443", true},
"FOO_HOST": {"bar", true},
"FOO_URL": {"mongodb://whatever:4444/db", true},
"BAR_URL": {"foo://nope", true},
}

l := converter.Linker{}

for envKey, caseVal := range cases {
matches := l.FindServiceString(envKey, caseVal.value)
if caseVal.shouldMatch && matches == nil {
match := l.FindServiceString(envKey, caseVal.value)
if caseVal.shouldMatch && match == "" {
t.Errorf("Expected an match for %q, got nothing.", caseVal.value)
} else if matches != nil && !caseVal.shouldMatch {
t.Errorf("Expected no match for %q, got: %v", caseVal.value, matches)
} else if match != "" && !caseVal.shouldMatch {
t.Errorf("Expected no match for %q, got: %v", caseVal.value, match)
}
}
}
Expand Down

0 comments on commit e42ff51

Please sign in to comment.