Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Repository files navigation

function-pythonic

Archived

function-pythonic is now hosted and maintained at https://github.com/crossplane-contrib/function-pythonic. Please submit all Issues and Pull Requests at the new location.

Introduction

A Crossplane composition function that lets you compose Composites using a set of python classes enabling an elegant and terse syntax. Here is what the following example is doing:

  • Create an MR named 'vpc' with apiVersion 'ec2.aws.crossplane.io/v1beta1' and kind 'VPC'
  • Set the vpc region and cidr from the XR spec values
  • Set the XR status.vpcId to the created vpc id
apiVersion: apiextensions.crossplane.io/v1kind: Compositionmetadata:
name: create-vpcspec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1kind: XRmode: Pipelinepipeline:
- step: functionRef:
name: function-pythonicinput:
apiVersion: pythonic.fn.fortra.com/v1alpha1kind: Compositecomposite: | class VpcComposite(BaseComposite): def compose(self): vpc = self.resources.vpc('ec2.aws.crossplane.io/v1beta1', 'VPC') vpc.spec.forProvider.region = self.spec.region vpc.spec.forProvider.cidrBlock = self.spec.cidr self.status.vpcId = vpc.status.atProvider.vpcId

In addtion to an inline script, the python implementation can be specified as the complete path to a python class. Python packages can be deployed using ConfigMaps, enabling using your IDE of choice for writting the code. See ConfigMap Packages and Filing System Packages.

Examples

In the examples directory are many exemples, including all of the function-go-templating examples implemented using function-pythonic. The eks-cluster example is a good complex example creating the entire vpc structure needed for an EKS cluster.

Installing function-pythonic

apiVersion: pkg.crossplane.io/v1kind: Functionmetadata:
name: function-pythonicspec:
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.1.5

Composed Resource Dependencies

function-pythonic automatically handles dependencies between composed resources.

Just compose everything as if it is immediately created and the framework will delay the creation of any resources which depend on other resources which do not exist yet. In other words, it accomplishes what function-sequencer provides, but it automatically detects the dependencies.

If a resource has been created and a dependency no longer exists due to some unexpected condition, the composition will be terminated or the observed value for that field will be used, depending on the unknownsFatal settings.

Take the following example:

vpc = self.resources.VPC('ec2.aws.crossplane.io/v1beta1', 'VPC')vpc.spec.forProvider.region = 'us-east-1vpc.spec.forProvider.cidrBlock = '10.0.0.0/16'subnet = self.resources.SubnetA('ec2.aws.crossplane.io/v1beta1', 'Subnet')subnet.spec.forProvider.region = 'us-east-1'subnet.spec.forProvider.vpcId = vpc.status.atProvider.vpcIdsubnet.spec.forProvider.availabilityZone = 'us-east-1a'subnet.spec.forProvider.cidrBlock = '10.0.0.0/20'

If the Subnet does not yet exist, the framework will detect if the vpcId set in the Subnet is unknown, and will delay the creation of the subnet.

Once the Subnet has been created, if for some unexpected reason the vpcId passed to the Subnet is unknown, the framework will detect it and either terminate the Composite composition or use the vpcId in the observed Subnet. The default action taken is to fast fail by terminating the composition. This can be overridden for all composed resource by setting the Composite self.unknownsFatal field to False, or at the individual composed resource level by setting the Resource.unknownsFatal field to False.

Usage Dependencies

function-pythonic can be configured to automatically create Crossplane Usages dependencies between resources. Modifying the above VPC example with:

self.usages = Truevpc = self.resources.VPC('ec2.aws.crossplane.io/v1beta1', 'VPC')vpc.spec.forProvider.region = 'us-east-1vpc.spec.forProvider.cidrBlock = '10.0.0.0/16'subnet = self.resources.SubnetA('ec2.aws.crossplane.io/v1beta1', 'Subnet')subnet.spec.forProvider.region = 'us-east-1'subnet.spec.forProvider.vpcId = vpc.status.atProvider.vpcIdsubnet.spec.forProvider.availabilityZone = 'us-east-1a'subnet.spec.forProvider.cidrBlock = '10.0.0.0/20'

Will generate the appropriate Crossplane Usage resource.

Pythonic access of Protobuf Messages

All Protobuf messages are wrapped by a set of python classes which enable using both object attribute names and dictionary key names to traverse the Protobuf message contents. For example, the following examples obtain the same value from the RunFunctionRequest message:

region=request.observed.composite.resource.spec.regionregion=request['observed']['composite']['resource']['spec']['region']

Getting values from free form map and list values will not throw errors for keys that do not exist, but will return an unknown placeholder which evaluates as False. For example, the following will evaluate as False with a just created RunFunctionResponse message:

vpcId=response.desired.resources.vpc.resource.status.atProvider.vpcIdifvpcId:
# The vpcId is available

Note that maps or lists that do exist but do not have any members will evaluate as True, contrary to Python dicts and lists. Use the len function to test if the map or list exists and has members.

When setting fields, all intermediary unknown placeholders will automatically be created. For example, this will create all items needed to set the region on the desired resource:

response.desired.resources.vpc.resource.spec.forProvider.region='us-east-1'

Calling a message or map will clear it and will set any provided key word arguments. For example, this will either create or clear the resource and then set its apiVersion and kind:

response.desired.resources.vpc.resource(apiVersion='ec2.aws.crossplane.io/v1beta1', kind='VPC')

The following functions are provided to create Protobuf structures:

FunctionDescription
MapCreate a new Protobuf map
ListCreate a new Protobuf list
UnknownCreate a new Protobuf unknown placeholder
YamlCreate a new Protobuf structure from a yaml string
JsonCreate a new Protobuf structure from a json string
B64EncodeEncode a string into base 64
B64DecodeDecode a string from base 64

The following items are supported in all the Protobuf Message wrapper classes: bool, len, contains, iter, hash, ==, str, format

To convert a Protobuf message to a string value, use either str or format.

yaml=str(request) # get the request as yamlyaml=format(request) # also get the request as yamlyaml=format(request, 'yaml') # yet another get the request as yamljson=format(request, 'json') # get the request as jsonjson=format(request, 'jsonc') # get the request as json compactproto=format(request, 'protobuf') # get the request as a protobuf string

Composite Composition

Composite composition is performed from a Composite orientation. A BaseComposite class is subclassed and the compose method is implemented.

classMyComposite(BaseComposite):
defcompose(self):
# Compose the Composite

The compose method can also declare itself as performing async io:

classMyAsyncComposite(BaseComposite):
asyncdefcompose(self):
# Compose the Composite using async io when needed

BaseComposite

The BaseComposite class provides the following fields for manipulating the Composite itself:

FieldTypeDescription
self.observedMapLow level direct access to the observed composite
self.desiredMapLow level direct access to the desired composite
self.apiVersionStringThe composite observed apiVersion
self.kindStringThe composite observed kind
self.metadataMapThe composite observed metadata
self.specMapThe composite observed spec
self.statusMapThe composite desired and observed status, read from observed if not in desired
self.conditionsConditionsThe composite desired and observed conditions, read from observed if not in desired
self.eventsEventsReturned events against the Composite and optionally on the Claim
self.connectionConnectionThe composite desired and observed connection detials, read from observed if not in desired
self.readyBooleanThe composite desired ready state

The BaseComposite also provides access to the following Crossplane Function level features:

FieldTypeDescription
self.requestMessageLow level direct access to the RunFunctionRequest message
self.responseMessageLow level direct access to the RunFunctionResponse message
self.loggerLoggerPython logger to log messages to the running function stdout
self.parametersMapThe configured step parameters
self.ttlIntegerGet or set the response TTL, in seconds
self.credentialsCredentialsThe request credentials
self.contextMapThe response context, initialized from the request context
self.environmentMapThe response environment, initialized from the request context environment
self.requiredsRequiredsRequest and read additional local Kubernetes resources
self.resourcesResourcesDefine and process composed resources
self.unknownsFatalBooleanTerminate the composition if already created resources are assigned unknown values, default True
self.usagesBooleanGenerate Crossplane Usages for resource dependencies, default False
self.autoReadyBooleanPerform auto ready processing on all composed resources, default True

Composed Resources

Creating and accessing composed resources is performed using the BaseComposite.resources field. BaseComposite.resources is a dictionary of the composed resources whose key is the composition resource name. The value returned when getting a resource from BaseComposite is the following Resource class:

FieldTypeDescription
Resource(apiVersion,kind,namespace,name)ResourceReset the resource and set the optional parameters
Resource.nameStringThe composition composed resource name
Resource.observedMapLow level direct access to the observed composed resource
Resource.desiredMapLow level direct access to the desired composed resource
Resource.apiVersionStringThe composed resource apiVersion
Resource.kindStringThe composed resource kind
Resource.externalNameStringThe composed resource external name
Resource.metadataMapThe composed resource desired metadata
Resource.specMapThe resource spec
Resource.dataMapThe resource data
Resource.statusMapThe resource status
Resource.conditionsConditionsThe resource conditions
Resource.connectionConnectionThe resource connection details
Resource.readyBooleanThe resource ready state
Resource.unknownsFatalBooleanTerminate the composition if this resource has been created and is assigned unknown values, default is Composite.unknownsFatal
Resource.usagesBooleanGenerate Crossplane Usages for this resource, default is Composite.autoReady
Resource.autoReadyBooleanPerform auto ready processing on this resource, default is Composite.autoReady

Required Resources (AKA Extra Resources)

Creating and accessing required resources is performed using the BaseComposite.requireds field. BaseComposite.requireds is a dictionary of the required resources whose key is the required resource name. The value returned when getting a required resource from BaseComposite is the following RequiredResources class:

FieldTypeDescription
RequiredResource(apiVersion,kind,namespace,name,labels)RequiredResourceReset the required resource and set the optional parameters
RequiredResources.nameStringThe required resources name
RequiredResources.apiVersionStringThe required resources apiVersion
RequiredResources.kindStringThe required resources kind
RequiredResources.namespaceStringThe namespace to match when returning the required resources, see note below
RequiredResources.matchNameStringThe names to match when returning the required resources
RequiredResources.matchLabelsMapThe labels to match when returning the required resources

The current version of crossplane-sdk-python used by function-pythonic does not support namespace selection. For now, use matchLabels and filter the results if required.

RequiredResources acts like a Python list to provide access to the found required resources. Each resource in the list is the following RequiredResource class:

FieldTypeDescription
RequiredResource.nameStringThe required resource name
RequiredResource.observedMapLow level direct access to the observed required resource
RequiredResource.apiVersionStringThe required resource apiVersion
RequiredResource.kindStringThe required resource kind
RequiredResource.metadataMapThe required resource metadata
RequiredResource.specMapThe required resource spec
RequiredResource.dataMapThe required resource data
RequiredResource.statusMapThe required resource status
RequiredResource.conditionsMapThe required resource conditions

Conditions

The BaseComposite.conditions, Resource.conditions, and RequiredResource.conditions fields are maps of that entity's status conditions array, with the map key being the condition type. The fields are read only for Resource.conditions and RequiredResource.conditions.

FieldTypeDescription
Condition.typeStringThe condtion type, or name
Condition.statusBooleanThe condition status
Condition.reasonStringPascalCase, machine-readable reason for this condition
Condition.messageStringHuman-readable details about the condition
Condition.lastTransitionTimeTimestampLast transition time, read only
Condition.claimBooleanAlso apply the condition the claim

Events

The BaseComposite.events field is a list of events to apply to the Composite and optionally to the Claim.

FieldTypeDescription
Event.infoBooleanNormal informational event
Event.warningBooleanWarning level event
Event.fatalBooleanFatal events also terminate composing the Composite
Event.reasonStringPascalCase, machine-readable reason for this event
Event.messageStringHuman-readable details about the event
Event.claimBooleanAlso apply the event to the claim

Single use Composites

Tired of creating a CompositeResourceDefinition, a Composition, and a Composite just to run that Composition once in a single use or initialize task?

function-pythonic installs a Composite CompositeResourceDefinition that enables creating such tasks using a single Composite resource:

apiVersion: pythonic.fortra.com/v1alpha1kind: Compositemetadata:
name: composite-examplespec:
composite: | class HelloComposite(BaseComposite): def compose(self): self.status.composite = 'Hello, World!'

Quick Start Development

The following example demonstrates how to locally render function-python compositions. First, install the crossplane-function-pythonic python package into the python environment:

$ pip install crossplane-function-pythonic

Next, create the following files:

xr.yaml

apiVersion: pythonic.fortra.com/v1alpha1kind: Hellometadata:
name: worldspec:
who: World

composition.yaml

apiVersion: apiextensions.crossplane.io/v1kind: Compositionmetadata:
name: hellos.pythonic.fortra.comspec:
compositeTypeRef:
apiVersion: pythonic.fortra.com/v1alpha1kind: Hellomode: Pipelinepipeline:
- step: pythonicfunctionRef:
name: function-pythonicinput:
apiVersion: pythonic.fn.fortra.com/v1alpha1kind: Compositecomposite: | class GreetingComposite(BaseComposite): def compose(self): self.status.greeting = f"Hello, {self.spec.who}!"

functions.yaml

apiVersion: pkg.crossplane.io/v1beta1kind: Functionmetadata:
name: function-pythonicannotations:
render.crossplane.io/runtime: Developmentspec:
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.1.5

In one terminal session, run function-pythonic:

$ function-pythonic --insecure --debug --render-unknowns
[2025-08-21 15:32:37.966] grpc._cython.cygrpc [DEBUG ] Using AsyncIOEngine.POLLER as I/O engine

In another terminal session, render the Composite:

$ crossplane render xr.yaml composition.yaml functions.yaml
---
apiVersion: pythonic.fortra.com/v1alpha1
kind: Hello
metadata:
name: world
status:
conditions:
- lastTransitionTime: "2024-01-01T00:00:00Z"
reason: Available
status: "True"
type: Ready
- lastTransitionTime: "2024-01-01T00:00:00Z"
message: All resources are composed
reason: AllComposed
status: "True"
type: ResourcesComposed
greeting: Hello, World!

ConfigMap Packages

ConfigMap based python packages are enable using the --packages and --packages-namespace command line options. ConfigMaps with the label function-pythonic.package will be incorporated in the python path at the location configured in the label value. For example, the following ConfigMap will enable python to use import example.pythonic.features

apiVersion: v1kind: ConfigMapmetadata:
namespace: crossplane-systemname: example-pythoniclabels:
function-pythonic.package: example.pythonicdata:
features.py: | def anything(): return 'something'

Then, in your Composition:

...
- step: pythonicfunctionRef:
name: function-pythonicinput:
apiVersion: pythonic.fn.fortra.com/v1alpha1kind: Compositecomposite: | from example.pythonic import features class FetureComposite(BaseComposite): def compose(self): anything = features.anything()...

The entire function-pythonic Composite class can be coded in the ConfigMap and only the complete Composite class path is needed in the step configuration.

apiVersion: v1kind: ConfigMapmetadata:
namespace: crossplane-systemname: example-pythoniclabels:
function-pythonic.package: example.pythonicdata:
features.py: | from crossplane.pythonic import BaseComposite class FeatureOneComposite(BaseComposite): def compose(self): # go at it!
...
- step: pythonicfunctionRef:
name: function-pythonicinput:
apiVersion: pythonic.fn.fortra.com/v1alpha1kind: Compositecomposite: example.pythonic.features.FeatureOneComposite...

This requires enabling the the packages support using the --packages command line option in the DeploymentRuntimeConfig and configuring the required Kubernetes RBAC permissions. For example:

apiVersion: pkg.crossplane.io/v1kind: Functionmetadata:
name: function-pythonicspec:
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.1.5runtimeConfigRef:
name: function-pythonic
---
apiVersion: pkg.crossplane.io/v1beta1kind: DeploymentRuntimeConfigmetadata:
name: function-pythonicspec:
deploymentTemplate:
spec:
selector: {}template:
spec:
containers:
- name: package-runtimeargs:
- --debug
- --packagesserviceAccountName: function-pythonicserviceAccountTemplate:
metadata:
name: function-pythonic
---
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:
name: function-pythonicrules:
- apiGroups:
- ''resources:
- configmapsverbs:
- list
- watch
- patch
- apiGroups:
- ''resources:
- eventsverbs:
- create
---
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:
name: function-pythonicroleRef:
apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: function-pythonicsubjects:
- kind: ServiceAccountnamespace: crossplane-systemname: function-pythonic

When enabled, labeled ConfigMaps are obtained cluster wide, requiring the above ClusterRole permissions. The --packages-namespace command line option will restrict to only using the supplied namespace. This option can be invoked multiple times. The above RBAC permission can then be per namespace RBAC Role permissions.

Secrets can also be used in an identical manner as ConfigMaps by enabling the --packages-secrets command line option. Secrets permissions need to be added to the above RBAC configuration.

Step Parameters

Step specific parameters can be configured to be used by the composite implementation. This is useful when setting the composite to the python class. For example:

apiVersion: v1kind: ConfigMapmetadata:
namespace: crossplane-systemname: example-pythoniclabels:
function-pythonic.package: example.pythonicdata:
features.py: | from crossplane.pythonic import BaseComposite class GreetingComposite(BaseComposite): def compose(self): cm = self.resources.ConfigMap('v1', 'ConfigMap') cm.data.greeting = f"Hello, {self.parameters.who}!"
...
- step: pythonicfunctionRef:
name: function-pythonicinput:
apiVersion: pythonic.fn.fortra.com/v1alpha1kind: Compositeparameters:
who: Worldcomposite: example.pythonic.features.GreetingComposite...

Filing System Packages

Composition Composite implementations can be coded in a stand alone python files by configuring the function-pythonic deployment with the code mounted into the package-runtime container, and then adding the mount point to the python path using the --python-path command line option.

apiVersion: pkg.crossplane.io/v1beta1kind: DeploymentRuntimeConfigmetadata:
name: function-pythonicspec:
deploymentTemplate:
spec:
template:
spec:
containers:
- name: package-runtimeargs:
- --debug
- --python-path
- /mnt/compositesvolumeMounts:
- name: compositesmountPath: /mnt/compositesvolumes:
- name: compositesconfigMap:
name: pythonic-composites

See the filing-system example.

Install Additional Python Packages

function-pythonic supports a --pip-install command line option which will run pip install with the configured pip install command. For example:

apiVersion: pkg.crossplane.io/v1beta1kind: DeploymentRuntimeConfigmetadata:
name: function-pythonicspec:
deploymentTemplate:
spec:
template:
spec:
containers:
- name: package-runtimeargs:
- --debug
- --pip-install
- --quiet aiobotocore==2.23.2

Enable Oversize Protos

The Protobuf python package used by function-pythonic limits the depth of yaml elements and the total size of yaml parsed. This results in a limit of approximately 30 levels of nested yaml fields. This check can be disabled using the --allow-oversize-protos command line option. For example:

apiVersion: pkg.crossplane.io/v1beta1kind: DeploymentRuntimeConfigmetadata:
name: function-pythonicspec:
deploymentTemplate:
spec:
template:
spec:
containers:
- name: package-runtimeargs:
- --debug
- --allow-oversize-protos

About

Python based Crossplane Function providing a clean and elegant syntax for writing Crossplane Compositions.

Resources

Stars

10 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages