Skip to content

Commit 3112fbc

Browse files
authored
Merge pull request #614 from gianlucam76/drift-detection
(feat) Don't track certain resources for configuration drift
2 parents 560941e + 15b0ba1 commit 3112fbc

11 files changed

+206
-39
lines changed

api/v1beta1/clusterconfiguration_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ type Resource struct {
5454

5555
// Owner is the list of ConfigMap/Secret containing this resource.
5656
Owner corev1.ObjectReference `json:"owner"`
57+
58+
// IgnoreForConfigurationDrift indicates to not track resource
59+
// for configuration drift detection.
60+
// This field has a meaning only when mode is ContinuousWithDriftDetection
61+
IgnoreForConfigurationDrift bool `json:"ignoreForConfigurationDrift"`
5762
}
5863

5964
type Chart struct {

config/crd/bases/config.projectsveltos.io_clusterconfigurations.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,12 @@ spec:
479479
description: Group of the resource deployed in the
480480
Cluster.
481481
type: string
482+
ignoreForConfigurationDrift:
483+
description: |-
484+
IgnoreForConfigurationDrift indicates to not track resource
485+
for configuration drift detection.
486+
This field has a meaning only when mode is ContinuousWithDriftDetection
487+
type: boolean
482488
kind:
483489
description: Kind of the resource deployed in the
484490
Cluster.
@@ -551,6 +557,7 @@ spec:
551557
type: string
552558
required:
553559
- group
560+
- ignoreForConfigurationDrift
554561
- kind
555562
- name
556563
- owner
@@ -644,6 +651,12 @@ spec:
644651
description: Group of the resource deployed in the
645652
Cluster.
646653
type: string
654+
ignoreForConfigurationDrift:
655+
description: |-
656+
IgnoreForConfigurationDrift indicates to not track resource
657+
for configuration drift detection.
658+
This field has a meaning only when mode is ContinuousWithDriftDetection
659+
type: boolean
647660
kind:
648661
description: Kind of the resource deployed in the
649662
Cluster.
@@ -716,6 +729,7 @@ spec:
716729
type: string
717730
required:
718731
- group
732+
- ignoreForConfigurationDrift
719733
- kind
720734
- name
721735
- owner

config/crd/bases/config.projectsveltos.io_clusterreports.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ spec:
384384
group:
385385
description: Group of the resource deployed in the Cluster.
386386
type: string
387+
ignoreForConfigurationDrift:
388+
description: |-
389+
IgnoreForConfigurationDrift indicates to not track resource
390+
for configuration drift detection.
391+
This field has a meaning only when mode is ContinuousWithDriftDetection
392+
type: boolean
387393
kind:
388394
description: Kind of the resource deployed in the Cluster.
389395
minLength: 1
@@ -453,6 +459,7 @@ spec:
453459
type: string
454460
required:
455461
- group
462+
- ignoreForConfigurationDrift
456463
- kind
457464
- name
458465
- owner
@@ -530,6 +537,12 @@ spec:
530537
group:
531538
description: Group of the resource deployed in the Cluster.
532539
type: string
540+
ignoreForConfigurationDrift:
541+
description: |-
542+
IgnoreForConfigurationDrift indicates to not track resource
543+
for configuration drift detection.
544+
This field has a meaning only when mode is ContinuousWithDriftDetection
545+
type: boolean
533546
kind:
534547
description: Kind of the resource deployed in the Cluster.
535548
minLength: 1
@@ -599,6 +612,7 @@ spec:
599612
type: string
600613
required:
601614
- group
615+
- ignoreForConfigurationDrift
602616
- kind
603617
- name
604618
- owner

controllers/drift-detection-utils.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2024. projectsveltos.io. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21+
)
22+
23+
const (
24+
// When this annotation is set, resource will be excluded from configuration
25+
// drift detection
26+
driftDetectionIgnoreAnnotation = "projectsveltos.io/driftDetectionIgnore"
27+
)
28+
29+
// hasIgnoreConfigurationDriftAnnotation verifies whether resource has
30+
// `projectsveltos.io/driftDetectionIgnore` annotation. Any resource with such
31+
// annotation set won't be tracked for configuration drift.
32+
func hasIgnoreConfigurationDriftAnnotation(resource *unstructured.Unstructured) bool {
33+
annotations := resource.GetAnnotations()
34+
if annotations != nil {
35+
if _, ok := annotations[driftDetectionIgnoreAnnotation]; ok {
36+
return true
37+
}
38+
}
39+
40+
return false
41+
}

controllers/handlers_helm.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func deployHelmCharts(ctx context.Context, c client.Client,
149149
if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeContinuousWithDriftDetection ||
150150
clusterSummary.Spec.ClusterProfileSpec.Reloader {
151151

152-
helmResources, err = collectResourcesFromManagedHelmCharts(ctx, c, clusterSummary, kubeconfig, logger)
152+
helmResources, err = collectResourcesFromManagedHelmChartsForDriftDetection(ctx, c, clusterSummary, kubeconfig, logger)
153153
if err != nil {
154154
return err
155155
}
@@ -1685,10 +1685,11 @@ func getHelmChartValuesFrom(ctx context.Context, c client.Client, clusterSummary
16851685
return getValuesFrom(ctx, c, clusterSummary, helmChart.ValuesFrom, false, logger)
16861686
}
16871687

1688-
// collectResourcesFromManagedHelmCharts collects resources considering all
1688+
// collectResourcesFromManagedHelmChartsForDriftDetection collects resources considering all
16891689
// helm charts contained in a ClusterSummary that are currently managed by the
1690-
// ClusterProfile instance
1691-
func collectResourcesFromManagedHelmCharts(ctx context.Context, c client.Client,
1690+
// ClusterProfile instance.
1691+
// Resources with "projectsveltos.io/driftDetectionIgnore" annotation won't be included
1692+
func collectResourcesFromManagedHelmChartsForDriftDetection(ctx context.Context, c client.Client,
16921693
clusterSummary *configv1beta1.ClusterSummary, kubeconfig string, logger logr.Logger,
16931694
) ([]libsveltosv1beta1.HelmResources, error) {
16941695

@@ -1764,11 +1765,12 @@ func unstructuredToSveltosResources(policies []*unstructured.Unstructured) []lib
17641765

17651766
for i := range policies {
17661767
r := libsveltosv1beta1.Resource{
1767-
Namespace: policies[i].GetNamespace(),
1768-
Name: policies[i].GetName(),
1769-
Kind: policies[i].GetKind(),
1770-
Group: policies[i].GetObjectKind().GroupVersionKind().Group,
1771-
Version: policies[i].GetObjectKind().GroupVersionKind().Version,
1768+
Namespace: policies[i].GetNamespace(),
1769+
Name: policies[i].GetName(),
1770+
Kind: policies[i].GetKind(),
1771+
Group: policies[i].GetObjectKind().GroupVersionKind().Group,
1772+
Version: policies[i].GetObjectKind().GroupVersionKind().Version,
1773+
IgnoreForConfigurationDrift: hasIgnoreConfigurationDriftAnnotation(policies[i]),
17721774
}
17731775

17741776
resources = append(resources, r)

controllers/handlers_resources.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,12 @@ func deployResourceSummary(ctx context.Context, c client.Client,
492492

493493
for i := range deployed {
494494
resources[i] = libsveltosv1beta1.Resource{
495-
Namespace: deployed[i].Namespace,
496-
Name: deployed[i].Name,
497-
Group: deployed[i].Group,
498-
Kind: deployed[i].Kind,
499-
Version: deployed[i].Version,
495+
Namespace: deployed[i].Namespace,
496+
Name: deployed[i].Name,
497+
Group: deployed[i].Group,
498+
Kind: deployed[i].Kind,
499+
Version: deployed[i].Version,
500+
IgnoreForConfigurationDrift: deployed[i].IgnoreForConfigurationDrift,
500501
}
501502
}
502503

controllers/handlers_utils.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func deployUnstructured(ctx context.Context, deployingToMgmtCluster bool, destCo
332332
logger.V(logs.LogDebug).Info(fmt.Sprintf("deploying resource %s %s/%s (deploy to management cluster: %v)",
333333
policy.GetKind(), policy.GetNamespace(), policy.GetName(), deployingToMgmtCluster))
334334

335-
resource, policyHash := getResource(policy, referencedObject, profileTier, featureID, logger)
335+
resource, policyHash := getResource(policy, hasIgnoreConfigurationDriftAnnotation(policy), referencedObject, profileTier, featureID, logger)
336336

337337
// If policy is namespaced, create namespace if not already existing
338338
err = createNamespace(ctx, destClient, clusterSummary, policy.GetNamespace())
@@ -520,17 +520,20 @@ func canDeployResource(ctx context.Context, dr dynamic.ResourceInterface, policy
520520
return resourceInfo, false, nil
521521
}
522522

523-
func generateResourceReport(policyHash string, resourceInfo *deployer.ResourceInfo, resource *configv1beta1.Resource,
524-
) *configv1beta1.ResourceReport {
523+
func generateResourceReport(policyHash string, resourceInfo *deployer.ResourceInfo,
524+
resource *configv1beta1.Resource) *configv1beta1.ResourceReport {
525525

526+
resourceReport := &configv1beta1.ResourceReport{Resource: *resource}
526527
if resourceInfo.ResourceVersion == "" {
527-
return &configv1beta1.ResourceReport{Resource: *resource, Action: string(configv1beta1.CreateResourceAction)}
528+
resourceReport.Action = string(configv1beta1.CreateResourceAction)
528529
} else if policyHash != resourceInfo.Hash {
529-
return &configv1beta1.ResourceReport{Resource: *resource, Action: string(configv1beta1.UpdateResourceAction)}
530+
resourceReport.Action = string(configv1beta1.UpdateResourceAction)
530531
} else {
531-
return &configv1beta1.ResourceReport{Resource: *resource, Action: string(configv1beta1.NoResourceAction),
532-
Message: "Object already deployed. And policy referenced by ClusterProfile has not changed since last deployment."}
532+
resourceReport.Action = string(configv1beta1.NoResourceAction)
533+
resourceReport.Message = "Object already deployed. And policy referenced by ClusterProfile has not changed since last deployment."
533534
}
535+
536+
return resourceReport
534537
}
535538

536539
// addExtraLabels adds ExtraLabels to policy.
@@ -580,8 +583,8 @@ func addExtraAnnotations(policy *unstructured.Unstructured, extraAnnotations map
580583
}
581584

582585
// getResource returns sveltos Resource and the resource hash hash
583-
func getResource(policy *unstructured.Unstructured, referencedObject *corev1.ObjectReference,
584-
tier int32, featureID configv1beta1.FeatureID, logger logr.Logger,
586+
func getResource(policy *unstructured.Unstructured, ignoreForConfigurationDrift bool,
587+
referencedObject *corev1.ObjectReference, tier int32, featureID configv1beta1.FeatureID, logger logr.Logger,
585588
) (resource *configv1beta1.Resource, policyHash string) {
586589

587590
resource = &configv1beta1.Resource{
@@ -595,6 +598,7 @@ func getResource(policy *unstructured.Unstructured, referencedObject *corev1.Obj
595598
Name: referencedObject.Name,
596599
Kind: referencedObject.Kind,
597600
},
601+
IgnoreForConfigurationDrift: ignoreForConfigurationDrift,
598602
}
599603

600604
var err error

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/onsi/ginkgo/v2 v2.19.0
1818
github.com/onsi/gomega v1.33.1
1919
github.com/pkg/errors v0.9.1
20-
github.com/projectsveltos/libsveltos v0.32.1-0.20240624142620-affdfeb694be
20+
github.com/projectsveltos/libsveltos v0.32.1-0.20240702090008-925c00e1be3d
2121
github.com/prometheus/client_golang v1.19.1
2222
github.com/spf13/pflag v1.0.5
2323
github.com/yuin/gopher-lua v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
364364
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
365365
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
366366
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
367-
github.com/projectsveltos/libsveltos v0.32.1-0.20240624142620-affdfeb694be h1:IC9Ca6OqzSQRv//9NSRZt34gdZDyjudwyEz2hZVae7c=
368-
github.com/projectsveltos/libsveltos v0.32.1-0.20240624142620-affdfeb694be/go.mod h1:z6avfRqeHbzqkThyqqqoGcCWMI0JBeAjdeZlbJ7P8TI=
367+
github.com/projectsveltos/libsveltos v0.32.1-0.20240702090008-925c00e1be3d h1:wT8qFe4Yf97G/y2sY2I+f0iSnfgrnFF3SXwau4PJxYU=
368+
github.com/projectsveltos/libsveltos v0.32.1-0.20240702090008-925c00e1be3d/go.mod h1:m2CcqCd9Gq/czJS1lYmMPrnQTvVzc7AL9xlgXaAaQRE=
369369
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
370370
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
371371
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=

manifest/manifest.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ spec:
483483
description: Group of the resource deployed in the
484484
Cluster.
485485
type: string
486+
ignoreForConfigurationDrift:
487+
description: |-
488+
IgnoreForConfigurationDrift indicates to not track resource
489+
for configuration drift detection.
490+
This field has a meaning only when mode is ContinuousWithDriftDetection
491+
type: boolean
486492
kind:
487493
description: Kind of the resource deployed in the
488494
Cluster.
@@ -555,6 +561,7 @@ spec:
555561
type: string
556562
required:
557563
- group
564+
- ignoreForConfigurationDrift
558565
- kind
559566
- name
560567
- owner
@@ -648,6 +655,12 @@ spec:
648655
description: Group of the resource deployed in the
649656
Cluster.
650657
type: string
658+
ignoreForConfigurationDrift:
659+
description: |-
660+
IgnoreForConfigurationDrift indicates to not track resource
661+
for configuration drift detection.
662+
This field has a meaning only when mode is ContinuousWithDriftDetection
663+
type: boolean
651664
kind:
652665
description: Kind of the resource deployed in the
653666
Cluster.
@@ -720,6 +733,7 @@ spec:
720733
type: string
721734
required:
722735
- group
736+
- ignoreForConfigurationDrift
723737
- kind
724738
- name
725739
- owner
@@ -3268,6 +3282,12 @@ spec:
32683282
group:
32693283
description: Group of the resource deployed in the Cluster.
32703284
type: string
3285+
ignoreForConfigurationDrift:
3286+
description: |-
3287+
IgnoreForConfigurationDrift indicates to not track resource
3288+
for configuration drift detection.
3289+
This field has a meaning only when mode is ContinuousWithDriftDetection
3290+
type: boolean
32713291
kind:
32723292
description: Kind of the resource deployed in the Cluster.
32733293
minLength: 1
@@ -3337,6 +3357,7 @@ spec:
33373357
type: string
33383358
required:
33393359
- group
3360+
- ignoreForConfigurationDrift
33403361
- kind
33413362
- name
33423363
- owner
@@ -3414,6 +3435,12 @@ spec:
34143435
group:
34153436
description: Group of the resource deployed in the Cluster.
34163437
type: string
3438+
ignoreForConfigurationDrift:
3439+
description: |-
3440+
IgnoreForConfigurationDrift indicates to not track resource
3441+
for configuration drift detection.
3442+
This field has a meaning only when mode is ContinuousWithDriftDetection
3443+
type: boolean
34173444
kind:
34183445
description: Kind of the resource deployed in the Cluster.
34193446
minLength: 1
@@ -3483,6 +3510,7 @@ spec:
34833510
type: string
34843511
required:
34853512
- group
3513+
- ignoreForConfigurationDrift
34863514
- kind
34873515
- name
34883516
- owner

0 commit comments

Comments
 (0)