Skip to content

Commit

Permalink
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
  • Loading branch information
davidor committed Jan 10, 2025
1 parent a3e7eab commit ef1721f
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 102 deletions.
6 changes: 3 additions & 3 deletions api/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module github.com/DataDog/datadog-operator/api

go 1.22
go 1.22.0

toolchain go1.22.7

require (
github.com/DataDog/datadog-api-client-go/v2 v2.27.0
github.com/DataDog/extendeddaemonset v0.10.0-rc.4
github.com/google/go-cmp v0.6.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
k8s.io/api v0.31.1
Expand All @@ -23,6 +21,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
Expand All @@ -35,6 +34,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
Expand Down
127 changes: 125 additions & 2 deletions api/go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,7 @@ github.com/coreos/go-iptables v0.5.0/go.mod h1:/mVI274lEDI2ns62jHCDnCyBF9Iwsmeka
github.com/coreos/go-iptables v0.6.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFETJConOQ//Q=
github.com/coreos/go-oidc v2.1.0+incompatible h1:sdJrfw8akMnCuUlaZU3tE/uYXFgfqom8DBE9so9EBsM=
github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/coreos/go-oidc v2.2.1+incompatible h1:mh48q/BqXqgjVHpy2ZY7WnWAbenxRjsz9N1i1YxjHAk=
github.com/coreos/go-oidc v2.2.1+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
Expand Down Expand Up @@ -2587,6 +2588,7 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 h1:0XM1XL/OFFJjXsYXlG30spTkV/E9+gmd5GD1w2HE8xM=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/pquerna/cachecontrol v0.1.0 h1:yJMy84ti9h/+OEWa752kBTKv4XC30OtVVHYv/8cTqKc=
github.com/pquerna/cachecontrol v0.1.0/go.mod h1:NrUG3Z7Rdu85UNR3vm7SOsl1nFIeSiQnrHV5K9mBcUI=
github.com/prometheus/client_golang v0.0.0-20180209125602-c332b6f63c06/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
Expand Down
21 changes: 11 additions & 10 deletions internal/controller/datadogagent/controller_reconcile_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/datadogagent/controller_reconcile_ccr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/datadogagent/controller_reconcile_dca.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions internal/controller/datadogagent/controller_reconcile_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

// ------------------------------
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit ef1721f

Please sign in to comment.