Skip to content

Commit

Permalink
fix(konnect): fix handling conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Feb 18, 2025
1 parent 5947298 commit ae2405e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@
[#1115](https://github.com/Kong/gateway-operator/pull/1115)
- Fix setting `DataPlane`'s readiness probe using `GatewayConfiguration`.
[#1118](https://github.com/Kong/gateway-operator/pull/1118)
- Fix handling Konnect API conflicts.
[#1176](https://github.com/Kong/gateway-operator/pull/1176)

## [v1.4.2]

Expand Down
20 changes: 13 additions & 7 deletions controller/konnect/ops/ops_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"slices"
"strings"

sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -94,7 +95,6 @@ func ParseSDKErrorBody(body string) (sdkErrorBody, error) {
}

const (
dataConstraintMesasge = "data constraint error"
validationErrorMessage = "validation error"
apiErrorOccurredMessage = "API error occurred"
)
Expand Down Expand Up @@ -202,14 +202,20 @@ func SDKErrorIsConflict(sdkError *sdkkonnecterrs.SDKError) bool {
return false
}

if sdkErrorBody.Message != dataConstraintMesasge {
return false
}
const (
dataConstraintMesasge = "data constraint error"
typeUniqueConstraintFailed = "(type: unique) constraint failed"
)

switch sdkErrorBody.Code {
case 3, 6:
return true
if sdkErrorBody.Message == dataConstraintMesasge ||
strings.Contains(sdkErrorBody.Message, typeUniqueConstraintFailed) {

switch sdkErrorBody.Code {
case 3, 6:
return true
}
}

return false
}

Expand Down
78 changes: 78 additions & 0 deletions controller/konnect/ops/ops_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,81 @@ func TestErrorIsCreateConflict(t *testing.T) {
})
}
}

func TestSDKErrorIsConflict(t *testing.T) {
tests := []struct {
name string
err *sdkkonnecterrs.SDKError
want bool
}{
{
name: "SDKError with data constraint error message and code 3",
err: &sdkkonnecterrs.SDKError{
Body: `{
"code": 3,
"message": "data constraint error",
"details": []
}`,
},
want: true,
},
{
name: "SDKError with data constraint error message and code 6",
err: &sdkkonnecterrs.SDKError{
Body: `{
"code": 6,
"message": "data constraint error",
"details": []
}`,
},
want: true,
},
{
name: "SDKError with unique constraint failed message",
err: &sdkkonnecterrs.SDKError{
Body: `{
"code": 3,
"message": "name (type: unique) constraint failed",
"details": []
}`,
},
want: true,
},
{
name: "SDKError with non-conflict message",
err: &sdkkonnecterrs.SDKError{
Body: `{
"code": 3,
"message": "some other error",
"details": []
}`,
},
want: false,
},
{
name: "SDKError with conflict message but different code",
err: &sdkkonnecterrs.SDKError{
Body: `{
"code": 4,
"message": "data constraint error",
"details": []
}`,
},
want: false,
},
{
name: "SDKError with invalid JSON body",
err: &sdkkonnecterrs.SDKError{
Body: `invalid json`,
},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := SDKErrorIsConflict(tt.err)
require.Equal(t, tt.want, got)
})
}
}

0 comments on commit ae2405e

Please sign in to comment.