Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor string match #2102

Merged
merged 6 commits into from
Nov 6, 2023
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
19 changes: 1 addition & 18 deletions api/v1alpha1/envoyproxy_metric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ProxyMetrics struct {
// `cluster.<cluster_name>.membership_degraded`,reference https://github.com/envoyproxy/envoy/issues/9856,
// https://github.com/envoyproxy/envoy/issues/14610
//
Matches []Match `json:"matches,omitempty"`
Matches []StringMatch `json:"matches,omitempty"`

// EnableVirtualHostStats enables envoy stat metrics for virtual hosts.
EnableVirtualHostStats bool `json:"enableVirtualHostStats,omitempty"`
Expand Down Expand Up @@ -58,20 +58,3 @@ type ProxyPrometheusProvider struct {
// Disable the Prometheus endpoint.
Disable bool `json:"disable,omitempty"`
}

// Match defines the stats match configuration.
type Match struct { // TODO: zhaohuabing this type should be renamed to StatsMatch
// MatcherType defines the stats matcher type
//
// +kubebuilder:validation:Enum=RegularExpression;Prefix;Suffix
Type MatcherType `json:"type"`
Value string `json:"value"`
}

type MatcherType string

const ( // TODO: zhaohuabing the const types should be prefixed with StatsMatch
Prefix MatcherType = "Prefix"
RegularExpression MatcherType = "RegularExpression"
Suffix MatcherType = "Suffix"
)
14 changes: 7 additions & 7 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ type StringMatch struct {
//
// +optional
// +kubebuilder:default=Exact
Type *MatchType `json:"type,omitempty"`
Type *StringMatchType `json:"type,omitempty"`

// Value specifies the string value that the match must have.
//
Expand All @@ -253,25 +253,25 @@ type StringMatch struct {
Value string `json:"value"`
}

// MatchType specifies the semantics of how a string value should be compared.
// StringMatchType specifies the semantics of how a string value should be compared.
// Valid MatchType values are "Exact", "Prefix", "Suffix", "RegularExpression".
//
// +kubebuilder:validation:Enum=Exact;Prefix;Suffix;RegularExpression
type MatchType string
type StringMatchType string

const (
// MatchExact :the input string must match exactly the match value.
MatchExact MatchType = "Exact"
StringMatchExact StringMatchType = "Exact"

// MatchPrefix :the input string must start with the match value.
MatchPrefix MatchType = "Prefix"
StringMatchPrefix StringMatchType = "Prefix"

// MatchSuffix :the input string must end with the match value.
MatchSuffix MatchType = "Suffix"
StringMatchSuffix StringMatchType = "Suffix"

// MatchRegularExpression :The input string must match the regular expression
// specified in the match value.
// The regex string must adhere to the syntax documented in
// https://github.com/google/re2/wiki/Syntax.
MatchRegularExpression MatchType = "RegularExpression"
StringMatchRegularExpression StringMatchType = "RegularExpression"
)
23 changes: 5 additions & 18 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -5424,19 +5424,26 @@ spec:
`cluster.<cluster_name>.membership_degraded`,reference https://github.com/envoyproxy/envoy/issues/9856,
https://github.com/envoyproxy/envoy/issues/14610'
items:
description: Match defines the stats match configuration.
description: StringMatch defines how to match any strings.
This is a general purpose match condition that can be
used by other EG APIs that need to match against a string.
properties:
type:
description: MatcherType defines the stats matcher type
default: Exact
description: Type specifies how to match against a string.
enum:
- RegularExpression
- Exact
- Prefix
- Suffix
- RegularExpression
type: string
value:
description: Value specifies the string value that the
match must have.
maxLength: 1024
minLength: 1
type: string
required:
- type
- value
type: object
type: array
Expand Down
10 changes: 5 additions & 5 deletions internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,26 +296,26 @@ func (t *Translator) buildCORS(policy *egv1a1.SecurityPolicy) *ir.CORS {
origin := origin.DeepCopy()

// matchType default to exact
matchType := egv1a1.MatchExact
matchType := egv1a1.StringMatchExact
if origin.Type != nil {
matchType = *origin.Type
}

// TODO zhaohuabing: extract a utils function to build StringMatch
switch matchType {
case egv1a1.MatchExact:
case egv1a1.StringMatchExact:
allowOrigins = append(allowOrigins, &ir.StringMatch{
Exact: &origin.Value,
})
case egv1a1.MatchPrefix:
case egv1a1.StringMatchPrefix:
allowOrigins = append(allowOrigins, &ir.StringMatch{
Prefix: &origin.Value,
})
case egv1a1.MatchSuffix:
case egv1a1.StringMatchSuffix:
allowOrigins = append(allowOrigins, &ir.StringMatch{
Suffix: &origin.Value,
})
case egv1a1.MatchRegularExpression:
case egv1a1.StringMatchRegularExpression:
allowOrigins = append(allowOrigins, &ir.StringMatch{
SafeRegex: &origin.Value,
})
Expand Down
16 changes: 12 additions & 4 deletions internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type readyServerParameters struct {
}

type StatsMatcherParameters struct {
Exacts []string
Prefixs []string
Suffixs []string
RegularExpressions []string
Expand Down Expand Up @@ -154,12 +155,19 @@ func GetRenderedBootstrapConfig(proxyMetrics *egv1a1.ProxyMetrics) (string, erro
if proxyMetrics.Matches != nil {
// Add custom envoy proxy stats
for _, match := range proxyMetrics.Matches {
switch match.Type {
case egv1a1.Prefix:
// matchType default to exact
matchType := egv1a1.StringMatchExact
if match.Type != nil {
matchType = *match.Type
}
switch matchType {
case egv1a1.StringMatchExact:
StatsMatcher.Exacts = append(StatsMatcher.Exacts, match.Value)
case egv1a1.StringMatchPrefix:
StatsMatcher.Prefixs = append(StatsMatcher.Prefixs, match.Value)
case egv1a1.Suffix:
case egv1a1.StringMatchSuffix:
StatsMatcher.Suffixs = append(StatsMatcher.Suffixs, match.Value)
case egv1a1.RegularExpression:
case egv1a1.StringMatchRegularExpression:
StatsMatcher.RegularExpressions = append(StatsMatcher.RegularExpressions, match.Value)
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/xds/bootstrap/bootstrap.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ stats_config:
stats_matcher:
inclusion_list:
patterns:
{{- range $_, $item := .StatsMatcher.Exacts }}
- exact: {{$item}}
{{- end}}
{{- range $_, $item := .StatsMatcher.Prefixs }}
- prefix: {{$item}}
{{- end}}
Expand Down
15 changes: 10 additions & 5 deletions internal/xds/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/utils/ptr"
)

func TestGetRenderedBootstrapConfig(t *testing.T) {
Expand Down Expand Up @@ -55,21 +56,25 @@ func TestGetRenderedBootstrapConfig(t *testing.T) {
{
name: "custom-stats-matcher",
proxyMetrics: &egv1a1.ProxyMetrics{
Matches: []egv1a1.Match{
Matches: []egv1a1.StringMatch{
{
Type: egv1a1.Prefix,
Type: ptr.To(egv1a1.StringMatchExact),
Value: "http.foo.bar.cluster.upstream_rq",
},
{
Type: ptr.To(egv1a1.StringMatchPrefix),
Value: "http",
},
{
Type: egv1a1.Suffix,
Type: ptr.To(egv1a1.StringMatchSuffix),
Value: "upstream_rq",
},
{
Type: egv1a1.RegularExpression,
Type: ptr.To(egv1a1.StringMatchRegularExpression),
Value: "virtual.*",
},
{
Type: egv1a1.Prefix,
Type: ptr.To(egv1a1.StringMatchPrefix),
Value: "cluster",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ stats_config:
stats_matcher:
inclusion_list:
patterns:
- exact: http.foo.bar.cluster.upstream_rq
- prefix: http
- prefix: cluster
- suffix: upstream_rq
Expand Down
53 changes: 14 additions & 39 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1059,43 +1059,6 @@ _Appears in:_



#### Match



Match defines the stats match configuration.

_Appears in:_
- [ProxyMetrics](#proxymetrics)

| Field | Description |
| --- | --- |
| `type` _[MatcherType](#matchertype)_ | MatcherType defines the stats matcher type |
| `value` _string_ | |


#### MatchType

_Underlying type:_ `string`

MatchType specifies the semantics of how a string value should be compared. Valid MatchType values are "Exact", "Prefix", "Suffix", "RegularExpression".

_Appears in:_
- [StringMatch](#stringmatch)



#### MatcherType

_Underlying type:_ `string`



_Appears in:_
- [Match](#match)



#### MetricSinkType

_Underlying type:_ `string`
Expand Down Expand Up @@ -1288,7 +1251,7 @@ _Appears in:_
| --- | --- |
| `prometheus` _[ProxyPrometheusProvider](#proxyprometheusprovider)_ | Prometheus defines the configuration for Admin endpoint `/stats/prometheus`. |
| `sinks` _[ProxyMetricSink](#proxymetricsink) array_ | Sinks defines the metric sinks where metrics are sent to. |
| `matches` _[Match](#match) array_ | Matches defines configuration for selecting specific metrics instead of generating all metrics stats that are enabled by default. This helps reduce CPU and memory overhead in Envoy, but eliminating some stats may after critical functionality. Here are the stats that we strongly recommend not disabling: `cluster_manager.warming_clusters`, `cluster.<cluster_name>.membership_total`,`cluster.<cluster_name>.membership_healthy`, `cluster.<cluster_name>.membership_degraded`,reference https://github.com/envoyproxy/envoy/issues/9856, https://github.com/envoyproxy/envoy/issues/14610 |
| `matches` _[StringMatch](#stringmatch) array_ | Matches defines configuration for selecting specific metrics instead of generating all metrics stats that are enabled by default. This helps reduce CPU and memory overhead in Envoy, but eliminating some stats may after critical functionality. Here are the stats that we strongly recommend not disabling: `cluster_manager.warming_clusters`, `cluster.<cluster_name>.membership_total`,`cluster.<cluster_name>.membership_healthy`, `cluster.<cluster_name>.membership_degraded`,reference https://github.com/envoyproxy/envoy/issues/9856, https://github.com/envoyproxy/envoy/issues/14610 |
| `enableVirtualHostStats` _boolean_ | EnableVirtualHostStats enables envoy stat metrics for virtual hosts. |


Expand Down Expand Up @@ -1628,13 +1591,25 @@ StringMatch defines how to match any strings. This is a general purpose match co

_Appears in:_
- [CORS](#cors)
- [ProxyMetrics](#proxymetrics)

| Field | Description |
| --- | --- |
| `type` _[MatchType](#matchtype)_ | Type specifies how to match against a string. |
| `type` _[StringMatchType](#stringmatchtype)_ | Type specifies how to match against a string. |
| `value` _string_ | Value specifies the string value that the match must have. |


#### StringMatchType

_Underlying type:_ `string`

StringMatchType specifies the semantics of how a string value should be compared. Valid MatchType values are "Exact", "Prefix", "Suffix", "RegularExpression".

_Appears in:_
- [StringMatch](#stringmatch)



#### TCPKeepalive


Expand Down