Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Support domain alias in fence module config #46

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Details at [Install&Use](./lazyload_tutorials.md#install-and-use)
- Automatic ServiceFence generation based on namespace/service label
- Custom undefined traffic dispatch
- Support for adding static service dependencies
- Support for custom service dependency aliases
- Log output to local file and rotate

Details at [Introduction of features](./lazyload_tutorials.md#Introduction-of-features)
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
- 基于namespace/service label自动生成ServiceFence
- 支持自定义兜底流量分派
- 支持添加静态服务依赖关系
- 支持自定义服务依赖别名
- 日志输出到本地并轮转

详见 [特性介绍](./lazyload_tutorials_zh.md#%E7%89%B9%E6%80%A7%E4%BB%8B%E7%BB%8D)
Expand Down
110 changes: 88 additions & 22 deletions api/v1alpha1/fence_module.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions api/v1alpha1/fence_module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ message Fence {
repeated string namespace = 3;
// custom outside dispatch traffic rules
repeated Dispatch dispatches = 4;
// can convert to one or many domain alias rules
repeated DomainAlias domainAliases = 5;
}

// The general idea is to assign different default traffic to different targets
Expand All @@ -29,4 +31,16 @@ message Dispatch {
repeated string domains = 2;
// target cluster
string cluster = 3;
}

// DomainAlias regexp expression, which is alias for target domain
// default value is empty
// example:
// domainAliases:
// - pattern: (?P<service>[^\.]+)\.(?P<namespace>[^\.]+)\.svc\.cluster\.local$
// template:
// - $namespace.$service.service.mailsaas
message DomainAlias {
string pattern = 1;
repeated string templates = 2;
}
81 changes: 81 additions & 0 deletions controllers/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package controllers

import (
"regexp"
lazyloadv1alpha1 "slime.io/slime/modules/lazyload/api/v1alpha1"
)

func newDomainAliasRules(domainAlias []*lazyloadv1alpha1.DomainAlias) []*domainAliasRule {
var rules []*domainAliasRule
if domainAlias == nil {
return nil
}

for _, da := range domainAlias {
log.Infof("lazyload domainAlias: get pattern %s, get templates %v", da.Pattern, da.Templates)

pattern := da.Pattern
re, err := regexp.Compile(pattern)
if err != nil {
log.Errorf("compile domainAlias pattern %s err: %+v", pattern, err)
return nil
}
templates := da.Templates
if len(templates) == 0 {
log.Errorf("domainAlias template is empty")
return nil
}
rule := &domainAliasRule{
pattern: pattern,
templates: templates,
re: re,
}
rules = append(rules, rule)
}
return rules
}

func domainAddAlias(src string, rules []*domainAliasRule) []string {
dest := []string{src}
if rules == nil {
return dest
}

for _, rule := range rules {
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)
}
continue
}
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,
}
}
7 changes: 7 additions & 0 deletions controllers/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package controllers

import (
"regexp"
"sync"

"slime.io/slime/framework/model"
Expand Down Expand Up @@ -33,3 +34,9 @@ type LabelSvcCache struct {
Data map[LabelItem]map[string]struct{}
sync.RWMutex
}

type domainAliasRule struct {
pattern string
templates []string
re *regexp.Regexp
}
Loading