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)