Skip to content

Commit

Permalink
Add md5 hash config annotation to helm check (#1111) (#1117)
Browse files Browse the repository at this point in the history
* Add md5 hash for config changes

* Apply review suggestions

(cherry picked from commit e0fa00d)
  • Loading branch information
fanny-jiang authored Mar 11, 2024
1 parent bf77e10 commit 61b6826
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
49 changes: 39 additions & 10 deletions controllers/datadogagent/feature/helmcheck/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
apiutils "github.com/DataDog/datadog-operator/apis/utils"
"github.com/DataDog/datadog-operator/controllers/datadogagent/common"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object/volume"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
)

Expand Down Expand Up @@ -49,8 +51,11 @@ type helmCheckFeature struct {
serviceAccountName string
rbacSuffix string

owner metav1.Object
configMapName string
owner metav1.Object
config *corev1.ConfigMap
configMapName string
configAnnotationKey string
configAnnotationValue string

logger logr.Logger
}
Expand Down Expand Up @@ -80,6 +85,24 @@ func (f *helmCheckFeature) Configure(dda *v2alpha1.DatadogAgent) (reqComp featur
f.serviceAccountName = v2alpha1.GetClusterChecksRunnerServiceAccount(dda)
reqComp.ClusterChecksRunner.IsRequired = apiutils.NewBoolPointer(true)
}

// Build configMap based on feature flags.
cm, err := f.buildHelmCheckConfigMap()
if err != nil {
f.logger.Error(err, "couldn't generate configMap for helm check")
}
f.config = cm

// Create hash based on configMap.
hash, err := comparison.GenerateMD5ForSpec(cm.Data)
if err != nil {
f.logger.Error(err, "couldn't generate hash for helm check config")
} else {
f.logger.V(2).Info("built helm check from config", "hash", hash)
}

f.configAnnotationValue = hash
f.configAnnotationKey = object.GetChecksumAnnotationKey(feature.HelmCheckIDType)
}

return reqComp
Expand All @@ -93,14 +116,15 @@ func (f *helmCheckFeature) ConfigureV1(dda *v1alpha1.DatadogAgent) feature.Requi
// ManageDependencies allows a feature to manage its dependencies.
// Feature's dependencies should be added in the store.
func (f *helmCheckFeature) ManageDependencies(managers feature.ResourceManagers, components feature.RequiredComponents) error {
// Create configMap based on feature flags.
cm, err := f.buildHelmCheckConfigMap()
if err != nil {
return err
}

if err := managers.Store().AddOrUpdate(kubernetes.ConfigMapKind, cm); err != nil {
return err
if f.config != nil {
// Add md5 hash annotation for configMap
if f.configAnnotationKey != "" && f.configAnnotationValue != "" {
annotations := object.MergeAnnotationsLabels(f.logger, f.config.GetAnnotations(), map[string]string{f.configAnnotationKey: f.configAnnotationValue}, "*")
f.config.SetAnnotations(annotations)
}
if err := managers.Store().AddOrUpdate(kubernetes.ConfigMapKind, f.config); err != nil {
return err
}
}

// Manage RBAC permission
Expand All @@ -126,6 +150,11 @@ func (f *helmCheckFeature) ManageClusterAgent(managers feature.PodTemplateManage
managers.VolumeMount().AddVolumeMountToContainer(&volMount, apicommonv1.ClusterAgentContainerName)
managers.Volume().AddVolume(&vol)

// Add md5 hash annotation for configMap
if f.configAnnotationKey != "" && f.configAnnotationValue != "" {
managers.Annotation().AddAnnotation(f.configAnnotationKey, f.configAnnotationValue)
}

return nil
}

Expand Down
31 changes: 28 additions & 3 deletions controllers/datadogagent/feature/helmcheck/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package helmcheck

import (
"fmt"
"strconv"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/fake"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object/configmap"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
)

Expand All @@ -50,7 +52,7 @@ func Test_helmCheckFeature_Configure(t *testing.T) {
Build(),
WantConfigure: true,
WantDependenciesFunc: helmCheckWantDepsFunc(false, true, valuesAsTags, "dca"),
ClusterAgent: helmCheckWantResourcesFunc(),
ClusterAgent: helmCheckWantResourcesFunc(false, true),
},
{
Name: "Helm check enabled and runs on cluster checks runner",
Expand All @@ -63,7 +65,7 @@ func Test_helmCheckFeature_Configure(t *testing.T) {
Build(),
WantConfigure: true,
WantDependenciesFunc: helmCheckWantDepsFunc(true, true, valuesAsTags, "ccr"),
ClusterAgent: helmCheckWantResourcesFunc(),
ClusterAgent: helmCheckWantResourcesFunc(true, true),
},
}

Expand Down Expand Up @@ -132,7 +134,7 @@ func helmCheckWantDepsFunc(ccr bool, collectEvents bool, valuesAsTags map[string
}
}

func helmCheckWantResourcesFunc() *test.ComponentTest {
func helmCheckWantResourcesFunc(ccr bool, collectEvents bool) *test.ComponentTest {
return test.NewDefaultComponentTest().WithWantFunc(
func(t testing.TB, mgrInterface feature.PodTemplateManagers) {
mgr := mgrInterface.(*fake.PodTemplateManagers)
Expand Down Expand Up @@ -175,5 +177,28 @@ func helmCheckWantResourcesFunc() *test.ComponentTest {
apiutils.IsEqualStruct(dcaVolMounts, expectedVolMounts),
"DCA VolumeMounts \ndiff = %s", cmp.Diff(dcaVolMounts, expectedVolMounts),
)

// Validate configMap annotations
config := map[string]string{
"helm.yaml": fmt.Sprintf(`---
cluster_check: %s
init_config:
instances:
- collect_events: %s
helm_values_as_tags:
foo: bar
zip: zap
`, strconv.FormatBool(ccr), strconv.FormatBool(collectEvents)),
}

hash, err := comparison.GenerateMD5ForSpec(config)
assert.NoError(t, err)

wantAnnotations := map[string]string{
fmt.Sprintf(apicommon.MD5ChecksumAnnotationKey, feature.HelmCheckIDType): hash,
}

annotations := mgr.AnnotationMgr.Annotations
assert.True(t, apiutils.IsEqualStruct(annotations, wantAnnotations), "Annotations \ndiff = %s", cmp.Diff(annotations, wantAnnotations))
})
}

0 comments on commit 61b6826

Please sign in to comment.