From b20c5e52fca6297b091ac8ad45c641ed21ca178f Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Thu, 21 Nov 2024 04:16:33 +0800 Subject: [PATCH] Added the drift detection how-to doc --- docs/howtos/drift-detection.md | 313 +++++++++++++++++++ pkg/controllers/updaterun/controller.go | 5 +- pkg/controllers/updaterun/controller_test.go | 3 +- 3 files changed, 318 insertions(+), 3 deletions(-) create mode 100644 docs/howtos/drift-detection.md diff --git a/docs/howtos/drift-detection.md b/docs/howtos/drift-detection.md new file mode 100644 index 000000000..9541506b1 --- /dev/null +++ b/docs/howtos/drift-detection.md @@ -0,0 +1,313 @@ +# How-to Guide: Enabling Drift Detection in Fleet + +This guide provides an overview on how to enable drift detection in Fleet. This feature can help +developers and admins identify (and act upon) configuration drifts in their Kubernetes system, +which are often brought by temporary fixes, inadvertent changes, and failed automations. + +> Before you begin +> +> The new drift detection experience is currently in preview. Contact the Fleet team for more information on how to have a peek at the experience. +> +> Note that the APIs for the new experience are only available in the Fleet v1beta1 API, not the v1 API. If you do not see the new APIs in command outputs, verify that you are explicitly requesting the v1beta1 API objects, as opposed to the v1 API objects (the default). + +## What is a drift? + +A drift occurs when a non-Fleet agent (e.g., a developer or a controller) makes changes to +a field of a Fleet-managed resource directly on the member cluster side without modifying +the corresponding resource template created on the hub cluster. + +See the steps below for an example; the code assumes that you have a Fleet of two clusters, +`member-1` and `member-2`. + +* Switch to the hub cluster in the preview environment: + + ```sh + kubectl config use-context hub-admin + ``` + +* Create a namespace, `work`, on the hub cluster, with some labels: + + ```sh + kubectl create ns work + kubectl label ns work app=work + kubectl label ns work owner=redfield + ``` + +* Create a CRP object, which places the namespace on all member clusters: + + ```sh + cat < Important: + > + > The presence of drifts will NOT stop Fleet from rolling out newer resource versions. If you choose to edit the resource template on the hub cluster, Fleet will always apply the new resource template in the rollout process, which may also resolve the drift. + +## Comparison options + +One may have found out that the namespace on the member cluster has another drift, the +label `use=hack`, which is not reported in the CRP status by Fleet. This is because by default +Fleet compares only managed fields, i.e., fields that are explicitly specified in the resource +template. If a field is not populated on the hub cluster side, Fleet will not recognize its +presence on the member cluster side as a drift. This allows controllers on the member cluster +side to manage some fields automatically without Fleet's involvement; for example, one might would +like to use an HPA solution to auto-scale Deployments as appropriate and consequently decide not +to include the `.spec.replicas field` in the resource template. + +Fleet recognizes that there might be cases where developers and admins would like to have their +resources look exactly the same across their fleet. If this scenario applies, one might set up +the `comparisonOptions` field in the apply strategy from the `partialComparison` value +(the default) to `fullComparison`: + +```yaml +apiVersion: placement.kubernetes-fleet.io/v1beta1 +kind: ClusterResourcePlacement +metadata: + name: work +spec: + resourceSelectors: + - group: "" + kind: Namespace + version: v1 + labelSelector: + matchLabels: + app: work + policy: + placementType: PickAll + strategy: + applyStrategy: + whenToApply: IfNotDrifted + comparisonOption: fullComparison +``` + +With this setting, Fleet will recognize the presence of any unmanaged fields (i.e., fields that +are present on the member cluster side, but not set on the hub cluster side) as drifts as well. +If anyone adds a field to a Fleet-managed object directly on the member cluster, it would trigger +an apply error, which you can find out about the details the same way as illustrated in the +section above. + +## Summary + +Below is a summary of the synergy between the whenToApply and comparisonOption settings: + +| `whenToApply` setting | `comparisonOption` setting | Drift scenario | Outcome +| -------- | ------- | -------- | ------- | +| `IfNotDrifted` | `partialComparison` | A managed field (i.e., a field that has been explicitly set in the hub cluster resource template) is edited. | Fleet will report an apply error in the status, plus the drift details. | +| `IfNotDrifted` | `partialComparison` | An unmanaged field (i.e., a field that has not been explicitly set in the hub cluster resource template) is edited/added. | N/A; the change is left untouched, and Fleet will ignore it. | +| `IfNotDrifted` | `fullComparison` | Any field is edited/added. | Fleet will report an apply error in the status, plus the drift details. | +| `Always` | `partialComparison` | A managed field (i.e., a field that has been explicitly set in the hub cluster resource template) is edited. | N/A; the change is overwritten shortly. | +| `Always` | `partialComparison` | An unmanaged field (i.e., a field that has not been explicitly set in the hub cluster resource template) is edited/added. | N/A; the change is left untouched, and Fleet will ignore it. | +| `Always` | `fullComparison` | Any field is edited/added. | The change on managed fields will be overwritten shortly; Fleet will report drift details about changes on unmanaged fields, but this is not considered as an apply error. | + + + diff --git a/pkg/controllers/updaterun/controller.go b/pkg/controllers/updaterun/controller.go index d570bf399..27d7a3bf5 100644 --- a/pkg/controllers/updaterun/controller.go +++ b/pkg/controllers/updaterun/controller.go @@ -11,8 +11,6 @@ import ( "fmt" "time" - "go.goms.io/fleet/pkg/utils" - "go.goms.io/fleet/pkg/utils/controller" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "k8s.io/client-go/util/workqueue" @@ -26,6 +24,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "go.goms.io/fleet/pkg/utils" + "go.goms.io/fleet/pkg/utils/controller" + placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" ) diff --git a/pkg/controllers/updaterun/controller_test.go b/pkg/controllers/updaterun/controller_test.go index bf7d07f53..2fd1b3032 100644 --- a/pkg/controllers/updaterun/controller_test.go +++ b/pkg/controllers/updaterun/controller_test.go @@ -8,11 +8,12 @@ package updaterun import ( "testing" - placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" "k8s.io/client-go/util/workqueue" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" ) func TestHandleClusterApprovalRequest(t *testing.T) {