-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix panic when handling KongConsumer without username
- Loading branch information
Showing
11 changed files
with
276 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package deckgen | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/kong/go-database-reconciler/pkg/file" | ||
) | ||
|
||
type fConsumerByUsernameAndCustomID []file.FConsumer | ||
|
||
func (f fConsumerByUsernameAndCustomID) Len() int { return len(f) } | ||
func (f fConsumerByUsernameAndCustomID) Swap(i, j int) { f[i], f[j] = f[j], f[i] } | ||
func (f fConsumerByUsernameAndCustomID) Less(i, j int) bool { | ||
if f[i].Username == nil && f[j].Username != nil { | ||
return true | ||
} | ||
if f[i].Username != nil && f[j].Username == nil { | ||
return false | ||
} | ||
if f[i].Username != nil && f[j].Username != nil { | ||
switch cmp := strings.Compare(*f[i].Username, *f[j].Username); cmp { | ||
case -1: | ||
return true | ||
case 1: | ||
return false | ||
case 0: | ||
break | ||
} | ||
} | ||
|
||
// Both usernames are equal, compare custom_id. | ||
if f[i].CustomID == nil && f[j].CustomID != nil { | ||
return true | ||
} | ||
if f[i].CustomID != nil && f[j].CustomID == nil { | ||
return false | ||
} | ||
if f[i].CustomID != nil && f[j].CustomID != nil { | ||
switch cmp := strings.Compare(*f[i].CustomID, *f[j].CustomID); cmp { | ||
case -1: | ||
return true | ||
case 1: | ||
return false | ||
case 0: | ||
break | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package deckgen | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/kong/go-database-reconciler/pkg/file" | ||
"github.com/kong/go-kong/kong" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConsumerCmp(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input []file.FConsumer | ||
expected []file.FConsumer | ||
}{ | ||
{ | ||
name: "sort by username", | ||
input: []file.FConsumer{ | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("b"), | ||
}, | ||
}, | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("a"), | ||
}, | ||
}, | ||
}, | ||
expected: []file.FConsumer{ | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("a"), | ||
}, | ||
}, | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("b"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "sort by custom_id", | ||
input: []file.FConsumer{ | ||
{ | ||
Consumer: kong.Consumer{ | ||
CustomID: kong.String("b"), | ||
}, | ||
}, | ||
{ | ||
Consumer: kong.Consumer{ | ||
CustomID: kong.String("a"), | ||
}, | ||
}, | ||
}, | ||
expected: []file.FConsumer{ | ||
{ | ||
Consumer: kong.Consumer{ | ||
CustomID: kong.String("a"), | ||
}, | ||
}, | ||
{ | ||
Consumer: kong.Consumer{ | ||
CustomID: kong.String("b"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "sort by username and custom_id", | ||
input: []file.FConsumer{ | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("b"), | ||
CustomID: kong.String("b"), | ||
}, | ||
}, | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("a"), | ||
CustomID: kong.String("a"), | ||
}, | ||
}, | ||
}, | ||
expected: []file.FConsumer{ | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("a"), | ||
CustomID: kong.String("a"), | ||
}, | ||
}, | ||
{ | ||
Consumer: kong.Consumer{ | ||
Username: kong.String("b"), | ||
CustomID: kong.String("b"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
sort.Sort(fConsumerByUsernameAndCustomID(tc.input)) | ||
assert.Equal(t, tc.expected, tc.input) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
internal/dataplane/testdata/golden/kongconsumer-with-custom-id/default_golden.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
_format_version: "3.0" | ||
consumers: | ||
- custom_id: 1234-1234 | ||
tags: | ||
- k8s-name:consumer | ||
- k8s-namespace:consumer-namespace | ||
- k8s-kind:KongConsumer | ||
- k8s-group:configuration.konghq.com | ||
- k8s-version:v1 |
9 changes: 9 additions & 0 deletions
9
...al/dataplane/testdata/golden/kongconsumer-with-custom-id/expression-routes-on_golden.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
_format_version: "3.0" | ||
consumers: | ||
- custom_id: 1234-1234 | ||
tags: | ||
- k8s-name:consumer | ||
- k8s-namespace:consumer-namespace | ||
- k8s-kind:KongConsumer | ||
- k8s-group:configuration.konghq.com | ||
- k8s-version:v1 |
2 changes: 2 additions & 0 deletions
2
.../dataplane/testdata/golden/kongconsumer-with-custom-id/expression-routes-on_settings.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feature_flags: | ||
ExpressionRoutes: true |
9 changes: 9 additions & 0 deletions
9
internal/dataplane/testdata/golden/kongconsumer-with-custom-id/in.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
apiVersion: configuration.konghq.com/v1 | ||
kind: KongConsumer | ||
metadata: | ||
name: consumer | ||
namespace: consumer-namespace | ||
annotations: | ||
kubernetes.io/ingress.class: kong | ||
custom_id: "1234-1234" |