Skip to content

Commit

Permalink
echance: fault injection HttpMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikykun committed Jan 31, 2024
1 parent d13cc87 commit 109968c
Show file tree
Hide file tree
Showing 13 changed files with 628 additions and 1,292 deletions.
366 changes: 194 additions & 172 deletions pkg/apis/ctrlmesh/proto/faultinjection.pb.go

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions pkg/apis/ctrlmesh/proto/faultinjection.proto
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,23 @@ message EffectiveTimeRange {
message Match {
repeated ResourceMatch resources = 1;
repeated HttpMatch httpMatch = 2;
repeated StringMatch stringMatch = 3;
}

message StringMatch {
StringMatchType matchType = 1;
repeated string contents = 2;
repeated string methods = 3;
enum StringMatchType {
NORMAL = 0;
REGEXP = 1;
}
message HttpMatch {
MatchContent host = 1;
MatchContent path = 2;
string method = 3;
repeated HttpHeader headers = 4;
}

message HttpMatch {
repeated string url = 1;
repeated string method = 2;
message MatchContent {
string exact = 1;
string regex = 2;
}

message HttpHeader {
string name = 1;
string value = 2;
}

// Describes how to match K8s resources.
Expand Down
53 changes: 37 additions & 16 deletions pkg/apis/ctrlmesh/proto/faultinjection_deepcopy.gen.go

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

69 changes: 27 additions & 42 deletions pkg/apis/ctrlmesh/utils/conv/faultconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func ConvertHTTPFaultInjection(faultInjection *ctrlmeshv1alpha1.HTTPFaultInjecti
}
}
if faultInjection.Match != nil {
protoFaultInjection.Match = ConvertHTTPMatch(faultInjection.Match)
protoFaultInjection.Match = ConvertMatch(faultInjection.Match)

}
if faultInjection.EffectiveTime != nil {
Expand All @@ -85,35 +85,42 @@ func ConvertHTTPFaultInjection(faultInjection *ctrlmeshv1alpha1.HTTPFaultInjecti
return protoFaultInjection
}

func ConvertHTTPMatch(match *ctrlmeshv1alpha1.Match) *ctrlmeshproto.Match {
Match := &ctrlmeshproto.Match{}
func ConvertMatch(match *ctrlmeshv1alpha1.Match) *ctrlmeshproto.Match {
protoMatch := &ctrlmeshproto.Match{}
if match.HttpMatch != nil {
Match.HttpMatch = make([]*ctrlmeshproto.HttpMatch, len(match.HttpMatch))
for i, restRule := range match.HttpMatch {
Match.HttpMatch[i] = &ctrlmeshproto.HttpMatch{}
if restRule.URL != nil {
Match.HttpMatch[i].Url = make([]string, len(restRule.URL))
copy(Match.HttpMatch[i].Url, restRule.URL)
protoMatch.HttpMatch = make([]*ctrlmeshproto.HttpMatch, len(match.HttpMatch))
for i, httpMatch := range match.HttpMatch {
temp := &ctrlmeshproto.HttpMatch{
Host: ConvertMatchContent(httpMatch.Host),
Path: ConvertMatchContent(httpMatch.Path),
Method: httpMatch.Method,
}
if restRule.Method != nil {
Match.HttpMatch[i].Method = make([]string, len(restRule.Method))
copy(Match.HttpMatch[i].Method, restRule.Method)
for _, header := range httpMatch.Headers {
temp.Headers = append(temp.Headers, &ctrlmeshproto.HttpHeader{
Name: header.Name,
Value: header.Name,
})
}
protoMatch.HttpMatch[i] = temp
}
}
if match.Resources != nil {
Match.Resources = make([]*ctrlmeshproto.ResourceMatch, len(match.Resources))
protoMatch.Resources = make([]*ctrlmeshproto.ResourceMatch, len(match.Resources))
for i, relatedResource := range match.Resources {
Match.Resources[i] = ConvertRelatedResources(relatedResource)
protoMatch.Resources[i] = ConvertRelatedResources(relatedResource)
}
}
if match.ContentMatch != nil {
Match.StringMatch = make([]*ctrlmeshproto.StringMatch, len(match.ContentMatch))
for i, stringMatch := range match.ContentMatch {
Match.StringMatch[i] = ConvertStringMatch(stringMatch)
}
return protoMatch
}

func ConvertMatchContent(content *ctrlmeshv1alpha1.MatchContent) *ctrlmeshproto.MatchContent {
if content == nil {
return nil
}
return &ctrlmeshproto.MatchContent{
Exact: content.Exact,
Regex: content.Regex,
}
return Match
}

func ConvertEffectiveTime(timeRange *ctrlmeshv1alpha1.EffectiveTimeRange) *ctrlmeshproto.EffectiveTimeRange {
Expand Down Expand Up @@ -164,25 +171,3 @@ func ConvertRelatedResources(resourceRule *ctrlmeshv1alpha1.ResourceMatch) *ctrl
}
return protoResourceRule
}

func ConvertStringMatch(stringMatch *ctrlmeshv1alpha1.StringMatch) *ctrlmeshproto.StringMatch {
res := &ctrlmeshproto.StringMatch{}
if stringMatch != nil {
if stringMatch.MatchType == ctrlmeshv1alpha1.StringMatchTypeNormal {
res.MatchType = ctrlmeshproto.StringMatch_NORMAL
} else if stringMatch.MatchType == ctrlmeshv1alpha1.StringMatchTypeRegexp {
res.MatchType = ctrlmeshproto.StringMatch_REGEXP
} else {
return res
}
if stringMatch.Contents != nil {
res.Contents = make([]string, len(stringMatch.Contents))
copy(res.Contents, stringMatch.Contents)
}
if stringMatch.Methods != nil {
res.Methods = make([]string, len(stringMatch.Methods))
copy(res.Methods, stringMatch.Methods)
}
}
return res
}
30 changes: 13 additions & 17 deletions pkg/apis/ctrlmesh/v1alpha1/faultinjection_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (

type StringMatchType string

const (
StringMatchTypeNormal StringMatchType = "Normal"
StringMatchTypeRegexp StringMatchType = "Regexp"
)

type HTTPFaultInjectionDelay struct {
// FixedDelay is used to indicate the amount of delay in seconds.
FixedDelay string `json:"fixedDelay,omitempty"`
Expand Down Expand Up @@ -54,18 +49,21 @@ type ResourceMatch struct {
// HttpMatch specifies the criteria for matching HTTP requests to RESTful resources
// as part of HTTP FaultInjection. Each rule can target one or more URLs and HTTP methods.
type HttpMatch struct {
// URL gives the location of the rest request, in standard URL form (`scheme://host:port/path`)
URL []string `json:"url"`
// Method specifies the http method of the request, like: PUT, POST, GET, DELETE.
Method []string `json:"method"`
Host *MatchContent `json:"host,omitempty"`
Path *MatchContent `json:"path,omitempty"`
Method string `json:"method,omitempty"`
//TODO: header match
Headers []*HttpHeader `json:"headers,omitempty"`
}

type MatchContent struct {
Exact string `json:"exact,omitempty"`
Regex string `json:"regex,omitempty"`
}

type StringMatch struct {
MatchType StringMatchType `json:"matchType,omitempty"`
// Content is the content of the fault injection rule
Contents []string `json:"contents"`
// Method specifies the http method of the request, like: PUT, POST, GET, DELETE.
Methods []string `json:"methods"`
type HttpHeader struct {
Name string `json:"name"`
Value string `json:"value,omitempty"`
}

// Match defines a set of rules and criteria for matching incoming HTTP requests.
Expand All @@ -74,8 +72,6 @@ type StringMatch struct {
type Match struct {
Resources []*ResourceMatch `json:"resources,omitempty"`
HttpMatch []*HttpMatch `json:"httpMatch,omitempty"`
// ContentMatch
ContentMatch []*StringMatch `json:"contentMatch,omitempty"`
}

// HTTPFaultInjection can be used to specify one or more faults to inject
Expand Down
93 changes: 49 additions & 44 deletions pkg/apis/ctrlmesh/v1alpha1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 109968c

Please sign in to comment.