Skip to content

Commit

Permalink
feat: add wasm filter-chain entity support
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm authored and flrgh committed May 14, 2024
1 parent 34f576e commit b6dbadd
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
Binary file added assets/filters/example-filter.wasm
Binary file not shown.
62 changes: 62 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
}
}

dumpConfig.IsFilterChainsSupported, err = determineFilterChainSupport(ctx, kongClient)
if err != nil {
return err
}

dumpConfig.SelectorTags, err = determineSelectorTag(*targetContent, dumpConfig)
if err != nil {
return err
Expand Down Expand Up @@ -318,6 +323,9 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
if err := checkForRBACResources(*rawState, dumpConfig.RBACResourcesOnly); err != nil {
return err
}
if err := checkFilterChainsAllowed(*rawState, dumpConfig, parsedKongVersion); err != nil {
return err
}
targetState, err := state.Get(rawState)
if err != nil {
return err
Expand Down Expand Up @@ -408,6 +416,59 @@ func determineSelectorTag(targetContent file.Content, config dump.Config) ([]str
return config.SelectorTags, nil
}

func determineFilterChainSupport(ctx context.Context, client *kong.Client) (bool, error) {
data, err := client.Root(ctx)
if err != nil {
return false, fmt.Errorf("failed to fetch gateway configuration: %w", err)
}

conf, ok := data["configuration"]
if !ok {
return false, errors.New("failed to extract gateway configuration")
}

confmap, ok := conf.(map[string]interface{})
if !ok {
return false, errors.New("failed to extract gateway configuration")
}

wasm, ok := confmap["wasm"]
// probably just an old version of Kong
if !ok {
return false, nil
}

var supported bool
supported, ok = wasm.(bool)
if !ok {
return false, errors.New("could not determine if wasm is enabled on this gateway node")
}

return supported, nil
}

func checkFilterChainsAllowed(
content reconcilerUtils.KongRawState,
dumpConfig dump.Config,
version semver.Version,
) error {
if len(content.FilterChains) < 1 {
return nil
} else if reconcilerUtils.Kong340Version.GT(version) {
return errors.New(
"one or more Wasm Filter Chain entities were found, but the target " +
"Kong Gateway node does not support Wasm.\n" +
"Wasm Filter Chain support requires Kong >= 3.4",
)
} else if !dumpConfig.IsFilterChainsSupported {
return errors.New("one or more Wasm Filter Chain entities were found, " +
"but Wasm is not enabled on the target Kong Gateway node",
)
}

return nil
}

func fetchCurrentState(ctx context.Context, client *kong.Client, dumpConfig dump.Config) (*state.KongState, error) {
rawState, err := dump.Get(ctx, client, dumpConfig)
if err != nil {
Expand Down Expand Up @@ -528,6 +589,7 @@ func containsProxyConfiguration(content reconcilerUtils.KongRawState) bool {
return len(content.Services) != 0 ||
len(content.Routes) != 0 ||
len(content.Plugins) != 0 ||
len(content.FilterChains) != 0 ||
len(content.Upstreams) != 0 ||
len(content.Certificates) != 0 ||
len(content.CACertificates) != 0 ||
Expand Down
5 changes: 5 additions & 0 deletions cmd/gateway_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func executeDump(cmd *cobra.Command, _ []string) error {
return err
}

dumpConfig.IsFilterChainsSupported, err = determineFilterChainSupport(ctx, wsClient)
if err != nil {
return err
}

format := file.Format(strings.ToUpper(dumpCmdStateFormat))

kongVersion, err := fetchKongVersion(ctx, rootConfig.ForWorkspace(dumpWorkspace))
Expand Down
3 changes: 3 additions & 0 deletions cmd/gateway_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,8 @@ func ensureGetAllMethods() error {
if _, err := reconcilerUtils.CallGetAll(dummyEmptyState.RBACRoles); err != nil {
return err
}
if _, err := reconcilerUtils.CallGetAll(dummyEmptyState.FilterChains); err != nil {
return err
}
return nil
}
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ go 1.21.6

replace github.com/yudai/gojsondiff v1.0.0 => github.com/Kong/gojsondiff v1.3.0

// TODO: remove me
replace github.com/kong/go-database-reconciler v1.10.0 => github.com/kong/go-database-reconciler v0.0.0-20240513201015-0cc539e9aad8

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/blang/semver/v4 v4.0.0
Expand All @@ -22,7 +25,7 @@ require (
k8s.io/api v0.29.3
k8s.io/apiextensions-apiserver v0.29.2
k8s.io/apimachinery v0.29.3
k8s.io/code-generator v0.29.3
k8s.io/code-generator v0.29.4
sigs.k8s.io/gateway-api v1.0.0
sigs.k8s.io/yaml v1.4.0
)
Expand All @@ -40,7 +43,7 @@ require (
github.com/pb33f/doctor v0.0.6 // indirect
github.com/pb33f/libopenapi v0.16.1 // indirect
github.com/pb33f/libopenapi-validator v0.0.49 // indirect
github.com/shirou/gopsutil/v3 v3.24.3 // indirect
github.com/shirou/gopsutil/v3 v3.24.4 // indirect
github.com/ssgelm/cookiejarparser v1.0.1 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down Expand Up @@ -131,8 +134,8 @@ require (
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y7
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/kong/go-apiops v0.1.32 h1:wMpn39+RTxEo90GauklBJLFVmQNbAswCUBpoSYewSDA=
github.com/kong/go-apiops v0.1.32/go.mod h1:LBnbLyi2P68VLflekMwNDBiEAHvXQJX2S4OZxSzdMVg=
github.com/kong/go-database-reconciler v1.10.0 h1:502Nn7CTsUNZyClL5bjrhkFrendVrPxDkP4ZGlBfsdg=
github.com/kong/go-database-reconciler v1.10.0/go.mod h1:88/u23NIhkQr7SeTP1p4nLcnWCm8AMCwCF1f7Telw+w=
github.com/kong/go-database-reconciler v0.0.0-20240513201015-0cc539e9aad8 h1:FEkhxU4tO+RVBNuc0SC3zKoOZwDuox4k4KxwsMdtMNE=
github.com/kong/go-database-reconciler v0.0.0-20240513201015-0cc539e9aad8/go.mod h1:TDHIV37v51C4mrRMvKl/EMx1VX9dejYS1hPgxjB7MAo=
github.com/kong/go-kong v0.54.0 h1:HZkZJRREJs/azkgJxMWr2jANQQfg8xXsAiFKu+d1nrs=
github.com/kong/go-kong v0.54.0/go.mod h1:51rSSjgSZKukXgn5nNYbJUx0Et/islqR+LTltwCyjG8=
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
Expand Down Expand Up @@ -304,8 +304,8 @@ github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPO
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shirou/gopsutil/v3 v3.24.3 h1:eoUGJSmdfLzJ3mxIhmOAhgKEKgQkeOwKpz1NbhVnuPE=
github.com/shirou/gopsutil/v3 v3.24.3/go.mod h1:JpND7O217xa72ewWz9zN2eIIkPWsDN/3pl0H8Qt0uwg=
github.com/shirou/gopsutil/v3 v3.24.4 h1:dEHgzZXt4LMNm+oYELpzl9YCqV65Yr/6SfrvgRBtXeU=
github.com/shirou/gopsutil/v3 v3.24.4/go.mod h1:lTd2mdiOspcqLgAnr9/nGi71NkeMpWKdmhuxm9GusH8=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
Expand Down Expand Up @@ -444,16 +444,16 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down Expand Up @@ -519,8 +519,8 @@ k8s.io/apiextensions-apiserver v0.29.2 h1:UK3xB5lOWSnhaCk0RFZ0LUacPZz9RY4wi/yt2I
k8s.io/apiextensions-apiserver v0.29.2/go.mod h1:aLfYjpA5p3OwtqNXQFkhJ56TB+spV8Gc4wfMhUA3/b8=
k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU=
k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU=
k8s.io/code-generator v0.29.3 h1:m7E25/t9R9NvejspO2zBdyu+/Gl0Z5m7dCRc680KS14=
k8s.io/code-generator v0.29.3/go.mod h1:x47ofBhN4gxYFcxeKA1PYXeaPreAGaDN85Y/lNUsPoM=
k8s.io/code-generator v0.29.4 h1:8ESudFNbY5/9BzB8KOEFG2uV9Q0AQxkc4mrQESr30Ks=
k8s.io/code-generator v0.29.4/go.mod h1:7TYnI0dYItL2cKuhhgPSuF3WED9uMdELgbVXFfn/joE=
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 h1:pWEwq4Asjm4vjW7vcsmijwBhOr1/shsbSYiWXmNGlks=
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
Expand Down
3 changes: 3 additions & 0 deletions validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ func (v *Validator) Validate(formatVersion semver.Version) []error {
if err := v.entities(v.state.Upstreams, "upstreams"); err != nil {
allErr = append(allErr, err...)
}
if err := v.entities(v.state.FilterChains, "filter_chains"); err != nil {
allErr = append(allErr, err...)
}

// validate routes format with Kong 3.x
parsed30, err := semver.ParseTolerant(utils.FormatVersion30)
Expand Down

0 comments on commit b6dbadd

Please sign in to comment.