Automatically copying resources to downstream clusters
|
This is an experimental feature. |
From Fleet v0.14.0 onwards, Fleet supports propagating external resources to downstream clusters.
This simplifies dealing with dependencies of charts, such as values coming from external resources. See also valuesFrom.
How it works
HelmOps support a new downstreamResources field, which can be used to reference resources by kind and name.
Those resources must:
-
Be either secrets or config maps. No other `kind`s are currently supported.
-
Exist before being referenced from the HelmOp, and live in the same namespace as the HelmOp referencing them.
-
Match an entry of
allowedDownstreamResourcesin aPolicyin that namespace, if aPolicythere setsrequireServiceAccount: trueor has any entries. See Tenant namespaces.
Example:
apiVersion: fleet.cattle.io/v1alpha1
kind: HelmOp
[...] # metadata
spec:
helm:
[...] # Helm options
downstreamResources:
- kind: Secret
name: my-secret
- kind: ConfigMap
name: my-config
This instructs the Fleet controller to copy those resources to each targeted downstream cluster, before deploying the workload (in this case specified through a Helm chart) to said downstream cluster.
When a cluster is not targeted anymore, the Fleet agent will delete those resources from the cluster as well. They will remain on the upstream cluster, though.
|
If resources referenced through |
Permissions
Since Fleet v0.15.7, when a ServiceAccount is resolved for the deployment on the downstream cluster, the agent writes the copies as that ServiceAccount. This is the account set in serviceAccount, or the fleet-default account in cattle-fleet-system if none is set and it exists. The ServiceAccount needs get, create and update on the copied kinds in the deployment namespace.
The deployment namespace must exist before the copy runs. If it does not, Fleet tries to create it as the ServiceAccount, which requires create on the cluster-scoped namespaces resource. Do not grant that verb to a tenant, because it cannot be limited to particular namespaces. Create the namespace as an operator instead.
Removing the copies when a cluster is no longer targeted is done by the agent itself and needs no permission on the ServiceAccount. The agent only removes objects that carry the fleet.cattle.io/bundledeployment label Fleet sets on the copies.
|
A missing permission is not a failed deployment
If the |
Tenant namespaces
The Fleet controller reads the referenced Secrets and ConfigMaps on the management cluster with its own credentials. Without a restriction, anyone who can create a deployment in the namespace could have any Secret or ConfigMap in it copied to a downstream cluster, where a workload in the deployment namespace can read it.
Since Fleet v0.15.9, a Policy decides which of them may be copied, through the field allowedDownstreamResources. Each entry names a kind, Secret or ConfigMap, and either a name or a namePattern:
kind: Policy
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: tenant-policy
namespace: workspace1
requireServiceAccount: true
allowedDownstreamResources:
- kind: Secret
name: my-secret
- kind: ConfigMap
namePattern: app-config-.*
The entries of all Policy objects in the namespace are combined into one list, and requireServiceAccount counts as true if any Policy there sets it:
-
If the combined list has entries, every Secret and ConfigMap in
downstreamResourcesmust match one of them, whicheverPolicyit comes from. -
If the combined list is empty and a
PolicysetsrequireServiceAccount: true, no Secret or ConfigMap is copied. -
If the combined list is empty and no
PolicysetsrequireServiceAccount: true, every referenced object is copied, as before.
If a referenced object is not allowed, Fleet refuses the Bundle. The Bundle, and the GitRepo or HelmOp it comes from, report Ready=False with a message naming the object:
downstream resource Secret my-secret is not allowed by Policy: it is not listed in allowedDownstreamResources
The check covers the downstreamResources of the deployment and those set on its targets. It only compares kinds and names and does not read the objects. While the Bundle is refused, its existing deployments stay as they are and no changes are rolled out. Once the Policy allows the object, Fleet picks up the change and proceeds without further action.
Removing an entry refuses the Bundle again. Copies that were already made are kept, on the management cluster and on downstream clusters, but no longer updated.
Only list objects whose content may be visible to whoever deploys from that namespace. Do not list credentials that are meant to be used but not read, such as Git or Helm repository credentials.
Whoever can write Policy objects in a namespace decides what may be copied from it. Restrict write access to Policy objects to administrators.
Finding the objects to list
After upgrading from a version before v0.15.9, Bundles in a namespace whose Policy sets requireServiceAccount: true are refused until every Secret and ConfigMap they reference matches an entry. The following lists those objects, and can be run before the upgrade. It needs kubectl and jq, and read access to Policy and Bundle objects on the management cluster:
for ns in $(kubectl get policies.fleet.cattle.io -A -o json |
jq -r '.items[] | select(.requireServiceAccount == true) | .metadata.namespace' | sort -u); do
kubectl get bundles.fleet.cattle.io -n "$ns" -o json |
jq -r --arg ns "$ns" '.items[]
| (.spec.downstreamResources // []) + ([.spec.targets[]?.downstreamResources // []] | add // [])
| .[] | select(.kind | ascii_downcase | . == "secret" or . == "configmap")
| "\($ns) \(.kind) \(.name)"'
done | sort -u
Each line is <namespace> <kind> <name>. The entries can go into any Policy in the namespace, including a new one, since the lists of all Policy objects in a namespace are combined.
Monitoring
From version v0.15.0 onwards, Fleet monitors changes made to secrets and configmaps that are listed in DownstreamResources. It also checks whether a secret or configmap was created after the Bundle was created.
When a referenced secret or configmap is created or updated on the upstream cluster, Fleet will propagate those changes to downstream clusters, and re-deploy the workload.
This enables deployments to make use of the latest versions of those referenced resources, which may represent configuration, Helm values, etc.
Since Fleet v0.15.9, changes to a Policy in the namespace are picked up the same way, see Tenant namespaces.