Skip to content

Commit

Permalink
fix: handle Certificate creation conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Jintao Zhang <[email protected]>
  • Loading branch information
tao12345666333 committed Oct 21, 2024
1 parent 9e8f425 commit d598b20
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions controller/konnect/ops/kongcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type CertificatesSDK interface {
CreateCertificate(ctx context.Context, controlPlaneID string, certificate sdkkonnectcomp.CertificateInput, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateCertificateResponse, error)
UpsertCertificate(ctx context.Context, request sdkkonnectops.UpsertCertificateRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertCertificateResponse, error)
DeleteCertificate(ctx context.Context, controlPlaneID string, certificateID string, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteCertificateResponse, error)
ListCertificate(ctx context.Context, request sdkkonnectops.ListCertificateRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.ListCertificateResponse, error)
}
74 changes: 74 additions & 0 deletions controller/konnect/ops/kongcertificate_mock.go

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

2 changes: 2 additions & 0 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func Create[
id, err = getKongKeyForUID(ctx, sdk.GetKeysSDK(), ent)
case *configurationv1alpha1.KongUpstream:
id, err = getKongUpstreamForUID(ctx, sdk.GetUpstreamsSDK(), ent)
case *configurationv1alpha1.KongCertificate:
id, err = getKongCertificateForUID(ctx, sdk.GetCertificatesSDK(), ent)
// ---------------------------------------------------------------------
// TODO: add other Konnect types
default:
Expand Down
34 changes: 28 additions & 6 deletions controller/konnect/ops/ops_kongcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ops
import (
"context"
"errors"
"fmt"
"github.com/samber/lo"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
Expand Down Expand Up @@ -33,12 +35,15 @@ func createCertificate(
// Can't adopt it as it will cause conflicts between the controller
// that created that entity and already manages it, hm
if errWrap := wrapErrIfKonnectOpFailed(err, CreateOp, cert); errWrap != nil {
SetKonnectEntityProgrammedConditionFalse(cert, "FailedToCreate", errWrap.Error())
return errWrap
}

cert.Status.Konnect.SetKonnectID(*resp.Certificate.ID)
SetKonnectEntityProgrammedCondition(cert)
if resp == nil || resp.Certificate == nil || resp.Certificate.ID == nil || *resp.Certificate.ID == "" {
return fmt.Errorf("failed creating %s: %w", cert.GetTypeName(), ErrNilResponse)
}

// At this point, the Certificate has been created successfully.
cert.SetKonnectID(*resp.Certificate.ID)

return nil
}
Expand Down Expand Up @@ -89,12 +94,9 @@ func updateCertificate(
}
}
}
SetKonnectEntityProgrammedConditionFalse(cert, "FailedToUpdate", errWrap.Error())
return errWrap
}

SetKonnectEntityProgrammedCondition(cert)

return nil
}

Expand Down Expand Up @@ -139,3 +141,23 @@ func kongCertificateToCertificateInput(cert *configurationv1alpha1.KongCertifica
Tags: GenerateTagsForObject(cert, cert.Spec.Tags...),
}
}

func getKongCertificateForUID(
ctx context.Context,
sdk CertificatesSDK,
cert *configurationv1alpha1.KongCertificate,
) (string, error) {
resp, err := sdk.ListCertificate(ctx, sdkkonnectops.ListCertificateRequest{
ControlPlaneID: cert.GetControlPlaneID(),
Tags: lo.ToPtr(UIDLabelForObject(cert)),
})
if err != nil {
return "", fmt.Errorf("failed to list %s: %w", cert.GetTypeName(), err)
}

if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", cert.GetTypeName(), ErrNilResponse)
}

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

0 comments on commit d598b20

Please sign in to comment.