From fe61cece4fdcc370efce081880718e7b86f6eb09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Mon, 30 Sep 2024 13:14:42 +0200 Subject: [PATCH] feat(konnect): fallback to create on not found error when upserting the SNI --- controller/konnect/ops/ops_kongsni.go | 32 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/controller/konnect/ops/ops_kongsni.go b/controller/konnect/ops/ops_kongsni.go index a172cf237..2bedd1c5d 100644 --- a/controller/konnect/ops/ops_kongsni.go +++ b/controller/konnect/ops/ops_kongsni.go @@ -23,7 +23,6 @@ func createSNI( sdk SNIsSDK, sni *configurationv1alpha1.KongSNI, ) error { - cpID := sni.GetControlPlaneID() if cpID == "" { return fmt.Errorf("can't create %T %s without a Konnect ControlPlane ID", sni, client.ObjectKeyFromObject(sni)) @@ -70,9 +69,34 @@ func updateSNI( SNIWithoutParents: kongSNIToSNIWithoutParents(sni), }) - if errWrapped := wrapErrIfKonnectOpFailed(err, UpdateOp, sni); errWrapped != nil { - SetKonnectEntityProgrammedConditionFalse(sni, "FailedToUpdate", errWrapped.Error()) - return errWrapped + // 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 + if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, sni); errWrap != nil { + // SNI update operation returns an SDKError instead of a NotFoundError. + var sdkError *sdkkonnecterrs.SDKError + if errors.As(errWrap, &sdkError) { + switch sdkError.StatusCode { + case 404: + if err := createSNI(ctx, sdk, sni); err != nil { + return FailedKonnectOpError[configurationv1alpha1.KongSNI]{ + Op: UpdateOp, + Err: err, + } + } + // Create succeeded, createSNI sets the status so no need to do this here. + + return nil + default: + return FailedKonnectOpError[configurationv1alpha1.KongSNI]{ + Op: UpdateOp, + Err: sdkError, + } + } + } + + SetKonnectEntityProgrammedConditionFalse(sni, "FailedToUpdate", errWrap.Error()) + return errWrap } SetKonnectEntityProgrammedCondition(sni)