Skip to content

Commit

Permalink
fix(konnect): handle KongTarget conflict on creation (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Oct 21, 2024
1 parent 9e8f425 commit ef17413
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
1 change: 1 addition & 0 deletions controller/konnect/ops/kongtarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type TargetsSDK interface {
CreateTargetWithUpstream(ctx context.Context, req sdkkonnectops.CreateTargetWithUpstreamRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateTargetWithUpstreamResponse, error)
UpsertTargetWithUpstream(ctx context.Context, req sdkkonnectops.UpsertTargetWithUpstreamRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertTargetWithUpstreamResponse, error)
DeleteTargetWithUpstream(ctx context.Context, req sdkkonnectops.DeleteTargetWithUpstreamRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteTargetWithUpstreamResponse, error)
ListTargetWithUpstream(ctx context.Context, request sdkkonnectops.ListTargetWithUpstreamRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.ListTargetWithUpstreamResponse, error)
}
74 changes: 74 additions & 0 deletions controller/konnect/ops/kongtarget_mock.go

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

2 changes: 2 additions & 0 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func Create[
id, err = getKongKeyForUID(ctx, sdk.GetKeysSDK(), ent)
case *configurationv1alpha1.KongUpstream:
id, err = getKongUpstreamForUID(ctx, sdk.GetUpstreamsSDK(), ent)
case *configurationv1alpha1.KongTarget:
id, err = getKongTargetForUID(ctx, sdk.GetTargetsSDK(), ent)
// ---------------------------------------------------------------------
// TODO: add other Konnect types
default:
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/ops/ops_kongkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func createKey(
return fmt.Errorf("failed creating %s: %w", key.GetTypeName(), ErrNilResponse)
}

key.Status.Konnect.SetKonnectID(*resp.Key.ID)
key.SetKonnectID(*resp.Key.ID)

return nil
}
Expand Down
37 changes: 32 additions & 5 deletions controller/konnect/ops/ops_kongtarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ func createTarget(
})

if errWrapped := wrapErrIfKonnectOpFailed(err, CreateOp, target); errWrapped != nil {
SetKonnectEntityProgrammedConditionFalse(target, "FailedToCreate", errWrapped.Error())
return errWrapped
}

target.Status.Konnect.SetKonnectID(*resp.Target.ID)
SetKonnectEntityProgrammedCondition(target)
if resp == nil || resp.Target == nil || resp.Target.ID == nil {
return fmt.Errorf("failed creating %s: %w", target.GetTypeName(), ErrNilResponse)
}

target.SetKonnectID(*resp.Target.ID)

return nil
}
Expand All @@ -68,11 +70,9 @@ func updateTarget(
})

if errWrapped := wrapErrIfKonnectOpFailed(err, UpdateOp, target); errWrapped != nil {
SetKonnectEntityProgrammedConditionFalse(target, "FailedToUpdate", errWrapped.Error())
return errWrapped
}

SetKonnectEntityProgrammedCondition(target)
return nil
}

Expand Down Expand Up @@ -128,3 +128,30 @@ func kongTargetToTargetWithoutParents(target *configurationv1alpha1.KongTarget)
Tags: GenerateTagsForObject(target, target.Spec.Tags...),
}
}

// getKongTargetForUID returns the Konnect ID of the KongTarget
// that matches the UID of the provided KongTarget.
func getKongTargetForUID(
ctx context.Context,
sdk TargetsSDK,
target *configurationv1alpha1.KongTarget,
) (string, error) {
reqList := sdkkonnectops.ListTargetWithUpstreamRequest{
// NOTE: only filter on object's UID.
// Other fields like name might have changed in the meantime but that's OK.
// Those will be enforced via subsequent updates.
Tags: lo.ToPtr(UIDLabelForObject(target)),
ControlPlaneID: target.GetControlPlaneID(),
}

resp, err := sdk.ListTargetWithUpstream(ctx, reqList)
if err != nil {
return "", fmt.Errorf("failed listing %s: %w", target.GetTypeName(), err)
}

if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", target.GetTypeName(), ErrNilResponse)
}

return getMatchingEntryFromListResponseData(sliceToEntityWithIDSlice(resp.Object.Data), target)
}

0 comments on commit ef17413

Please sign in to comment.