Merge pull request #126 from jkaninda/develop
fix: issue of extra configs as configMap: duplicated routes and middle…
This commit is contained in:
@@ -19,12 +19,14 @@ package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/goma-gateway/pkg/logger"
|
||||
"github.com/jkaninda/goma-gateway/util"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// CheckConfig checks configs
|
||||
func CheckConfig(fileName string) error {
|
||||
if !util.FileExists(fileName) {
|
||||
return fmt.Errorf("config file not found: %s", fileName)
|
||||
@@ -81,6 +83,7 @@ func CheckConfig(fileName string) error {
|
||||
|
||||
}
|
||||
|
||||
// checkRoutes checks routes
|
||||
func checkRoutes(routes []Route, middlewares []Middleware) {
|
||||
midNames := middlewareNames(middlewares)
|
||||
for index, route := range routes {
|
||||
@@ -106,6 +109,43 @@ func checkRoutes(routes []Route, middlewares []Middleware) {
|
||||
}
|
||||
}
|
||||
|
||||
// checkConfig checks configurations and returns error
|
||||
func checkConfig(routes []Route, middlewares []Middleware) error {
|
||||
logger.Info("Checking configurations...")
|
||||
midNames := middlewareNames(middlewares)
|
||||
for index, route := range routes {
|
||||
if len(route.Name) == 0 {
|
||||
logger.Warn("Warning: route name is empty, index: [%d]", index)
|
||||
}
|
||||
if route.Destination == "" && len(route.Backends) == 0 {
|
||||
return fmt.Errorf("Error: no destination or backends specified for route: %s | index: [%d] \n", route.Name, index)
|
||||
}
|
||||
// checking middleware applied to routes
|
||||
for _, middleware := range route.Middlewares {
|
||||
if !slices.Contains(midNames, middleware) {
|
||||
logger.Warn("Couldn't find a middleware with the name: %s | route: %s", middleware, route.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// find duplicated middleware name
|
||||
duplicates := findDuplicateMiddlewareNames(dynamicMiddlewares)
|
||||
if len(duplicates) != 0 {
|
||||
for _, duplicate := range duplicates {
|
||||
return fmt.Errorf("duplicated middleware name: %s, the name of the middleware should be unique", duplicate)
|
||||
}
|
||||
}
|
||||
// find duplicated route name
|
||||
duplicates = findDuplicateRouteNames(dynamicRoutes)
|
||||
if len(duplicates) != 0 {
|
||||
for _, duplicate := range duplicates {
|
||||
logger.Warn("Duplicated route name was found: %s ", duplicate)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// middlewareNames reruns middleware names
|
||||
func middlewareNames(middlewares []Middleware) []string {
|
||||
names := []string{}
|
||||
for _, mid := range middlewares {
|
||||
|
||||
@@ -32,6 +32,10 @@ func loadExtraFiles(routePath string) ([]string, error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Skip hidden folders
|
||||
if info.IsDir() && info.Name()[0] == '.' {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
// Check for .yaml or .yml file extension
|
||||
if !info.IsDir() && (filepath.Ext(path) == ".yaml" || filepath.Ext(path) == ".yml") {
|
||||
yamlFiles = append(yamlFiles, path)
|
||||
@@ -40,7 +44,7 @@ func loadExtraFiles(routePath string) ([]string, error) {
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading extra route files: %v", err)
|
||||
return nil, fmt.Errorf("error loading extra config files: %v", err)
|
||||
}
|
||||
return yamlFiles, nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func getMiddleware(rules []string, middlewares []Middleware) (Middleware, error)
|
||||
continue
|
||||
}
|
||||
|
||||
return Middleware{}, errors.New("middlewares not found with name: [" + strings.Join(rules, ";") + "]")
|
||||
return Middleware{}, errors.New("middleware not found with name: [" + strings.Join(rules, ";") + "]")
|
||||
}
|
||||
|
||||
func doesExist(tyName string) bool {
|
||||
|
||||
@@ -49,6 +49,7 @@ func loadExtraRoutes(routePath string) ([]Route, error) {
|
||||
return extraRoutes, nil
|
||||
}
|
||||
|
||||
// findDuplicateRouteNames finds duplicated route names
|
||||
func findDuplicateRouteNames(routes []Route) []string {
|
||||
// Create a map to track occurrences of names
|
||||
nameMap := make(map[string]int)
|
||||
|
||||
@@ -56,20 +56,10 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
||||
logger.Info("Loaded %d additional routes", len(extraRoutes))
|
||||
|
||||
}
|
||||
|
||||
// find duplicated middleware name
|
||||
duplicates := findDuplicateMiddlewareNames(dynamicMiddlewares)
|
||||
if len(duplicates) != 0 {
|
||||
for _, duplicate := range duplicates {
|
||||
logger.Fatal("Duplicated middleware name: %s, the name of the middleware should be unique.", duplicate)
|
||||
}
|
||||
}
|
||||
// find duplicated route name
|
||||
duplicates = findDuplicateRouteNames(dynamicRoutes)
|
||||
if len(duplicates) != 0 {
|
||||
for _, duplicate := range duplicates {
|
||||
logger.Error("Duplicated route name was found: %s ", duplicate)
|
||||
}
|
||||
// Check configs
|
||||
err = checkConfig(dynamicRoutes, dynamicMiddlewares)
|
||||
if err != nil {
|
||||
logger.Fatal("Error: %v", err)
|
||||
}
|
||||
m := dynamicMiddlewares
|
||||
redisBased := false
|
||||
|
||||
Reference in New Issue
Block a user