Skip to content

Commit

Permalink
fix: handle KongUpstream conflict on creation (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Oct 18, 2024
1 parent b9abeb9 commit 9e8f425
Show file tree
Hide file tree
Showing 28 changed files with 329 additions and 160 deletions.
1 change: 1 addition & 0 deletions controller/konnect/ops/kongupstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type UpstreamsSDK interface {
CreateUpstream(ctx context.Context, controlPlaneID string, upstream sdkkonnectcomp.UpstreamInput, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateUpstreamResponse, error)
UpsertUpstream(ctx context.Context, req sdkkonnectops.UpsertUpstreamRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertUpstreamResponse, error)
DeleteUpstream(ctx context.Context, controlPlaneID, upstreamID string, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteUpstreamResponse, error)
ListUpstream(ctx context.Context, request sdkkonnectops.ListUpstreamRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.ListUpstreamResponse, error)
}
74 changes: 74 additions & 0 deletions controller/konnect/ops/kongupstream_mock.go

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

8 changes: 5 additions & 3 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,17 @@ func Create[
case *configurationv1alpha1.KongRoute:
id, err = getKongRouteForUID(ctx, sdk.GetRoutesSDK(), ent)
case *configurationv1alpha1.KongSNI:
id, err = getSNIForUID(ctx, sdk.GetSNIsSDK(), ent)
id, err = getKongSNIForUID(ctx, sdk.GetSNIsSDK(), ent)
case *configurationv1.KongConsumer:
id, err = getConsumerForUID(ctx, sdk.GetConsumersSDK(), ent)
id, err = getKongConsumerForUID(ctx, sdk.GetConsumersSDK(), ent)
case *configurationv1beta1.KongConsumerGroup:
id, err = getConsumerGroupForUID(ctx, sdk.GetConsumerGroupsSDK(), ent)
id, err = getKongConsumerGroupForUID(ctx, sdk.GetConsumerGroupsSDK(), ent)
case *configurationv1alpha1.KongKeySet:
id, err = getKongKeySetForUID(ctx, sdk.GetKeySetsSDK(), ent)
case *configurationv1alpha1.KongKey:
id, err = getKongKeyForUID(ctx, sdk.GetKeysSDK(), ent)
case *configurationv1alpha1.KongUpstream:
id, err = getKongUpstreamForUID(ctx, sdk.GetUpstreamsSDK(), ent)
// ---------------------------------------------------------------------
// TODO: add other Konnect types
default:
Expand Down
3 changes: 0 additions & 3 deletions controller/konnect/ops/ops_controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ func createControlPlane(

resp, err := sdk.CreateControlPlane(ctx, req)

// Can't adopt it as it will cause conflicts between the controller
// that created that entity and already manages it.
// TODO: implement entity adoption https://github.com/Kong/gateway-operator/issues/460
if errWrap := wrapErrIfKonnectOpFailed(err, CreateOp, cp); errWrap != nil {
return errWrap
}
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_credentialacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"github.com/samber/lo"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -22,7 +20,7 @@ func createKongCredentialACL(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}

resp, err := sdk.CreateACLWithConsumer(ctx,
Expand Down Expand Up @@ -58,7 +56,7 @@ func updateKongCredentialACL(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}

_, err := sdk.UpsertACLWithConsumer(ctx,
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_credentialapikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"github.com/samber/lo"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -22,7 +20,7 @@ func createKongCredentialAPIKey(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}

resp, err := sdk.CreateKeyAuthWithConsumer(ctx,
Expand Down Expand Up @@ -58,7 +56,7 @@ func updateKongCredentialAPIKey(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}

_, err := sdk.UpsertKeyAuthWithConsumer(ctx,
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_credentialbasicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"github.com/samber/lo"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -22,7 +20,7 @@ func createKongCredentialBasicAuth(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}

resp, err := sdk.CreateBasicAuthWithConsumer(ctx,
Expand Down Expand Up @@ -58,7 +56,7 @@ func updateKongCredentialBasicAuth(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}

_, err := sdk.UpsertBasicAuthWithConsumer(ctx, sdkkonnectops.UpsertBasicAuthWithConsumerRequest{
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_credentialhmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -21,7 +19,7 @@ func createKongCredentialHMAC(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}

resp, err := sdk.CreateHmacAuthWithConsumer(ctx,
Expand Down Expand Up @@ -57,7 +55,7 @@ func updateKongCredentialHMAC(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}

_, err := sdk.UpsertHmacAuthWithConsumer(ctx,
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_credentialjwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -21,7 +19,7 @@ func createKongCredentialJWT(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}

resp, err := sdk.CreateJwtWithConsumer(ctx,
Expand Down Expand Up @@ -57,7 +55,7 @@ func updateKongCredentialJWT(
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T %s without a Konnect ControlPlane ID", cred, client.ObjectKeyFromObject(cred))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}

_, err := sdk.UpsertJwtWithConsumer(ctx,
Expand Down
15 changes: 15 additions & 0 deletions controller/konnect/ops/ops_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ func (e EntityWithMatchingUIDNotFoundError) Error() string {
)
}

// CantPerformOperationWithoutControlPlaneIDError is an error indicating that an
// operation cannot be performed without a ControlPlane ID.
type CantPerformOperationWithoutControlPlaneIDError struct {
Entity entity
Op Op
}

// Error implements the error interface.
func (e CantPerformOperationWithoutControlPlaneIDError) Error() string {
return fmt.Sprintf(
"can't %s %s %s without a Konnect ControlPlane ID",
e.Op, e.Entity.GetTypeName(), client.ObjectKeyFromObject(e.Entity),
)
}

type sdkErrorBody struct {
Code int `json:"code"`
Message string `json:"message"`
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_kongcacertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -23,7 +21,7 @@ func createCACertificate(
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cert, client.ObjectKeyFromObject(cert))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: CreateOp}
}

resp, err := sdk.CreateCaCertificate(ctx,
Expand Down Expand Up @@ -55,7 +53,7 @@ func updateCACertificate(
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T without a ControlPlaneID", cert)
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: UpdateOp}
}

_, err := sdk.UpsertCaCertificate(ctx,
Expand Down
6 changes: 2 additions & 4 deletions controller/konnect/ops/ops_kongcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package ops
import (
"context"
"errors"
"fmt"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -23,7 +21,7 @@ func createCertificate(
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", cert, client.ObjectKeyFromObject(cert))
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: CreateOp}
}

resp, err := sdk.CreateCertificate(ctx,
Expand Down Expand Up @@ -55,7 +53,7 @@ func updateCertificate(
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return fmt.Errorf("can't update %T without a ControlPlaneID", cert)
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: UpdateOp}
}

_, err := sdk.UpsertCertificate(ctx,
Expand Down
Loading

0 comments on commit 9e8f425

Please sign in to comment.