Skip to content

Commit

Permalink
Fix adapter revive failure
Browse files Browse the repository at this point in the history
  • Loading branch information
pubudu538 committed Feb 11, 2024
1 parent a00fe41 commit a3430e4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
7 changes: 5 additions & 2 deletions adapter/internal/discovery/xds/semantic_versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,14 @@ func getRoutesForAPIIdentifier(organizationID, apiIdentifier string) []*routev3.
func isSemanticVersioningEnabled(apiName, apiVersion string) bool {

conf := config.ReadConfigs()
apiSemVersion, err := semantic_version.ValidateAndGetVersionComponents(apiVersion, apiName)
if !conf.Envoy.EnableIntelligentRouting {
return false
}

apiSemVersion, err := semantic_version.ValidateAndGetVersionComponents(apiVersion, apiName)
if err != nil && apiSemVersion == nil {
return false
}

return conf.Envoy.EnableIntelligentRouting
return true
}
10 changes: 3 additions & 7 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,7 @@ func createRoutes(params *routeCreateParams) (routes []*routev3.Route, err error
resourcePath = removeFirstOccurrence(resource.GetPath(), "/"+version)
}

conf := config.ReadConfigs()
if conf.Envoy.EnableIntelligentRouting && strings.HasPrefix(version, "v") {
if pathMatchType != gwapiv1b1.PathMatchExact {
resourcePath = strings.Replace(resourcePath, basePath, regexp.QuoteMeta(basePath), 1)
}
routePath := generateRoutePath(resourcePath, pathMatchType)
Expand Down Expand Up @@ -1213,10 +1212,7 @@ func CreateAPIDefinitionEndpoint(adapterInternalAPI *model.AdapterInternalAPI, v
matchPath = basePathWithoutVersion + endpoint
}

conf := config.ReadConfigs()
if conf.Envoy.EnableIntelligentRouting && strings.HasPrefix(version, "v") {
matchPath = strings.Replace(matchPath, basePath, regexp.QuoteMeta(basePath), 1)
}
matchPath = strings.Replace(matchPath, basePath, regexp.QuoteMeta(basePath), 1)
routePath := generateRoutePath(matchPath, gwapiv1b1.PathMatchRegularExpression)

match = &routev3.RouteMatch{
Expand Down Expand Up @@ -1390,7 +1386,7 @@ func generateRoutePath(resourcePath string, pathMatchType gwapiv1b1.PathMatchTyp
case gwapiv1b1.PathMatchPathPrefix:
fallthrough
default:
return fmt.Sprintf("^%s((?:/.*)*)", regexp.QuoteMeta(newPath))
return fmt.Sprintf("^%s((?:/.*)*)", newPath)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestCreateRoutesWithClustersWithExactAndRegularExpressionRules(t *testing.T

assert.Equal(t, 3, len(routes), "Created number of routes are incorrect.")
assert.Contains(t, []string{"^/test-api/2\\.0\\.0/exact-path-api/2\\.0\\.0/\\(\\.\\*\\)/exact-path([/]{0,1})"}, routes[1].GetMatch().GetSafeRegex().Regex)
assert.Contains(t, []string{"^/test-api/2.0.0/regex-path/2.0.0/userId/([^/]+)/orderId/([^/]+)([/]{0,1})"}, routes[2].GetMatch().GetSafeRegex().Regex)
assert.Contains(t, []string{"^/test-api/2\\.0\\.0/regex-path/2.0.0/userId/([^/]+)/orderId/([^/]+)([/]{0,1})"}, routes[2].GetMatch().GetSafeRegex().Regex)
assert.NotEqual(t, routes[1].GetMatch().GetSafeRegex().Regex, routes[2].GetMatch().GetSafeRegex().Regex,
"The route regex for the two paths should not be the same")
}
Expand Down
41 changes: 19 additions & 22 deletions adapter/pkg/semanticversion/semantic_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ValidateAndGetVersionComponents(version string, apiName string) (*SemVersio
versionComponents := strings.Split(version, ".")

// If the versionComponents length is less than 2, return error
if len(versionComponents) < 2 {
if len(versionComponents) < 2 || !strings.HasPrefix(versionComponents[0], "v") {
logger.LoggerSemanticVersion.Errorf("API version validation failed for API: %v. API Version: %v", apiName, version)
errMessage := "Invalid version: " + version + " for API: " + apiName +
". API version should be in the format x.y.z, x.y, vx.y.z or vx.y where x,y,z are non-negative integers" +
Expand Down Expand Up @@ -85,28 +85,25 @@ func ValidateAndGetVersionComponents(version string, apiName string) (*SemVersio
// Compare - compares two semantic versions and returns true
// if `version` is greater or equal than `baseVersion`
func (baseVersion SemVersion) Compare(version SemVersion) bool {
if baseVersion.Major < version.Major {
return true
} else if baseVersion.Major > version.Major {
// Compare major version
if baseVersion.Major != version.Major {
return baseVersion.Major < version.Major
}

// Compare minor version
if baseVersion.Minor != version.Minor {
return baseVersion.Minor < version.Minor
}

// Compare patch version
if baseVersion.Patch != nil && version.Patch != nil {
return *baseVersion.Patch < *version.Patch
} else if baseVersion.Patch != nil {
return false
} else {
if baseVersion.Minor < version.Minor {
return true
} else if baseVersion.Minor > version.Minor {
return false
} else {
if baseVersion.Patch != nil && version.Patch != nil {
if *baseVersion.Patch < *version.Patch {
return true
} else if *baseVersion.Patch > *version.Patch {
return false
}
} else if baseVersion.Patch == nil && version.Patch != nil {
return true
} else if baseVersion.Patch != nil && version.Patch == nil {
return false
}
}
} else if version.Patch != nil {
return true
}

// Versions are equal
return true
}

0 comments on commit a3430e4

Please sign in to comment.