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: handle missing service and route names detecting duplicates #945

Merged
merged 1 commit into from
Jun 14, 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
44 changes: 44 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3414,3 +3414,47 @@ func Test_Sync_UpdateWithExplicitIDs(t *testing.T) {
},
}, ignoreFieldsIrrelevantForIDsTests)
}

// test scope:
// - 3.0.0+
// - konnect
func Test_Sync_UpdateWithExplicitIDsWithNoNames(t *testing.T) {
runWhenKongOrKonnect(t, ">=3.0.0")

client, err := getTestClient()
if err != nil {
t.Errorf(err.Error())
}

const (
beforeConfig = "testdata/sync/022-update-with-explicit-ids-with-no-names/before.yaml"
afterConfig = "testdata/sync/022-update-with-explicit-ids-with-no-names/after.yaml"
)

// First, create entities with IDs assigned explicitly.
err = sync(beforeConfig)
require.NoError(t, err)

// Then, sync again, adding tags to every entity just to trigger an update.
err = sync(afterConfig)
require.NoError(t, err)

// Finally, verify that the update was successful.
testKongState(t, client, false, utils.KongRawState{
Services: []*kong.Service{
{
ID: kong.String("c75a775b-3a32-4b73-8e05-f68169c23941"),
Tags: kong.StringSlice("after"),
},
},
Routes: []*kong.Route{
{
ID: kong.String("97b6a97e-f3f7-4c47-857a-7464cb9e202b"),
Tags: kong.StringSlice("after"),
Service: &kong.Service{
ID: kong.String("c75a775b-3a32-4b73-8e05-f68169c23941"),
},
},
},
}, ignoreFieldsIrrelevantForIDsTests)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_format_version: "3.0"
services:
- enabled: true
host: mockbin.org
id: c75a775b-3a32-4b73-8e05-f68169c23941 # Leaving ID
port: 80
tags: [after]
routes:
- id: 97b6a97e-f3f7-4c47-857a-7464cb9e202b # Leaving ID
paths:
- /r1
tags: [after]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_format_version: "3.0"
services:
- enabled: true
host: mockbin.org
id: c75a775b-3a32-4b73-8e05-f68169c23941 # Leaving ID
port: 80
routes:
- id: 97b6a97e-f3f7-4c47-857a-7464cb9e202b # Leaving ID
paths:
- /r1
5 changes: 5 additions & 0 deletions types/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ func (d *routeDiffer) DuplicatesDeletes() ([]crud.Event, error) {
}

func (d *routeDiffer) deleteDuplicateRoute(targetRoute *state.Route) (*crud.Event, error) {
if targetRoute == nil || targetRoute.Name == nil {
// Nothing to do, cannot be a duplicate with no name.
return nil, nil
}

currentRoute, err := d.currentState.Routes.Get(*targetRoute.Name)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
Expand Down
5 changes: 5 additions & 0 deletions types/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ func (d *serviceDiffer) DuplicatesDeletes() ([]crud.Event, error) {
}

func (d *serviceDiffer) deleteDuplicateService(targetService *state.Service) ([]crud.Event, error) {
if targetService == nil || targetService.Name == nil {
// Nothing to do, cannot be a duplicate with no name.
return nil, nil
}

currentService, err := d.currentState.Services.Get(*targetService.Name)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
Expand Down