Skip to content

Commit

Permalink
add discovery filter config (#46)
Browse files Browse the repository at this point in the history
* initial filter

* filter services

* filter updates

* reformat filter prop description

---------

Co-authored-by: Jason Collins <[email protected]>
  • Loading branch information
dgghinea and jcollins-axway authored Nov 30, 2023
1 parent c1f7fbe commit 1262c18
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions APIGOV-26720-new
Submodule APIGOV-26720-new added at ccc5f3
4 changes: 4 additions & 0 deletions helm/kong-agents/templates/discovery-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ spec:
- name: KONG_SPEC_LOCALPATH
value: /specs
{{- end }}
{{- if .Values.kong.spec.filter }}
- name: KONG_SPEC_FILTER
value: "{{ .Values.kong.spec.filter }}"
{{- end }}
{{- if .Values.kong.admin.auth.apikey.value }}
- name: KONG_ADMIN_AUTH_APIKEY_VALUE
valueFrom:
Expand Down
1 change: 1 addition & 0 deletions helm/kong-agents/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ kong:
http: 8000
https: 8443
spec:
filter:
urlPaths: []
localPath:

Expand Down
7 changes: 4 additions & 3 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package common

const (
AttrServiceID = "serviceID"
AttrRouteID = "routeID"
AttrChecksum = "checksum"
AttrAppID = "kongApplicationID"
AttrServiceName = "serviceName"
AttrRouteName = "routeName"
AttrRouteID = "routeID"
AttrServiceTag = "serviceTag"
AttrChecksum = "checksum"
AttrAppID = "kongApplicationId"

AttrCredentialID = "kongCredentialID"
AttrCredUpdater = "kongCredentialUpdate"
Expand Down
8 changes: 6 additions & 2 deletions pkg/config/discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ const (
cfgKongProxyPortHttps = "kong.proxy.port.https"
cfgKongSpecURLPaths = "kong.spec.urlPaths"
cfgKongSpecLocalPath = "kong.spec.localPath"
cfgKongSpecFilter = "kong.spec.filter"
)

func AddKongProperties(rootProps properties.Properties) {
rootProps.AddStringProperty(cfgKongAdminURL, "", "The Kong admin endpoint")
rootProps.AddStringProperty(cfgKongAdminAPIKey, "", "API Key value to authenticate with Kong Gateway")
rootProps.AddStringProperty(cfgKongAdminAPIKeyHeader, "", "API Key header to authenticate with Kong Gateway")
rootProps.AddStringProperty(cfgKongProxyHost, "", "The Kong proxy endpoint")
rootProps.AddIntProperty(cfgKongProxyPortHttp, 80, "The Kong proxy http port")
rootProps.AddIntProperty(cfgKongProxyPortHttps, 443, "The Kong proxy https port")
rootProps.AddIntProperty(cfgKongProxyPortHttp, 0, "The Kong proxy http port")
rootProps.AddIntProperty(cfgKongProxyPortHttps, 0, "The Kong proxy https port")
rootProps.AddStringSliceProperty(cfgKongSpecURLPaths, []string{}, "URL paths that the agent will look in for spec files")
rootProps.AddStringProperty(cfgKongSpecLocalPath, "", "Local paths where the agent will look for spec files")
rootProps.AddStringProperty(cfgKongSpecFilter, "", "SDK Filter format. Empty means filters are ignored.")
}

// AgentConfig - represents the config for agent
Expand Down Expand Up @@ -63,6 +65,7 @@ type KongSpecConfig struct {
URLPaths []string `config:"urlPaths"`
LocalPath string `config:"localPath"`
DevPortalEnabled bool `config:"devPortalEnabled"`
Filter string `config:"filter"`
}

// KongGatewayConfig - represents the config for gateway
Expand Down Expand Up @@ -109,6 +112,7 @@ func ParseProperties(rootProps properties.Properties) *KongGatewayConfig {
Spec: KongSpecConfig{
URLPaths: rootProps.StringSlicePropertyValue(cfgKongSpecURLPaths),
LocalPath: rootProps.StringPropertyValue(cfgKongSpecLocalPath),
Filter: rootProps.StringPropertyValue(cfgKongSpecFilter),
},
}
}
20 changes: 20 additions & 0 deletions pkg/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

"github.com/Axway/agent-sdk/pkg/apic/provisioning"
"github.com/Axway/agent-sdk/pkg/filter"
"github.com/Axway/agents-kong/pkg/common"
"github.com/Axway/agents-kong/pkg/subscription"

Expand Down Expand Up @@ -43,6 +44,11 @@ func NewClient(agentConfig config.AgentConfig) (*Client, error) {
return nil, err
}

discoveryFilter, err := filter.NewFilter(agentConfig.KongGatewayCfg.Spec.Filter)
if err != nil {
return nil, err
}

if err := hasACLEnabledInPlugins(plugins); err != nil {
return nil, err
}
Expand All @@ -57,6 +63,7 @@ func NewClient(agentConfig config.AgentConfig) (*Client, error) {
kongClient: kongClient,
cache: daCache,
mode: common.Marketplace,
filter: discoveryFilter,
}, nil
}

Expand Down Expand Up @@ -92,6 +99,10 @@ func (gc *Client) DiscoverAPIs() error {
func (gc *Client) processKongServicesList(ctx context.Context, services []*klib.Service) {
wg := new(sync.WaitGroup)
for _, service := range services {
if !gc.filter.Evaluate(toTagsMap(service)) {
gc.logger.WithField(common.AttrServiceName, *service.Name).Info("Service not passing tag filters. Skipping discovery for this service.")
continue
}
wg.Add(1)
go func(service *klib.Service, wg *sync.WaitGroup) {
defer wg.Done()
Expand All @@ -104,6 +115,15 @@ func (gc *Client) processKongServicesList(ctx context.Context, services []*klib.
wg.Wait()
}

func toTagsMap(service *klib.Service) map[string]string {
// The SDK currently only supports map[string]string format.
filters := make(map[string]string)
for i, t := range service.Tags {
filters[fmt.Sprintf("t%d", i)] = *t
}
return filters
}

func (gc *Client) processSingleKongService(ctx context.Context, service *klib.Service) error {
log := gc.logger.WithField(common.AttrServiceName, *service.Name)
log.Info("processing service")
Expand Down
2 changes: 2 additions & 0 deletions pkg/gateway/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/Axway/agent-sdk/pkg/apic"
"github.com/Axway/agent-sdk/pkg/cache"
corecfg "github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/filter"
"github.com/Axway/agent-sdk/pkg/util/log"

config "github.com/Axway/agents-kong/pkg/config/discovery"
Expand All @@ -18,6 +19,7 @@ type Client struct {
plugins kong.Plugins
cache cache.Cache
mode string
filter filter.Filter
}

type KongAPI struct {
Expand Down

0 comments on commit 1262c18

Please sign in to comment.