crossplane-contrib / function-kcl

Crossplane Composition Functions using KCL Programming Language
Apache License 2.0
41 stars 17 forks source link

Retry delay one kcl error? #184

Open dennybaa opened 2 weeks ago

dennybaa commented 2 weeks ago

Hello @Peefy,

I've been learning how to use KCL function. And it's great thanks a lot)! But I've bumped into not very neat behavior, if KCL exits with an error function is immediately reevaluated and this pollutes info logs. I wonder if this case is specific to the KCL function only or it's crossplane composite functions architecture feature. Btw I haven't noticed this type of behaviour with patch-and-transform function.

Here's the example:

_groupPolicies = option("params").groupPolicies
_spec = option("params").oxr.spec
# Pick vault provider
_provider = "iaas-security-${_spec.env}-vault"
_outputs = option("params").ocds["datasource"]?.Resource?.status?.atProvider?.outputs
_items = []

if _outputs?.group_id:
    # Effectevly wait for a composed resource to appear
    _items = [{
        **_groupPolicies
        metadata.annotations: {"krm.kcl.dev/composition-resource-name" = option("params").name}
        spec.providerConfigRef.name = _provider
        spec.forProvider: {
            policies = _spec.policies
            groupId = _outputs?.group_id
        }
    }]

items = _items

To suppress immediate reevaluation and logs pollution I have to manually guard resource creation. Is this expected behavior?

Peefy commented 2 weeks ago

Have you tried the function-go-templating? The logic of retry is not determined by KCL itself, and I seem to have not found how to set the retry time in the function.

dennybaa commented 2 weeks ago

No, I haven't tried( Let's try to ask @ytsarev . Yury would you mind please sharing your suggestion? What's the best way to handle the delay, should it be inside the KCL function implementation, or maybe the function should return 0 code? The later of course would be odd, however if functions successfully exist crossplane doesn't immediately re-evaluate them, kinda odd mitigation...

Thank you!

ytsarev commented 2 weeks ago

We end up using https://github.com/crossplane-contrib/function-sequencer together with the function-kcl for sequenced creation. Example: https://github.com/upbound/configuration-aws-eks/blob/main/apis/kcl/generate.k#L42-L72