diff --git a/controller/konnect/ops/kongtarget.go b/controller/konnect/ops/kongtarget.go index ed1326170..d8411e18f 100644 --- a/controller/konnect/ops/kongtarget.go +++ b/controller/konnect/ops/kongtarget.go @@ -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) } diff --git a/controller/konnect/ops/kongtarget_mock.go b/controller/konnect/ops/kongtarget_mock.go index dbc1f71f3..7988af137 100644 --- a/controller/konnect/ops/kongtarget_mock.go +++ b/controller/konnect/ops/kongtarget_mock.go @@ -170,6 +170,80 @@ func (_c *MockTargetsSDK_DeleteTargetWithUpstream_Call) RunAndReturn(run func(co return _c } +// ListTargetWithUpstream provides a mock function with given fields: ctx, request, opts +func (_m *MockTargetsSDK) ListTargetWithUpstream(ctx context.Context, request operations.ListTargetWithUpstreamRequest, opts ...operations.Option) (*operations.ListTargetWithUpstreamResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, request) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for ListTargetWithUpstream") + } + + var r0 *operations.ListTargetWithUpstreamResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, operations.ListTargetWithUpstreamRequest, ...operations.Option) (*operations.ListTargetWithUpstreamResponse, error)); ok { + return rf(ctx, request, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, operations.ListTargetWithUpstreamRequest, ...operations.Option) *operations.ListTargetWithUpstreamResponse); ok { + r0 = rf(ctx, request, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*operations.ListTargetWithUpstreamResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, operations.ListTargetWithUpstreamRequest, ...operations.Option) error); ok { + r1 = rf(ctx, request, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockTargetsSDK_ListTargetWithUpstream_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListTargetWithUpstream' +type MockTargetsSDK_ListTargetWithUpstream_Call struct { + *mock.Call +} + +// ListTargetWithUpstream is a helper method to define mock.On call +// - ctx context.Context +// - request operations.ListTargetWithUpstreamRequest +// - opts ...operations.Option +func (_e *MockTargetsSDK_Expecter) ListTargetWithUpstream(ctx interface{}, request interface{}, opts ...interface{}) *MockTargetsSDK_ListTargetWithUpstream_Call { + return &MockTargetsSDK_ListTargetWithUpstream_Call{Call: _e.mock.On("ListTargetWithUpstream", + append([]interface{}{ctx, request}, opts...)...)} +} + +func (_c *MockTargetsSDK_ListTargetWithUpstream_Call) Run(run func(ctx context.Context, request operations.ListTargetWithUpstreamRequest, opts ...operations.Option)) *MockTargetsSDK_ListTargetWithUpstream_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]operations.Option, len(args)-2) + for i, a := range args[2:] { + if a != nil { + variadicArgs[i] = a.(operations.Option) + } + } + run(args[0].(context.Context), args[1].(operations.ListTargetWithUpstreamRequest), variadicArgs...) + }) + return _c +} + +func (_c *MockTargetsSDK_ListTargetWithUpstream_Call) Return(_a0 *operations.ListTargetWithUpstreamResponse, _a1 error) *MockTargetsSDK_ListTargetWithUpstream_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockTargetsSDK_ListTargetWithUpstream_Call) RunAndReturn(run func(context.Context, operations.ListTargetWithUpstreamRequest, ...operations.Option) (*operations.ListTargetWithUpstreamResponse, error)) *MockTargetsSDK_ListTargetWithUpstream_Call { + _c.Call.Return(run) + return _c +} + // UpsertTargetWithUpstream provides a mock function with given fields: ctx, req, opts func (_m *MockTargetsSDK) UpsertTargetWithUpstream(ctx context.Context, req operations.UpsertTargetWithUpstreamRequest, opts ...operations.Option) (*operations.UpsertTargetWithUpstreamResponse, error) { _va := make([]interface{}, len(opts)) diff --git a/controller/konnect/ops/ops.go b/controller/konnect/ops/ops.go index 88f4ea63a..ce58ed913 100644 --- a/controller/konnect/ops/ops.go +++ b/controller/konnect/ops/ops.go @@ -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: diff --git a/controller/konnect/ops/ops_kongkey.go b/controller/konnect/ops/ops_kongkey.go index be2f8fe20..4f2ce3a68 100644 --- a/controller/konnect/ops/ops_kongkey.go +++ b/controller/konnect/ops/ops_kongkey.go @@ -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 } diff --git a/controller/konnect/ops/ops_kongtarget.go b/controller/konnect/ops/ops_kongtarget.go index d172d6116..1addec4b3 100644 --- a/controller/konnect/ops/ops_kongtarget.go +++ b/controller/konnect/ops/ops_kongtarget.go @@ -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 } @@ -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 } @@ -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) +}