-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce ResourceSet
API
#98
Conversation
54aaa3c
to
bc20588
Compare
b5209e1
to
1d25e13
Compare
81999fb
to
023b4f1
Compare
b8389fa
to
fcee5c7
Compare
a3e85b9
to
9195fb3
Compare
d0a2611
to
63b8450
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First batch of comments 🤗
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
Signed-off-by: Stefan Prodan <[email protected]>
1fd2333
to
d85159e
Compare
Signed-off-by: Stefan Prodan <[email protected]>
8251d57
to
5e746ca
Compare
Signed-off-by: Matheus Pimenta <[email protected]>
5e746ca
to
2af55cf
Compare
Signed-off-by: Stefan Prodan <[email protected]>
d1436b9
to
ff4e0fe
Compare
3da18fa
to
cda51ae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, after resolving the remaining comments I think we can merge 👍
Signed-off-by: Stefan Prodan <[email protected]>
cda51ae
to
3377938
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 🚀 🚀
ResourceSet is a declarative API for generating a group of Kubernetes objects
based on a matrix of input values and a set of templated resources.
The ResourceSet API offers a high-level abstraction for defining and managing
Flux resources and related Kubernetes objects as a single unit. It is designed
to reduce the complexity of Kustomize overlays by providing a compact way
of defining different configurations for a set of workloads per tenant and/or environment.
Use cases:
Example
The following example shows a ResourceSet that generates an application instance consisting of a
Flux HelmRelease and OCIRepository for each tenant with a specific version and replica count.
You can run this example by saving the manifest into
podinfo.yaml
.Apply the ResourceSet on the cluster:
Wait for the ResourceSet to reconcile the generated resources:
kubectl wait resourceset/podinfo --for=condition=ready --timeout=5m
Run
kubectl get resourceset
to see the status of the resource:Run
kubectl describe resourceset
to see the reconciliation status conditions and events:Run
kubectl events
to see the events generated by the flux-operator:Run
kubectl delete
to remove the ResourceSet and its generated resources:Writing a ResourceSet spec
As with all other Kubernetes config, a ResourceSet needs
apiVersion
,kind
,metadata.name
andmetadata.namespace
fields.The name of a ResourceSet object must be a valid DNS subdomain name.
A ResourceSet also needs a
.spec
section.Inputs configuration
The
.spec.inputs
field is optional and specifies a list of input valuesto be used in the resources templates.
Example inputs:
An input value is a key-value pair of strings and structs, where the key is the input name
which can be referenced in the resource templates using the
<< inputs.name >>
syntax.Resources configuration
The
.spec.resources
field is optional and specifies the list of Kubernetes resourceto be generated and reconciled on the cluster.
Example of plain resources without any templating:
Templating resources
The resources can be templated using the
<< inputs.name >>
syntax. The templating engineis based on Go text template. The
<< >>
delimiters are used instead of{{ }}
to avoidconflicts with Helm templating and allow ResourceSets to be included in Helm charts.
Example of templated resources:
The above example will generate a
Namespace
,ServiceAccount
andRoleBinding
for each tenantwith the specified role.
Templating functions
The templating engine supports slim-sprig functions.
It is recommended to use the
quote
function when templating strings to avoid issues withspecial characters e.g.
<< inputs.version | quote >>
.When templating integers, use the
int
function to convert the string to an integere.g.
<< inputs.replicas | int >>
.When templating booleans, use the
bool
function to convert the string to a booleane.g.
<< inputs.enabled | bool >>
.When using integer or boolean inputs as metadata label values, use the
quote
function to convertthe value to a string e.g.
<< inputs.enabled | quote >>
.When templating nested fields, use the
toYaml
andnindent
functionsto properly format the string e.g.:
In addition to the slim-sprig functions, a
slugify
function is available to normalize a string for use in a Kuberneteslabel value
e.g.
<< inputs.tenant | slugify >>
.Resource deduplication
The flux-operator deduplicates resources based on the
apiVersion
,kind
,metadata.name
andmetadata.namespace
fields.This allows defining shared resources that are applied only once, regardless of the number of inputs.
Example of a shared Flux source:
In the above example, the
OCIRepository
resource is created only onceand referred by all
HelmRelease
resources.Conditionally resource exclusion
To exclude a resource based on input values, the
fluxcd.controlplane.io/reconcile
annotation can be setto
disabled
on the resource metadata. This will prevent the resource from being reconciled by the operator.Example of excluding a resource based on an input value:
In the above example, the
ServiceAccount
resource is generated only for theteam1
tenant.Resources template
The
.spec.resourcesTemplate
field is optional and offers an alternative to the.spec.resources
.The
.spec.resourcesTemplate
is a single string that contains the multi-document YAML of the resourcesdefinitions. This field can be used for complex templating scenarios with the trade-off of reduced readability.
Note that when both
.spec.resources
and.spec.resourcesTemplate
are set, the resulting resourcesare the union of the two. If duplicate resources are defined in both fields, the resources from
.spec.resources
take precedence.Example of a template containing conditional and repeated blocks:
The above example generates two
OCIRepository
resources (one for each bundle) and fourKustomization
resources (one for each component in each bundle).Common metadata
The
.spec.commonMetadata
field is optional and specifies common metadata to be applied to all resources.It has two optional fields:
labels
: A map used for setting labelson an object. Any existing label will be overridden if it matches with a key in
this map.
annotations
: A map used for setting annotationson an object. Any existing annotation will be overridden if it matches with a key
in this map.
Example common metadata:
In the above example, all resources generated by the ResourceSet
will not be pruned by the garbage collection process as
the
fluxcd.controlplane.io/prune
annotation is set todisabled
.Dependency management
.spec.dependsOn
is an optional list used to refer to Kubernetesobjects that the ResourceSet depends on. If specified, then the ResourceSet
is reconciled after the referred objects exist in the cluster.
A dependency is a reference to a Kubernetes object with the following fields:
apiVersion
: The API version of the referred object (required).kind
: The kind of the referred object (required).name
: The name of the referred object (required).namespace
: The namespace of the referred object (optional).ready
: A boolean indicating if the referred object must have theReady
status condition set toTrue
(optional, default isfalse
).readyExpr
: A CEL expression that evaluates to a boolean indicating if the referred object is ready (optional).Example of conditional reconciliation based on the existence of CustomResourceDefinitions
and the readiness of a ResourceSet:
Note that is recommended to define dependencies on CustomResourceDefinitions if the ResourceSet
deploys Flux HelmReleases which contain custom resources.
When the dependencies are not met, the flux-operator will reevaluate the requirements
every five seconds and reconcile the ResourceSet when the dependencies are satisfied.
Failed dependencies are reported in the ResourceSet
Ready
status condition,in log messages and Kubernetes events.
CEL readiness expressions
The
readyExpr
field allows for more complex readiness checks andcan be used for gating the reconciliation of a ResourceSet based on the evaluation
of the CEL expression.
The expression is evaluated in the context of the referred object and has access to all the fields of the object,
including the status conditions and the status subfields. The expression must evaluate to a boolean value, any syntax
or runtime errors will be reported in the ResourceSet status conditions.
Example readiness expression:
For testing the CEL expressions, you can use the CEL playground.
Reconciliation configuration
The reconciliation of behaviour of a ResourceSet can be configured using the following annotations:
fluxcd.controlplane.io/reconcile
: Enable or disable the reconciliation loop. Default isenabled
, set todisabled
to pause the reconciliation.fluxcd.controlplane.io/reconcileEvery
: Set the reconciliation interval used for drift detection and correction. Default is1h
.fluxcd.controlplane.io/reconcileTimeout
: Set the reconciliation timeout including health checks. Default is5m
.Health check configuration
The
.spec.wait
field is optional and instructs the flux-operator to performa health check on all applied resources and waits for them to become ready. The health
check is disabled by default and can be enabled by setting the
.spec.wait
field totrue
.The health check is performed for the following resources types:
PersistentVolumeClaim, Service, Ingress, CustomResourceDefinition.
By default, the wait timeout is
5m
and can be changed with thefluxcd.controlplane.io/reconcileTimeout
annotation, set on the ResourceSet object.Role-based access control
The
.spec.serviceAccountName
field is optional and specifies the name of theKubernetes ServiceAccount used by the flux-operator to reconcile the ResourceSet.
The ServiceAccount must exist in the same namespace as the ResourceSet
and must have the necessary permissions to create, update and delete
the resources defined in the ResourceSet.
On multi-tenant clusters, it is recommended to use a dedicated ServiceAccount per tenant namespace
with the minimum required permissions. To enforce a ServiceAccount for all ResourceSets,
the
--default-service-account=flux-operator
flag can be set in the flux-operator container arguments.With this flag set, only the ResourceSets created in the same namespace as the flux-operator
will run with cluster-admin permissions.
Garbage collection
The operator performs garbage collection of the resources previously generated by a ResourceSet
that are no longer present in the current revision. Garbage collection is also performed
when a ResourceSet object is deleted, triggering a removal of all Kubernetes
objects previously applied on the cluster.
The garbage collection process removes stale resources in stages, first it deletes the Flux
custom resources and waits for the Flux Kustomizations and HelmReleases to be finalized by
the controllers. After the Flux resources are removed (max wait is one minute), the operator
proceeds with the deletion of the remaining Kubernetes objects. This ensures that the
Flux controllers have a chance to clean up the resources they manage before the operator
deletes the Kubernetes ServiceAccount and RoleBinding used by Flux impersonation.
After the garbage collection process is completed, the operator issues a Kubernetes event
containing the list of removed resources and the duration of the cleanup.
The garbage collection is enabled by default and can be disabled for certain resources
by setting the
fluxcd.controlplane.io/prune
annotation todisabled
. To fully disablethe garbage collection for a ResourceSet, the annotation must be set on all resources
using the
.spec.commonMetadata
field.ResourceSet Status
Conditions
A ResourceSet enters various states during its lifecycle, reflected as Kubernetes Conditions.
It can be reconciling while applying the
resources on the cluster, it can be ready, or it can fail during
reconciliation.
The ResourceSet API is compatible with the kstatus specification,
and reports
Reconciling
andStalled
conditions where applicable toprovide better (timeout) support to solutions polling the ResourceSet to
become
Ready
.Reconciling ResourceSet
The flux-operator marks a ResourceSet as reconciling when it starts
the reconciliation of the same. The Condition added to the ResourceSet's
.status.conditions
has the following attributes:type: Reconciling
status: "True"
reason: Progressing
|reason: ProgressingWithRetry
The Condition
message
is updated during the course of the reconciliation toreport the action being performed at any particular moment such as
building manifests, detecting drift, etc.
The
Ready
Condition'sstatus
is also marked asUnknown
.Ready ResourceSet
The flux-operator marks a ResourceSet as ready when the resources were
built and applied on the cluster and all health checks are observed to be passing.
When the ResourceSet is "ready", the flux-operator sets a Condition with the
following attributes in the ResourceSet’s
.status.conditions
:type: Ready
status: "True"
reason: ReconciliationSucceeded
Failed ResourceSet
The flux-operator may get stuck trying to reconcile and apply a
ResourceSet without completing. This can occur due to some of the following factors:
When this happens, the flux-operator sets the
Ready
Condition status to Falseand adds a Condition with the following attributes to the ResourceSet’s
.status.conditions
:type: Ready
status: "False"
reason: DependencyNotReady | BuildFailed | ReconciliationFailed | HealthCheckFailed
The
message
field of the Condition will contain more information about whythe reconciliation failed.
While the ResourceSet has one or more of these Conditions, the flux-operator
will continue to attempt a reconciliation with an
exponential backoff, until it succeeds and the ResourceSet is marked as ready.
Inventory status
In order to perform operations such as drift detection, garbage collection, upgrades, etc.,
the flux-operator needs to keep track of all Kubernetes objects that are
reconciled as part of a ResourceSet. To do this, it maintains an inventory
containing the list of Kubernetes resource object references that have been
successfully applied and records it in
.status.inventory
. The inventoryrecords are in the format
Id: <namespace>_<name>_<group>_<kind>, V: <version>
.Example: