Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
fix: stable filter name (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean authored Sep 26, 2024
1 parent 2dd5306 commit fade8af
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions kontrol-service/engine/flow/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package flow

import (
"fmt"
"hash/fnv"
"regexp"
"slices"
"strings"

Expand Down Expand Up @@ -434,8 +434,11 @@ func getIngresses(
newPaths = append(newPaths, path)

// Set Envoy FIlter for the service
luaFilterFrontend := generateDynamicLuaScript(allServices, activeFlowID, namespace, hostnames)
inboundFilter := getInboundFilter(target.ServiceID, namespace, -1, &target.Version, &luaFilterFrontend)
filter := &externalInboudFilter{
filter: generateDynamicLuaScript(allServices, activeFlowID, namespace, hostnames),
name: strings.Join(hostnames, "-"),
}
inboundFilter := getInboundFilter(target.ServiceID, namespace, -1, &target.Version, filter)
logrus.Debugf("Adding inbound filter to setup routing table for flow '%s' on service '%s', version '%s'", activeFlowID, target.ServiceID, target.Version)
filters = append(filters, inboundFilter)
}
Expand Down Expand Up @@ -497,8 +500,11 @@ func getHTTPRoutes(

hostnames := lo.Map(routeSpec.Hostnames, func(item gateway.Hostname, _ int) string { return string(item) })
// Set Envoy FIlter for the service
luaFilterFrontend := generateDynamicLuaScript(allServices, activeFlowID, namespace, hostnames)
inboundFilter := getInboundFilter(target.ServiceID, namespace, -1, &target.Version, &luaFilterFrontend)
filter := &externalInboudFilter{
filter: generateDynamicLuaScript(allServices, activeFlowID, namespace, hostnames),
name: strings.Join(hostnames, "-"),
}
inboundFilter := getInboundFilter(target.ServiceID, namespace, -1, &target.Version, filter)
logrus.Debugf("Adding inbound filter to setup routing table for flow '%s' on service '%s', version '%s'", activeFlowID, target.ServiceID, target.Version)
filters = append(filters, inboundFilter)
}
Expand Down Expand Up @@ -540,7 +546,6 @@ func getEnvoyFilters(
targetServices []string,
) []istioclient.EnvoyFilter {
filters := []istioclient.EnvoyFilter{}
traceIdEnforcerLuaScript := generateTraceIDEnforcerLuaScript()

// HttpRoute (workload) are applied at the serviceID level, not the serviceID-version level
groupedServices := lo.GroupBy(allServices, func(item *resolved.Service) string { return item.ServiceID })
Expand All @@ -562,11 +567,11 @@ func getEnvoyFilters(
// more inbound EnvoyFilters for routing routing traffic on frontend services are added by the getHTTPRoutes function
if isTargertService {
logrus.Debugf("Adding inbound filter to enforce trace IDs for service '%s'", serviceID)
inboundFilter := getInboundFilter(serviceID, namespace, 0, nil, &traceIdEnforcerLuaScript)
inboundFilter := getInboundFilter(serviceID, namespace, 0, nil, &traceIdEnforcer{})
filters = append(filters, inboundFilter)
} else {
logrus.Debugf("Adding inbound filter for inner service '%s'", serviceID)
inboundFilter := getInboundFilter(serviceID, namespace, 0, nil, nil)
inboundFilter := getInboundFilter(serviceID, namespace, 0, nil, &innerInboundFilter{})
filters = append(filters, inboundFilter)
}

Expand All @@ -577,28 +582,58 @@ func getEnvoyFilters(
return filters
}

func getInboundFilter(serviceID, namespace string, priority int32, versionSelector, luaFilterFrontend *string) istioclient.EnvoyFilter {
type luaFilter interface {
getName() string
getFilter() string
}

type innerInboundFilter struct{}

type externalInboudFilter struct {
filter string
name string
}

type traceIdEnforcer struct{}

func (f *innerInboundFilter) getName() string {
return "inbound-router"
}

func (f *innerInboundFilter) getFilter() string {
return inboundRequestTraceIDFilter
}

func (f *externalInboudFilter) getName() string {
id := regexp.MustCompile(`[^a-z0-9.-]`).ReplaceAllString(f.name, "")
return "inbound-router-" + id + "-external"
}

func (f *externalInboudFilter) getFilter() string {
return f.filter
}

func (f *traceIdEnforcer) getName() string {
return "trace-id-enforcer"
}

func (f *traceIdEnforcer) getFilter() string {
return generateTraceIDEnforcerLuaScript()
}

func getInboundFilter(serviceID, namespace string, priority int32, versionSelector *string, luaFilter luaFilter) istioclient.EnvoyFilter {
labelSelector := map[string]string{
"app": serviceID,
}
if versionSelector != nil {
labelSelector["version"] = *versionSelector
}

var filterHashStr *string
luaFilter := inboundRequestTraceIDFilter
if luaFilterFrontend != nil {
luaFilter = *luaFilterFrontend

hash := fnv.New32a()
hash.Write([]byte(*luaFilterFrontend))
hashStr := fmt.Sprintf("%d", hash.Sum32())
filterHashStr = &hashStr

if luaFilter == nil {
luaFilter = &innerInboundFilter{}
}

idStr := "inbound-router"
ids := []*string{&serviceID, &idStr, versionSelector, filterHashStr}
filterName := luaFilter.getName()
ids := []*string{&serviceID, versionSelector, &filterName}
name := strings.Join(lo.FilterMap(ids, func(id *string, _ int) (string, bool) {
if id != nil {
return *id, true
Expand Down Expand Up @@ -646,7 +681,7 @@ func getInboundFilter(serviceID, namespace string, priority int32, versionSelect
StructValue: &structpb.Struct{
Fields: map[string]*structpb.Value{
"@type": {Kind: &structpb.Value_StringValue{StringValue: luaFilterType}},
"inlineCode": {Kind: &structpb.Value_StringValue{StringValue: luaFilter}},
"inlineCode": {Kind: &structpb.Value_StringValue{StringValue: luaFilter.getFilter()}},
},
},
},
Expand Down

0 comments on commit fade8af

Please sign in to comment.