Skip to content

Commit 89e6046

Browse files
DAP create strategy: use lastTransition field (#2419) (#2426)
* Preserve createStrategy status and update lastTransition * Create DAP strategy last transition fill * small if simplification * fix tests + duplicate imports (cherry picked from commit 490d736) Co-authored-by: Timothée Bavelier <97530782+tbavelier@users.noreply.github.com>
1 parent 3e38fff commit 89e6046

File tree

4 files changed

+297
-93
lines changed

4 files changed

+297
-93
lines changed

pkg/agentprofile/agent_profile.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ func GetMaxUnavailable(logger logr.Logger, ddaSpec *v2alpha1.DatadogAgentSpec, p
607607
// - determines the max number of nodes that can be labeled based on max unavailable values
608608
// - updates the profile status based on the create strategy
609609
func ApplyCreateStrategy(logger logr.Logger, profilesByNode map[string]types.NamespacedName, csInfo *CreateStrategyInfo, profile *v1alpha1.DatadogAgentProfile, ddaEDSMaxUnavailable intstr.IntOrString, numNodes int, dsStatus *appsv1.DaemonSetStatus) {
610+
// Preserve previous status fields to ensure stable transitions across reconciles.
611+
var previousStatus v1alpha1.CreateStrategyStatus
612+
var previousLastTransition *metav1.Time
613+
if profile.Status.CreateStrategy != nil {
614+
previousStatus = profile.Status.CreateStrategy.Status
615+
previousLastTransition = profile.Status.CreateStrategy.LastTransition
616+
}
617+
610618
if profile.Status.CreateStrategy == nil {
611619
profile.Status.CreateStrategy = &v1alpha1.CreateStrategy{}
612620
}
@@ -624,7 +632,7 @@ func ApplyCreateStrategy(logger logr.Logger, profilesByNode map[string]types.Nam
624632
// # of new unavailable pods after labeling new nodes
625633
newUnavailable := currentUnavailable + numNodesToLabel
626634

627-
updateCreateStrategyStatus(profile, csInfo, numNodesToLabel, maxUnavailable, newUnavailable, dsStatus)
635+
updateCreateStrategyStatus(profile, csInfo, numNodesToLabel, maxUnavailable, newUnavailable, dsStatus, previousStatus, previousLastTransition)
628636
}
629637

630638
// applyMaxNodesToLabel trims the list of nodes based on max unavailable and returns the number of nodes to be labeled
@@ -652,26 +660,38 @@ func applyMaxNodesToLabel(profilesByNode map[string]types.NamespacedName, csInfo
652660
}
653661

654662
// updateCreateStrategyStatus updates the profile's create strategy status fields
655-
func updateCreateStrategyStatus(profile *v1alpha1.DatadogAgentProfile, info *CreateStrategyInfo, numNodesToLabel int32, maxUnavailable int32, newUnavailable int32, dsStatus *appsv1.DaemonSetStatus) {
663+
func updateCreateStrategyStatus(profile *v1alpha1.DatadogAgentProfile, info *CreateStrategyInfo, numNodesToLabel int32, maxUnavailable int32, newUnavailable int32, dsStatus *appsv1.DaemonSetStatus, previousStatus v1alpha1.CreateStrategyStatus, previousLastTransition *metav1.Time) {
656664
profile.Status.CreateStrategy.MaxUnavailable = maxUnavailable
657665
profile.Status.CreateStrategy.NodesLabeled = info.nodesAlreadyLabeled + numNodesToLabel
658-
profile.Status.CreateStrategy.PodsReady = dsStatus.NumberReady
666+
if dsStatus != nil {
667+
profile.Status.CreateStrategy.PodsReady = dsStatus.NumberReady
668+
}
659669

660670
totalNodes := info.nodesAlreadyLabeled + int32(len(info.nodesNeedingLabel))
661671

672+
var newStatus v1alpha1.CreateStrategyStatus
662673
if totalNodes == 0 {
663674
// no nodes match this profile - completed
664-
profile.Status.CreateStrategy.Status = v1alpha1.CompletedStatus
675+
newStatus = v1alpha1.CompletedStatus
665676
} else if info.nodesAlreadyLabeled == totalNodes {
666677
// all matching nodes are already labeled - completed
667-
profile.Status.CreateStrategy.Status = v1alpha1.CompletedStatus
678+
newStatus = v1alpha1.CompletedStatus
668679
} else if numNodesToLabel > 0 || newUnavailable < maxUnavailable {
669680
// labeled nodes in this round or can label more nodes - in progress
670-
profile.Status.CreateStrategy.Status = v1alpha1.InProgressStatus
681+
newStatus = v1alpha1.InProgressStatus
671682
} else {
672683
// can't label any nodes, waiting for pods to become ready
673-
profile.Status.CreateStrategy.Status = v1alpha1.WaitingStatus
684+
newStatus = v1alpha1.WaitingStatus
674685
}
686+
687+
// Only bump LastTransition when the Status field changes.
688+
if previousStatus != newStatus {
689+
now := metav1.Now()
690+
profile.Status.CreateStrategy.LastTransition = &now
691+
} else {
692+
profile.Status.CreateStrategy.LastTransition = previousLastTransition
693+
}
694+
profile.Status.CreateStrategy.Status = newStatus
675695
}
676696

677697
// GetMaxUnavailableFromSpecAndEDS gets the max unavailable value from a dda spec and eds options, and allows for a custom default value to be provided
@@ -722,10 +742,5 @@ func CreateStrategyNeeded(profile *v1alpha1.DatadogAgentProfile, csInfo map[type
722742
return false
723743
}
724744

725-
info := csInfo[profileNSName]
726-
if info == nil {
727-
return false
728-
}
729-
730-
return true
745+
return csInfo[profileNSName] != nil
731746
}

0 commit comments

Comments
 (0)