diff --git a/controller/konnect/ops.go b/controller/konnect/ops.go index c48d15a5f..aadc33b4e 100644 --- a/controller/konnect/ops.go +++ b/controller/konnect/ops.go @@ -47,13 +47,13 @@ func Create[ switch ent := any(e).(type) { case *konnectv1alpha1.KonnectControlPlane: - return e, createControlPlane(ctx, sdk, ent) + return e, createControlPlane(ctx, sdk.ControlPlanes, ent) case *configurationv1alpha1.KongService: - return e, createService(ctx, sdk, ent) + return e, createService(ctx, sdk.Services, ent) case *configurationv1alpha1.KongRoute: - return e, createRoute(ctx, sdk, ent) + return e, createRoute(ctx, sdk.Routes, ent) case *configurationv1.KongConsumer: - return e, createConsumer(ctx, sdk, ent) + return e, createConsumer(ctx, sdk.Consumers, ent) case *configurationv1beta1.KongConsumerGroup: return e, createConsumerGroup(ctx, sdk, ent) @@ -83,13 +83,13 @@ func Delete[ switch ent := any(e).(type) { case *konnectv1alpha1.KonnectControlPlane: - return deleteControlPlane(ctx, sdk, ent) + return deleteControlPlane(ctx, sdk.ControlPlanes, ent) case *configurationv1alpha1.KongService: - return deleteService(ctx, sdk, ent) + return deleteService(ctx, sdk.Services, ent) case *configurationv1alpha1.KongRoute: - return deleteRoute(ctx, sdk, ent) + return deleteRoute(ctx, sdk.Routes, ent) case *configurationv1.KongConsumer: - return deleteConsumer(ctx, sdk, ent) + return deleteConsumer(ctx, sdk.Consumers, ent) case *configurationv1beta1.KongConsumerGroup: return deleteConsumerGroup(ctx, sdk, ent) @@ -144,13 +144,13 @@ func Update[ switch ent := any(e).(type) { case *konnectv1alpha1.KonnectControlPlane: - return ctrl.Result{}, updateControlPlane(ctx, sdk, ent) + return ctrl.Result{}, updateControlPlane(ctx, sdk.ControlPlanes, ent) case *configurationv1alpha1.KongService: - return ctrl.Result{}, updateService(ctx, sdk, cl, ent) + return ctrl.Result{}, updateService(ctx, sdk.Services, cl, ent) case *configurationv1alpha1.KongRoute: - return ctrl.Result{}, updateRoute(ctx, sdk, cl, ent) + return ctrl.Result{}, updateRoute(ctx, sdk.Routes, cl, ent) case *configurationv1.KongConsumer: - return ctrl.Result{}, updateConsumer(ctx, sdk, cl, ent) + return ctrl.Result{}, updateConsumer(ctx, sdk.Consumers, cl, ent) case *configurationv1beta1.KongConsumerGroup: return ctrl.Result{}, updateConsumerGroup(ctx, sdk, cl, ent) diff --git a/controller/konnect/ops_controlplane.go b/controller/konnect/ops_controlplane.go index 40f53d165..5f951017d 100644 --- a/controller/konnect/ops_controlplane.go +++ b/controller/konnect/ops_controlplane.go @@ -6,6 +6,7 @@ import ( sdkkonnectgo "github.com/Kong/sdk-konnect-go" "github.com/Kong/sdk-konnect-go/models/components" + sdkkonnectgoops "github.com/Kong/sdk-konnect-go/models/operations" "github.com/Kong/sdk-konnect-go/models/sdkerrors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrllog "sigs.k8s.io/controller-runtime/pkg/log" @@ -15,12 +16,19 @@ import ( konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" ) +// ControlPlaneSDK is the interface for the Konnect ControlPlane SDK. +type ControlPlaneSDK interface { + CreateControlPlane(ctx context.Context, req components.CreateControlPlaneRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.CreateControlPlaneResponse, error) + DeleteControlPlane(ctx context.Context, id string, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.DeleteControlPlaneResponse, error) + UpdateControlPlane(ctx context.Context, id string, req components.UpdateControlPlaneRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.UpdateControlPlaneResponse, error) +} + func createControlPlane( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ControlPlaneSDK, cp *konnectv1alpha1.KonnectControlPlane, ) error { - resp, err := sdk.ControlPlanes.CreateControlPlane(ctx, cp.Spec.CreateControlPlaneRequest) + resp, err := sdk.CreateControlPlane(ctx, cp.Spec.CreateControlPlaneRequest) // 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 @@ -58,11 +66,11 @@ func createControlPlane( // It is assumed that the Konnect ControlPlane has a Konnect ID. func deleteControlPlane( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ControlPlaneSDK, cp *konnectv1alpha1.KonnectControlPlane, ) error { id := cp.GetKonnectStatus().GetKonnectID() - _, err := sdk.ControlPlanes.DeleteControlPlane(ctx, id) + _, err := sdk.DeleteControlPlane(ctx, id) if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cp); errWrap != nil { var sdkNotFoundError *sdkerrors.NotFoundError if errors.As(err, &sdkNotFoundError) { @@ -93,7 +101,7 @@ func deleteControlPlane( // It returns an error if the operation fails. func updateControlPlane( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ControlPlaneSDK, cp *konnectv1alpha1.KonnectControlPlane, ) error { id := cp.GetKonnectStatus().GetKonnectID() @@ -105,7 +113,7 @@ func updateControlPlane( Labels: cp.Spec.Labels, } - resp, err := sdk.ControlPlanes.UpdateControlPlane(ctx, id, req) + resp, err := sdk.UpdateControlPlane(ctx, id, req) var sdkError *sdkerrors.NotFoundError if errors.As(err, &sdkError) { ctrllog.FromContext(ctx). diff --git a/controller/konnect/ops_kongconsumer.go b/controller/konnect/ops_kongconsumer.go index b036b527c..d92429ed9 100644 --- a/controller/konnect/ops_kongconsumer.go +++ b/controller/konnect/ops_kongconsumer.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" - sdkkonnectgo "github.com/Kong/sdk-konnect-go" sdkkonnectgocomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectgoops "github.com/Kong/sdk-konnect-go/models/operations" "github.com/Kong/sdk-konnect-go/models/sdkerrors" @@ -21,16 +20,23 @@ import ( "github.com/kong/kubernetes-configuration/pkg/metadata" ) +// ConsumersSDK is the interface for the Konnect Consumers SDK. +type ConsumersSDK interface { + CreateConsumer(ctx context.Context, controlPlaneID string, consumerInput sdkkonnectgocomp.ConsumerInput, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.CreateConsumerResponse, error) + UpsertConsumer(ctx context.Context, upsertConsumerRequest sdkkonnectgoops.UpsertConsumerRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.UpsertConsumerResponse, error) + DeleteConsumer(ctx context.Context, controlPlaneID string, consumerID string, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.DeleteConsumerResponse, error) +} + func createConsumer( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ConsumersSDK, consumer *configurationv1.KongConsumer, ) error { if consumer.GetControlPlaneID() == "" { return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", consumer, client.ObjectKeyFromObject(consumer)) } - resp, err := sdk.Consumers.CreateConsumer(ctx, + resp, err := sdk.CreateConsumer(ctx, consumer.Status.Konnect.ControlPlaneID, kongConsumerToSDKConsumerInput(consumer), ) @@ -72,7 +78,7 @@ func createConsumer( // It returns an error if the KongConsumer does not have a ControlPlaneRef. func updateConsumer( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ConsumersSDK, cl client.Client, consumer *configurationv1.KongConsumer, ) error { @@ -100,7 +106,7 @@ func updateConsumer( ) } - resp, err := sdk.Consumers.UpsertConsumer(ctx, + resp, err := sdk.UpsertConsumer(ctx, sdkkonnectgoops.UpsertConsumerRequest{ ControlPlaneID: cp.Status.ID, ConsumerID: consumer.GetKonnectStatus().GetKonnectID(), @@ -146,11 +152,11 @@ func updateConsumer( // It returns an error if the operation fails. func deleteConsumer( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ConsumersSDK, consumer *configurationv1.KongConsumer, ) error { id := consumer.Status.Konnect.GetKonnectID() - _, err := sdk.Consumers.DeleteConsumer(ctx, consumer.Status.Konnect.ControlPlaneID, id) + _, err := sdk.DeleteConsumer(ctx, consumer.Status.Konnect.ControlPlaneID, id) if errWrapped := wrapErrIfKonnectOpFailed(err, DeleteOp, consumer); errWrapped != nil { // Consumer delete operation returns an SDKError instead of a NotFoundError. var sdkError *sdkerrors.SDKError diff --git a/controller/konnect/ops_kongroute.go b/controller/konnect/ops_kongroute.go index 85d39feb2..a830e36d2 100644 --- a/controller/konnect/ops_kongroute.go +++ b/controller/konnect/ops_kongroute.go @@ -20,16 +20,23 @@ import ( konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" ) +// RoutesSDK is the interface for the Konnect Routes SDK. +type RoutesSDK interface { + CreateRoute(ctx context.Context, controlPlaneID string, route sdkkonnectgocomp.RouteInput, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.CreateRouteResponse, error) + UpsertRoute(ctx context.Context, req sdkkonnectgoops.UpsertRouteRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.UpsertRouteResponse, error) + DeleteRoute(ctx context.Context, controlPlaneID, routeID string, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.DeleteRouteResponse, error) +} + func createRoute( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk RoutesSDK, route *configurationv1alpha1.KongRoute, ) error { if route.GetControlPlaneID() == "" { return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", route, client.ObjectKeyFromObject(route)) } - resp, err := sdk.Routes.CreateRoute(ctx, route.Status.Konnect.ControlPlaneID, kongRouteToSDKRouteInput(route)) + resp, err := sdk.CreateRoute(ctx, route.Status.Konnect.ControlPlaneID, kongRouteToSDKRouteInput(route)) // TODO: handle already exists // Can't adopt it as it will cause conflicts between the controller @@ -69,7 +76,8 @@ func createRoute( // if the operation fails. func updateRoute( ctx context.Context, - sdk *sdkkonnectgo.SDK, + // sdk *sdkkonnectgo.SDK, + sdk RoutesSDK, cl client.Client, route *configurationv1alpha1.KongRoute, ) error { @@ -114,7 +122,8 @@ func updateRoute( ) } - resp, err := sdk.Routes.UpsertRoute(ctx, sdkkonnectgoops.UpsertRouteRequest{ + resp, err := sdk.UpsertRoute(ctx, sdkkonnectgoops.UpsertRouteRequest{ + // resp, err := sdk.UpsertRoute(ctx, sdkkonnectgoops.UpsertRouteRequest{ ControlPlaneID: cp.Status.ID, RouteID: route.Status.Konnect.ID, Route: kongRouteToSDKRouteInput(route), @@ -159,11 +168,11 @@ func updateRoute( // It returns an error if the operation fails. func deleteRoute( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk RoutesSDK, route *configurationv1alpha1.KongRoute, ) error { id := route.GetKonnectStatus().GetKonnectID() - _, err := sdk.Routes.DeleteRoute(ctx, route.Status.Konnect.ControlPlaneID, id) + _, err := sdk.DeleteRoute(ctx, route.Status.Konnect.ControlPlaneID, id) if errWrapped := wrapErrIfKonnectOpFailed(err, DeleteOp, route); errWrapped != nil { // Service delete operation returns an SDKError instead of a NotFoundError. var sdkError *sdkerrors.SDKError diff --git a/controller/konnect/ops_kongservice.go b/controller/konnect/ops_kongservice.go index 635b31dcc..f42aa60b2 100644 --- a/controller/konnect/ops_kongservice.go +++ b/controller/konnect/ops_kongservice.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" - sdkkonnectgo "github.com/Kong/sdk-konnect-go" sdkkonnectgocomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectgoops "github.com/Kong/sdk-konnect-go/models/operations" "github.com/Kong/sdk-konnect-go/models/sdkerrors" @@ -20,16 +19,23 @@ import ( konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" ) +// ServicesSDK is the interface for the Konnect Service SDK. +type ServicesSDK interface { + CreateService(ctx context.Context, controlPlaneID string, service sdkkonnectgocomp.ServiceInput, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.CreateServiceResponse, error) + UpsertService(ctx context.Context, req sdkkonnectgoops.UpsertServiceRequest, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.UpsertServiceResponse, error) + DeleteService(ctx context.Context, controlPlaneID, serviceID string, opts ...sdkkonnectgoops.Option) (*sdkkonnectgoops.DeleteServiceResponse, error) +} + func createService( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ServicesSDK, svc *configurationv1alpha1.KongService, ) error { if svc.GetControlPlaneID() == "" { return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", svc, client.ObjectKeyFromObject(svc)) } - resp, err := sdk.Services.CreateService(ctx, + resp, err := sdk.CreateService(ctx, svc.Status.Konnect.ControlPlaneID, kongServiceToSDKServiceInput(svc), ) @@ -72,7 +78,7 @@ func createService( // if the operation fails. func updateService( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ServicesSDK, cl client.Client, svc *configurationv1alpha1.KongService, ) error { @@ -100,7 +106,7 @@ func updateService( ) } - resp, err := sdk.Services.UpsertService(ctx, + resp, err := sdk.UpsertService(ctx, sdkkonnectgoops.UpsertServiceRequest{ ControlPlaneID: cp.Status.ID, ServiceID: svc.GetKonnectStatus().GetKonnectID(), @@ -146,11 +152,11 @@ func updateService( // It returns an error if the operation fails. func deleteService( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk ServicesSDK, svc *configurationv1alpha1.KongService, ) error { id := svc.GetKonnectStatus().GetKonnectID() - _, err := sdk.Services.DeleteService(ctx, svc.Status.Konnect.ControlPlaneID, id) + _, err := sdk.DeleteService(ctx, svc.Status.Konnect.ControlPlaneID, id) if errWrapped := wrapErrIfKonnectOpFailed(err, DeleteOp, svc); errWrapped != nil { // Service delete operation returns an SDKError instead of a NotFoundError. var sdkError *sdkerrors.SDKError