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(konnect): handle KongTarget conflict on creation #757

Merged
merged 1 commit into from
Oct 21, 2024
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
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)
}
Loading