Skip to content

Commit

Permalink
feat: handle KongPlugin conflict on creation (#762)
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca authored Oct 21, 2024
1 parent ef17413 commit e358cb1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 12 deletions.
2 changes: 2 additions & 0 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func Create[
id, err = getKongUpstreamForUID(ctx, sdk.GetUpstreamsSDK(), ent)
case *configurationv1alpha1.KongTarget:
id, err = getKongTargetForUID(ctx, sdk.GetTargetsSDK(), ent)
case *configurationv1alpha1.KongPluginBinding:
id, err = getPluginForUID(ctx, sdk.GetPluginSDK(), ent)
// ---------------------------------------------------------------------
// TODO: add other Konnect types
default:
Expand Down
49 changes: 37 additions & 12 deletions controller/konnect/ops/ops_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func createPlugin(
ctx context.Context,
cl client.Client,
sdk PluginSDK,
pb *configurationv1alpha1.KongPluginBinding,
pluginBinding *configurationv1alpha1.KongPluginBinding,
) error {
controlPlaneID := pb.GetControlPlaneID()
controlPlaneID := pluginBinding.GetControlPlaneID()
if controlPlaneID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: pb, Op: CreateOp}
return CantPerformOperationWithoutControlPlaneIDError{Entity: pluginBinding, Op: CreateOp}
}
pluginInput, err := kongPluginBindingToSDKPluginInput(ctx, cl, pb)
pluginInput, err := kongPluginBindingToSDKPluginInput(ctx, cl, pluginBinding)
if err != nil {
return err
}
Expand All @@ -46,16 +46,16 @@ func createPlugin(
*pluginInput,
)

// TODO: handle already exists
// Can't adopt it as it will cause conflicts between the controller
// that created that entity and already manages it, hm
if errWrap := wrapErrIfKonnectOpFailed[configurationv1alpha1.KongPluginBinding](err, CreateOp, pb); errWrap != nil {
SetKonnectEntityProgrammedConditionFalse(pb, "FailedToCreate", errWrap.Error())
return errWrap
if errWrapped := wrapErrIfKonnectOpFailed(err, CreateOp, pluginBinding); errWrapped != nil {
return errWrapped
}

pb.SetKonnectID(*resp.Plugin.ID)
SetKonnectEntityProgrammedCondition(pb)
if resp == nil || resp.Plugin == nil || resp.Plugin.ID == nil {
return fmt.Errorf("failed creating %s: %w", pluginBinding.GetTypeName(), ErrNilResponse)
}

pluginBinding.SetKonnectID(*resp.Plugin.ID)
SetKonnectEntityProgrammedCondition(pluginBinding)

return nil
}
Expand Down Expand Up @@ -129,6 +129,31 @@ func deletePlugin(
return nil
}

// getPluginForUID lists plugins in Konnect with given k8s uid as its tag.
func getPluginForUID(
ctx context.Context,
sdk PluginSDK,
pluginBinding *configurationv1alpha1.KongPluginBinding,
) (string, error) {
cpID := pluginBinding.GetControlPlaneID()
reqList := sdkkonnectops.ListPluginRequest{
// 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.
ControlPlaneID: cpID,
Tags: lo.ToPtr(UIDLabelForObject(pluginBinding)),
}
resp, err := sdk.ListPlugin(ctx, reqList)
if err != nil {
return "", fmt.Errorf("failed listing %s: %w", pluginBinding.GetTypeName(), err)
}
if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", pluginBinding.GetTypeName(), ErrNilResponse)
}

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

// -----------------------------------------------------------------------------
// Konnect KongPlugin - ops helpers
// -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions controller/konnect/ops/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type PluginSDK interface {
CreatePlugin(ctx context.Context, controlPlaneID string, plugin sdkkonnectcomp.PluginInput, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreatePluginResponse, error)
UpsertPlugin(ctx context.Context, request sdkkonnectops.UpsertPluginRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertPluginResponse, error)
DeletePlugin(ctx context.Context, controlPlaneID string, pluginID string, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeletePluginResponse, error)
ListPlugin(ctx context.Context, request sdkkonnectops.ListPluginRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.ListPluginResponse, error)
}
74 changes: 74 additions & 0 deletions controller/konnect/ops/plugin_mock.go

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

0 comments on commit e358cb1

Please sign in to comment.