crossplane-contrib / crossplane-contrib/function-kro

Reusable code/snippets

Open
#31 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
23
Forks
8
Avg merge
14h 29m
Merged PRs (30d)
1

Description

### What problem are you facing?

Inline CEL expressions might be reused by different resources in a single composition. Other functions such as function-go-templating address this with helper functions.
`function-kro` doesn't look to support a mechanism to reuse code.

### How could this Function help solve your problem?

I can think of several options, specified by example:

#### Computed variables

As an example, see the following snippet where we define the ´managementPolicies´ and ´providerConfig´ once, and use it many:

```yaml
input:
apiVersion: kro.fn.crossplane.io/v1beta1
kind: ResourceGraph
variables:
managementPolicies: >-
${schema.spec.deletionProtection
? ["Create", "LateInitialize", "Observe", "Update"]
: ["Create", "Delete", "LateInitialize", "Observe", "Update"])}
providerConfigKind: ${schema.spec.providerConfigRef.?kind.orValue("ClusterProviderConfig")}
providerConfigName: ${schema.spec.providerConfigRef.?name.orValue("default")}
resources:
- id: role
template:
spec:
managementPolicies: ${variables.managementPolicies}
providerConfigRef:
kind: ${variables.providerConfigKind}
name: ${variables.providerConfigName}
...
- ...
```

#### Registered CEL custom functions

Implementation of functions, which could be registered as CEL custom functions.

```yaml
input:
apiVersion: kro.fn.crossplane.io/v1beta1
kind: ResourceGraph
# List of custom functions
functions:
managementPolicies:
params:
- name: deletionProtection
type: bool
- name: hasImports
type: bool
body: >-
deletionProtection
? ["Create", "LateInitialize", "Observe", "Update"]
: ["Create", "Delete", "LateInitialize", "Observe", "Update"]
providerConfigRef:
params:
- name: kind
type: optional(string)
- name: name
type: optional(string)
body:
kind: ${kind.orValue("ClusterProviderConfig")}
name: ${name.orValue("default")}

resources:
- id: role
template:
spec:
managementPolicies: >-
${fn.managementPolicies(schema.spec.deletionProtection, has(schema.spec.imports))}
providerConfigRef: >-
${fn.providerConfigRef(schema.spec.providerConfigRef.?kind, schema.spec.providerConfigRef.?name)}
forProvider:
...
- ...
```

or even something like the following, where after defining the functions, we might reference them for all resources in the composition:

```yaml
input:
...
functions: ... # same as above
resourceDefaults:
spec:
managementPolicies: >-
${fn.managementPolicies(schema.spec.deletionProtection, has(schema.spec.imports))}
providerConfigRef: >-
${fn.providerConfigRef(schema.spec.providerConfigRef.?kind, schema.spec.providerConfigRef.?name)}
resources: ...
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.