Skip to content

Commit

Permalink
feat(meshhealthcheck): add x-kuma-tags to requests (#5722)
Browse files Browse the repository at this point in the history
We add x-kuma-tags to all requests that way things like faultInjection
can also apply to health checks

Fix #5718

Signed-off-by: Charly Molter <charly.molter@konghq.com>
  • Loading branch information
lahabana authored Jan 17, 2023
1 parent 81c8718 commit 9648bc6
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func applyToOutbounds(rules core_xds.ToRules, outboundClusters map[string]*envoy
for cluster, serviceName := range targetedClusters {
protocol := policies_xds.InferProtocol(routing, serviceName)

if err := configure(rules.Rules, core_xds.MeshService(serviceName), protocol, cluster); err != nil {
if err := configure(dataplane, rules.Rules, core_xds.MeshService(serviceName), protocol, cluster); err != nil {
return err
}
}

return nil
}

func configure(rules core_xds.Rules, subset core_xds.Subset, protocol core_mesh.Protocol, cluster *envoy_cluster.Cluster) error {
func configure(dataplane *core_mesh.DataplaneResource, rules core_xds.Rules, subset core_xds.Subset, protocol core_mesh.Protocol, cluster *envoy_cluster.Cluster) error {
var conf api.Conf
if computed := rules.Compute(subset); computed != nil {
conf = computed.Conf.(api.Conf)
Expand All @@ -76,6 +76,7 @@ func configure(rules core_xds.Rules, subset core_xds.Subset, protocol core_mesh.
configurer := plugin_xds.Configurer{
Conf: conf,
Protocol: protocol,
Tags: dataplane.Spec.TagSet(),
}

if err := configurer.Configure(cluster); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ healthChecks:
start: "201"
path: /health
requestHeadersToAdd:
- header:
key: x-kuma-tags
value: '&kuma.io/service=backend&'
- append: true
header:
key: x-some-header
Expand Down
22 changes: 15 additions & 7 deletions pkg/plugins/policies/meshhealthcheck/plugin/xds/configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import (
envoy_type "github.com/envoyproxy/go-control-plane/envoy/type/v3"
"github.com/pkg/errors"

"github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
api "github.com/kumahq/kuma/pkg/plugins/policies/meshhealthcheck/api/v1alpha1"
util_proto "github.com/kumahq/kuma/pkg/util/proto"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
)

type Configurer struct {
Conf api.Conf
Protocol core_mesh.Protocol
Tags v1alpha1.MultiValueTagSet
}

type HCProtocol string
Expand Down Expand Up @@ -48,7 +51,7 @@ func (e *Configurer) Configure(cluster *envoy_cluster.Cluster) error {
case HCProtocolTCP:
healthChecker = tcpHealthCheck(tcp)
case HCProtocolHTTP:
healthChecker = httpHealthCheck(e.Protocol, http)
healthChecker = httpHealthCheck(e.Protocol, http, e.Tags)
case HCProtocolGRPC:
healthChecker = grpcHealthCheck(grpc)
}
Expand Down Expand Up @@ -92,8 +95,16 @@ func mapUInt32ToInt64Range(value uint32) *envoy_type.Int64Range {
}
}

func mapHttpHeaders(headers *[]api.HeaderValueOption) []*envoy_core.HeaderValueOption {
func mapHttpHeaders(headers *[]api.HeaderValueOption, srcTags v1alpha1.MultiValueTagSet) []*envoy_core.HeaderValueOption {
var envoyHeaders []*envoy_core.HeaderValueOption
if len(srcTags) > 0 {
envoyHeaders = append(envoyHeaders, &envoy_core.HeaderValueOption{
Header: &envoy_core.HeaderValue{
Key: tags.TagsHeaderName,
Value: tags.Serialize(srcTags),
},
})
}
if headers != nil {
for _, header := range *headers {
hvo := &envoy_core.HeaderValueOption{
Expand Down Expand Up @@ -143,10 +154,7 @@ func tcpHealthCheck(
}
}

func httpHealthCheck(
protocol core_mesh.Protocol,
httpConf *api.HttpHealthCheck,
) *envoy_core.HealthCheck_HttpHealthCheck_ {
func httpHealthCheck(protocol core_mesh.Protocol, httpConf *api.HttpHealthCheck, srcTags v1alpha1.MultiValueTagSet) *envoy_core.HealthCheck_HttpHealthCheck_ {
var expectedStatuses []*envoy_type.Int64Range
if httpConf.ExpectedStatuses != nil {
for _, status := range *httpConf.ExpectedStatuses {
Expand All @@ -164,7 +172,7 @@ func httpHealthCheck(

httpHealthCheck := envoy_core.HealthCheck_HttpHealthCheck{
Path: httpConf.Path,
RequestHeadersToAdd: mapHttpHeaders(httpConf.RequestHeadersToAdd),
RequestHeadersToAdd: mapHttpHeaders(httpConf.RequestHeadersToAdd, srcTags),
ExpectedStatuses: expectedStatuses,
CodecClientType: codecClientType,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/runtime/gateway/route/configurers.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func RouteActionForward(mesh *core_mesh.MeshResource, endpoints core_xds.Endpoin

if isMeshCluster {
requestHeadersToAdd = []*envoy_config_core.HeaderValueOption{{
Header: &envoy_config_core.HeaderValue{Key: v3.TagsHeaderName, Value: tags.Serialize(proxyTags)},
Header: &envoy_config_core.HeaderValue{Key: tags.TagsHeaderName, Value: tags.Serialize(proxyTags)},
}}
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/xds/envoy/listeners/v3/fault_injection_configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
"github.com/kumahq/kuma/pkg/util/proto"
envoy_routes "github.com/kumahq/kuma/pkg/xds/envoy/routes/v3"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
)

Expand Down Expand Up @@ -70,7 +69,7 @@ func createHeaders(selectors []mesh_proto.SingleValueTagSet) *envoy_route.Header
regexOR := tags.RegexOR(selectorRegexs...)

return &envoy_route.HeaderMatcher{
Name: envoy_routes.TagsHeaderName,
Name: tags.TagsHeaderName,
HeaderMatchSpecifier: &envoy_route.HeaderMatcher_SafeRegexMatch{
SafeRegexMatch: &envoy_type_matcher.RegexMatcher{
EngineType: &envoy_type_matcher.RegexMatcher_GoogleRe2{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
util_proto "github.com/kumahq/kuma/pkg/util/proto"
envoy_common "github.com/kumahq/kuma/pkg/xds/envoy"
. "github.com/kumahq/kuma/pkg/xds/envoy/listeners"
v3 "github.com/kumahq/kuma/pkg/xds/envoy/routes/v3"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
)

Expand All @@ -35,7 +34,7 @@ var _ = Describe("HttpInboundRouteConfigurer", func() {
}
regexOR := tags.RegexOR(selectorRegexs...)

route.Match.Headers[v3.TagsHeaderName] = &v1alpha1.TrafficRoute_Http_Match_StringMatcher{
route.Match.Headers[tags.TagsHeaderName] = &v1alpha1.TrafficRoute_Http_Match_StringMatcher{
MatcherType: &v1alpha1.TrafficRoute_Http_Match_StringMatcher_Regex{
Regex: regexOR,
},
Expand Down
8 changes: 6 additions & 2 deletions pkg/xds/envoy/routes/v3/reset_tags_header_configurer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package v3

import envoy_route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
import (
envoy_route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"

"github.com/kumahq/kuma/pkg/xds/envoy/tags"
)

type ResetTagsHeaderConfigurer struct {
}

func (r *ResetTagsHeaderConfigurer) Configure(rc *envoy_route.RouteConfiguration) error {
rc.RequestHeadersToRemove = append(rc.RequestHeadersToRemove, TagsHeaderName)
rc.RequestHeadersToRemove = append(rc.RequestHeadersToRemove, tags.TagsHeaderName)
return nil
}
4 changes: 1 addition & 3 deletions pkg/xds/envoy/routes/v3/tags_header_configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
)

const TagsHeaderName = "x-kuma-tags"

type TagsHeaderConfigurer struct {
Tags mesh_proto.MultiValueTagSet
}
Expand All @@ -19,7 +17,7 @@ func (t *TagsHeaderConfigurer) Configure(rc *envoy_route.RouteConfiguration) err
return nil
}
rc.RequestHeadersToAdd = append(rc.RequestHeadersToAdd, &envoy_core.HeaderValueOption{
Header: &envoy_core.HeaderValue{Key: TagsHeaderName, Value: tags.Serialize(t.Tags)},
Header: &envoy_core.HeaderValue{Key: tags.TagsHeaderName, Value: tags.Serialize(t.Tags)},
})
return nil
}
2 changes: 2 additions & 0 deletions pkg/xds/envoy/tags/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
core_policy "github.com/kumahq/kuma/pkg/core/policy"
)

const TagsHeaderName = "x-kuma-tags"

type Tags map[string]string

func (t Tags) WithoutTags(tags ...string) Tags {
Expand Down
3 changes: 1 addition & 2 deletions pkg/xds/generator/egress/external_services_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
envoy_clusters "github.com/kumahq/kuma/pkg/xds/envoy/clusters"
envoy_listeners "github.com/kumahq/kuma/pkg/xds/envoy/listeners"
envoy_names "github.com/kumahq/kuma/pkg/xds/envoy/names"
envoy_routes "github.com/kumahq/kuma/pkg/xds/envoy/routes/v3"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
"github.com/kumahq/kuma/pkg/xds/envoy/tls"
)
Expand Down Expand Up @@ -199,7 +198,7 @@ func (g *ExternalServicesGenerator) addFilterChains(

routes = append(routes, envoy_common.NewRoute(
envoy_common.WithCluster(cluster),
envoy_common.WithMatchHeaderRegex(envoy_routes.TagsHeaderName, tags.MatchSourceRegex(rl)),
envoy_common.WithMatchHeaderRegex(tags.TagsHeaderName, tags.MatchSourceRegex(rl)),
envoy_common.WithRateLimit(rl.Spec),
))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/xds/generator/inbound_proxy_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
envoy_clusters "github.com/kumahq/kuma/pkg/xds/envoy/clusters"
envoy_listeners "github.com/kumahq/kuma/pkg/xds/envoy/listeners"
envoy_names "github.com/kumahq/kuma/pkg/xds/envoy/names"
envoy_routes "github.com/kumahq/kuma/pkg/xds/envoy/routes/v3"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
xds_tls "github.com/kumahq/kuma/pkg/xds/envoy/tls"
)
Expand Down Expand Up @@ -80,7 +79,7 @@ func (g InboundProxyGenerator) Generate(ctx xds_context.Context, proxy *core_xds

routes = append(routes, envoy_common.NewRoute(
envoy_common.WithCluster(cluster),
envoy_common.WithMatchHeaderRegex(envoy_routes.TagsHeaderName, tags.MatchSourceRegex(rl)),
envoy_common.WithMatchHeaderRegex(tags.TagsHeaderName, tags.MatchSourceRegex(rl)),
envoy_common.WithRateLimit(rl.Spec),
))
}
Expand Down

0 comments on commit 9648bc6

Please sign in to comment.