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

Commit

Permalink
Support domain alias in fence module config
Browse files Browse the repository at this point in the history
  • Loading branch information
cywang1905 committed Apr 1, 2022
1 parent 8079874 commit 9bbe404
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 80 deletions.
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
109 changes: 87 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;

DomainAlias domainAlias = 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:
// domainAlias:
// pattern: (?P<service>[^\.]+)\.(?P<namespace>[^\.]+)\.svc\.cluster\.local$
// template:
// - $namespace.$service.service.mailsaas
message DomainAlias {
string pattern = 1;
repeated string templates = 2;
}
72 changes: 72 additions & 0 deletions controllers/domain.go
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,
}
}
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

0 comments on commit 9bbe404

Please sign in to comment.