Skip to content

Commit

Permalink
refactor(konnect): add SDK interface types for each entity type
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Aug 22, 2024
1 parent ee6f390 commit 05a1c72
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
24 changes: 12 additions & 12 deletions controller/konnect/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
20 changes: 14 additions & 6 deletions controller/konnect/ops_controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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).
Expand Down
20 changes: 13 additions & 7 deletions controller/konnect/ops_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions controller/konnect/ops_kongroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions controller/konnect/ops_kongservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05a1c72

Please sign in to comment.