Skip to content

Commit

Permalink
refactor to address comments around how to print warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Sep 21, 2022
1 parent 93314bd commit abcdf77
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
22 changes: 18 additions & 4 deletions file/builder.go
Expand Up @@ -602,13 +602,31 @@ func (b *stateBuilder) routes() {
return
}

unsupportedRoutes := []string{}
for _, r := range b.targetContent.Routes {
r := r
if err := b.ingestRoute(r); err != nil {
b.err = err
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() {
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 26 additions & 18 deletions utils/utils.go
Expand Up @@ -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 <config-file> --output-file <new-config-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")
}
8 changes: 7 additions & 1 deletion validate/validate.go
Expand Up @@ -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()
}
}

0 comments on commit abcdf77

Please sign in to comment.