Skip to content

Commit

Permalink
fix: handle KongSNI conflict on creation (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo authored Oct 17, 2024
1 parent 879ba4a commit 8e6269f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
1 change: 1 addition & 0 deletions controller/konnect/ops/kongsni.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type SNIsSDK interface {
CreateSniWithCertificate(context.Context, sdkkonnectops.CreateSniWithCertificateRequest, ...sdkkonnectops.Option) (*sdkkonnectops.CreateSniWithCertificateResponse, error)
UpsertSniWithCertificate(ctx context.Context, request sdkkonnectops.UpsertSniWithCertificateRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertSniWithCertificateResponse, error)
DeleteSniWithCertificate(ctx context.Context, request sdkkonnectops.DeleteSniWithCertificateRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteSniWithCertificateResponse, error)
ListSni(ctx context.Context, request sdkkonnectops.ListSniRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.ListSniResponse, error)
}
74 changes: 74 additions & 0 deletions controller/konnect/ops/kongsni_mock.go

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

3 changes: 2 additions & 1 deletion controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func Create[
id, err = getControlPlaneForUID(ctx, sdk.GetControlPlaneSDK(), ent)
case *configurationv1alpha1.KongService:
id, err = getKongServiceForUID(ctx, sdk.GetServicesSDK(), ent)
case *configurationv1alpha1.KongSNI:
id, err = getSNIForUID(ctx, sdk.GetSNIsSDK(), ent)
// ---------------------------------------------------------------------
// TODO: add other Konnect types
default:
Expand All @@ -152,7 +154,6 @@ func Create[
}
case errors.As(err, &errRelationsFailed):
SetKonnectEntityProgrammedConditionFalse(e, errRelationsFailed.Reason, err.Error())
e.SetKonnectID(errRelationsFailed.KonnectID)
case err != nil:
SetKonnectEntityProgrammedConditionFalse(e, consts.KonnectEntitiesFailedToCreateReason, err.Error())
default:
Expand Down
35 changes: 30 additions & 5 deletions controller/konnect/ops/ops_kongsni.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
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"

Expand All @@ -33,14 +34,16 @@ func createSNI(
CertificateID: sni.Status.Konnect.CertificateID,
SNIWithoutParents: kongSNIToSNIWithoutParents(sni),
})

if errWrapped := wrapErrIfKonnectOpFailed(err, CreateOp, sni); errWrapped != nil {
SetKonnectEntityProgrammedConditionFalse(sni, "FailedToCreate", errWrapped.Error())
return errWrapped
}

if resp == nil || resp.Sni == nil || resp.Sni.ID == nil || *resp.Sni.ID == "" {
return fmt.Errorf("failed creating %s: %w", sni.GetTypeName(), ErrNilResponse)
}

// At this point, the SNI has been created successfully.
sni.Status.Konnect.SetKonnectID(*resp.Sni.ID)
SetKonnectEntityProgrammedCondition(sni)

return nil
}
Expand Down Expand Up @@ -75,6 +78,7 @@ func updateSNI(
if errors.As(errWrap, &sdkError) {
switch sdkError.StatusCode {
case 404:
logEntityNotFoundRecreating(ctx, sni, id)
if err := createSNI(ctx, sdk, sni); err != nil {
return FailedKonnectOpError[configurationv1alpha1.KongSNI]{
Op: UpdateOp,
Expand All @@ -92,11 +96,9 @@ func updateSNI(
}
}

SetKonnectEntityProgrammedConditionFalse(sni, "FailedToUpdate", errWrap.Error())
return errWrap
}

SetKonnectEntityProgrammedCondition(sni)
return nil
}

Expand Down Expand Up @@ -151,3 +153,26 @@ func kongSNIToSNIWithoutParents(sni *configurationv1alpha1.KongSNI) sdkkonnectco
Tags: GenerateTagsForObject(sni, sni.Spec.Tags...),
}
}

func getSNIForUID(ctx context.Context, sdk SNIsSDK, sni *configurationv1alpha1.KongSNI) (string, error) {
resp, err := sdk.ListSni(ctx, sdkkonnectops.ListSniRequest{
ControlPlaneID: sni.GetControlPlaneID(),
Tags: lo.ToPtr(UIDLabelForObject(sni)),
})
if err != nil {
return "", fmt.Errorf("failed to list SNI entities: %w", err)
}
if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing SNIs: %w", ErrNilResponse)
}

for _, item := range resp.Object.Data {
if item.Name == sni.Spec.Name {
return *item.ID, nil
}
}

return "", EntityWithMatchingUIDNotFoundError{
Entity: sni,
}
}

0 comments on commit 8e6269f

Please sign in to comment.