Skip to content

Commit

Permalink
update service scenario_config underlying support
Browse files Browse the repository at this point in the history
Signed-off-by: YarBor <[email protected]>
  • Loading branch information
YarBor committed Sep 2, 2024
1 parent 4c7c58e commit df33df9
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 61 deletions.
1 change: 1 addition & 0 deletions api/mesh/v1alpha1/virtual_service.pb.go

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

2 changes: 1 addition & 1 deletion api/mesh/v1alpha1/virtual_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ message HTTPBody {
// response body as a string
string string = 1;

// response body as base64 encoded bytes.
// response body as base64 encoded bytes.
bytes bytes = 2;
}
}
Expand Down
147 changes: 143 additions & 4 deletions api/mesh/v1alpha1/virtual_service_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@

package v1alpha1

import (
"github.com/apache/dubbo-kubernetes/pkg/util/sets"
"github.com/golang/protobuf/proto"
"golang.org/x/exp/slices"
"regexp"
"strings"
)

import (
"google.golang.org/protobuf/types/known/durationpb"
)

type TrafficRoute_Http_Match struct {
Method *StringMatch
Path *StringMatch
Headers map[string]*StringMatch
Params map[string]*StringMatch
Authority *StringMatch
Method *StringMatch
Path *StringMatch
Headers map[string]*StringMatch
Params map[string]*StringMatch
}

func (m TrafficRoute_Http_Match) GetPath() *StringMatch {
Expand All @@ -40,12 +49,17 @@ func (m TrafficRoute_Http_Match) GetMethod() *StringMatch {
return m.Method
}

func (m TrafficRoute_Http_Match) GetAuthority() *StringMatch {
return m.Authority
}

func (m TrafficRoute_Http_Match) GetParam() map[string]*StringMatch {
return m.Params
}

type TrafficRoute_Http_Modify struct {
TimeOut *durationpb.Duration
Retries *HTTPRetry
Path *TrafficRoute_Http_Modify_Path
Host *TrafficRoute_Http_Modify_Host
RequestHeaders *TrafficRoute_Http_Modify_Headers
Expand Down Expand Up @@ -284,3 +298,128 @@ type TrafficRoute_Http_Modify_Path_Rewrite struct {
}

func (*TrafficRoute_Http_Modify_Path_Regex) isTrafficRoute_Http_Modify_Path_Type() {}

func (x *StringMatch) Match(target string) bool {
if x == nil {
return true
}
switch x.MatchType.(type) {
case *StringMatch_Exact:
return x.GetExact() == target
case *StringMatch_Prefix:
return strings.HasPrefix(target, x.GetPrefix())
case *StringMatch_Regex:
i, _ := regexp.Match(x.GetRegex(), []byte(target))
return i
}
return true
}

func (x *VirtualService) httpConfigDeduplicateAndMerge() {
uniqueMap := make(map[string]*HTTPRoute)
uniqueMatchMap := make(map[string]sets.String)
res := make([]*HTTPRoute, 0, len(x.Http))
getHash := func(msg proto.Message) string {
bt, _ := proto.Marshal(msg)
return string(bt)
}
for _, route := range x.Http {
m := route.Match
route.Match = nil
h := getHash(route)
if h == "" {
continue
}
if uniqueMap[h] == nil {
uniqueMap[h] = route
}
if uniqueMatchMap[h] == nil {
uniqueMatchMap[h] = sets.String{}
}
for _, m := range m {
if mh := getHash(m); mh != "" && !uniqueMatchMap[h].Contains(mh) {
uniqueMatchMap[h].Insert(mh)
uniqueMap[h].Match = append(uniqueMap[h].Match, m)
}
}
}
x.Http = res
}

// Split the match configuration into separate
func (x *VirtualService) preDealHttpConfig() {
res := make([]*HTTPRoute, 0, len(x.Http))
for i := len(x.Http) - 1; i >= 0; i-- {
msg := x.Http[i]
m := msg.Match
msg.Match = nil
for _, match := range m {
n := proto.Clone(msg).(*HTTPRoute)
n.Match = []*HTTPMatchRequest{match}
res = append(res, n)
}
}
x.Http = res
}

// RangeHTTPReferenceUpdate it ensure one piece httpRoute only has one match field in range, and merge match field after range done
func (x *VirtualService) RangeHTTPReferenceUpdate(
ref_range func(index int, ref *HTTPRoute) (insert *HTTPRoute, remove bool, stop bool),
) {
if ref_range == nil {
return
}
var (
insert *HTTPRoute
remove bool
stop bool
newhttpReverselist = make([]*HTTPRoute, 0, len(x.Http)*2)
)
x.preDealHttpConfig()
// reverse range for ensure config priority in appending
for i := len(x.Http) - 1; i >= 0; i-- {
if !stop {
insert, remove, stop = ref_range(i, x.Http[i])
if !remove {
newhttpReverselist = append(newhttpReverselist, x.Http[i])
}
if insert != nil {
newhttpReverselist = append(newhttpReverselist, insert)
}
} else {
newhttpReverselist = append(newhttpReverselist, x.Http[i])
}
}
slices.Reverse(newhttpReverselist)
x.Http = newhttpReverselist
x.httpConfigDeduplicateAndMerge()
}

func (x *HTTPMatchRequest) WithoutURIEmpty() bool {
if x.Scheme != nil {
return false
} else if x.Method != nil {
return false
} else if x.Authority != nil {
return false
} else if x.Headers != nil {
return false
} else if x.Port != 0 {
return false
} else if x.SourceLabels != nil {
return false
} else if x.Gateways != nil {
return false
} else if x.QueryParams != nil {
return false
} else if x.IgnoreUriCase != false {
return false
} else if x.WithoutHeaders != nil {
return false
} else if x.SourceNamespace != "" {
return false
} else if x.StatPrefix != "" {
return false
}
return true
}
5 changes: 5 additions & 0 deletions pkg/core/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package consts

import (
set "github.com/dubbogo/gost/container/set"
"time"
)

const (
Expand Down Expand Up @@ -92,6 +93,10 @@ const (
SideConsumer = `consumer`
)

var DefaultRetryPreTryTimeOut = 200 * time.Millisecond

const DefaultRetriesOn = "connect-failure,refused-stream,5xx"

const (
NotEqual = "!="
Equal = "="
Expand Down
15 changes: 12 additions & 3 deletions pkg/core/xds/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ type ZoneIngressProxy struct {
}

type ClusterSelectorList struct {
MatchInfo mesh_proto.TrafficRoute_Http_Match
ModifyInfo mesh_proto.TrafficRoute_Http_Modify
EndSelectors []ClusterSelector
MatchInfo mesh_proto.TrafficRoute_Http_Match
ModifyInfo mesh_proto.TrafficRoute_Http_Modify
DirectResp *mesh_proto.HTTPDirectResponse
ClusterSelectors []ClusterSelector
}

type ServiceSelectorMap map[ServiceName][]ClusterSelectorList
Expand All @@ -189,6 +190,9 @@ type ClusterSelector struct {
}

func (c *ClusterSelector) Select(l EndpointList) EndpointList {
if c.TagSelect == nil || len(c.TagSelect) == 0 {
return l
}
res := EndpointList{}
for _, endpoint := range l {
if c.TagSelect.Matches(endpoint.Tags) {
Expand All @@ -201,10 +205,15 @@ func (c *ClusterSelector) Select(l EndpointList) EndpointList {
func (e *ClusterSelectorList) GetMatchInfo() *mesh_proto.TrafficRoute_Http_Match {
return &e.MatchInfo
}

func (e *ClusterSelectorList) GetModifyInfo() *mesh_proto.TrafficRoute_Http_Modify {
return &e.ModifyInfo
}

func (e *ClusterSelectorList) GetDirectResp() *mesh_proto.HTTPDirectResponse {
return e.DirectResp
}

type Routing struct {
OutboundSelector ServiceSelectorMap
OutboundTargets EndpointMap
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ func MustToStruct(message proto.Message) *structpb.Struct {
}
return str
}

func ProtoClone(from proto.Message) (to proto.Message) {
return proto.Clone(from)
}
5 changes: 3 additions & 2 deletions pkg/xds/envoy/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
)

type Route struct {
Match *mesh_proto.TrafficRoute_Http_Match
Modify *mesh_proto.TrafficRoute_Http_Modify
DirectResponse *mesh_proto.HTTPDirectResponse
Match *mesh_proto.TrafficRoute_Http_Match
Modify *mesh_proto.TrafficRoute_Http_Modify
// rateLimit
Clusters []Cluster
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/xds/envoy/tags/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (t Tags) WithoutTags(tags ...string) Tags {
}

func (t Tags) WithTags(keysAndValues ...string) Tags {
if len(keysAndValues) == 0 {
return t
}
result := Tags{}
for tagName, tagValue := range t {
result[tagName] = tagValue
Expand Down
Loading

0 comments on commit df33df9

Please sign in to comment.