Skip to content

Commit

Permalink
Fix behavior when overriding names of DCA and CCR components (#1448)
Browse files Browse the repository at this point in the history
* cleanup old dca/ccr deployments when overriding names

* added test cases and correct cleanup dca/ccr logic

* refactor the cleanup code a bit more

* remove unused params

* refactor the dca/ccr cleanup functions
  • Loading branch information
mrdoggopat authored Oct 9, 2024
1 parent 0361cbf commit 582f311
Show file tree
Hide file tree
Showing 5 changed files with 656 additions and 4 deletions.
36 changes: 36 additions & 0 deletions internal/controller/datadogagent/controller_reconcile_ccr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"time"

apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
apiutils "github.com/DataDog/datadog-operator/api/utils"
componentccr "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/clusterchecksrunner"
Expand All @@ -22,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand Down Expand Up @@ -119,3 +121,37 @@ func deleteStatusWithClusterChecksRunner(newStatus *datadoghqv2alpha1.DatadogAge
newStatus.ClusterChecksRunner = nil
datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType)
}

// cleanupOldCCRDeployments deletes CCR deployments when a CCR Deployment's name is changed using clusterChecksRunner name override
func (r *Reconciler) cleanupOldCCRDeployments(ctx context.Context, logger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, newStatus *datadoghqv2alpha1.DatadogAgentStatus) error {
matchLabels := client.MatchingLabels{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
}
deploymentName := getDeploymentNameFromCCR(dda)
deploymentList := appsv1.DeploymentList{}
if err := r.client.List(ctx, &deploymentList, matchLabels); err != nil {
return err
}
for _, deployment := range deploymentList.Items {
if deploymentName != deployment.Name {
if _, err := r.cleanupV2ClusterChecksRunner(logger, dda, &deployment, newStatus); err != nil {
return err
}
}
}

return nil
}

// getDeploymentNameFromCCR returns the expected CCR deployment name based on
// the DDA name and clusterChecksRunner name override
func getDeploymentNameFromCCR(dda *datadoghqv2alpha1.DatadogAgent) string {
deploymentName := componentccr.GetClusterChecksRunnerName(dda)
if componentOverride, ok := dda.Spec.Override[datadoghqv2alpha1.ClusterChecksRunnerComponentName]; ok {
if componentOverride.Name != nil && *componentOverride.Name != "" {
deploymentName = *componentOverride.Name
}
}
return deploymentName
}
278 changes: 278 additions & 0 deletions internal/controller/datadogagent/controller_reconcile_ccr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
package datadogagent

import (
"context"
"testing"

apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
apiutils "github.com/DataDog/datadog-operator/api/utils"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

func Test_getDeploymentNameFromCCR(t *testing.T) {
testCases := []struct {
name string
dda *datadoghqv2alpha1.DatadogAgent
wantDeploymentName string
}{
{
name: "ccr no override",
dda: &datadoghqv2alpha1.DatadogAgent{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
},
wantDeploymentName: "foo-cluster-checks-runner",
},
{
name: "ccr override with no name override",
dda: &datadoghqv2alpha1.DatadogAgent{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: datadoghqv2alpha1.DatadogAgentSpec{
Override: map[datadoghqv2alpha1.ComponentName]*datadoghqv2alpha1.DatadogAgentComponentOverride{
datadoghqv2alpha1.ClusterAgentComponentName: {
Replicas: apiutils.NewInt32Pointer(10),
},
},
},
},
wantDeploymentName: "foo-cluster-checks-runner",
},
{
name: "ccr override with name override",
dda: &datadoghqv2alpha1.DatadogAgent{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: datadoghqv2alpha1.DatadogAgentSpec{
Override: map[datadoghqv2alpha1.ComponentName]*datadoghqv2alpha1.DatadogAgentComponentOverride{
datadoghqv2alpha1.ClusterChecksRunnerComponentName: {
Name: apiutils.NewStringPointer("bar"),
Replicas: apiutils.NewInt32Pointer(10),
},
},
},
},
wantDeploymentName: "bar",
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
deploymentName := getDeploymentNameFromCCR(tt.dda)
assert.Equal(t, tt.wantDeploymentName, deploymentName)
})
}
}

func Test_cleanupOldCCRDeployments(t *testing.T) {
sch := runtime.NewScheme()
_ = scheme.AddToScheme(sch)
ctx := context.Background()

testCases := []struct {
name string
description string
existingAgents []client.Object
wantDeployment *appsv1.DeploymentList
}{
{
name: "no unused CCR deployments",
description: "DCA deployment `dda-foo-cluster-checks-runner` should not be deleted",
existingAgents: []client.Object{
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo-cluster-checks-runner",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
},
},
},
},
wantDeployment: &appsv1.DeploymentList{
TypeMeta: metav1.TypeMeta{
Kind: "DeploymentList",
APIVersion: "apps/v1",
},
Items: []appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo-cluster-checks-runner",
ResourceVersion: "999",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
},
},
},
},
},
},
{
name: "multiple unused CCR deployments",
description: "all deployments except `dda-foo-cluster-checks-runner` should be deleted",
existingAgents: []client.Object{
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo-cluster-checks-runner",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
},
},
},
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-ccr",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
},
},
},
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-ccr",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
},
},
},
},
wantDeployment: &appsv1.DeploymentList{
TypeMeta: metav1.TypeMeta{
Kind: "DeploymentList",
APIVersion: "apps/v1",
},
Items: []appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo-cluster-checks-runner",
ResourceVersion: "999",
Labels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterChecksRunnerResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
},
},
},
},
},
},
{
name: "deployments are not created by the operator (do not have the expected labels) and should not be removed",
description: "No deployments should be deleted",
existingAgents: []client.Object{
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo-cluster-checks-runner",
Namespace: "ns-1",
},
},
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "datadog-test-one-cluster-checks-runner",
Namespace: "ns-1",
Labels: map[string]string{
"foo": "bar",
},
},
},
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "datadog-test-two-cluster-checks-runner",
Namespace: "ns-1",
Labels: map[string]string{
"bar": "foo",
},
},
},
},
wantDeployment: &appsv1.DeploymentList{
TypeMeta: metav1.TypeMeta{
Kind: "DeploymentList",
APIVersion: "apps/v1",
},
Items: []appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "datadog-test-one-cluster-checks-runner",
Namespace: "ns-1",
Labels: map[string]string{
"foo": "bar",
},
ResourceVersion: "999",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "datadog-test-two-cluster-checks-runner",
Namespace: "ns-1",
Labels: map[string]string{
"bar": "foo",
},
ResourceVersion: "999",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo-cluster-checks-runner",
Namespace: "ns-1",
ResourceVersion: "999",
},
},
},
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(sch).WithObjects(tt.existingAgents...).Build()
logger := logf.Log.WithName("Test_cleanupOldCCRDeployments")
eventBroadcaster := record.NewBroadcaster()
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "Test_cleanupOldCCRDeployments"})

r := &Reconciler{
client: fakeClient,
log: logger,
recorder: recorder,
}

dda := datadoghqv2alpha1.DatadogAgent{
TypeMeta: metav1.TypeMeta{
Kind: "DatadogAgent",
APIVersion: "datadoghq.com/v2alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "dda-foo",
Namespace: "ns-1",
},
}
ddaStatus := datadoghqv2alpha1.DatadogAgentStatus{}

err := r.cleanupOldCCRDeployments(ctx, logger, &dda, &ddaStatus)
assert.NoError(t, err)

deploymentList := &appsv1.DeploymentList{}

err = fakeClient.List(ctx, deploymentList)
assert.NoError(t, err)

assert.Equal(t, tt.wantDeployment, deploymentList)
})
}
}
36 changes: 36 additions & 0 deletions internal/controller/datadogagent/controller_reconcile_dca.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"time"

apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
apiutils "github.com/DataDog/datadog-operator/api/utils"
componentdca "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/clusteragent"
Expand All @@ -23,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand Down Expand Up @@ -132,3 +134,37 @@ func (r *Reconciler) cleanupV2ClusterAgent(logger logr.Logger, dda *datadoghqv2a

return reconcile.Result{}, nil
}

// cleanupOldDCADeployments deletes DCA deployments when a DCA Deployment's name is changed using clusterAgent name override
func (r *Reconciler) cleanupOldDCADeployments(ctx context.Context, logger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, resourcesManager feature.ResourceManagers, newStatus *datadoghqv2alpha1.DatadogAgentStatus) error {
matchLabels := client.MatchingLabels{
apicommon.AgentDeploymentComponentLabelKey: datadoghqv2alpha1.DefaultClusterAgentResourceSuffix,
kubernetes.AppKubernetesManageByLabelKey: "datadog-operator",
}
deploymentName := getDeploymentNameFromDCA(dda)
deploymentList := appsv1.DeploymentList{}
if err := r.client.List(ctx, &deploymentList, matchLabels); err != nil {
return err
}
for _, deployment := range deploymentList.Items {
if deploymentName != deployment.Name {
if _, err := r.cleanupV2ClusterAgent(logger, dda, &deployment, resourcesManager, newStatus); err != nil {
return err
}
}
}

return nil
}

// getDeploymentNameFromDCA returns the expected DCA deployment name based on
// the DDA name and clusterAgent name override
func getDeploymentNameFromDCA(dda *datadoghqv2alpha1.DatadogAgent) string {
deploymentName := componentdca.GetClusterAgentName(dda)
if componentOverride, ok := dda.Spec.Override[datadoghqv2alpha1.ClusterAgentComponentName]; ok {
if componentOverride.Name != nil && *componentOverride.Name != "" {
deploymentName = *componentOverride.Name
}
}
return deploymentName
}
Loading

0 comments on commit 582f311

Please sign in to comment.