Overview
A managed resource (MR) represents an external service in a Provider. When
users create a new managed resource, the Provider reacts by creating an external
resource inside the Provider's environment. Every external service managed by
Crossplane maps to a managed resource.
Crossplane calls the object inside Kubernetes a managed resource and the external object inside the Provider an external resource.
Examples of managed resources include:
- Amazon AWS EC2
Instancedefined in the Official Provider for AWS. - Google Cloud GKE
Clusterdefined in the Official Provider for GCP. - Microsoft Azure PostgreSQL
Databasedefined in Official Provider for Azure.
Managed resource fields
The Provider defines the group, kind and version of a managed resource. The Provider also defines the available settings of a managed resource.
Group, kind and version
Each managed resource is a unique API endpoint with their own group, kind and version.
For example the AWS Provider defines the Instance kind from the group ec2.aws.upbound.io
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Instance
forProvider
The spec.forProvider of a
managed resource maps to the parameters of the external resource.
For example, when creating an AWS EC2 instance, the Provider supports defining the AWS region and the VM size, called the instanceType.
The Provider defines the settings and their valid values. Providers also define
required and optional values in the forProvider definition.
Refer to the documentation of your specific Provider for details.
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Instance
# Removed for brevity
spec:
forProvider:
region: us-west-1
instanceType: t2.micro
Crossplane considers the forProvider field of a managed resource
the "source of truth" for external resources. Crossplane overrides any changes
made to an external resource outside of Crossplane. If a user makes a change
inside a Provider's web console, Crossplane reverts that change back to what's
configured in the forProvider setting.
Referencing other resources
Some fields in a managed resource may depend on values from other managed resources. For example a VM may need the name of a virtual network to use.
Managed resources can reference other managed resources by external name, name reference or selector.
Matching by external name
When matching a resource by name Crossplane looks for the name of the external resource in the Provider.
For example, a AWS VPC object named test-vpc has the external name
vpc-01353cfe93950a8ff.
kubectl get vpc
NAME READY SYNCED EXTERNAL-NAME AGE
test-vpc True True vpc-01353cfe93950a8ff 49m
To match the VPC by name, use the external name. For example, creating a Subnet managed resource attached to this VPC.
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Subnet
spec:
forProvider:
# Removed for brevity
vpcId: vpc-01353cfe93950a8ff
Matching by name reference
To match a resource based on the name of the managed resource and not the
external resource name inside the Provider, use a nameRef.
For example, a AWS VPC object named test-vpc has the external name
vpc-01353cfe93950a8ff.
kubectl get vpc
NAME READY SYNCED EXTERNAL-NAME AGE
test-vpc True True vpc-01353cfe93950a8ff 49m
To match the VPC by name reference, use the managed resource name. For example, creating a Subnet managed resource attached to this VPC.
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Subnet
spec:
forProvider:
# Removed for brevity
vpcIdRef:
name: test-vpc
Matching by selector
Matching by selector is the most flexible matching method.
Use matchLabels to match the labels applied to a resource. For example, this
Subnet resource only matches VPC resources with the label
test-label: label-value.
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Subnet
spec:
forProvider:
# Removed for brevity
vpcIdSelector:
matchLabels:
test-label: label-value
Matching by controller reference
Matching a controller reference ensures that the matching resource has the same Kubernetes controller reference.
Matching helps trace resources composed by the same composite resource (XR).
Learn more about composite resources in the Composite Resources section.
Matching only a controller reference simplifies the matching process without requiring labels or more information.
For example, creating an AWS InternetGateway requires a VPC.
The InternetGateway could match a label, but every VPC created by this
Composition shares the same label.
Using matchControllerRef matches only the VPC created in the same composite
resource that created the InternetGateway.
Immutable fields
Some providers don't support changing the fields of some managed resources after
creation. For example, you can't change the region of an Amazon AWS
RDSInstance. These fields are immutable fields. Amazon requires you delete
and recreate the resource.
Crossplane allows you to edit the immutable field of a managed resource, but
doesn't apply the change. Crossplane never deletes a resource based on a
forProvider change.
Crossplane behaves differently than other tools like Terraform. Terraform deletes and recreates a resource to change an immutable field. Crossplane only deletes an external resource if their corresponding managed resource object is deleted from Kubernetes.
Late initialization
Crossplane treats the managed resource as the source of truth by default;
it expects to have all values under spec.forProvider including the
optional ones. If not provided, Crossplane populates the empty fields with
the values assigned by the provider. For example, consider fields such as
region and availabilityZone. You might specify only the region and let the
cloud provider choose the availability zone. In this case, if the provider
assigns an availability zone, Crossplane uses that value to populate the
spec.forProvider.availabilityZone field.
With managementPolicies, you can turn this off by not including the
LateInitialize policy in the managementPolicies list.
initProvider
The managed resource initProvider option is a beta feature related to
managementPolicies.
The
initProvider defines
settings Crossplane applies only when creating a new managed resource.
Crossplane ignores settings defined in the
initProvider
field that change after creation.
Settings in forProvider are always enforced by Crossplane. Crossplane reverts
any changes to a forProvider field in the external resource.
Settings in initProvider aren't enforced by Crossplane. Crossplane ignores any
changes to a initProvider field in the external resource.
Using initProvider is useful for setting initial values that a Provider may
automatically change, like an auto scaling group.
For example, creating a
NodeGroup
with an initial
desiredSize.
Crossplane doesn't change the
desiredSize
setting back when an autoscaler scales the Node Group external resource.
Crossplane recommends configuring
managementPolicies without
LateInitialize to avoid conflicts with initProvider settings.
apiVersion: eks.aws.upbound.io/v1beta1
kind: NodeGroup
metadata:
namespace: default
name: sample-eks-ng
spec:
managementPolicies: ["Observe", "Create", "Update", "Delete"]
initProvider:
scalingConfig:
- desiredSize: 1
forProvider:
region: us-west-1
scalingConfig:
- maxSize: 4
minSize: 1
managementPolicies
The managed resource managementPolicies option is a beta feature. Crossplane enables
beta features by default.
The Provider determines support for management policies.
Refer to the Provider's documentation to see if the Provider supports
management policies.
Crossplane
managementPolicies
determine which actions Crossplane can take on a
managed resource and its corresponding external resource.
Apply one or more
managementPolicies
to a managed resource to determine what permissions
Crossplane has over the resource.
For example, give Crossplane permission to create and delete an external resource, but not make any changes, set the policies to ["Create", "Delete", "Observe"].
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Subnet
spec:
managementPolicies: ["Create", "Delete", "Observe"]
forProvider:
# Removed for brevity
The default policy grants Crossplane full control over the resources.
Defining the managementPolicies field with an empty array pauses
the resource.
The Provider determines support for management policies.
Refer to the Provider's documentation to see if the Provider supports
management policies.
Crossplane supports the following policies:
| Policy | Description |
|---|---|
* | Default policy. Crossplane has full control over a resource. |
Create | If the external resource doesn't exist, Crossplane creates it based on the managed resource settings. |
Delete | Crossplane can delete the external resource when deleting the managed resource. |
LateInitialize | Crossplane initializes some external resource settings not defined in the spec.forProvider of the managed resource. See the late initialization section for more details. |
Observe | Crossplane only observes the resource and doesn't make any changes. Used for observe only resources. |
Update | Crossplane changes the external resource when changing the managed resource. |
providerConfigRef
The providerConfigRef on a managed resource tells the Provider which
ProviderConfig to
use when creating the managed resource.
Use a ProviderConfig to define the authentication method to use when communicating to the Provider.
If providerConfigRef isn't applied, Providers use the ProviderConfig named default.
For example, a managed resource references a ProviderConfig named user-keys.
This matches the name of a ProviderConfig.
apiVersion: ec2.aws.upbound.io/v1beta1
kind: Instance
spec:
forProvider:
# Removed for brevity
providerConfigRef: user-keys
apiVersion: aws.upbound.io/v1beta1
kind: ProviderConfig
metadata:
name: user-keys
# Removed for brevity
Each managed resource can reference different ProviderConfigs. This allows different managed resources to authenticate with different credentials to the same Provider.