@@ -8,6 +8,7 @@ package datadogagentinternal
88import (
99 "context"
1010 "fmt"
11+ "maps"
1112 "time"
1213
1314 edsv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
@@ -21,9 +22,11 @@ import (
2122 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2223 "sigs.k8s.io/controller-runtime/pkg/reconcile"
2324
25+ apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
2426 "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1"
2527 "github.com/DataDog/datadog-operator/pkg/agentprofile"
2628 "github.com/DataDog/datadog-operator/pkg/condition"
29+ "github.com/DataDog/datadog-operator/pkg/constants"
2730 "github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
2831 "github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
2932 "github.com/DataDog/datadog-operator/pkg/kubernetes"
@@ -96,6 +99,14 @@ func (r *Reconciler) createOrUpdateDeployment(parentLogger logr.Logger, ddai *v1
9699 }
97100 logger .Info ("Deployment owner reference patched" )
98101 }
102+
103+ if restartDeployment (deployment , currentDeployment ) {
104+ if err = deleteObjectAndOrphanDependents (context .TODO (), logger , r .client , deployment , deployment .GetLabels ()[apicommon .AgentDeploymentComponentLabelKey ]); err != nil {
105+ return result , err
106+ }
107+ return result , nil
108+ }
109+
99110 // check if same hash
100111 needUpdate := ! comparison .IsSameSpecMD5Hash (hash , currentDeployment .GetAnnotations ())
101112 if ! needUpdate {
@@ -195,6 +206,14 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, ddai *v1a
195206 }
196207 logger .Info ("Daemonset owner reference patched" )
197208 }
209+
210+ if restartDaemonset (daemonset , currentDaemonset ) {
211+ if err = deleteObjectAndOrphanDependents (context .TODO (), logger , r .client , daemonset , constants .DefaultAgentResourceSuffix ); err != nil {
212+ return result , err
213+ }
214+ return result , nil
215+ }
216+
198217 now := metav1 .Now ()
199218
200219 // When overriding node labels in <1.7.0, the hash could be updated
@@ -443,3 +462,31 @@ func IsEqualStatus(current *v1alpha1.DatadogAgentInternalStatus, newStatus *v1al
443462
444463 return condition .IsEqualConditions (current .Conditions , newStatus .Conditions )
445464}
465+
466+ func restartDaemonset (daemonset , currentDaemonset * appsv1.DaemonSet ) bool {
467+ // name change
468+ if daemonset .Name != currentDaemonset .Name {
469+ return true
470+ }
471+
472+ // selectors are immutable
473+ if ! maps .Equal (daemonset .Spec .Selector .MatchLabels , currentDaemonset .Spec .Selector .MatchLabels ) {
474+ return true
475+ }
476+
477+ return false
478+ }
479+
480+ func restartDeployment (deployment , currentDeployment * appsv1.Deployment ) bool {
481+ // name change
482+ if deployment .Name != currentDeployment .Name {
483+ return true
484+ }
485+
486+ // selectors are immutable
487+ if ! maps .Equal (deployment .Spec .Selector .MatchLabels , currentDeployment .Spec .Selector .MatchLabels ) {
488+ return true
489+ }
490+
491+ return false
492+ }
0 commit comments