Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move condition logic to main module
Browse files Browse the repository at this point in the history
To avoid depending on EDS in the api module
davidor committed Jan 10, 2025
1 parent fb1cc16 commit 24adfda
Showing 10 changed files with 84 additions and 101 deletions.
4 changes: 1 addition & 3 deletions api/go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
@@ -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=
21 changes: 11 additions & 10 deletions internal/controller/datadogagent/controller_reconcile_agent.go
Original file line number Diff line number Diff line change
@@ -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"
@@ -20,7 +22,6 @@ import (
"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 +108,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(
datadog.UpdateDatadogAgentStatusConditions(
newStatus,
metav1.NewTime(time.Now()),
datadoghqv2alpha1.OverrideReconcileConflictConditionType,
@@ -182,7 +183,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(
datadog.UpdateDatadogAgentStatusConditions(
newStatus,
metav1.NewTime(time.Now()),
datadoghqv2alpha1.OverrideReconcileConflictConditionType,
@@ -203,15 +204,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 = datadog.UpdateDaemonSetStatus(ds, newStatus.AgentList, &updateTime)
datadog.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true)
newStatus.Agent = datadog.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 = datadog.UpdateExtendedDaemonSetStatus(eds, newStatus.AgentList, &updateTime)
datadog.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true)
newStatus.Agent = datadog.UpdateCombinedDaemonSetStatus(newStatus.AgentList)
}

func (r *Reconciler) deleteV2DaemonSet(logger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, ds *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus) error {
@@ -242,7 +243,7 @@ func (r *Reconciler) deleteV2ExtendedDaemonSet(logger logr.Logger, dda *datadogh

func deleteStatusWithAgent(newStatus *datadoghqv2alpha1.DatadogAgentStatus) {
newStatus.Agent = nil
datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.AgentReconcileConditionType)
datadog.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.AgentReconcileConditionType)
}

// removeStaleStatus removes a DaemonSet's status from a DatadogAgent's
8 changes: 4 additions & 4 deletions internal/controller/datadogagent/controller_reconcile_ccr.go
Original file line number Diff line number Diff line change
@@ -65,7 +65,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(
datadog.UpdateDatadogAgentStatusConditions(
newStatus,
metav1.NewTime(time.Now()),
datadoghqv2alpha1.OverrideReconcileConflictConditionType,
@@ -88,8 +88,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 = datadog.UpdateDeploymentStatus(deployment, newStatus.ClusterChecksRunner, &updateTime)
datadog.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 +120,7 @@ func (r *Reconciler) cleanupV2ClusterChecksRunner(logger logr.Logger, dda *datad

func deleteStatusWithClusterChecksRunner(newStatus *datadoghqv2alpha1.DatadogAgentStatus) {
newStatus.ClusterChecksRunner = nil
datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType)
datadog.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterChecksRunnerReconcileConditionType)
}

// cleanupOldCCRDeployments deletes CCR deployments when a CCR Deployment's name is changed using clusterChecksRunner name override
8 changes: 4 additions & 4 deletions internal/controller/datadogagent/controller_reconcile_dca.go
Original file line number Diff line number Diff line change
@@ -63,7 +63,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(
datadog.UpdateDatadogAgentStatusConditions(
newStatus,
metav1.NewTime(time.Now()),
datadoghqv2alpha1.OverrideReconcileConflictConditionType,
@@ -88,13 +88,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 = datadog.UpdateDeploymentStatus(dca, newStatus.ClusterAgent, &updateTime)
datadog.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.ClusterAgentReconcileConditionType, status, reason, message, true)
}

func deleteStatusV2WithClusterAgent(newStatus *datadoghqv2alpha1.DatadogAgentStatus) {
newStatus.ClusterAgent = nil
datadoghqv2alpha1.DeleteDatadogAgentStatusCondition(newStatus, datadoghqv2alpha1.ClusterAgentReconcileConditionType)
datadog.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) {
15 changes: 8 additions & 7 deletions internal/controller/datadogagent/controller_reconcile_v2.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ import (
"github.com/DataDog/datadog-operator/internal/controller/metrics"
"github.com/DataDog/datadog-operator/pkg/agentprofile"
"github.com/DataDog/datadog-operator/pkg/controller/utils"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
"github.com/DataDog/datadog-operator/pkg/kubernetes"

"github.com/go-logr/logr"
@@ -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)
datadog.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.ClusterAgentReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false)
}

// Start with an "empty" profile and provider
@@ -198,15 +199,15 @@ 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)
datadog.UpdateDatadogAgentStatusConditions(newStatus, now, datadoghqv2alpha1.AgentReconcileConditionType, metav1.ConditionTrue, "reconcile_succeed", "reconcile succeed", false)
}

result, err = r.reconcileV2ClusterChecksRunner(logger, requiredComponents, features, instance, resourceManagers, newStatus)
if utils.ShouldReturn(result, err) {
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)
datadog.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)
datadog.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)
datadog.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(
datadog.UpdateDatadogAgentStatusConditions(
newStatus,
forwarderCondition.LastUpdateTime,
forwarderCondition.ConditionType,
datadoghqv2alpha1.GetMetav1ConditionStatus(forwarderCondition.Status),
datadog.GetMetav1ConditionStatus(forwarderCondition.Status),
forwarderCondition.Reason,
forwarderCondition.Message,
true,
Original file line number Diff line number Diff line change
@@ -218,8 +218,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 = datadog.UpdateDaemonSetStatus(currentDaemonset, newStatus.AgentList, &now)
newStatus.Agent = datadog.UpdateCombinedDaemonSetStatus(newStatus.AgentList)

// Stop reconcile loop since DaemonSet hasn't changed
return reconcile.Result{}, nil
@@ -317,8 +317,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 = datadog.UpdateExtendedDaemonSetStatus(currentEDS, newStatus.AgentList, &now)
newStatus.Agent = datadog.UpdateCombinedDaemonSetStatus(newStatus.AgentList)

// Stop reconcile loop since EDS hasn't changed
return reconcile.Result{}, nil
Loading

0 comments on commit 24adfda

Please sign in to comment.