Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update Route's priority type to uint64 #378

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Table of Contents

- [v0.48.0](#v0480)
- [v0.47.0](#v0470)
- [v0.46.0](#v0460)
- [v0.45.0](#v0450)
Expand Down Expand Up @@ -59,9 +60,13 @@
- [0.2.0](#020)
- [0.1.0](#010)

## Unreleased
## [v0.48.0]

- Fix a bug preventing users to set fields to emtpy arrays when
> Release date: 2023/10/30

- `Route`'s `priority` field type is changed from `int` to `uint64`.
[#378](https://github.com/Kong/go-kong/pull/378)
- Fix a bug preventing users to set fields to empty arrays when
a default for those fields exist.
[#374](https://github.com/Kong/go-kong/pull/374)

Expand Down
2 changes: 1 addition & 1 deletion kong/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Route struct {
Paths []*string `json:"paths,omitempty" yaml:"paths,omitempty"`
PathHandling *string `json:"path_handling,omitempty" yaml:"path_handling,omitempty"`
PreserveHost *bool `json:"preserve_host,omitempty" yaml:"preserve_host,omitempty"`
Priority *int `json:"priority,omitempty" yaml:"priority,omitempty"`
Priority *uint64 `json:"priority,omitempty" yaml:"priority,omitempty"`
Protocols []*string `json:"protocols,omitempty" yaml:"protocols,omitempty"`
RegexPriority *int `json:"regex_priority,omitempty" yaml:"regex_priority,omitempty"`
Service *Service `json:"service,omitempty" yaml:"service,omitempty"`
Expand Down
73 changes: 73 additions & 0 deletions kong/route_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,71 @@ func TestRouteWithTags(T *testing.T) {
require.NoError(err)
}

func TestCreateExpressionRoutes(T *testing.T) {
RunWhenDBMode(T, "postgres")
RunWhenKongRouterFlavor(T, Expressions)

client, err := NewTestClient(nil, nil)
require.NoError(T, err)
require.NotNil(T, client)

for _, tc := range []struct {
name string
route *Route
valid bool
assert func(t *testing.T, route *Route)
}{
{
name: "route with expression and high priority",
route: &Route{
Expression: String(`http.path == "/"`),
Priority: Uint64(33820977671045),
},
valid: true,
assert: func(t *testing.T, route *Route) {
assert.Equal(t, `http.path == "/"`, *route.Expression)
assert.Equal(t, uint64(33820977671045), *route.Priority)
},
},
{
name: "route with expression and priority of 1",
route: &Route{
Expression: String(`http.path == "/"`),
Priority: Uint64(1),
},
valid: true,
assert: func(t *testing.T, route *Route) {
assert.Equal(t, `http.path == "/"`, *route.Expression)
assert.Equal(t, uint64(1), *route.Priority)
},
},
// TODO: this fails now because Gateway returns priority in scientific notation:
// failed decoding response body: json: cannot unmarshal number 3.3820977671045e+15 into Go struct field Route.priority of type int64
// Ref: https://konghq.atlassian.net/browse/FTI-5515
// {
// route: &Route{
// Expression: String(`lower(http.path) ^= "/"`),
// Priority: Int64(3382097767104500),
// },
// valid: true,
// },
} {
T.Run(tc.name, func(T *testing.T) {
createdRoute, err := client.Routes.Create(defaultCtx, tc.route)
if tc.valid {
assert.NoError(T, err)
require.NotNil(T, createdRoute)
T.Cleanup(func() {
assert.NoError(T, client.Routes.Delete(defaultCtx, createdRoute.ID))
})
tc.assert(T, createdRoute)
} else {
assert.Error(T, err)
}
})
}
}

func TestCreateInRoute(T *testing.T) {
RunWhenDBMode(T, "postgres")
SkipWhenKongRouterFlavor(T, Expressions)
Expand Down Expand Up @@ -349,6 +414,14 @@ func TestRoutesValidationExpressions(T *testing.T) {
},
valid: true,
},
{
name: "valid expression with priority",
route: &Route{
Expression: String(`lower(http.path) ^= "/prefix/"`),
Priority: Uint64(3382097767104500),
},
valid: true,
},
} {
T.Run(tC.name, func(t *testing.T) {
ok, msg, err := client.Routes.Validate(defaultCtx, tC.route)
Expand Down
12 changes: 12 additions & 0 deletions kong/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ func SkipWhenKongRouterFlavor(t *testing.T, flavor ...RouterFlavor) {
}
}

func RunWhenKongRouterFlavor(t *testing.T, flavor RouterFlavor) {
t.Helper()

routerFlavor, err := getKongConfigValue(t, "router_flavor")
if err != nil {
t.Skip(err.Error())
}
if RouterFlavor(routerFlavor) != flavor {
t.Skipf("router flavor:%q, expecting %q, skipping", routerFlavor, flavor)
}
}

func getKongConfigValue(t *testing.T, key string) (string, error) {
t.Helper()
client, err := NewTestClient(nil, nil)
Expand Down
5 changes: 5 additions & 0 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func Int(i int) *int {
return &i
}

// Uint64 returns a pointer to i.
func Uint64(i uint64) *uint64 {
return &i
}

// Float64 returns a pointer to f.
func Float64(f float64) *float64 {
return &f
Expand Down
2 changes: 1 addition & 1 deletion kong/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.