Skip to content

Commit 17f3e32

Browse files
committed
feat: preliminary implementation of traffic routing
1 parent 0807185 commit 17f3e32

16 files changed

+605
-0
lines changed

core/route/base/headers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package base
2+
3+
type Headers struct {
4+
Request *HeaderOperations
5+
Response *HeaderOperations
6+
}
7+
8+
type HeaderOperations struct {
9+
Set map[string]string
10+
Add map[string]string
11+
Remove []string
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package base
2+
3+
type HTTPMatchRequest struct {
4+
Name string
5+
Headers map[string]*StringMatch
6+
Uri *StringMatch
7+
Scheme *StringMatch
8+
Authority *StringMatch
9+
Method *StringMatch
10+
Port *int
11+
QueryParams map[string]*StringMatch
12+
}
13+
14+
// TODO
15+
func (h *HTTPMatchRequest) IsMatch(context *TrafficContext) bool {
16+
if !h.Method.IsMatch(context.MethodName) {
17+
return false
18+
}
19+
20+
for key, match := range h.Headers {
21+
if v, ok := context.Headers[key]; ok && !match.IsMatch(v) {
22+
return false
23+
}
24+
}
25+
26+
return true
27+
}

core/route/base/http_route.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package base
2+
3+
type HTTPRoute struct {
4+
Name string
5+
Match []*HTTPMatchRequest
6+
Route []*HTTPRouteDestination
7+
}
8+
9+
func (h *HTTPRoute) IsMatch(context *TrafficContext) bool {
10+
for _, match := range h.Match {
11+
if match.IsMatch(context) {
12+
return true
13+
}
14+
}
15+
return false
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package base
2+
3+
import "fmt"
4+
5+
type HTTPRouteDestination struct {
6+
Weight int
7+
Destination *Destination
8+
Headers Headers // TODO modifies headers
9+
}
10+
11+
func (H HTTPRouteDestination) String() string {
12+
return fmt.Sprintf("{Weight: %v, Destination: %+v}\n", H.Weight, H.Destination)
13+
}
14+
15+
type Destination struct {
16+
Host string
17+
Subset string
18+
Port uint32
19+
Fallback *HTTPRouteDestination
20+
}

core/route/base/instance.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package base
2+
3+
type Instance struct {
4+
AppName string
5+
Host string
6+
Port int
7+
Metadata map[string]string
8+
TargetInstance interface{}
9+
}

core/route/base/string_match.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package base
2+
3+
import "regexp"
4+
5+
type StringMatch struct {
6+
Exact string
7+
Prefix string
8+
Regex string
9+
}
10+
11+
func (s *StringMatch) IsMatch(input string) bool {
12+
if input == "" {
13+
return false
14+
}
15+
16+
if s.Exact != "" {
17+
return input == s.Exact
18+
} else if s.Prefix != "" {
19+
return len(input) >= len(s.Prefix) && input[:len(s.Prefix)] == s.Prefix
20+
} else if s.Regex != "" {
21+
matched, _ := regexp.MatchString(s.Regex, input)
22+
return matched
23+
}
24+
return true
25+
}

core/route/base/traffic_context.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package base
2+
3+
type TrafficContext struct {
4+
Path string
5+
ServiceName string
6+
Group string
7+
Version string
8+
MethodName string
9+
ParamTypes []string
10+
Args []interface{}
11+
Headers map[string]string
12+
Baggage map[string]string
13+
}

core/route/base/traffic_policy.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package base
2+
3+
type TrafficPolicy struct {
4+
LoadBalancer string
5+
}

core/route/base/traffic_router.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package base
2+
3+
type TrafficRouter struct {
4+
Host []string
5+
Http []*HTTPRoute
6+
}
7+
8+
type Fallback struct {
9+
Host string
10+
Subset string
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package base
2+
3+
type VirtualWorkload struct {
4+
Host string
5+
trafficPolicy *TrafficPolicy
6+
Subsets []*Subset
7+
}
8+
9+
type Subset struct {
10+
Name string
11+
Labels map[string]string
12+
}

0 commit comments

Comments
 (0)