diff --git a/internal/controller/datadogagent/controller_reconcile_agent.go b/internal/controller/datadogagent/controller_reconcile_agent.go index d0e41d54e..19089b631 100644 --- a/internal/controller/datadogagent/controller_reconcile_agent.go +++ b/internal/controller/datadogagent/controller_reconcile_agent.go @@ -204,8 +204,8 @@ func (r *Reconciler) reconcileV2Agent(logger logr.Logger, requiredComponents fea return r.createOrUpdateDaemonset(daemonsetLogger, dda, daemonset, newStatus, updateDSStatusV2WithAgent, profile) } -func updateDSStatusV2WithAgent(ds *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) { - newStatus.AgentList = condition.UpdateDaemonSetStatus(ds, newStatus.AgentList, &updateTime) +func updateDSStatusV2WithAgent(dsName string, ds *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) { + newStatus.AgentList = condition.UpdateDaemonSetStatus(dsName, ds, newStatus.AgentList, &updateTime) condition.UpdateDatadogAgentStatusConditions(newStatus, updateTime, datadoghqv2alpha1.AgentReconcileConditionType, status, reason, message, true) newStatus.Agent = condition.UpdateCombinedDaemonSetStatus(newStatus.AgentList) } diff --git a/internal/controller/datadogagent/controller_reconcile_v2_common.go b/internal/controller/datadogagent/controller_reconcile_v2_common.go index 31398f3d1..266f04099 100644 --- a/internal/controller/datadogagent/controller_reconcile_v2_common.go +++ b/internal/controller/datadogagent/controller_reconcile_v2_common.go @@ -35,7 +35,7 @@ const ( ) type updateDepStatusComponentFunc func(deployment *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) -type updateDSStatusComponentFunc func(daemonset *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) +type updateDSStatusComponentFunc func(daemonsetName string, daemonset *appsv1.DaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) type updateEDSStatusComponentFunc func(eds *edsv1alpha1.ExtendedDaemonSet, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateTime metav1.Time, status metav1.ConditionStatus, reason, message string) func (r *Reconciler) createOrUpdateDeployment(parentLogger logr.Logger, dda *datadoghqv2alpha1.DatadogAgent, deployment *appsv1.Deployment, newStatus *datadoghqv2alpha1.DatadogAgentStatus, updateStatusFunc updateDepStatusComponentFunc) (reconcile.Result, error) { @@ -219,7 +219,7 @@ 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 = condition.UpdateDaemonSetStatus(currentDaemonset, newStatus.AgentList, &now) + newStatus.AgentList = condition.UpdateDaemonSetStatus(currentDaemonset.Name, currentDaemonset, newStatus.AgentList, &now) newStatus.Agent = condition.UpdateCombinedDaemonSetStatus(newStatus.AgentList) // Stop reconcile loop since DaemonSet hasn't changed @@ -243,12 +243,12 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *data err = kubernetes.UpdateFromObject(context.TODO(), r.client, updateDaemonset, currentDaemonset.ObjectMeta) if err != nil { - updateStatusFunc(updateDaemonset, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update Daemonset") + updateStatusFunc(updateDaemonset.Name, updateDaemonset, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update Daemonset") return reconcile.Result{}, err } event := buildEventInfo(updateDaemonset.Name, updateDaemonset.Namespace, kubernetes.DaemonSetKind, datadog.UpdateEvent) r.recordEvent(dda, event) - updateStatusFunc(updateDaemonset, newStatus, now, metav1.ConditionTrue, updateSucceeded, "Daemonset updated") + updateStatusFunc(updateDaemonset.Name, updateDaemonset, newStatus, now, metav1.ConditionTrue, updateSucceeded, "Daemonset updated") } else { // From here the PodTemplateSpec should be ready, we can generate the hash that will be added to this daemonset. _, err = comparison.SetMD5DatadogAgentGenerationAnnotation(&daemonset.ObjectMeta, daemonset.Spec) @@ -260,12 +260,12 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *data err = r.client.Create(context.TODO(), daemonset) if err != nil { - updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create Daemonset") + updateStatusFunc(daemonset.Name, nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create Daemonset") return reconcile.Result{}, err } event := buildEventInfo(daemonset.Name, daemonset.Namespace, kubernetes.DaemonSetKind, datadog.CreationEvent) r.recordEvent(dda, event) - updateStatusFunc(daemonset, newStatus, now, metav1.ConditionTrue, createSucceeded, "Daemonset created") + updateStatusFunc(daemonset.Name, daemonset, newStatus, now, metav1.ConditionTrue, createSucceeded, "Daemonset created") } logger.Info("Creating Daemonset") diff --git a/pkg/condition/condition.go b/pkg/condition/condition.go index 798157089..8582e0d2e 100644 --- a/pkg/condition/condition.go +++ b/pkg/condition/condition.go @@ -148,47 +148,48 @@ func UpdateDeploymentStatus(dep *appsv1.Deployment, depStatus *v2alpha1.Deployme } // UpdateDaemonSetStatus updates a daemonset's DaemonSetStatus -func UpdateDaemonSetStatus(ds *appsv1.DaemonSet, dsStatus []*v2alpha1.DaemonSetStatus, updateTime *metav1.Time) []*v2alpha1.DaemonSetStatus { +func UpdateDaemonSetStatus(dsName string, ds *appsv1.DaemonSet, dsStatus []*v2alpha1.DaemonSetStatus, updateTime *metav1.Time) []*v2alpha1.DaemonSetStatus { if dsStatus == nil { dsStatus = []*v2alpha1.DaemonSetStatus{} } - if ds == nil { - dsStatus = append(dsStatus, &v2alpha1.DaemonSetStatus{ - State: string(DatadogAgentStateFailed), - Status: string(DatadogAgentStateFailed), - }) - return dsStatus - } - newStatus := v2alpha1.DaemonSetStatus{ - Desired: ds.Status.DesiredNumberScheduled, - Current: ds.Status.CurrentNumberScheduled, - Ready: ds.Status.NumberReady, - Available: ds.Status.NumberAvailable, - UpToDate: ds.Status.UpdatedNumberScheduled, - DaemonsetName: ds.ObjectMeta.Name, - } + var newStatus v2alpha1.DaemonSetStatus + if ds == nil { + newStatus = v2alpha1.DaemonSetStatus{ + State: string(DatadogAgentStateFailed), + Status: string(DatadogAgentStateFailed), + DaemonsetName: dsName, + } + } else { + newStatus = v2alpha1.DaemonSetStatus{ + Desired: ds.Status.DesiredNumberScheduled, + Current: ds.Status.CurrentNumberScheduled, + Ready: ds.Status.NumberReady, + Available: ds.Status.NumberAvailable, + UpToDate: ds.Status.UpdatedNumberScheduled, + DaemonsetName: ds.ObjectMeta.Name, + } + if updateTime != nil { + newStatus.LastUpdate = updateTime + } + if hash, ok := ds.Annotations[v2alpha1.MD5AgentDeploymentAnnotationKey]; ok { + newStatus.CurrentHash = hash + } - if updateTime != nil { - newStatus.LastUpdate = updateTime - } - if hash, ok := ds.Annotations[v2alpha1.MD5AgentDeploymentAnnotationKey]; ok { - newStatus.CurrentHash = hash - } + var deploymentState DatadogAgentState + switch { + case newStatus.UpToDate != newStatus.Desired: + deploymentState = DatadogAgentStateUpdating + case newStatus.Ready == 0 && newStatus.Desired != 0: + deploymentState = DatadogAgentStateProgressing + default: + deploymentState = DatadogAgentStateRunning + } - var deploymentState DatadogAgentState - switch { - case newStatus.UpToDate != newStatus.Desired: - deploymentState = DatadogAgentStateUpdating - case newStatus.Ready == 0 && newStatus.Desired != 0: - deploymentState = DatadogAgentStateProgressing - default: - deploymentState = DatadogAgentStateRunning + newStatus.State = fmt.Sprintf("%v", deploymentState) + newStatus.Status = fmt.Sprintf("%v (%d/%d/%d)", deploymentState, newStatus.Desired, newStatus.Ready, newStatus.UpToDate) } - newStatus.State = fmt.Sprintf("%v", deploymentState) - newStatus.Status = fmt.Sprintf("%v (%d/%d/%d)", deploymentState, newStatus.Desired, newStatus.Ready, newStatus.UpToDate) - // match ds name to ds status found := false for id := range dsStatus { diff --git a/pkg/condition/condition_test.go b/pkg/condition/condition_test.go index 87db83793..00a2e2d97 100644 --- a/pkg/condition/condition_test.go +++ b/pkg/condition/condition_test.go @@ -112,10 +112,10 @@ func TestDeleteDatadogAgentStatusCondition(t *testing.T) { } } -func TestDSUpdateWhenNil(t *testing.T) { +func TestUpdateWhenDSNil(t *testing.T) { var ds *appsv1.DaemonSet - dsStatus := UpdateDaemonSetStatus(ds, []*v2alpha1.DaemonSetStatus{}, &metav1.Time{Time: time.Now()}) - dsStatus = UpdateDaemonSetStatus(ds, dsStatus, &metav1.Time{Time: time.Now()}) - dsStatus = UpdateDaemonSetStatus(ds, dsStatus, &metav1.Time{Time: time.Now()}) + dsStatus := UpdateDaemonSetStatus("ds", ds, []*v2alpha1.DaemonSetStatus{}, &metav1.Time{Time: time.Now()}) + dsStatus = UpdateDaemonSetStatus("ds", ds, dsStatus, &metav1.Time{Time: time.Now()}) + dsStatus = UpdateDaemonSetStatus("ds", ds, dsStatus, &metav1.Time{Time: time.Now()}) assert.Equal(t, 1, len(dsStatus)) }