From abcdf7769376f9864858925c951aa31eb89323cb Mon Sep 17 00:00:00 2001 From: Gabriele Gerbino Date: Wed, 21 Sep 2022 23:29:47 +0200 Subject: [PATCH] refactor to address comments around how to print warnings --- file/builder.go | 22 ++++++++++++++++++---- utils/utils.go | 44 ++++++++++++++++++++++++++------------------ validate/validate.go | 8 +++++++- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/file/builder.go b/file/builder.go index edb093ad2..c3b150902 100644 --- a/file/builder.go +++ b/file/builder.go @@ -602,6 +602,7 @@ func (b *stateBuilder) routes() { return } + unsupportedRoutes := []string{} for _, r := range b.targetContent.Routes { r := r if err := b.ingestRoute(r); err != nil { @@ -609,6 +610,23 @@ func (b *stateBuilder) routes() { return } } + + // check routes' paths format + if b.checkRoutePaths { + allRoutes, err := b.intermediate.Routes.GetAll() + if err != nil { + b.err = err + return + } + for _, r := range allRoutes { + if utils.HasPathsWithRegex300AndAbove(r.Route) { + unsupportedRoutes = append(unsupportedRoutes, *r.Route.ID) + } + } + } + if len(unsupportedRoutes) > 0 { + utils.PrintRouteRegexWarning() + } } func (b *stateBuilder) enterprise() { @@ -799,10 +817,6 @@ func (b *stateBuilder) ingestRoute(r FRoute) error { return err } - if b.checkRoutePaths { - utils.CheckRoutePaths300AndAbove(r.Route) - } - // plugins for the route var plugins []FPlugin for _, p := range r.Plugins { diff --git a/utils/utils.go b/utils/utils.go index 05ffe9fb6..4b868dd77 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -190,27 +190,35 @@ func ConfigFilesInDir(dir string) ([]string, error) { return res, nil } -// CheckRoutePaths300AndAbove checks routes' paths format and prints out -// a warning if regex-like patters without the '~' prefix are found. -func CheckRoutePaths300AndAbove(route kong.Route) { - pathsWarnings := []string{} +// HasPathsWithRegex300AndAbove checks routes' paths format and returns true +// if these math a regex-pattern without a '~' prefix. +func HasPathsWithRegex300AndAbove(route kong.Route) bool { for _, p := range route.Paths { if strings.HasPrefix(*p, "~/") || !IsPathRegexLike(*p) { continue } - pathsWarnings = append(pathsWarnings, *p) - } - if len(pathsWarnings) > 0 { - cprint.UpdatePrintf( - "In Route '%s', a path with regular expression was detected.\n"+ - "In Kong Gateway versions 3.0 and above, all paths that use regular expressions \n"+ - "must be prefixed with a ~ character. Without the ~ prefix, regular expression \n"+ - "based paths will not be matched and processed correctly. \n\n"+ - "Please run the following command to upgrade your config: \n\n"+ - "deck convert --from kong-gateway-2.x --to kong-gateway-3.x "+ - "--input-file --output-file \n\n"+ - "Please refer to the following document for further details:\n"+ - "https://docs.konghq.com/deck/latest/3.0-upgrade.\n\n", - *route.ID) + return true } + return false +} + +// PrintRouteRegexWarning prints out a warning about 3.x routes' path usage. +func PrintRouteRegexWarning() { + cprint.UpdatePrintf( + "unsupported routes' paths format with Kong version 3.0\n" + + "or above were detected.\n" + + "Please upgrade your configuration to account for 3.0\n" + + "breaking changes using the following command:\n\n" + + "deck convert --from kong-gateway-2.x --to kong-gateway-3.x\n\n" + + "This command performs the following changes:\n" + + " - upgrade the `_format_version` value to `3.0`\n" + + " - add the `~` prefix to all routes' paths containing a regex-pattern\n\n" + + "These changes may not be correct or exhaustive enough.\n" + + "It is strongly recommended to perform a manual audit\n" + + "of the updated configuration file before applying\n" + + "the configuration in production. Incorrect changes will result in\n" + + "unintended traffic routing by Kong Gateway.\n\n" + + + "For more information about this and related changes,\n" + + "please visit: https://docs.konghq.com/deck/latest/3.0-upgrade\n\n") } diff --git a/validate/validate.go b/validate/validate.go index cc95c91f7..110a6ec8e 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -186,7 +186,13 @@ func (v *Validator) Validate(kongVersion semver.Version) []error { func validate3xRoutes(routes *state.RoutesCollection) { results, _ := routes.GetAll() + unsupportedRoutes := []string{} for _, r := range results { - utils.CheckRoutePaths300AndAbove(r.Route) + if utils.HasPathsWithRegex300AndAbove(r.Route) { + unsupportedRoutes = append(unsupportedRoutes, *r.Route.ID) + } + } + if len(unsupportedRoutes) > 0 { + utils.PrintRouteRegexWarning() } }