diff --git a/api/go.mod b/api/go.mod index 7c79e2093..1bf186044 100644 --- a/api/go.mod +++ b/api/go.mod @@ -6,9 +6,7 @@ toolchain go1.22.7 require ( github.com/DataDog/datadog-api-client-go/v2 v2.27.0 - // TODO: pin to an EDS released version once there is a release that includes the api module - github.com/DataDog/extendeddaemonset/api v0.0.0-20250108205105-6c4d337b78a1 - github.com/google/go-cmp v0.6.0 + github.com/google/go-cmp v0.6.0 // indirect github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.9.0 k8s.io/api v0.31.1 diff --git a/api/go.sum b/api/go.sum index 28ab5f71a..24c541030 100644 --- a/api/go.sum +++ b/api/go.sum @@ -1,7 +1,5 @@ github.com/DataDog/datadog-api-client-go/v2 v2.27.0 h1:AGZj41frjnjMufQHQbJH2fzmifOs20wpmVDtIBCv33E= github.com/DataDog/datadog-api-client-go/v2 v2.27.0/go.mod h1:QKOu6vscsh87fMY1lHfLEmNSunyXImj8BUaUWJXOehc= -github.com/DataDog/extendeddaemonset/api v0.0.0-20250108205105-6c4d337b78a1 h1:K9SD6kTk58j3vG99mExuVIFh994JSpPALQRr2MPszXQ= -github.com/DataDog/extendeddaemonset/api v0.0.0-20250108205105-6c4d337b78a1/go.mod h1:CLkfm3awFwTY5aBDm8esFwiocXIoNxY28eK6zZ5u9t0= github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= diff --git a/go.sum b/go.sum index 7824d7712..cfb7553cc 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,7 @@ github.com/DataDog/datadog-api-client-go/v2 v2.27.0/go.mod h1:QKOu6vscsh87fMY1lH github.com/DataDog/datadog-go/v5 v5.5.0 h1:G5KHeB8pWBNXT4Jtw0zAkhdxEAWSpWH00geHI6LDrKU= github.com/DataDog/datadog-go/v5 v5.5.0/go.mod h1:K9kcYBlxkcPP8tvvjZZKs/m1edNAUFzBbdpTUKfCsuw= github.com/DataDog/extendeddaemonset/api v0.0.0-20250108205105-6c4d337b78a1 h1:K9SD6kTk58j3vG99mExuVIFh994JSpPALQRr2MPszXQ= +github.com/DataDog/extendeddaemonset/api v0.0.0-20250108205105-6c4d337b78a1/go.mod h1:CLkfm3awFwTY5aBDm8esFwiocXIoNxY28eK6zZ5u9t0= github.com/DataDog/go-libddwaf/v3 v3.3.0 h1:jS72fuQpFgJZEdEJDmHJCPAgNTEMZoz1EUvimPUOiJ4= github.com/DataDog/go-libddwaf/v3 v3.3.0/go.mod h1:Bz/0JkpGf689mzbUjKJeheJINqsyyhM8p9PDuHdK2Ec= github.com/DataDog/go-sqllexer v0.0.15 h1:rUUu52dP8EQhJLnUw0MIAxZp0BQx2fOTuMztr3vtHUU= @@ -170,10 +171,12 @@ github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= +github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= +github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= @@ -219,6 +222,7 @@ github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9 github.com/google/cel-go v0.20.1 h1:nDx9r8S3L4pE61eDdt8igGj8rf5kjYR3ILxWIpWNi84= github.com/google/cel-go v0.20.1/go.mod h1:kWcIzTsPX0zmQ+H3TirHstLLf9ep5QTsZBN9u4dOYLg= github.com/google/gnostic-models v0.6.9 h1:MU/8wDLif2qCXZmzncUQ/BOfxWfthHi63KqpoNbWqVw= +github.com/google/gnostic-models v0.6.9/go.mod h1:CiWsm0s6BSQd1hRn8/QmxqB6BesYcbSZxsz9b0KuDBw= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -735,6 +739,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= +google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/DataDog/dd-trace-go.v1 v1.68.0 h1:8WPoOHJcMAtcxTVKM0DYnFweBjxxfNit3Sjo/rf+Hkw= gopkg.in/DataDog/dd-trace-go.v1 v1.68.0/go.mod h1:mkZpWVLO/ERW5NqlW+w5d8waQKNvMSTUQLJfoI0vlvw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= diff --git a/internal/controller/datadogagent/controller_reconcile_agent.go b/internal/controller/datadogagent/controller_reconcile_agent.go index 54fcdb42d..d0e41d54e 100644 --- a/internal/controller/datadogagent/controller_reconcile_agent.go +++ b/internal/controller/datadogagent/controller_reconcile_agent.go @@ -9,6 +9,8 @@ import ( "context" "time" + edsv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1" + apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common" "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1" datadoghqv2alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1" @@ -17,10 +19,10 @@ import ( "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature" "github.com/DataDog/datadog-operator/internal/controller/datadogagent/override" "github.com/DataDog/datadog-operator/pkg/agentprofile" + "github.com/DataDog/datadog-operator/pkg/condition" "github.com/DataDog/datadog-operator/pkg/constants" "github.com/DataDog/datadog-operator/pkg/controller/utils/datadog" "github.com/DataDog/datadog-operator/pkg/kubernetes" - edsv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1" "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" @@ -107,7 +109,7 @@ func (r *Reconciler) reconcileV2Agent(logger logr.Logger, requiredComponents fea if disabledByOverride { if agentEnabled { // The override supersedes what's set in requiredComponents; update status to reflect the conflict - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions( + condition.UpdateDatadogAgentStatusConditions( newStatus, metav1.NewTime(time.Now()), datadoghqv2alpha1.OverrideReconcileConflictConditionType, @@ -182,7 +184,7 @@ func (r *Reconciler) reconcileV2Agent(logger logr.Logger, requiredComponents fea if disabledByOverride { if agentEnabled { // The override supersedes what's set in requiredComponents; update status to reflect the conflict - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions( + condition.UpdateDatadogAgentStatusConditions( newStatus, metav1.NewTime(time.Now()), datadoghqv2alpha1.OverrideReconcileConflictConditionType, @@ -203,15 +205,15 @@ func (r *Reconciler) reconcileV2Agent(logger logr.Logger, requiredComponents fea } func updateDSStatusV2WithAgent(ds *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) { - newStatus.AgentList = datadoghqv2alpha1.UpdateDaemonSetStatus(ds, newStatus.AgentList, &updateTime) - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true) - newStatus.Agent = datadoghqv2alpha1.UpdateCombinedDaemonSetStatus(newStatus.AgentList) + newStatus.AgentList = condition.UpdateDaemonSetStatus(ds, newStatus.AgentList, &updateTime) + condition.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true) + newStatus.Agent = condition.UpdateCombinedDaemonSetStatus(newStatus.AgentList) } func updateEDSStatusV2WithAgent(eds *edsv1alpha1.ExtendedDaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) { - newStatus.AgentList = datadoghqv2alpha1.UpdateExtendedDaemonSetStatus(eds, newStatus.AgentList, &updateTime) - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true) - newStatus.Agent = datadoghqv2alpha1.UpdateCombinedDaemonSetStatus(newStatus.AgentList) + newStatus.AgentList = condition.UpdateExtendedDaemonSetStatus(eds, newStatus.AgentList, &updateTime) + condition.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true) + newStatus.Agent = condition.UpdateCombinedDaemonSetStatus(newStatus.AgentList) } func (r *Reconciler) deleteV2DaemonSet(logger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, ds *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus) error { @@ -242,7 +244,7 @@ func (r *Reconciler) deleteV2ExtendedDaemonSet(logger logr.Logger, dda *datadogh func deleteStatusWithAgent(newStatus *datadoghqv2alpha1.DatadogAgentStatus) { newStatus.Agent = nil - datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.AgentReconcileConditionType) + condition.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.AgentReconcileConditionType) } // removeStaleStatus removes a DaemonSet's status from a DatadogAgent's diff --git a/internal/controller/datadogagent/controller_reconcile_ccr.go b/internal/controller/datadogagent/controller_reconcile_ccr.go index 59742be38..1a799efcb 100644 --- a/internal/controller/datadogagent/controller_reconcile_ccr.go +++ b/internal/controller/datadogagent/controller_reconcile_ccr.go @@ -15,6 +15,7 @@ import ( componentccr "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/clusterchecksrunner" "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature" "github.com/DataDog/datadog-operator/internal/controller/datadogagent/override" + "github.com/DataDog/datadog-operator/pkg/condition" "github.com/DataDog/datadog-operator/pkg/constants" "github.com/DataDog/datadog-operator/pkg/controller/utils/datadog" "github.com/DataDog/datadog-operator/pkg/kubernetes" @@ -65,7 +66,7 @@ func (r *Reconciler) reconcileV2ClusterChecksRunner(logger logr.Logger, required if apiutils.BoolValue(componentOverride.Disabled) { if ccrEnabled { // The override supersedes what's set in requiredComponents; update status to reflect the conflict - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions( + condition.UpdateDatadogAgentStatusConditions( newStatus, metav1.NewTime(time.Now()), datadoghqv2alpha1.OverrideReconcileConflictConditionType, @@ -88,8 +89,8 @@ func (r *Reconciler) reconcileV2ClusterChecksRunner(logger logr.Logger, required } func updateStatusV2WithClusterChecksRunner(deployment *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) { - newStatus.ClusterChecksRunner = datadoghqv2alpha1.UpdateDeploymentStatus(deployment, newStatus.ClusterChecksRunner, &updateTime) - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType, status, reason, message, true) + newStatus.ClusterChecksRunner = condition.UpdateDeploymentStatus(deployment, newStatus.ClusterChecksRunner, &updateTime) + condition.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType, status, reason, message, true) } func (r *Reconciler) cleanupV2ClusterChecksRunner(logger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, deployment *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus) (reconcile.Result, error) { @@ -120,7 +121,7 @@ func (r *Reconciler) cleanupV2ClusterChecksRunner(logger logr.Logger, dda *datad func deleteStatusWithClusterChecksRunner(newStatus *datadoghqv2alpha1.DatadogAgentStatus) { newStatus.ClusterChecksRunner = nil - datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType) + condition.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType) } // cleanupOldCCRDeployments deletes CCR deployments when a CCR Deployment's name is changed using clusterChecksRunner name override diff --git a/internal/controller/datadogagent/controller_reconcile_dca.go b/internal/controller/datadogagent/controller_reconcile_dca.go index 3b78145da..764a61bb9 100644 --- a/internal/controller/datadogagent/controller_reconcile_dca.go +++ b/internal/controller/datadogagent/controller_reconcile_dca.go @@ -15,6 +15,7 @@ import ( componentdca "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/clusteragent" "github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature" "github.com/DataDog/datadog-operator/internal/controller/datadogagent/override" + "github.com/DataDog/datadog-operator/pkg/condition" "github.com/DataDog/datadog-operator/pkg/constants" "github.com/DataDog/datadog-operator/pkg/controller/utils/datadog" "github.com/DataDog/datadog-operator/pkg/kubernetes" @@ -63,7 +64,7 @@ func (r *Reconciler) reconcileV2ClusterAgent(logger logr.Logger, requiredCompone if apiutils.BoolValue(componentOverride.Disabled) { if dcaEnabled { // The override supersedes what's set in requiredComponents; update status to reflect the conflict - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions( + condition.UpdateDatadogAgentStatusConditions( newStatus, metav1.NewTime(time.Now()), datadoghqv2alpha1.OverrideReconcileConflictConditionType, @@ -88,13 +89,13 @@ func (r *Reconciler) reconcileV2ClusterAgent(logger logr.Logger, requiredCompone } func updateStatusV2WithClusterAgent(dca *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) { - newStatus.ClusterAgent = datadoghqv2alpha1.UpdateDeploymentStatus(dca, newStatus.ClusterAgent, &updateTime) - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.ClusterAgentReconcileConditionType, status, reason, message, true) + newStatus.ClusterAgent = condition.UpdateDeploymentStatus(dca, newStatus.ClusterAgent, &updateTime) + condition.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.ClusterAgentReconcileConditionType, status, reason, message, true) } func deleteStatusV2WithClusterAgent(newStatus *datadoghqv2alpha1.DatadogAgentStatus) { newStatus.ClusterAgent = nil - datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterAgentReconcileConditionType) + condition.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterAgentReconcileConditionType) } func (r *Reconciler) cleanupV2ClusterAgent(logger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, deployment *appsv1.Deployment, resourcesManager feature.ResourceManagers, newStatus *datadoghqv2alpha1.DatadogAgentStatus) (reconcile.Result, error) { diff --git a/internal/controller/datadogagent/controller_reconcile_v2.go b/internal/controller/datadogagent/controller_reconcile_v2.go index de2c05f3a..693a71854 100644 --- a/internal/controller/datadogagent/controller_reconcile_v2.go +++ b/internal/controller/datadogagent/controller_reconcile_v2.go @@ -18,6 +18,7 @@ import ( "github.com/DataDog/datadog-operator/internal/controller/datadogagent/store" "github.com/DataDog/datadog-operator/internal/controller/metrics" "github.com/DataDog/datadog-operator/pkg/agentprofile" + "github.com/DataDog/datadog-operator/pkg/condition" "github.com/DataDog/datadog-operator/pkg/controller/utils" "github.com/DataDog/datadog-operator/pkg/kubernetes" @@ -147,7 +148,7 @@ func (r *Reconciler) reconcileInstanceV2(ctx context.Context, logger logr.Logger return r.updateStatusIfNeededV2(logger, instance, newStatus, result, err, now) } else { // Update the status to make it the ClusterAgentReconcileConditionType successful - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.ClusterAgentReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false) + condition.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.ClusterAgentReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false) } // Start with an "empty" profile and provider @@ -198,7 +199,7 @@ func (r *Reconciler) reconcileInstanceV2(ctx context.Context, logger logr.Logger return r.updateStatusIfNeededV2(logger, instance, newStatus, result, errors.NewAggregate(errs), now) } else { // Update the status to set AgentReconcileConditionType to successful - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.AgentReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false) + condition.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.AgentReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false) } result, err = r.reconcileV2ClusterChecksRunner(logger, requiredComponents, features, instance, resourceManagers, newStatus) @@ -206,7 +207,7 @@ func (r *Reconciler) reconcileInstanceV2(ctx context.Context, logger logr.Logger return r.updateStatusIfNeededV2(logger, instance, newStatus, result, err, now) } else { // Update the status to set ClusterChecksRunnerReconcileConditionType to successful - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false) + condition.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false) } // ------------------------------ @@ -251,9 +252,9 @@ func (r *Reconciler) reconcileInstanceV2(ctx context.Context, logger logr.Logger func (r *Reconciler) updateStatusIfNeededV2(logger logr.Logger, agentdeployment *datadoghqv2alpha1.DatadogAgent, newStatus *datadoghqv2alpha1.DatadogAgentStatus, result reconcile.Result, currentError error, now metav1.Time) (reconcile.Result, error) { if currentError == nil { - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.DatadogAgentReconcileErrorConditionType, metav1.ConditionFalse, "DatadogAgent_reconcile_ok", "DatadogAgent reconcile ok", false) + condition.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.DatadogAgentReconcileErrorConditionType, metav1.ConditionFalse, "DatadogAgent_reconcile_ok", "DatadogAgent reconcile ok", false) } else { - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.DatadogAgentReconcileErrorConditionType, metav1.ConditionTrue, "DatadogAgent_reconcile_error", "DatadogAgent reconcile error", false) + condition.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.DatadogAgentReconcileErrorConditionType, metav1.ConditionTrue, "DatadogAgent_reconcile_error", "DatadogAgent reconcile error", false) } r.setMetricsForwarderStatusV2(logger, agentdeployment, newStatus) @@ -290,11 +291,11 @@ func (r *Reconciler) updateDAPStatus(logger logr.Logger, profile *datadoghqv1alp func (r *Reconciler) setMetricsForwarderStatusV2(logger logr.Logger, agentdeployment *datadoghqv2alpha1.DatadogAgent, newStatus *datadoghqv2alpha1.DatadogAgentStatus) { if r.options.OperatorMetricsEnabled { if forwarderCondition := r.forwarders.MetricsForwarderStatusForObj(agentdeployment); forwarderCondition != nil { - datadoghqv2alpha1.UpdateDatadogAgentStatusConditions( + condition.UpdateDatadogAgentStatusConditions( newStatus, forwarderCondition.LastUpdateTime, forwarderCondition.ConditionType, - datadoghqv2alpha1.GetMetav1ConditionStatus(forwarderCondition.Status), + condition.GetMetav1ConditionStatus(forwarderCondition.Status), forwarderCondition.Reason, forwarderCondition.Message, true, diff --git a/internal/controller/datadogagent/controller_reconcile_v2_common.go b/internal/controller/datadogagent/controller_reconcile_v2_common.go index 631251665..31398f3d1 100644 --- a/internal/controller/datadogagent/controller_reconcile_v2_common.go +++ b/internal/controller/datadogagent/controller_reconcile_v2_common.go @@ -13,6 +13,7 @@ import ( "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1" datadoghqv2alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1" "github.com/DataDog/datadog-operator/pkg/agentprofile" + "github.com/DataDog/datadog-operator/pkg/condition" "github.com/DataDog/datadog-operator/pkg/controller/utils/comparison" "github.com/DataDog/datadog-operator/pkg/controller/utils/datadog" "github.com/DataDog/datadog-operator/pkg/kubernetes" @@ -218,8 +219,8 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *data // Even if the DaemonSet is still the same, its status might have // changed (for example, the number of pods ready). This call is // needed to keep the agent status updated. - newStatus.AgentList = datadoghqv2alpha1.UpdateDaemonSetStatus(currentDaemonset, newStatus.AgentList, &now) - newStatus.Agent = datadoghqv2alpha1.UpdateCombinedDaemonSetStatus(newStatus.AgentList) + newStatus.AgentList = condition.UpdateDaemonSetStatus(currentDaemonset, newStatus.AgentList, &now) + newStatus.Agent = condition.UpdateCombinedDaemonSetStatus(newStatus.AgentList) // Stop reconcile loop since DaemonSet hasn't changed return reconcile.Result{}, nil @@ -317,8 +318,8 @@ func (r *Reconciler) createOrUpdateExtendedDaemonset(parentLogger logr.Logger, d // changed (for example, the number of pods ready). This call is // needed to keep the agent status updated. now := metav1.NewTime(time.Now()) - newStatus.AgentList = datadoghqv2alpha1.UpdateExtendedDaemonSetStatus(currentEDS, newStatus.AgentList, &now) - newStatus.Agent = datadoghqv2alpha1.UpdateCombinedDaemonSetStatus(newStatus.AgentList) + newStatus.AgentList = condition.UpdateExtendedDaemonSetStatus(currentEDS, newStatus.AgentList, &now) + newStatus.Agent = condition.UpdateCombinedDaemonSetStatus(newStatus.AgentList) // Stop reconcile loop since EDS hasn't changed return reconcile.Result{}, nil diff --git a/api/datadoghq/v2alpha1/condition.go b/pkg/condition/condition.go similarity index 72% rename from api/datadoghq/v2alpha1/condition.go rename to pkg/condition/condition.go index d2a69cabf..798157089 100644 --- a/api/datadoghq/v2alpha1/condition.go +++ b/pkg/condition/condition.go @@ -3,7 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package v2alpha1 +package condition import ( "fmt" @@ -13,6 +13,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" edsdatadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1" + + "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1" ) // DatadogAgentState type representing the deployment state of the different Agent components. @@ -31,28 +33,19 @@ const ( DatadogAgentStateFailed DatadogAgentState = "Failed" ) -// UpdateDatadogAgentStatusConditionsFailure used to update the failure StatusConditions -func UpdateDatadogAgentStatusConditionsFailure(status *DatadogAgentStatus, now metav1.Time, conditionType, reason, message string, err error) { - if err != nil { - UpdateDatadogAgentStatusConditions(status, now, conditionType, metav1.ConditionTrue, reason, fmt.Sprintf("msg:%s, err:%v", message, err), true) - } else { - UpdateDatadogAgentStatusConditions(status, now, conditionType, metav1.ConditionFalse, reason, message, true) - } -} - // UpdateDatadogAgentStatusConditions used to update a specific string in conditions -func UpdateDatadogAgentStatusConditions(status *DatadogAgentStatus, now metav1.Time, t string, conditionStatus metav1.ConditionStatus, reason, message string, writeFalseIfNotExist bool) { +func UpdateDatadogAgentStatusConditions(status *v2alpha1.DatadogAgentStatus, now metav1.Time, t string, conditionStatus metav1.ConditionStatus, reason, message string, writeFalseIfNotExist bool) { idConditionComplete := getIndexForConditionType(status, t) if idConditionComplete >= 0 { - UpdateDatadogAgentStatusCondition(&status.Conditions[idConditionComplete], now, t, conditionStatus, reason, message) + updateDatadogAgentStatusCondition(&status.Conditions[idConditionComplete], now, conditionStatus, reason, message) } else if conditionStatus == metav1.ConditionTrue || writeFalseIfNotExist { // Only add if the condition is True - status.Conditions = append(status.Conditions, NewDatadogAgentStatusCondition(t, conditionStatus, now, reason, message)) + status.Conditions = append(status.Conditions, newDatadogAgentStatusCondition(t, conditionStatus, now, reason, message)) } } -// UpdateDatadogAgentStatusCondition used to update a specific string -func UpdateDatadogAgentStatusCondition(condition *metav1.Condition, now metav1.Time, t string, conditionStatus metav1.ConditionStatus, reason, message string) *metav1.Condition { +// updateDatadogAgentStatusCondition used to update a specific string +func updateDatadogAgentStatusCondition(condition *metav1.Condition, now metav1.Time, conditionStatus metav1.ConditionStatus, reason, message string) *metav1.Condition { if condition.Status != conditionStatus { condition.LastTransitionTime = now condition.Status = conditionStatus @@ -63,26 +56,16 @@ func UpdateDatadogAgentStatusCondition(condition *metav1.Condition, now metav1.T return condition } -// SetDatadogAgentStatusCondition use to set a condition -func SetDatadogAgentStatusCondition(status *DatadogAgentStatus, condition *metav1.Condition) { - idConditionComplete := getIndexForConditionType(status, condition.Type) - if idConditionComplete >= 0 { - status.Conditions[idConditionComplete] = *condition - } else { - status.Conditions = append(status.Conditions, *condition) - } -} - // DeleteDatadogAgentStatusCondition is used to delete a condition -func DeleteDatadogAgentStatusCondition(status *DatadogAgentStatus, conditionType string) { +func DeleteDatadogAgentStatusCondition(status *v2alpha1.DatadogAgentStatus, conditionType string) { idConditionComplete := getIndexForConditionType(status, conditionType) if idConditionComplete >= 0 { status.Conditions = append(status.Conditions[:idConditionComplete], status.Conditions[idConditionComplete+1:]...) } } -// NewDatadogAgentStatusCondition returns new metav1.Condition instance -func NewDatadogAgentStatusCondition(conditionType string, conditionStatus metav1.ConditionStatus, now metav1.Time, reason, message string) metav1.Condition { +// newDatadogAgentStatusCondition returns new metav1.Condition instance +func newDatadogAgentStatusCondition(conditionType string, conditionStatus metav1.ConditionStatus, now metav1.Time, reason, message string) metav1.Condition { return metav1.Condition{ Type: conditionType, Status: conditionStatus, @@ -100,7 +83,7 @@ func GetMetav1ConditionStatus(status bool) metav1.ConditionStatus { return metav1.ConditionFalse } -func getIndexForConditionType(status *DatadogAgentStatus, t string) int { +func getIndexForConditionType(status *v2alpha1.DatadogAgentStatus, t string) int { idCondition := -1 if status == nil { return idCondition @@ -117,9 +100,9 @@ func getIndexForConditionType(status *DatadogAgentStatus, t string) int { } // UpdateDeploymentStatus updates a deployment's DeploymentStatus -func UpdateDeploymentStatus(dep *appsv1.Deployment, depStatus *DeploymentStatus, updateTime *metav1.Time) *DeploymentStatus { +func UpdateDeploymentStatus(dep *appsv1.Deployment, depStatus *v2alpha1.DeploymentStatus, updateTime *metav1.Time) *v2alpha1.DeploymentStatus { if depStatus == nil { - depStatus = &DeploymentStatus{} + depStatus = &v2alpha1.DeploymentStatus{} } if dep == nil { depStatus.State = string(DatadogAgentStateFailed) @@ -127,7 +110,7 @@ func UpdateDeploymentStatus(dep *appsv1.Deployment, depStatus *DeploymentStatus, return depStatus } - if hash, ok := dep.Annotations[MD5AgentDeploymentAnnotationKey]; ok { + if hash, ok := dep.Annotations[v2alpha1.MD5AgentDeploymentAnnotationKey]; ok { depStatus.CurrentHash = hash } if updateTime != nil { @@ -165,19 +148,19 @@ func UpdateDeploymentStatus(dep *appsv1.Deployment, depStatus *DeploymentStatus, } // UpdateDaemonSetStatus updates a daemonset's DaemonSetStatus -func UpdateDaemonSetStatus(ds *appsv1.DaemonSet, dsStatus []*DaemonSetStatus, updateTime *metav1.Time) []*DaemonSetStatus { +func UpdateDaemonSetStatus(ds *appsv1.DaemonSet, dsStatus []*v2alpha1.DaemonSetStatus, updateTime *metav1.Time) []*v2alpha1.DaemonSetStatus { if dsStatus == nil { - dsStatus = []*DaemonSetStatus{} + dsStatus = []*v2alpha1.DaemonSetStatus{} } if ds == nil { - dsStatus = append(dsStatus, &DaemonSetStatus{ + dsStatus = append(dsStatus, &v2alpha1.DaemonSetStatus{ State: string(DatadogAgentStateFailed), Status: string(DatadogAgentStateFailed), }) return dsStatus } - newStatus := DaemonSetStatus{ + newStatus := v2alpha1.DaemonSetStatus{ Desired: ds.Status.DesiredNumberScheduled, Current: ds.Status.CurrentNumberScheduled, Ready: ds.Status.NumberReady, @@ -189,7 +172,7 @@ func UpdateDaemonSetStatus(ds *appsv1.DaemonSet, dsStatus []*DaemonSetStatus, up if updateTime != nil { newStatus.LastUpdate = updateTime } - if hash, ok := ds.Annotations[MD5AgentDeploymentAnnotationKey]; ok { + if hash, ok := ds.Annotations[v2alpha1.MD5AgentDeploymentAnnotationKey]; ok { newStatus.CurrentHash = hash } @@ -222,12 +205,12 @@ func UpdateDaemonSetStatus(ds *appsv1.DaemonSet, dsStatus []*DaemonSetStatus, up } // UpdateExtendedDaemonSetStatus updates an ExtendedDaemonSet's DaemonSetStatus -func UpdateExtendedDaemonSetStatus(eds *edsdatadoghqv1alpha1.ExtendedDaemonSet, dsStatus []*DaemonSetStatus, updateTime *metav1.Time) []*DaemonSetStatus { +func UpdateExtendedDaemonSetStatus(eds *edsdatadoghqv1alpha1.ExtendedDaemonSet, dsStatus []*v2alpha1.DaemonSetStatus, updateTime *metav1.Time) []*v2alpha1.DaemonSetStatus { if dsStatus == nil { - dsStatus = []*DaemonSetStatus{} + dsStatus = []*v2alpha1.DaemonSetStatus{} } - newStatus := DaemonSetStatus{ + newStatus := v2alpha1.DaemonSetStatus{ Desired: eds.Status.Desired, Current: eds.Status.Current, Ready: eds.Status.Ready, @@ -239,7 +222,7 @@ func UpdateExtendedDaemonSetStatus(eds *edsdatadoghqv1alpha1.ExtendedDaemonSet, if updateTime != nil { newStatus.LastUpdate = updateTime } - if hash, ok := eds.Annotations[MD5AgentDeploymentAnnotationKey]; ok { + if hash, ok := eds.Annotations[v2alpha1.MD5AgentDeploymentAnnotationKey]; ok { newStatus.CurrentHash = hash } @@ -274,8 +257,8 @@ func UpdateExtendedDaemonSetStatus(eds *edsdatadoghqv1alpha1.ExtendedDaemonSet, } // UpdateCombinedDaemonSetStatus combines the status of multiple DaemonSetStatus -func UpdateCombinedDaemonSetStatus(dsStatus []*DaemonSetStatus) *DaemonSetStatus { - combinedStatus := DaemonSetStatus{} +func UpdateCombinedDaemonSetStatus(dsStatus []*v2alpha1.DaemonSetStatus) *v2alpha1.DaemonSetStatus { + combinedStatus := v2alpha1.DaemonSetStatus{} if len(dsStatus) == 0 { return &combinedStatus } @@ -328,13 +311,3 @@ func assignNumeralState(state string) int { return 0 } } - -// DatadogForwarderConditionType type use to represent a Datadog Metrics Forwarder condition. -type DatadogForwarderConditionType string - -const ( - // DatadogMetricsActive forwarding metrics and events to Datadog is active. - DatadogMetricsActive DatadogForwarderConditionType = "ActiveDatadogMetrics" - // DatadogMetricsError cannot forward deployment metrics and events to Datadog. - DatadogMetricsError DatadogForwarderConditionType = "DatadogMetricsError" -) diff --git a/api/datadoghq/v2alpha1/condition_test.go b/pkg/condition/condition_test.go similarity index 77% rename from api/datadoghq/v2alpha1/condition_test.go rename to pkg/condition/condition_test.go index d24ffd98d..44d9aa05b 100644 --- a/api/datadoghq/v2alpha1/condition_test.go +++ b/pkg/condition/condition_test.go @@ -3,39 +3,41 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package v2alpha1 +package condition import ( "testing" - apiutils "github.com/DataDog/datadog-operator/api/utils" "github.com/google/go-cmp/cmp" assert "github.com/stretchr/testify/require" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1" + apiutils "github.com/DataDog/datadog-operator/api/utils" ) func TestDeleteDatadogAgentStatusCondition(t *testing.T) { type args struct { - status *DatadogAgentStatus + status *v2alpha1.DatadogAgentStatus condition string } tests := []struct { name string args args - expectedStatus *DatadogAgentStatus + expectedStatus *v2alpha1.DatadogAgentStatus }{ { name: "empty status", args: args{ - status: &DatadogAgentStatus{}, + status: &v2alpha1.DatadogAgentStatus{}, condition: "fooType", }, - expectedStatus: &DatadogAgentStatus{}, + expectedStatus: &v2alpha1.DatadogAgentStatus{}, }, { name: "not present status", args: args{ - status: &DatadogAgentStatus{ + status: &v2alpha1.DatadogAgentStatus{ Conditions: []v1.Condition{ { Type: "barType", @@ -44,7 +46,7 @@ func TestDeleteDatadogAgentStatusCondition(t *testing.T) { }, condition: "fooType", }, - expectedStatus: &DatadogAgentStatus{ + expectedStatus: &v2alpha1.DatadogAgentStatus{ Conditions: []v1.Condition{ { Type: "barType", @@ -55,7 +57,7 @@ func TestDeleteDatadogAgentStatusCondition(t *testing.T) { { name: "status present at the end", args: args{ - status: &DatadogAgentStatus{ + status: &v2alpha1.DatadogAgentStatus{ Conditions: []v1.Condition{ { Type: "barType", @@ -67,7 +69,7 @@ func TestDeleteDatadogAgentStatusCondition(t *testing.T) { }, condition: "fooType", }, - expectedStatus: &DatadogAgentStatus{ + expectedStatus: &v2alpha1.DatadogAgentStatus{ Conditions: []v1.Condition{ { Type: "barType", @@ -78,7 +80,7 @@ func TestDeleteDatadogAgentStatusCondition(t *testing.T) { { name: "status present at the begining", args: args{ - status: &DatadogAgentStatus{ + status: &v2alpha1.DatadogAgentStatus{ Conditions: []v1.Condition{ { Type: "fooType", @@ -90,7 +92,7 @@ func TestDeleteDatadogAgentStatusCondition(t *testing.T) { }, condition: "fooType", }, - expectedStatus: &DatadogAgentStatus{ + expectedStatus: &v2alpha1.DatadogAgentStatus{ Conditions: []v1.Condition{ { Type: "barType", diff --git a/pkg/controller/utils/datadog/metrics_forwarder.go b/pkg/controller/utils/datadog/metrics_forwarder.go index 8c157df3f..caccb963a 100644 --- a/pkg/controller/utils/datadog/metrics_forwarder.go +++ b/pkg/controller/utils/datadog/metrics_forwarder.go @@ -67,6 +67,16 @@ var ( errInitValue = errors.New("last error init value") ) +// datadogForwarderConditionType type use to represent a Datadog Metrics Forwarder condition. +type datadogForwarderConditionType string + +const ( + // datadogMetricsActive forwarding metrics and events to Datadog is active. + datadogMetricsActive datadogForwarderConditionType = "ActiveDatadogMetrics" + // datadogMetricsError cannot forward deployment metrics and events to Datadog. + datadogMetricsError datadogForwarderConditionType = "DatadogMetricsError" +) + // delegatedAPI is used for testing purpose, it serves for mocking the Datadog API type delegatedAPI interface { delegatedSendDeploymentMetric(float64, string, []string) error @@ -668,13 +678,13 @@ func (mf *metricsForwarder) updateStatusIfNeeded(err error) { conditionStatus := true message := "Datadog metrics forwarding ok" reason := "MetricsForwardingSucceeded" - conditionType := string(v2alpha1.DatadogMetricsActive) + conditionType := string(datadogMetricsActive) if err != nil { conditionStatus = false message = "Datadog metrics forwarding error" reason = "MetricsForwardingError" - conditionType = string(v2alpha1.DatadogMetricsError) + conditionType = string(datadogMetricsError) } newConditionStatus := &ConditionCommon{