This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support domain alias in fence module config
- Loading branch information
1 parent
8079874
commit 9bbe404
Showing
9 changed files
with
412 additions
and
80 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,72 @@ | ||
package controllers | ||
|
||
import ( | ||
"regexp" | ||
lazyloadv1alpha1 "slime.io/slime/modules/lazyload/api/v1alpha1" | ||
) | ||
|
||
func newDomainAliasRule(domainAlias *lazyloadv1alpha1.DomainAlias) *domainAliasRule { | ||
if domainAlias == nil { | ||
return nil | ||
} | ||
|
||
log.Infof("get pattern %s, get templates %v from lazyload addAlias", domainAlias.Pattern, domainAlias.Templates) | ||
|
||
pattern := domainAlias.Pattern | ||
re, err := regexp.Compile(pattern) | ||
if err != nil { | ||
log.Errorf("compile addAlias pattern %s err: %+v", pattern, err) | ||
return nil | ||
} | ||
templates := domainAlias.Templates | ||
if len(templates) == 0 { | ||
log.Errorf("addAlias template is empty") | ||
return nil | ||
} | ||
return &domainAliasRule{ | ||
pattern: pattern, | ||
templates: templates, | ||
re: re, | ||
} | ||
} | ||
|
||
func domainAddAlias(src string, rule *domainAliasRule) []string { | ||
dest := []string{src} | ||
if rule == nil { | ||
return dest | ||
} | ||
allIndexes := rule.re.FindAllSubmatchIndex([]byte(src), -1) | ||
if idxNum := len(allIndexes); idxNum != 1 { | ||
if idxNum > 1 { | ||
log.Warnf("domain %s matches more than once on pattern %s", src, rule.pattern) | ||
} | ||
return dest | ||
} | ||
for _, template := range rule.templates { | ||
var domain []byte | ||
// expand the template according allIndexes | ||
domain = rule.re.ExpandString(domain, template, src, allIndexes[0]) | ||
if len(domain) == 0 { | ||
continue | ||
} | ||
dest = append(dest, string(domain)) | ||
} | ||
log.Debugf("domainAddAlias src: %s, dest: %v", src, dest) | ||
return dest | ||
} | ||
|
||
func addToDomains(domains map[string]*lazyloadv1alpha1.Destinations, fh string) { | ||
if domains[fh] != nil { | ||
return | ||
} | ||
|
||
allHost := []string{fh} | ||
if hs := getDestination(fh); len(hs) > 0 { | ||
allHost = append(allHost, hs...) | ||
} | ||
|
||
domains[fh] = &lazyloadv1alpha1.Destinations{ | ||
Hosts: allHost, | ||
Status: lazyloadv1alpha1.Destinations_ACTIVE, | ||
} | ||
} |
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
Oops, something went wrong.